xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision aac4464e77bf22cf47326cc3c6e2c200f9423dac)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.SelImm
6import xiangshan.backend.roq.RoqPtr
7import xiangshan.backend.decode.{ImmUnion, XDecode, WaitTableParameters}
8import xiangshan.mem.{LqPtr, SqPtr}
9import xiangshan.frontend.PreDecodeInfoForDebug
10import xiangshan.frontend.PreDecodeInfo
11import xiangshan.frontend.HasBPUParameter
12import xiangshan.frontend.PreDecodeInfo
13import xiangshan.frontend.HasTageParameter
14import xiangshan.frontend.HasSCParameter
15import xiangshan.frontend.HasIFUConst
16import xiangshan.frontend.GlobalHistory
17import xiangshan.frontend.RASEntry
18import xiangshan.frontend.BPUCtrl
19import utils._
20
21import scala.math.max
22import Chisel.experimental.chiselName
23import xiangshan.backend.ftq.FtqPtr
24
25// Fetch FetchWidth x 32-bit insts from Icache
26class FetchPacket extends XSBundle with WaitTableParameters {
27  val instrs = Vec(PredictWidth, UInt(32.W))
28  val mask = UInt(PredictWidth.W)
29  val pdmask = UInt(PredictWidth.W)
30  // val pc = UInt(VAddrBits.W)
31  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
32  val foldpc = Vec(PredictWidth, UInt(WaitTableAddrWidth.W))
33  val pd = Vec(PredictWidth, new PreDecodeInfo)
34  val ipf = Bool()
35  val acf = Bool()
36  val crossPageIPFFix = Bool()
37  val pred_taken = UInt(PredictWidth.W)
38  val ftqPtr = new FtqPtr
39}
40
41class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
42  val valid = Bool()
43  val bits = gen.cloneType.asInstanceOf[T]
44
45  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
46}
47
48object ValidUndirectioned {
49  def apply[T <: Data](gen: T) = {
50    new ValidUndirectioned[T](gen)
51  }
52}
53
54class SCMeta(val useSC: Boolean) extends XSBundle with HasSCParameter {
55  val tageTaken = if (useSC) Bool() else UInt(0.W)
56  val scUsed = if (useSC) Bool() else UInt(0.W)
57  val scPred = if (useSC) Bool() else UInt(0.W)
58  // Suppose ctrbits of all tables are identical
59  val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
60}
61
62class TageMeta extends XSBundle with HasTageParameter {
63  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
64  val altDiffers = Bool()
65  val providerU = UInt(2.W)
66  val providerCtr = UInt(3.W)
67  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
68  val taken = Bool()
69  val scMeta = new SCMeta(EnableSC)
70}
71
72@chiselName
73class BranchPrediction extends XSBundle with HasIFUConst {
74  // val redirect = Bool()
75  val takens = UInt(PredictWidth.W)
76  // val jmpIdx = UInt(log2Up(PredictWidth).W)
77  val brMask = UInt(PredictWidth.W)
78  val jalMask = UInt(PredictWidth.W)
79  val targets = Vec(PredictWidth, UInt(VAddrBits.W))
80
81  // half RVI could only start at the end of a packet
82  val hasHalfRVI = Bool()
83
84  def brNotTakens = (~takens & brMask)
85
86  def sawNotTakenBr = VecInit((0 until PredictWidth).map(i =>
87    (if (i == 0) false.B else ParallelORR(brNotTakens(i - 1, 0)))))
88
89  // if not taken before the half RVI inst
90  def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth - 2, 0)))
91
92  // could get PredictWidth-1 when only the first bank is valid
93  def jmpIdx = ParallelPriorityEncoder(takens)
94
95  // only used when taken
96  def target = {
97    val generator = new PriorityMuxGenerator[UInt]
98    generator.register(takens.asBools, targets, List.fill(PredictWidth)(None))
99    generator()
100  }
101
102  def taken = ParallelORR(takens)
103
104  def takenOnBr = taken && ParallelPriorityMux(takens, brMask.asBools)
105
106  def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(takens, sawNotTakenBr), ParallelORR(brNotTakens))
107}
108
109class PredictorAnswer extends XSBundle {
110  val hit    = if (!env.FPGAPlatform) Bool() else UInt(0.W)
111  val taken  = if (!env.FPGAPlatform) Bool() else UInt(0.W)
112  val target = if (!env.FPGAPlatform) UInt(VAddrBits.W) else UInt(0.W)
113}
114
115class BpuMeta extends XSBundle with HasBPUParameter {
116  val btbWriteWay = UInt(log2Up(BtbWays).W)
117  val bimCtr = UInt(2.W)
118  val tageMeta = new TageMeta
119  // for global history
120
121  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
122  val debug_btb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
123  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
124
125  val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor}
126
127  val ubtbAns = new PredictorAnswer
128  val btbAns = new PredictorAnswer
129  val tageAns = new PredictorAnswer
130  val rasAns = new PredictorAnswer
131  val loopAns = new PredictorAnswer
132
133  // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
134  //   this.histPtr := histPtr
135  //   this.tageMeta := tageMeta
136  //   this.rasSp := rasSp
137  //   this.rasTopCtr := rasTopCtr
138  //   this.asUInt
139  // }
140  def size = 0.U.asTypeOf(this).getWidth
141
142  def fromUInt(x: UInt) = x.asTypeOf(this)
143}
144
145class Predecode extends XSBundle with HasIFUConst {
146  val hasLastHalfRVI = Bool()
147  val mask = UInt(PredictWidth.W)
148  val lastHalf = Bool()
149  val pd = Vec(PredictWidth, (new PreDecodeInfo))
150}
151
152class CfiUpdateInfo extends XSBundle with HasBPUParameter {
153  // from backend
154  val pc = UInt(VAddrBits.W)
155  // frontend -> backend -> frontend
156  val pd = new PreDecodeInfo
157  val rasSp = UInt(log2Up(RasSize).W)
158  val rasEntry = new RASEntry
159  val hist = new GlobalHistory
160  val predHist = new GlobalHistory
161  val specCnt = Vec(PredictWidth, UInt(10.W))
162  // need pipeline update
163  val sawNotTakenBranch = Bool()
164  val predTaken = Bool()
165  val target = UInt(VAddrBits.W)
166  val taken = Bool()
167  val isMisPred = Bool()
168}
169
170// Dequeue DecodeWidth insts from Ibuffer
171class CtrlFlow extends XSBundle with WaitTableParameters {
172  val instr = UInt(32.W)
173  val pc = UInt(VAddrBits.W)
174  val foldpc = UInt(WaitTableAddrWidth.W)
175  val exceptionVec = ExceptionVec()
176  val intrVec = Vec(12, Bool())
177  val pd = new PreDecodeInfo
178  val pred_taken = Bool()
179  val crossPageIPFFix = Bool()
180  val loadWaitBit = Bool() // load inst should not be executed until all former store addr calcuated
181  val ftqPtr = new FtqPtr
182  val ftqOffset = UInt(log2Up(PredictWidth).W)
183}
184
185class FtqEntry extends XSBundle {
186  // fetch pc, pc of each inst could be generated by concatenation
187  val ftqPC = UInt(VAddrBits.W)
188  val lastPacketPC = ValidUndirectioned(UInt(VAddrBits.W))
189  // prediction metas
190  val hist = new GlobalHistory
191  val predHist = new GlobalHistory
192  val rasSp = UInt(log2Ceil(RasSize).W)
193  val rasTop = new RASEntry()
194  val specCnt = Vec(PredictWidth, UInt(10.W))
195  val metas = Vec(PredictWidth, new BpuMeta)
196
197  val cfiIsCall, cfiIsRet, cfiIsRVC = Bool()
198  val rvc_mask = Vec(PredictWidth, Bool())
199  val br_mask = Vec(PredictWidth, Bool())
200  val cfiIndex = ValidUndirectioned(UInt(log2Up(PredictWidth).W))
201  val valids = Vec(PredictWidth, Bool())
202
203  // backend update
204  val mispred = Vec(PredictWidth, Bool())
205  val target = UInt(VAddrBits.W)
206
207  // For perf counters
208  val pd = Vec(PredictWidth, new PreDecodeInfoForDebug(!env.FPGAPlatform))
209
210  def takens = VecInit((0 until PredictWidth).map(i => cfiIndex.valid && cfiIndex.bits === i.U))
211  def hasLastPrev = lastPacketPC.valid
212
213  override def toPrintable: Printable = {
214    p"ftqPC: ${Hexadecimal(ftqPC)} lastPacketPC: ${Hexadecimal(lastPacketPC.bits)} hasLastPrev:$hasLastPrev " +
215      p"rasSp:$rasSp specCnt:$specCnt brmask:${Binary(Cat(br_mask))} rvcmask:${Binary(Cat(rvc_mask))} " +
216      p"valids:${Binary(valids.asUInt())} cfi valid: ${cfiIndex.valid} " +
217      p"cfi index: ${cfiIndex.bits} isCall:$cfiIsCall isRet:$cfiIsRet isRvc:$cfiIsRVC " +
218      p"mispred:${Binary(Cat(mispred))} target:${Hexadecimal(target)}\n"
219  }
220
221}
222
223
224class FPUCtrlSignals extends XSBundle {
225  val isAddSub = Bool() // swap23
226  val typeTagIn = UInt(2.W)
227  val typeTagOut = UInt(2.W)
228  val fromInt = Bool()
229  val wflags = Bool()
230  val fpWen = Bool()
231  val fmaCmd = UInt(2.W)
232  val div = Bool()
233  val sqrt = Bool()
234  val fcvt = Bool()
235  val typ = UInt(2.W)
236  val fmt = UInt(2.W)
237  val ren3 = Bool() //TODO: remove SrcType.fp
238  val rm = UInt(3.W)
239}
240
241// Decode DecodeWidth insts at Decode Stage
242class CtrlSignals extends XSBundle {
243  val src1Type, src2Type, src3Type = SrcType()
244  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
245  val ldest = UInt(5.W)
246  val fuType = FuType()
247  val fuOpType = FuOpType()
248  val rfWen = Bool()
249  val fpWen = Bool()
250  val isXSTrap = Bool()
251  val noSpecExec = Bool() // wait forward
252  val blockBackward = Bool() // block backward
253  val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit
254  val isRVF = Bool()
255  val selImm = SelImm()
256  val imm = UInt(ImmUnion.maxLen.W)
257  val commitType = CommitType()
258  val fpu = new FPUCtrlSignals
259  val isMove = Bool()
260
261  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
262    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
263    val signals =
264      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
265        isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
266    signals zip decoder map { case (s, d) => s := d }
267    commitType := DontCare
268    this
269  }
270}
271
272class CfCtrl extends XSBundle {
273  val cf = new CtrlFlow
274  val ctrl = new CtrlSignals
275}
276
277class PerfDebugInfo extends XSBundle {
278  val src1MoveElim = Bool()
279  val src2MoveElim = Bool()
280  // val fetchTime = UInt(64.W)
281  val renameTime = UInt(64.W)
282  val dispatchTime = UInt(64.W)
283  val issueTime = UInt(64.W)
284  val writebackTime = UInt(64.W)
285  // val commitTime = UInt(64.W)
286}
287
288// Separate LSQ
289class LSIdx extends XSBundle {
290  val lqIdx = new LqPtr
291  val sqIdx = new SqPtr
292}
293
294// CfCtrl -> MicroOp at Rename Stage
295class MicroOp extends CfCtrl {
296  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
297  val src1State, src2State, src3State = SrcState()
298  val roqIdx = new RoqPtr
299  val lqIdx = new LqPtr
300  val sqIdx = new SqPtr
301  val diffTestDebugLrScValid = Bool()
302  val debugInfo = new PerfDebugInfo
303}
304
305class Redirect extends XSBundle {
306  val roqIdx = new RoqPtr
307  val ftqIdx = new FtqPtr
308  val ftqOffset = UInt(log2Up(PredictWidth).W)
309  val level = RedirectLevel()
310  val interrupt = Bool()
311  val cfiUpdate = new CfiUpdateInfo
312
313
314  // def isUnconditional() = RedirectLevel.isUnconditional(level)
315  def flushItself() = RedirectLevel.flushItself(level)
316  // def isException() = RedirectLevel.isException(level)
317}
318
319class Dp1ToDp2IO extends XSBundle {
320  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
321  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
322  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
323}
324
325class ReplayPregReq extends XSBundle {
326  // NOTE: set isInt and isFp both to 'false' when invalid
327  val isInt = Bool()
328  val isFp = Bool()
329  val preg = UInt(PhyRegIdxWidth.W)
330}
331
332class DebugBundle extends XSBundle {
333  val isMMIO = Bool()
334  val isPerfCnt = Bool()
335  val paddr = UInt(PAddrBits.W)
336}
337
338class ExuInput extends XSBundle {
339  val uop = new MicroOp
340  val src1, src2, src3 = UInt((XLEN + 1).W)
341}
342
343class ExuOutput extends XSBundle {
344  val uop = new MicroOp
345  val data = UInt((XLEN + 1).W)
346  val fflags = UInt(5.W)
347  val redirectValid = Bool()
348  val redirect = new Redirect
349  val debug = new DebugBundle
350}
351
352class ExternalInterruptIO extends XSBundle {
353  val mtip = Input(Bool())
354  val msip = Input(Bool())
355  val meip = Input(Bool())
356}
357
358class CSRSpecialIO extends XSBundle {
359  val exception = Flipped(ValidIO(new MicroOp))
360  val isInterrupt = Input(Bool())
361  val memExceptionVAddr = Input(UInt(VAddrBits.W))
362  val trapTarget = Output(UInt(VAddrBits.W))
363  val externalInterrupt = new ExternalInterruptIO
364  val interrupt = Output(Bool())
365}
366
367class ExceptionInfo extends XSBundle {
368  val uop = new MicroOp
369  val isInterrupt = Bool()
370}
371
372class RoqCommitInfo extends XSBundle {
373  val ldest = UInt(5.W)
374  val rfWen = Bool()
375  val fpWen = Bool()
376  val wflags = Bool()
377  val commitType = CommitType()
378  val pdest = UInt(PhyRegIdxWidth.W)
379  val old_pdest = UInt(PhyRegIdxWidth.W)
380  val ftqIdx = new FtqPtr
381  val ftqOffset = UInt(log2Up(PredictWidth).W)
382
383  // these should be optimized for synthesis verilog
384  val pc = UInt(VAddrBits.W)
385}
386
387class RoqCommitIO extends XSBundle {
388  val isWalk = Output(Bool())
389  val valid = Vec(CommitWidth, Output(Bool()))
390  val info = Vec(CommitWidth, Output(new RoqCommitInfo))
391
392  def hasWalkInstr = isWalk && valid.asUInt.orR
393
394  def hasCommitInstr = !isWalk && valid.asUInt.orR
395}
396
397class TlbFeedback extends XSBundle {
398  val rsIdx = UInt(log2Up(IssQueSize).W)
399  val hit = Bool()
400  val flushState = Bool()
401}
402
403class RSFeedback extends TlbFeedback
404
405class FrontendToBackendIO extends XSBundle {
406  // to backend end
407  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
408  val fetchInfo = DecoupledIO(new FtqEntry)
409  // from backend
410  val redirect_cfiUpdate = Flipped(ValidIO(new Redirect))
411  val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry))
412  val ftqEnqPtr = Input(new FtqPtr)
413  val ftqLeftOne = Input(Bool())
414}
415
416class TlbCsrBundle extends XSBundle {
417  val satp = new Bundle {
418    val mode = UInt(4.W) // TODO: may change number to parameter
419    val asid = UInt(16.W)
420    val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
421  }
422  val priv = new Bundle {
423    val mxr = Bool()
424    val sum = Bool()
425    val imode = UInt(2.W)
426    val dmode = UInt(2.W)
427  }
428
429  override def toPrintable: Printable = {
430    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
431      p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
432  }
433}
434
435class SfenceBundle extends XSBundle {
436  val valid = Bool()
437  val bits = new Bundle {
438    val rs1 = Bool()
439    val rs2 = Bool()
440    val addr = UInt(VAddrBits.W)
441  }
442
443  override def toPrintable: Printable = {
444    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
445  }
446}
447
448class WaitTableUpdateReq extends XSBundle with WaitTableParameters {
449  val valid = Bool()
450  val waddr = UInt(WaitTableAddrWidth.W)
451  val wdata = Bool() // true.B by default
452}
453
454class DifftestBundle extends XSBundle {
455  val fromSbuffer = new Bundle() {
456    val sbufferResp = Output(Bool())
457    val sbufferAddr = Output(UInt(64.W))
458    val sbufferData = Output(Vec(64, UInt(8.W)))
459    val sbufferMask = Output(UInt(64.W))
460  }
461  val fromSQ = new Bundle() {
462    val storeCommit = Output(UInt(2.W))
463    val storeAddr   = Output(Vec(2, UInt(64.W)))
464    val storeData   = Output(Vec(2, UInt(64.W)))
465    val storeMask   = Output(Vec(2, UInt(8.W)))
466  }
467  val fromXSCore = new Bundle() {
468    val r = Output(Vec(64, UInt(XLEN.W)))
469  }
470  val fromCSR = new Bundle() {
471    val intrNO = Output(UInt(64.W))
472    val cause = Output(UInt(64.W))
473    val priviledgeMode = Output(UInt(2.W))
474    val mstatus = Output(UInt(64.W))
475    val sstatus = Output(UInt(64.W))
476    val mepc = Output(UInt(64.W))
477    val sepc = Output(UInt(64.W))
478    val mtval = Output(UInt(64.W))
479    val stval = Output(UInt(64.W))
480    val mtvec = Output(UInt(64.W))
481    val stvec = Output(UInt(64.W))
482    val mcause = Output(UInt(64.W))
483    val scause = Output(UInt(64.W))
484    val satp = Output(UInt(64.W))
485    val mip = Output(UInt(64.W))
486    val mie = Output(UInt(64.W))
487    val mscratch = Output(UInt(64.W))
488    val sscratch = Output(UInt(64.W))
489    val mideleg = Output(UInt(64.W))
490    val medeleg = Output(UInt(64.W))
491  }
492  val fromRoq = new Bundle() {
493    val commit = Output(UInt(32.W))
494    val thisPC = Output(UInt(XLEN.W))
495    val thisINST = Output(UInt(32.W))
496    val skip = Output(UInt(32.W))
497    val wen = Output(UInt(32.W))
498    val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
499    val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6
500    val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
501    val lpaddr = Output(Vec(CommitWidth, UInt(64.W)))
502    val ltype = Output(Vec(CommitWidth, UInt(32.W)))
503    val lfu = Output(Vec(CommitWidth, UInt(4.W)))
504    val isRVC = Output(UInt(32.W))
505    val scFailed = Output(Bool())
506  }
507  val fromAtomic = new Bundle() {
508    val atomicResp = Output(Bool())
509    val atomicAddr = Output(UInt(64.W))
510    val atomicData = Output(UInt(64.W))
511    val atomicMask = Output(UInt(8.W))
512    val atomicFuop = Output(UInt(8.W))
513    val atomicOut  = Output(UInt(64.W))
514  }
515  val fromPtw = new Bundle() {
516    val ptwResp = Output(Bool())
517    val ptwAddr = Output(UInt(64.W))
518    val ptwData = Output(Vec(4, UInt(64.W)))
519  }
520}
521
522class TrapIO extends XSBundle {
523  val valid = Output(Bool())
524  val code = Output(UInt(3.W))
525  val pc = Output(UInt(VAddrBits.W))
526  val cycleCnt = Output(UInt(XLEN.W))
527  val instrCnt = Output(UInt(XLEN.W))
528}
529
530class PerfInfoIO extends XSBundle {
531  val clean = Input(Bool())
532  val dump = Input(Bool())
533}
534
535class CustomCSRCtrlIO extends XSBundle {
536  // Prefetcher
537  val l1plus_pf_enable = Output(Bool())
538  val l2_pf_enable = Output(Bool())
539  // Labeled XiangShan
540  val dsid = Output(UInt(8.W)) // TODO: DsidWidth as parameter
541  // Load violation predictor
542  val lvpred_disable = Output(Bool())
543  val no_spec_load = Output(Bool())
544  val waittable_timeout = Output(UInt(5.W))
545  // Branch predictor
546  val bp_ctrl = Output(new BPUCtrl)
547  // Memory Block
548  val sbuffer_threshold = Output(UInt(4.W))
549  // Rename
550  val move_elim_enable = Output(Bool())
551}
552