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