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