xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 7e6acce3068fd7397d644527e57588635956aa6d)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.brq.BrqPtr
6import xiangshan.backend.fu.fpu.Fflags
7import xiangshan.backend.rename.FreeListPtr
8import xiangshan.backend.roq.RoqPtr
9import xiangshan.mem.{LqPtr, SqPtr}
10import xiangshan.frontend.PreDecodeInfo
11import xiangshan.frontend.HasBPUParameter
12import xiangshan.frontend.HasTageParameter
13import xiangshan.frontend.HasIFUConst
14import utils._
15import scala.math.max
16
17// Fetch FetchWidth x 32-bit insts from Icache
18class FetchPacket extends XSBundle {
19  val instrs = Vec(PredictWidth, UInt(32.W))
20  val mask = UInt(PredictWidth.W)
21  // val pc = UInt(VAddrBits.W)
22  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
23  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
24  val brInfo = Vec(PredictWidth, new BranchInfo)
25  val pd = Vec(PredictWidth, new PreDecodeInfo)
26  val ipf = Bool()
27  val acf = Bool()
28  val crossPageIPFFix = Bool()
29  val predTaken = Bool()
30}
31
32class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
33  val valid = Bool()
34  val bits = gen.cloneType.asInstanceOf[T]
35  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
36}
37
38object ValidUndirectioned {
39  def apply[T <: Data](gen: T) = {
40    new ValidUndirectioned[T](gen)
41  }
42}
43
44class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter {
45  def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_)
46  def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_))
47  def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1
48  val tageTaken = if (useSC) Bool() else UInt(0.W)
49  val scUsed    = if (useSC) Bool() else UInt(0.W)
50  val scPred    = if (useSC) Bool() else UInt(0.W)
51  // Suppose ctrbits of all tables are identical
52  val ctrs      = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
53  val sumAbs    = if (useSC) UInt(sumCtrBits.W) else UInt(0.W)
54}
55
56class TageMeta extends XSBundle with HasTageParameter {
57  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
58  val altDiffers = Bool()
59  val providerU = UInt(2.W)
60  val providerCtr = UInt(3.W)
61  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
62  val taken = Bool()
63  val scMeta = new SCMeta(EnableSC)
64}
65
66class BranchPrediction extends XSBundle with HasIFUConst {
67  // val redirect = Bool()
68  val takens = UInt(PredictWidth.W)
69  // val jmpIdx = UInt(log2Up(PredictWidth).W)
70  val brMask = UInt(PredictWidth.W)
71  val jalMask = UInt(PredictWidth.W)
72  val targets = Vec(PredictWidth, UInt(VAddrBits.W))
73
74  // marks the last 2 bytes of this fetch packet
75  // val endsAtTheEndOfFirstBank = Bool()
76  // val endsAtTheEndOfLastBank = Bool()
77
78  // half RVI could only start at the end of a bank
79  val firstBankHasHalfRVI = Bool()
80  val lastBankHasHalfRVI = Bool()
81
82  def lastHalfRVIMask = Mux(firstBankHasHalfRVI, UIntToOH((bankWidth-1).U),
83                          Mux(lastBankHasHalfRVI, UIntToOH((PredictWidth-1).U),
84                            0.U(PredictWidth.W)
85                          )
86                        )
87
88  def lastHalfRVIClearMask = ~lastHalfRVIMask
89  // is taken from half RVI
90  def lastHalfRVITaken = (takens & lastHalfRVIMask).orR
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 = ~realTakens & realBrMask
101  def sawNotTakenBr = VecInit((0 until PredictWidth).map(i =>
102                       (if (i == 0) false.B else brNotTakens(i-1,0).orR)))
103  def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR
104  def unmaskedJmpIdx = PriorityEncoder(takens)
105  def saveHalfRVI = (firstBankHasHalfRVI && (unmaskedJmpIdx === (bankWidth-1).U || !(takens.orR))) ||
106                    (lastBankHasHalfRVI  &&  unmaskedJmpIdx === (PredictWidth-1).U)
107  // could get PredictWidth-1 when only the first bank is valid
108  def jmpIdx = PriorityEncoder(realTakens)
109  // only used when taken
110  def target = targets(jmpIdx)
111  def taken = realTakens.orR
112  def takenOnBr = taken && realBrMask(jmpIdx)
113}
114
115class BranchInfo extends XSBundle with HasBPUParameter {
116  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
117  val ubtbHits = Bool()
118  val btbWriteWay = UInt(log2Up(BtbWays).W)
119  val btbHitJal = Bool()
120  val bimCtr = UInt(2.W)
121  val histPtr = UInt(log2Up(ExtHistoryLength).W)
122  val predHistPtr = UInt(log2Up(ExtHistoryLength).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  val sawNotTakenBranch = Bool()
130
131  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
132  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
133  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
134
135  def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
136    this.histPtr := histPtr
137    this.tageMeta := tageMeta
138    this.rasSp := rasSp
139    this.rasTopCtr := rasTopCtr
140    this.asUInt
141  }
142  def size = 0.U.asTypeOf(this).getWidth
143  def fromUInt(x: UInt) = x.asTypeOf(this)
144}
145
146class Predecode extends XSBundle with HasIFUConst {
147  val hasLastHalfRVI = Bool()
148  val mask = UInt((FetchWidth*2).W)
149  val lastHalf = UInt(nBanksInPacket.W)
150  val pd = Vec(FetchWidth*2, (new PreDecodeInfo))
151}
152
153class BranchUpdateInfo extends XSBundle {
154  // from backend
155  val pc = UInt(VAddrBits.W)
156  val pnpc = UInt(VAddrBits.W)
157  val target = UInt(VAddrBits.W)
158  val brTarget = UInt(VAddrBits.W)
159  val taken = Bool()
160  val fetchIdx = UInt(log2Up(FetchWidth*2).W)
161  val isMisPred = Bool()
162  val brTag = new BrqPtr
163
164  // frontend -> backend -> frontend
165  val pd = new PreDecodeInfo
166  val brInfo = new BranchInfo
167}
168
169// Dequeue DecodeWidth insts from Ibuffer
170class CtrlFlow extends XSBundle {
171  val instr = UInt(32.W)
172  val pc = UInt(VAddrBits.W)
173  val exceptionVec = Vec(16, Bool())
174  val intrVec = Vec(12, Bool())
175  val brUpdate = new BranchUpdateInfo
176  val crossPageIPFFix = Bool()
177}
178
179// Decode DecodeWidth insts at Decode Stage
180class CtrlSignals extends XSBundle {
181  val src1Type, src2Type, src3Type = SrcType()
182  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
183  val ldest = UInt(5.W)
184  val fuType = FuType()
185  val fuOpType = FuOpType()
186  val rfWen = Bool()
187  val fpWen = Bool()
188  val isXSTrap = Bool()
189  val noSpecExec = Bool()  // wait forward
190  val blockBackward  = Bool()  // block backward
191  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
192  val isRVF = Bool()
193  val imm = UInt(XLEN.W)
194  val commitType = CommitType()
195}
196
197class CfCtrl extends XSBundle {
198  val cf = new CtrlFlow
199  val ctrl = new CtrlSignals
200  val brTag = new BrqPtr
201}
202
203// Load / Store Index
204//
205// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type.
206trait HasLSIdx { this: HasXSParameter =>
207  // Separate LSQ
208  val lqIdx = new LqPtr
209  val sqIdx = new SqPtr
210}
211
212class LSIdx extends XSBundle with HasLSIdx {}
213
214// CfCtrl -> MicroOp at Rename Stage
215class MicroOp extends CfCtrl with HasLSIdx {
216  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
217  val src1State, src2State, src3State = SrcState()
218  val roqIdx = new RoqPtr
219  val diffTestDebugLrScValid = Bool()
220}
221
222class Redirect extends XSBundle {
223  val roqIdx = new RoqPtr
224  val isException = Bool()
225  val isMisPred = Bool()
226  val isReplay = Bool()
227  val isFlushPipe = Bool()
228  val pc = UInt(VAddrBits.W)
229  val target = UInt(VAddrBits.W)
230  val brTag = new BrqPtr
231}
232
233class Dp1ToDp2IO extends XSBundle {
234  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
235  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
236  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
237}
238
239class ReplayPregReq extends XSBundle {
240  // NOTE: set isInt and isFp both to 'false' when invalid
241  val isInt = Bool()
242  val isFp = Bool()
243  val preg = UInt(PhyRegIdxWidth.W)
244}
245
246class DebugBundle extends XSBundle{
247  val isMMIO = Bool()
248}
249
250class ExuInput extends XSBundle {
251  val uop = new MicroOp
252  val src1, src2, src3 = UInt((XLEN+1).W)
253}
254
255class ExuOutput extends XSBundle {
256  val uop = new MicroOp
257  val data = UInt((XLEN+1).W)
258  val fflags  = new Fflags
259  val redirectValid = Bool()
260  val redirect = new Redirect
261  val brUpdate = new BranchUpdateInfo
262  val debug = new DebugBundle
263}
264
265class ExternalInterruptIO extends XSBundle {
266  val mtip = Input(Bool())
267  val msip = Input(Bool())
268  val meip = Input(Bool())
269}
270
271class CSRSpecialIO extends XSBundle {
272  val exception = Flipped(ValidIO(new MicroOp))
273  val isInterrupt = Input(Bool())
274  val memExceptionVAddr = Input(UInt(VAddrBits.W))
275  val trapTarget = Output(UInt(VAddrBits.W))
276  val externalInterrupt = new ExternalInterruptIO
277  val interrupt = Output(Bool())
278}
279
280//class ExuIO extends XSBundle {
281//  val in = Flipped(DecoupledIO(new ExuInput))
282//  val redirect = Flipped(ValidIO(new Redirect))
283//  val out = DecoupledIO(new ExuOutput)
284//  // for csr
285//  val csrOnly = new CSRSpecialIO
286//  val mcommit = Input(UInt(3.W))
287//}
288
289class RoqCommit extends XSBundle {
290  val uop = new MicroOp
291  val isWalk = Bool()
292}
293
294class TlbFeedback extends XSBundle {
295  val roqIdx = new RoqPtr
296  val hit = Bool()
297}
298
299class FrontendToBackendIO extends XSBundle {
300  // to backend end
301  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
302  // from backend
303  val redirect = Flipped(ValidIO(UInt(VAddrBits.W)))
304  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
305  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
306}
307
308class TlbCsrBundle extends XSBundle {
309  val satp = new Bundle {
310    val mode = UInt(4.W) // TODO: may change number to parameter
311    val asid = UInt(16.W)
312    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
313  }
314  val priv = new Bundle {
315    val mxr = Bool()
316    val sum = Bool()
317    val imode = UInt(2.W)
318    val dmode = UInt(2.W)
319  }
320
321  override def toPrintable: Printable = {
322    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
323    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
324  }
325}
326
327class SfenceBundle extends XSBundle {
328  val valid = Bool()
329  val bits = new Bundle {
330    val rs1 = Bool()
331    val rs2 = Bool()
332    val addr = UInt(VAddrBits.W)
333  }
334
335  override def toPrintable: Printable = {
336    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
337  }
338}
339