xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 818ec9f9724a96d5d772ea7e5940361db4d08d72)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.SelImm
6import xiangshan.backend.brq.BrqPtr
7import xiangshan.backend.fu.fpu.Fflags
8import xiangshan.backend.rename.FreeListPtr
9import xiangshan.backend.roq.RoqPtr
10import xiangshan.backend.decode.XDecode
11import xiangshan.mem.{LqPtr, SqPtr}
12import xiangshan.frontend.PreDecodeInfo
13import xiangshan.frontend.HasBPUParameter
14import xiangshan.frontend.HasTageParameter
15import xiangshan.frontend.HasIFUConst
16import xiangshan.frontend.GlobalHistory
17import utils._
18import scala.math.max
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 pc = UInt(VAddrBits.W)
25  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
26  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
27  val brInfo = Vec(PredictWidth, new BranchInfo)
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 BranchInfo 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 hist = new GlobalHistory
131  val predHist = new GlobalHistory
132  val sawNotTakenBranch = Bool()
133
134  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
135  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
136  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
137
138  // def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
139  //   this.histPtr := histPtr
140  //   this.tageMeta := tageMeta
141  //   this.rasSp := rasSp
142  //   this.rasTopCtr := rasTopCtr
143  //   this.asUInt
144  // }
145  def size = 0.U.asTypeOf(this).getWidth
146  def fromUInt(x: UInt) = x.asTypeOf(this)
147}
148
149class Predecode extends XSBundle with HasIFUConst {
150  val hasLastHalfRVI = Bool()
151  val mask = UInt((FetchWidth*2).W)
152  val lastHalf = UInt(nBanksInPacket.W)
153  val pd = Vec(FetchWidth*2, (new PreDecodeInfo))
154}
155
156class BranchUpdateInfo extends XSBundle {
157  // from backend
158  val pc = UInt(VAddrBits.W)
159  val pnpc = UInt(VAddrBits.W)
160  val target = UInt(VAddrBits.W)
161  val brTarget = UInt(VAddrBits.W)
162  val taken = Bool()
163  val fetchIdx = UInt(log2Up(FetchWidth*2).W)
164  val isMisPred = Bool()
165  val brTag = new BrqPtr
166
167  // frontend -> backend -> frontend
168  val pd = new PreDecodeInfo
169  val brInfo = new BranchInfo
170}
171
172// Dequeue DecodeWidth insts from Ibuffer
173class CtrlFlow extends XSBundle {
174  val instr = UInt(32.W)
175  val pc = UInt(VAddrBits.W)
176  val exceptionVec = Vec(16, Bool())
177  val intrVec = Vec(12, Bool())
178  val brUpdate = new BranchUpdateInfo
179  val crossPageIPFFix = Bool()
180}
181
182// Decode DecodeWidth insts at Decode Stage
183class CtrlSignals extends XSBundle {
184  val src1Type, src2Type, src3Type = SrcType()
185  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
186  val ldest = UInt(5.W)
187  val fuType = FuType()
188  val fuOpType = FuOpType()
189  val rfWen = Bool()
190  val fpWen = Bool()
191  val isXSTrap = Bool()
192  val noSpecExec = Bool()  // wait forward
193  val blockBackward  = Bool()  // block backward
194  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
195  val isRVF = Bool()
196  val selImm = SelImm()
197  val imm = UInt(XLEN.W)
198  val commitType = CommitType()
199
200  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
201    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
202    val signals =
203      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
204          isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
205    signals zip decoder map { case(s, d) => s := d }
206    commitType := DontCare
207    this
208  }
209}
210
211class CfCtrl extends XSBundle {
212  val cf = new CtrlFlow
213  val ctrl = new CtrlSignals
214  val brTag = new BrqPtr
215}
216
217// Load / Store Index
218//
219// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type.
220trait HasLSIdx { this: HasXSParameter =>
221  // Separate LSQ
222  val lqIdx = new LqPtr
223  val sqIdx = new SqPtr
224}
225
226class LSIdx extends XSBundle with HasLSIdx {}
227
228// CfCtrl -> MicroOp at Rename Stage
229class MicroOp extends CfCtrl with HasLSIdx {
230  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
231  val src1State, src2State, src3State = SrcState()
232  val roqIdx = new RoqPtr
233  val diffTestDebugLrScValid = Bool()
234}
235
236class Redirect extends XSBundle {
237  val roqIdx = new RoqPtr
238  val isException = Bool()
239  val isMisPred = Bool()
240  val isReplay = Bool()
241  val isFlushPipe = Bool()
242  val pc = UInt(VAddrBits.W)
243  val target = UInt(VAddrBits.W)
244  val brTag = new BrqPtr
245}
246
247class Dp1ToDp2IO extends XSBundle {
248  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
249  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
250  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
251}
252
253class ReplayPregReq extends XSBundle {
254  // NOTE: set isInt and isFp both to 'false' when invalid
255  val isInt = Bool()
256  val isFp = Bool()
257  val preg = UInt(PhyRegIdxWidth.W)
258}
259
260class DebugBundle extends XSBundle{
261  val isMMIO = Bool()
262}
263
264class ExuInput extends XSBundle {
265  val uop = new MicroOp
266  val src1, src2, src3 = UInt((XLEN+1).W)
267}
268
269class ExuOutput extends XSBundle {
270  val uop = new MicroOp
271  val data = UInt((XLEN+1).W)
272  val fflags  = new Fflags
273  val redirectValid = Bool()
274  val redirect = new Redirect
275  val brUpdate = new BranchUpdateInfo
276  val debug = new DebugBundle
277}
278
279class ExternalInterruptIO extends XSBundle {
280  val mtip = Input(Bool())
281  val msip = Input(Bool())
282  val meip = Input(Bool())
283}
284
285class CSRSpecialIO extends XSBundle {
286  val exception = Flipped(ValidIO(new MicroOp))
287  val isInterrupt = Input(Bool())
288  val memExceptionVAddr = Input(UInt(VAddrBits.W))
289  val trapTarget = Output(UInt(VAddrBits.W))
290  val externalInterrupt = new ExternalInterruptIO
291  val interrupt = Output(Bool())
292}
293
294//class ExuIO extends XSBundle {
295//  val in = Flipped(DecoupledIO(new ExuInput))
296//  val redirect = Flipped(ValidIO(new Redirect))
297//  val out = DecoupledIO(new ExuOutput)
298//  // for csr
299//  val csrOnly = new CSRSpecialIO
300//  val mcommit = Input(UInt(3.W))
301//}
302
303class RoqCommitIO extends XSBundle {
304  val isWalk = Output(Bool())
305  val valid = Vec(CommitWidth, Output(Bool()))
306  val uop = Vec(CommitWidth, Output(new MicroOp))
307
308  def hasWalkInstr = isWalk && valid.asUInt.orR
309  def hasCommitInstr = !isWalk && valid.asUInt.orR
310}
311
312class TlbFeedback extends XSBundle {
313  val roqIdx = new RoqPtr
314  val hit = Bool()
315}
316
317class FrontendToBackendIO extends XSBundle {
318  // to backend end
319  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
320  // from backend
321  val redirect = Flipped(ValidIO(UInt(VAddrBits.W)))
322  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
323  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
324}
325
326class TlbCsrBundle extends XSBundle {
327  val satp = new Bundle {
328    val mode = UInt(4.W) // TODO: may change number to parameter
329    val asid = UInt(16.W)
330    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
331  }
332  val priv = new Bundle {
333    val mxr = Bool()
334    val sum = Bool()
335    val imode = UInt(2.W)
336    val dmode = UInt(2.W)
337  }
338
339  override def toPrintable: Printable = {
340    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
341    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
342  }
343}
344
345class SfenceBundle extends XSBundle {
346  val valid = Bool()
347  val bits = new Bundle {
348    val rs1 = Bool()
349    val rs2 = Bool()
350    val addr = UInt(VAddrBits.W)
351  }
352
353  override def toPrintable: Printable = {
354    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
355  }
356}
357