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