xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 58225d66e331e5da8ac36521c1dd3c4d172eb12f)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.SelImm
6import xiangshan.backend.roq.RoqPtr
7import xiangshan.backend.decode.{ImmUnion, XDecode}
8import xiangshan.mem.{LqPtr, SqPtr}
9import xiangshan.frontend.PreDecodeInfo
10import xiangshan.frontend.HasBPUParameter
11import xiangshan.frontend.HasTageParameter
12import xiangshan.frontend.HasIFUConst
13import xiangshan.frontend.GlobalHistory
14import xiangshan.frontend.RASEntry
15import utils._
16
17import scala.math.max
18import Chisel.experimental.chiselName
19import xiangshan.backend.ftq.FtqPtr
20
21// Fetch FetchWidth x 32-bit insts from Icache
22class FetchPacket extends XSBundle {
23  val instrs = Vec(PredictWidth, UInt(32.W))
24  val mask = UInt(PredictWidth.W)
25  val pdmask = UInt(PredictWidth.W)
26  // val pc = UInt(VAddrBits.W)
27  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
28  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
29  val pd = Vec(PredictWidth, new PreDecodeInfo)
30  val ipf = Bool()
31  val acf = Bool()
32  val crossPageIPFFix = Bool()
33  val pred_taken = UInt(PredictWidth.W)
34  val ftqPtr = new FtqPtr
35}
36
37class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
38  val valid = Bool()
39  val bits = gen.cloneType.asInstanceOf[T]
40  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
41}
42
43object ValidUndirectioned {
44  def apply[T <: Data](gen: T) = {
45    new ValidUndirectioned[T](gen)
46  }
47}
48
49class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter {
50  def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_)
51  def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_))
52  def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1
53  val tageTaken = if (useSC) Bool() else UInt(0.W)
54  val scUsed    = if (useSC) Bool() else UInt(0.W)
55  val scPred    = if (useSC) Bool() else UInt(0.W)
56  // Suppose ctrbits of all tables are identical
57  val ctrs      = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
58  val sumAbs    = if (useSC) UInt(sumCtrBits.W) else UInt(0.W)
59}
60
61class TageMeta extends XSBundle with HasTageParameter {
62  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
63  val altDiffers = Bool()
64  val providerU = UInt(2.W)
65  val providerCtr = UInt(3.W)
66  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
67  val taken = Bool()
68  val scMeta = new SCMeta(EnableSC)
69}
70
71@chiselName
72class BranchPrediction extends XSBundle with HasIFUConst {
73  // val redirect = Bool()
74  val takens = UInt(PredictWidth.W)
75  // val jmpIdx = UInt(log2Up(PredictWidth).W)
76  val brMask = UInt(PredictWidth.W)
77  val jalMask = UInt(PredictWidth.W)
78  val targets = Vec(PredictWidth, UInt(VAddrBits.W))
79
80  // marks the last 2 bytes of this fetch packet
81  // val endsAtTheEndOfFirstBank = Bool()
82  // val endsAtTheEndOfLastBank = Bool()
83
84  // half RVI could only start at the end of a packet
85  val hasHalfRVI = Bool()
86
87
88  // assumes that only one of the two conditions could be true
89  def lastHalfRVIMask = Cat(hasHalfRVI.asUInt, 0.U((PredictWidth-1).W))
90
91  def lastHalfRVIClearMask = ~lastHalfRVIMask
92  // is taken from half RVI
93  def lastHalfRVITaken = takens(PredictWidth-1) && hasHalfRVI
94
95  def lastHalfRVIIdx = (PredictWidth-1).U
96  // should not be used if not lastHalfRVITaken
97  def lastHalfRVITarget = targets(PredictWidth-1)
98
99  def realTakens  = takens  & lastHalfRVIClearMask
100  def realBrMask  = brMask  & lastHalfRVIClearMask
101  def realJalMask = jalMask & lastHalfRVIClearMask
102
103  def brNotTakens = (~takens & realBrMask)
104  def sawNotTakenBr = VecInit((0 until PredictWidth).map(i =>
105                       (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0)))))
106  // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR
107  def unmaskedJmpIdx = ParallelPriorityEncoder(takens)
108  // if not taken before the half RVI inst
109  def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0)))
110  // could get PredictWidth-1 when only the first bank is valid
111  def jmpIdx = ParallelPriorityEncoder(realTakens)
112  // only used when taken
113  def target = {
114    val generator = new PriorityMuxGenerator[UInt]
115    generator.register(realTakens.asBools, targets, List.fill(PredictWidth)(None))
116    generator()
117  }
118  def taken = ParallelORR(realTakens)
119  def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools)
120  def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens))
121}
122
123class BpuMeta extends XSBundle with HasBPUParameter {
124  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
125  val ubtbHits = Bool()
126  val btbWriteWay = UInt(log2Up(BtbWays).W)
127  val bimCtr = UInt(2.W)
128  val tageMeta = new TageMeta
129  // for global history
130
131  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
132  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
133  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
134
135  val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor}
136
137  // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
138  //   this.histPtr := histPtr
139  //   this.tageMeta := tageMeta
140  //   this.rasSp := rasSp
141  //   this.rasTopCtr := rasTopCtr
142  //   this.asUInt
143  // }
144  def size = 0.U.asTypeOf(this).getWidth
145  def fromUInt(x: UInt) = x.asTypeOf(this)
146}
147
148class Predecode extends XSBundle with HasIFUConst {
149  val hasLastHalfRVI = Bool()
150  val mask = UInt(PredictWidth.W)
151  val lastHalf = Bool()
152  val pd = Vec(PredictWidth, (new PreDecodeInfo))
153}
154
155class CfiUpdateInfo extends XSBundle with HasBPUParameter {
156  // from backend
157  val pc = UInt(VAddrBits.W)
158  // frontend -> backend -> frontend
159  val pd = new PreDecodeInfo
160  val rasSp = UInt(log2Up(RasSize).W)
161  val rasEntry = new RASEntry
162  val hist = new GlobalHistory
163  val predHist = new GlobalHistory
164  val specCnt = UInt(10.W)
165  // need pipeline update
166  val sawNotTakenBranch = Bool()
167  val predTaken = Bool()
168  val target = UInt(VAddrBits.W)
169  val taken = Bool()
170  val isMisPred = Bool()
171}
172
173// Dequeue DecodeWidth insts from Ibuffer
174class CtrlFlow extends XSBundle {
175  val instr = UInt(32.W)
176  val pc = UInt(VAddrBits.W)
177  val exceptionVec = ExceptionVec()
178  val intrVec = Vec(12, Bool())
179  val pd = new PreDecodeInfo
180  val pred_taken = Bool()
181  val crossPageIPFFix = Bool()
182  val ftqPtr = new FtqPtr
183  val ftqOffset = UInt(log2Up(PredictWidth).W)
184}
185
186class FtqEntry extends XSBundle {
187    // fetch pc, pc of each inst could be generated by concatenation
188    val ftqPC = UInt((VAddrBits.W))
189
190    val hasLastPrev = Bool()
191    // prediction metas
192    val hist = new GlobalHistory
193    val predHist = new GlobalHistory
194    val rasSp = UInt(log2Ceil(RasSize).W)
195    val rasTop = new RASEntry()
196    val specCnt = Vec(PredictWidth, UInt(10.W))
197    val metas = Vec(PredictWidth, new BpuMeta)
198
199    val cfiIsCall, cfiIsRet, cfiIsRVC = Bool()
200    val rvc_mask = Vec(PredictWidth, Bool())
201    val br_mask = Vec(PredictWidth, Bool())
202    val cfiIndex = ValidUndirectioned(UInt(log2Up(PredictWidth).W))
203    val valids = Vec(PredictWidth, Bool())
204
205    // backend update
206    val mispred = Vec(PredictWidth, Bool())
207    val target = UInt(VAddrBits.W)
208
209    def takens = VecInit((0 until PredictWidth).map(i => cfiIndex.valid && cfiIndex.bits === i.U))
210}
211
212
213
214class FPUCtrlSignals extends XSBundle {
215  val isAddSub = Bool() // swap23
216	val typeTagIn = UInt(2.W)
217	val typeTagOut = UInt(2.W)
218  val fromInt = Bool()
219  val wflags = Bool()
220  val fpWen = Bool()
221  val fmaCmd = UInt(2.W)
222  val div = Bool()
223  val sqrt = Bool()
224  val fcvt = Bool()
225  val typ = UInt(2.W)
226  val fmt = UInt(2.W)
227  val ren3 = Bool() //TODO: remove SrcType.fp
228}
229
230// Decode DecodeWidth insts at Decode Stage
231class CtrlSignals extends XSBundle {
232  val src1Type, src2Type, src3Type = SrcType()
233  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
234  val ldest = UInt(5.W)
235  val fuType = FuType()
236  val fuOpType = FuOpType()
237  val rfWen = Bool()
238  val fpWen = Bool()
239  val isXSTrap = Bool()
240  val noSpecExec = Bool()  // wait forward
241  val blockBackward  = Bool()  // block backward
242  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
243  val isRVF = Bool()
244  val selImm = SelImm()
245  val imm = UInt(ImmUnion.maxLen.W)
246  val commitType = CommitType()
247  val fpu = new FPUCtrlSignals
248
249  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
250    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
251    val signals =
252      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
253          isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
254    signals zip decoder map { case(s, d) => s := d }
255    commitType := DontCare
256    this
257  }
258}
259
260class CfCtrl extends XSBundle {
261  val cf = new CtrlFlow
262  val ctrl = new CtrlSignals
263}
264
265class PerfDebugInfo extends XSBundle {
266  // val fetchTime = UInt(64.W)
267  val renameTime = UInt(64.W)
268  val dispatchTime = UInt(64.W)
269  val issueTime = UInt(64.W)
270  val writebackTime = UInt(64.W)
271  // val commitTime = UInt(64.W)
272}
273
274// Separate LSQ
275class LSIdx extends XSBundle {
276  val lqIdx = new LqPtr
277  val sqIdx = new SqPtr
278}
279
280// CfCtrl -> MicroOp at Rename Stage
281class MicroOp extends CfCtrl {
282  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
283  val src1State, src2State, src3State = SrcState()
284  val roqIdx = new RoqPtr
285  val lqIdx = new LqPtr
286  val sqIdx = new SqPtr
287  val diffTestDebugLrScValid = Bool()
288  val debugInfo = new PerfDebugInfo
289}
290
291class Redirect extends XSBundle {
292  val roqIdx = new RoqPtr
293  val ftqIdx = new FtqPtr
294  val ftqOffset = UInt(log2Up(PredictWidth).W)
295  val level = RedirectLevel()
296  val interrupt = Bool()
297  val cfiUpdate = new CfiUpdateInfo
298
299  def isUnconditional() = RedirectLevel.isUnconditional(level)
300  def flushItself() = RedirectLevel.flushItself(level)
301  def isException() = RedirectLevel.isException(level)
302}
303
304class Dp1ToDp2IO extends XSBundle {
305  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
306  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
307  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
308}
309
310class ReplayPregReq extends XSBundle {
311  // NOTE: set isInt and isFp both to 'false' when invalid
312  val isInt = Bool()
313  val isFp = Bool()
314  val preg = UInt(PhyRegIdxWidth.W)
315}
316
317class DebugBundle extends XSBundle{
318  val isMMIO = Bool()
319  val isPerfCnt = Bool()
320}
321
322class ExuInput extends XSBundle {
323  val uop = new MicroOp
324  val src1, src2, src3 = UInt((XLEN+1).W)
325}
326
327class ExuOutput extends XSBundle {
328  val uop = new MicroOp
329  val data = UInt((XLEN+1).W)
330  val fflags  = UInt(5.W)
331  val redirectValid = Bool()
332  val redirect = new Redirect
333  val debug = new DebugBundle
334}
335
336class ExternalInterruptIO extends XSBundle {
337  val mtip = Input(Bool())
338  val msip = Input(Bool())
339  val meip = Input(Bool())
340}
341
342class CSRSpecialIO extends XSBundle {
343  val exception = Flipped(ValidIO(new MicroOp))
344  val isInterrupt = Input(Bool())
345  val memExceptionVAddr = Input(UInt(VAddrBits.W))
346  val trapTarget = Output(UInt(VAddrBits.W))
347  val externalInterrupt = new ExternalInterruptIO
348  val interrupt = Output(Bool())
349}
350
351class RoqCommitInfo extends XSBundle {
352  val ldest = UInt(5.W)
353  val rfWen = Bool()
354  val fpWen = Bool()
355  val wflags = Bool()
356  val commitType = CommitType()
357  val pdest = UInt(PhyRegIdxWidth.W)
358  val old_pdest = UInt(PhyRegIdxWidth.W)
359  val lqIdx = new LqPtr
360  val sqIdx = new SqPtr
361  val ftqIdx = new FtqPtr
362  val ftqOffset = UInt(log2Up(PredictWidth).W)
363
364  // these should be optimized for synthesis verilog
365  val pc = UInt(VAddrBits.W)
366}
367
368class RoqCommitIO extends XSBundle {
369  val isWalk = Output(Bool())
370  val valid = Vec(CommitWidth, Output(Bool()))
371  val info = Vec(CommitWidth, Output(new RoqCommitInfo))
372
373  def hasWalkInstr = isWalk && valid.asUInt.orR
374  def hasCommitInstr = !isWalk && valid.asUInt.orR
375}
376
377class TlbFeedback extends XSBundle {
378  val roqIdx = new RoqPtr
379  val hit = Bool()
380}
381
382class FrontendToBackendIO extends XSBundle {
383  // to backend end
384  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
385  val fetchInfo = DecoupledIO(new FtqEntry)
386  // from backend
387  val redirect_cfiUpdate = Flipped(ValidIO(new Redirect))
388  val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry))
389  val ftqEnqPtr = Input(new FtqPtr)
390  val ftqLeftOne = Input(Bool())
391}
392
393class TlbCsrBundle extends XSBundle {
394  val satp = new Bundle {
395    val mode = UInt(4.W) // TODO: may change number to parameter
396    val asid = UInt(16.W)
397    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
398  }
399  val priv = new Bundle {
400    val mxr = Bool()
401    val sum = Bool()
402    val imode = UInt(2.W)
403    val dmode = UInt(2.W)
404  }
405
406  override def toPrintable: Printable = {
407    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
408    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
409  }
410}
411
412class SfenceBundle extends XSBundle {
413  val valid = Bool()
414  val bits = new Bundle {
415    val rs1 = Bool()
416    val rs2 = Bool()
417    val addr = UInt(VAddrBits.W)
418  }
419
420  override def toPrintable: Printable = {
421    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
422  }
423}
424