xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision b086c6da80b5e7e939f9ce8dde0b13f881c26a65)
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 typ = UInt(2.W)
198  val fmt = UInt(2.W)
199  val ren3 = Bool() //TODO: remove SrcType.fp
200}
201
202// Decode DecodeWidth insts at Decode Stage
203class CtrlSignals extends XSBundle {
204  val src1Type, src2Type, src3Type = SrcType()
205  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
206  val ldest = UInt(5.W)
207  val fuType = FuType()
208  val fuOpType = FuOpType()
209  val rfWen = Bool()
210  val fpWen = Bool()
211  val isXSTrap = Bool()
212  val noSpecExec = Bool()  // wait forward
213  val blockBackward  = Bool()  // block backward
214  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
215  val isRVF = Bool()
216  val selImm = SelImm()
217  val imm = UInt(XLEN.W)
218  val commitType = CommitType()
219  val fpu = new FPUCtrlSignals
220
221  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
222    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
223    val signals =
224      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
225          isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
226    signals zip decoder map { case(s, d) => s := d }
227    commitType := DontCare
228    this
229  }
230}
231
232class CfCtrl extends XSBundle {
233  val cf = new CtrlFlow
234  val ctrl = new CtrlSignals
235  val brTag = new BrqPtr
236}
237
238class LSIdx extends XSBundle {
239  val lqIdx = new LqPtr
240  val sqIdx = new SqPtr
241}
242
243// CfCtrl -> MicroOp at Rename Stage
244class MicroOp extends CfCtrl {
245  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
246  val src1State, src2State, src3State = SrcState()
247  val roqIdx = new RoqPtr
248  val lqIdx = new LqPtr
249  val sqIdx = new SqPtr
250  val diffTestDebugLrScValid = Bool()
251}
252
253class Redirect extends XSBundle {
254  val roqIdx = new RoqPtr
255  val level = RedirectLevel()
256  val interrupt = Bool()
257  val pc = UInt(VAddrBits.W)
258  val target = UInt(VAddrBits.W)
259  val brTag = new BrqPtr
260
261  def isUnconditional() = RedirectLevel.isUnconditional(level)
262  def flushItself() = RedirectLevel.flushItself(level)
263  def isException() = RedirectLevel.isException(level)
264}
265
266class Dp1ToDp2IO extends XSBundle {
267  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
268  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
269  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
270}
271
272class ReplayPregReq extends XSBundle {
273  // NOTE: set isInt and isFp both to 'false' when invalid
274  val isInt = Bool()
275  val isFp = Bool()
276  val preg = UInt(PhyRegIdxWidth.W)
277}
278
279class DebugBundle extends XSBundle{
280  val isMMIO = Bool()
281}
282
283class ExuInput extends XSBundle {
284  val uop = new MicroOp
285  val src1, src2, src3 = UInt((XLEN+1).W)
286}
287
288class ExuOutput extends XSBundle {
289  val uop = new MicroOp
290  val data = UInt((XLEN+1).W)
291  val fflags  = UInt(5.W)
292  val redirectValid = Bool()
293  val redirect = new Redirect
294  val brUpdate = new CfiUpdateInfo
295  val debug = new DebugBundle
296}
297
298class ExternalInterruptIO extends XSBundle {
299  val mtip = Input(Bool())
300  val msip = Input(Bool())
301  val meip = Input(Bool())
302}
303
304class CSRSpecialIO extends XSBundle {
305  val exception = Flipped(ValidIO(new MicroOp))
306  val isInterrupt = Input(Bool())
307  val memExceptionVAddr = Input(UInt(VAddrBits.W))
308  val trapTarget = Output(UInt(VAddrBits.W))
309  val externalInterrupt = new ExternalInterruptIO
310  val interrupt = Output(Bool())
311}
312
313class RoqCommitInfo extends XSBundle {
314  val ldest = UInt(5.W)
315  val rfWen = Bool()
316  val fpWen = Bool()
317  val wflags = Bool()
318  val commitType = CommitType()
319  val pdest = UInt(PhyRegIdxWidth.W)
320  val old_pdest = UInt(PhyRegIdxWidth.W)
321  val lqIdx = new LqPtr
322  val sqIdx = new SqPtr
323
324  // these should be optimized for synthesis verilog
325  val pc = UInt(VAddrBits.W)
326}
327
328class RoqCommitIO extends XSBundle {
329  val isWalk = Output(Bool())
330  val valid = Vec(CommitWidth, Output(Bool()))
331  val info = Vec(CommitWidth, Output(new RoqCommitInfo))
332
333  def hasWalkInstr = isWalk && valid.asUInt.orR
334  def hasCommitInstr = !isWalk && valid.asUInt.orR
335}
336
337class TlbFeedback extends XSBundle {
338  val roqIdx = new RoqPtr
339  val hit = Bool()
340}
341
342class FrontendToBackendIO extends XSBundle {
343  // to backend end
344  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
345  // from backend
346  val redirect = Flipped(ValidIO(UInt(VAddrBits.W)))
347  // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo))
348  val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo))
349}
350
351class TlbCsrBundle extends XSBundle {
352  val satp = new Bundle {
353    val mode = UInt(4.W) // TODO: may change number to parameter
354    val asid = UInt(16.W)
355    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
356  }
357  val priv = new Bundle {
358    val mxr = Bool()
359    val sum = Bool()
360    val imode = UInt(2.W)
361    val dmode = UInt(2.W)
362  }
363
364  override def toPrintable: Printable = {
365    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
366    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
367  }
368}
369
370class SfenceBundle extends XSBundle {
371  val valid = Bool()
372  val bits = new Bundle {
373    val rs1 = Bool()
374    val rs2 = Bool()
375    val addr = UInt(VAddrBits.W)
376  }
377
378  override def toPrintable: Printable = {
379    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
380  }
381}
382