xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 53c7ab8b2e79de0fa70909ea81a51720f666aaeb)
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 pd = Vec(PredictWidth, new PreDecodeInfo)
29  val ipf = Bool()
30  val acf = Bool()
31  val crossPageIPFFix = Bool()
32  val pred_taken = UInt(PredictWidth.W)
33  val ftqPtr = new FtqPtr
34}
35
36class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
37  val valid = Bool()
38  val bits = gen.cloneType.asInstanceOf[T]
39
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
52  def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map { case (_, cb, _) => 1 << cb }.reduce(_ + _))
53
54  def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal + 1)) + 1
55
56  val tageTaken = if (useSC) Bool() else UInt(0.W)
57  val scUsed = if (useSC) Bool() else UInt(0.W)
58  val scPred = if (useSC) Bool() else UInt(0.W)
59  // Suppose ctrbits of all tables are identical
60  val ctrs = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
61  val sumAbs = if (useSC) UInt(sumCtrBits.W) else UInt(0.W)
62}
63
64class TageMeta extends XSBundle with HasTageParameter {
65  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
66  val altDiffers = Bool()
67  val providerU = UInt(2.W)
68  val providerCtr = UInt(3.W)
69  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
70  val taken = Bool()
71  val scMeta = new SCMeta(EnableSC)
72}
73
74@chiselName
75class BranchPrediction extends XSBundle with HasIFUConst {
76  // val redirect = Bool()
77  val takens = UInt(PredictWidth.W)
78  // val jmpIdx = UInt(log2Up(PredictWidth).W)
79  val brMask = UInt(PredictWidth.W)
80  val jalMask = UInt(PredictWidth.W)
81  val targets = Vec(PredictWidth, UInt(VAddrBits.W))
82
83  // half RVI could only start at the end of a packet
84  val hasHalfRVI = Bool()
85
86  def brNotTakens = (~takens & brMask)
87
88  def sawNotTakenBr = VecInit((0 until PredictWidth).map(i =>
89    (if (i == 0) false.B else ParallelORR(brNotTakens(i - 1, 0)))))
90
91  // if not taken before the half RVI inst
92  def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth - 2, 0)))
93
94  // could get PredictWidth-1 when only the first bank is valid
95  def jmpIdx = ParallelPriorityEncoder(takens)
96
97  // only used when taken
98  def target = {
99    val generator = new PriorityMuxGenerator[UInt]
100    generator.register(takens.asBools, targets, List.fill(PredictWidth)(None))
101    generator()
102  }
103
104  def taken = ParallelORR(takens)
105
106  def takenOnBr = taken && ParallelPriorityMux(takens, brMask.asBools)
107
108  def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(takens, sawNotTakenBr), ParallelORR(brNotTakens))
109}
110
111class PredictorAnswer extends XSBundle {
112  val hit    = if (!env.FPGAPlatform) Bool() else UInt(0.W)
113  val taken  = if (!env.FPGAPlatform) Bool() else UInt(0.W)
114  val target = if (!env.FPGAPlatform) UInt(VAddrBits.W) else UInt(0.W)
115}
116
117class BpuMeta extends XSBundle with HasBPUParameter {
118  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
119  val ubtbHits = Bool()
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 = 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
191  val hasLastPrev = Bool()
192  // prediction metas
193  val hist = new GlobalHistory
194  val predHist = new GlobalHistory
195  val rasSp = UInt(log2Ceil(RasSize).W)
196  val rasTop = new RASEntry()
197  val specCnt = Vec(PredictWidth, UInt(10.W))
198  val metas = Vec(PredictWidth, new BpuMeta)
199
200  val cfiIsCall, cfiIsRet, cfiIsRVC = Bool()
201  val rvc_mask = Vec(PredictWidth, Bool())
202  val br_mask = Vec(PredictWidth, Bool())
203  val cfiIndex = ValidUndirectioned(UInt(log2Up(PredictWidth).W))
204  val valids = Vec(PredictWidth, Bool())
205
206  // backend update
207  val mispred = Vec(PredictWidth, Bool())
208  val target = UInt(VAddrBits.W)
209
210  def takens = VecInit((0 until PredictWidth).map(i => cfiIndex.valid && cfiIndex.bits === i.U))
211
212  override def toPrintable: Printable = {
213    p"ftqPC: ${Hexadecimal(ftqPC)} hasLastPrec:$hasLastPrev " +
214      p"rasSp:$rasSp specCnt:$specCnt brmask:${Binary(Cat(br_mask))} rvcmask:${Binary(Cat(rvc_mask))} " +
215      p"valids:${Binary(valids.asUInt())} cfi valid: ${cfiIndex.valid} " +
216      p"cfi index: ${cfiIndex.bits} isCall:$cfiIsCall isRet:$cfiIsRet isRvc:$cfiIsRVC " +
217      p"mispred:${Binary(Cat(mispred))} target:${Hexadecimal(target)}\n"
218  }
219
220}
221
222
223class FPUCtrlSignals extends XSBundle {
224  val isAddSub = Bool() // swap23
225  val typeTagIn = UInt(2.W)
226  val typeTagOut = UInt(2.W)
227  val fromInt = Bool()
228  val wflags = Bool()
229  val fpWen = Bool()
230  val fmaCmd = UInt(2.W)
231  val div = Bool()
232  val sqrt = Bool()
233  val fcvt = Bool()
234  val typ = UInt(2.W)
235  val fmt = UInt(2.W)
236  val ren3 = Bool() //TODO: remove SrcType.fp
237}
238
239// Decode DecodeWidth insts at Decode Stage
240class CtrlSignals extends XSBundle {
241  val src1Type, src2Type, src3Type = SrcType()
242  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
243  val ldest = UInt(5.W)
244  val fuType = FuType()
245  val fuOpType = FuOpType()
246  val rfWen = Bool()
247  val fpWen = Bool()
248  val isXSTrap = Bool()
249  val noSpecExec = Bool() // wait forward
250  val blockBackward = Bool() // block backward
251  val flushPipe = Bool() // This inst will flush all the pipe when commit, like exception but can commit
252  val isRVF = Bool()
253  val selImm = SelImm()
254  val imm = UInt(ImmUnion.maxLen.W)
255  val commitType = CommitType()
256  val fpu = new FPUCtrlSignals
257
258  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
259    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
260    val signals =
261      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
262        isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
263    signals zip decoder map { case (s, d) => s := d }
264    commitType := DontCare
265    this
266  }
267}
268
269class CfCtrl extends XSBundle {
270  val cf = new CtrlFlow
271  val ctrl = new CtrlSignals
272}
273
274class PerfDebugInfo extends XSBundle {
275  // val fetchTime = UInt(64.W)
276  val renameTime = UInt(64.W)
277  val dispatchTime = UInt(64.W)
278  val issueTime = UInt(64.W)
279  val writebackTime = UInt(64.W)
280  // val commitTime = UInt(64.W)
281}
282
283// Separate LSQ
284class LSIdx extends XSBundle {
285  val lqIdx = new LqPtr
286  val sqIdx = new SqPtr
287}
288
289// CfCtrl -> MicroOp at Rename Stage
290class MicroOp extends CfCtrl {
291  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
292  val src1State, src2State, src3State = SrcState()
293  val roqIdx = new RoqPtr
294  val lqIdx = new LqPtr
295  val sqIdx = new SqPtr
296  val diffTestDebugLrScValid = Bool()
297  val debugInfo = new PerfDebugInfo
298}
299
300class Redirect extends XSBundle {
301  val roqIdx = new RoqPtr
302  val ftqIdx = new FtqPtr
303  val ftqOffset = UInt(log2Up(PredictWidth).W)
304  val level = RedirectLevel()
305  val interrupt = Bool()
306  val cfiUpdate = new CfiUpdateInfo
307
308
309  // def isUnconditional() = RedirectLevel.isUnconditional(level)
310  def flushItself() = RedirectLevel.flushItself(level)
311  // def isException() = RedirectLevel.isException(level)
312}
313
314class Dp1ToDp2IO extends XSBundle {
315  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
316  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
317  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
318}
319
320class ReplayPregReq extends XSBundle {
321  // NOTE: set isInt and isFp both to 'false' when invalid
322  val isInt = Bool()
323  val isFp = Bool()
324  val preg = UInt(PhyRegIdxWidth.W)
325}
326
327class DebugBundle extends XSBundle {
328  val isMMIO = Bool()
329  val isPerfCnt = Bool()
330}
331
332class ExuInput extends XSBundle {
333  val uop = new MicroOp
334  val src1, src2, src3 = UInt((XLEN + 1).W)
335}
336
337class ExuOutput extends XSBundle {
338  val uop = new MicroOp
339  val data = UInt((XLEN + 1).W)
340  val fflags = UInt(5.W)
341  val redirectValid = Bool()
342  val redirect = new Redirect
343  val debug = new DebugBundle
344}
345
346class ExternalInterruptIO extends XSBundle {
347  val mtip = Input(Bool())
348  val msip = Input(Bool())
349  val meip = Input(Bool())
350}
351
352class CSRSpecialIO extends XSBundle {
353  val exception = Flipped(ValidIO(new MicroOp))
354  val isInterrupt = Input(Bool())
355  val memExceptionVAddr = Input(UInt(VAddrBits.W))
356  val trapTarget = Output(UInt(VAddrBits.W))
357  val externalInterrupt = new ExternalInterruptIO
358  val interrupt = Output(Bool())
359}
360
361class RoqCommitInfo extends XSBundle {
362  val ldest = UInt(5.W)
363  val rfWen = Bool()
364  val fpWen = Bool()
365  val wflags = Bool()
366  val commitType = CommitType()
367  val pdest = UInt(PhyRegIdxWidth.W)
368  val old_pdest = UInt(PhyRegIdxWidth.W)
369  val ftqIdx = new FtqPtr
370  val ftqOffset = UInt(log2Up(PredictWidth).W)
371
372  // these should be optimized for synthesis verilog
373  val pc = UInt(VAddrBits.W)
374}
375
376class RoqCommitIO extends XSBundle {
377  val isWalk = Output(Bool())
378  val valid = Vec(CommitWidth, Output(Bool()))
379  val info = Vec(CommitWidth, Output(new RoqCommitInfo))
380
381  def hasWalkInstr = isWalk && valid.asUInt.orR
382
383  def hasCommitInstr = !isWalk && valid.asUInt.orR
384}
385
386class TlbFeedback extends XSBundle {
387  val rsIdx = UInt(log2Up(IssQueSize).W)
388  val hit = Bool()
389}
390
391class RSFeedback extends TlbFeedback
392
393class FrontendToBackendIO extends XSBundle {
394  // to backend end
395  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
396  val fetchInfo = DecoupledIO(new FtqEntry)
397  // from backend
398  val redirect_cfiUpdate = Flipped(ValidIO(new Redirect))
399  val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry))
400  val ftqEnqPtr = Input(new FtqPtr)
401  val ftqLeftOne = Input(Bool())
402}
403
404class TlbCsrBundle extends XSBundle {
405  val satp = new Bundle {
406    val mode = UInt(4.W) // TODO: may change number to parameter
407    val asid = UInt(16.W)
408    val ppn = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
409  }
410  val priv = new Bundle {
411    val mxr = Bool()
412    val sum = Bool()
413    val imode = UInt(2.W)
414    val dmode = UInt(2.W)
415  }
416
417  override def toPrintable: Printable = {
418    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
419      p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
420  }
421}
422
423class SfenceBundle extends XSBundle {
424  val valid = Bool()
425  val bits = new Bundle {
426    val rs1 = Bool()
427    val rs2 = Bool()
428    val addr = UInt(VAddrBits.W)
429  }
430
431  override def toPrintable: Printable = {
432    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
433  }
434}
435
436class DifftestBundle extends XSBundle {
437  val fromSbuffer = new Bundle() {
438    val sbufferResp = Output(Bool())
439    val sbufferAddr = Output(UInt(64.W))
440    val sbufferData = Output(Vec(64, UInt(8.W)))
441    val sbufferMask = Output(UInt(64.W))
442  }
443  val fromSQ = new Bundle() {
444    val storeCommit = Output(UInt(2.W))
445    val storeAddr   = Output(Vec(2, UInt(64.W)))
446    val storeData   = Output(Vec(2, UInt(64.W)))
447    val storeMask   = Output(Vec(2, UInt(8.W)))
448  }
449  val fromXSCore = new Bundle() {
450    val r = Output(Vec(64, UInt(XLEN.W)))
451  }
452  val fromCSR = new Bundle() {
453    val intrNO = Output(UInt(64.W))
454    val cause = Output(UInt(64.W))
455    val priviledgeMode = Output(UInt(2.W))
456    val mstatus = Output(UInt(64.W))
457    val sstatus = Output(UInt(64.W))
458    val mepc = Output(UInt(64.W))
459    val sepc = Output(UInt(64.W))
460    val mtval = Output(UInt(64.W))
461    val stval = Output(UInt(64.W))
462    val mtvec = Output(UInt(64.W))
463    val stvec = Output(UInt(64.W))
464    val mcause = Output(UInt(64.W))
465    val scause = Output(UInt(64.W))
466    val satp = Output(UInt(64.W))
467    val mip = Output(UInt(64.W))
468    val mie = Output(UInt(64.W))
469    val mscratch = Output(UInt(64.W))
470    val sscratch = Output(UInt(64.W))
471    val mideleg = Output(UInt(64.W))
472    val medeleg = Output(UInt(64.W))
473  }
474  val fromRoq = new Bundle() {
475    val commit = Output(UInt(32.W))
476    val thisPC = Output(UInt(XLEN.W))
477    val thisINST = Output(UInt(32.W))
478    val skip = Output(UInt(32.W))
479    val wen = Output(UInt(32.W))
480    val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
481    val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6
482    val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6
483    val isRVC = Output(UInt(32.W))
484    val scFailed = Output(Bool())
485  }
486}
487
488class TrapIO extends XSBundle {
489  val valid = Output(Bool())
490  val code = Output(UInt(3.W))
491  val pc = Output(UInt(VAddrBits.W))
492  val cycleCnt = Output(UInt(XLEN.W))
493  val instrCnt = Output(UInt(XLEN.W))
494}