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