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