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