xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision cde9280d25efc101d8b0845edca84c1a95dea6e9)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.SelImm
6import xiangshan.backend.roq.RoqPtr
7import xiangshan.backend.decode.{ImmUnion, XDecode}
8import xiangshan.mem.{LqPtr, SqPtr}
9import xiangshan.frontend.PreDecodeInfo
10import xiangshan.frontend.HasBPUParameter
11import xiangshan.frontend.HasTageParameter
12import xiangshan.frontend.HasIFUConst
13import xiangshan.frontend.GlobalHistory
14import xiangshan.frontend.RASEntry
15import utils._
16
17import scala.math.max
18import Chisel.experimental.chiselName
19import xiangshan.backend.ftq.FtqPtr
20
21// Fetch FetchWidth x 32-bit insts from Icache
22class FetchPacket extends XSBundle {
23  val instrs = Vec(PredictWidth, UInt(32.W))
24  val mask = UInt(PredictWidth.W)
25  val pdmask = UInt(PredictWidth.W)
26  // val pc = UInt(VAddrBits.W)
27  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
28  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
29  val bpuMeta = Vec(PredictWidth, new BpuMeta)
30  val pd = Vec(PredictWidth, new PreDecodeInfo)
31  val ipf = Bool()
32  val acf = Bool()
33  val crossPageIPFFix = Bool()
34  val predTaken = Bool()
35}
36
37class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
38  val valid = Bool()
39  val bits = gen.cloneType.asInstanceOf[T]
40  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
41}
42
43object ValidUndirectioned {
44  def apply[T <: Data](gen: T) = {
45    new ValidUndirectioned[T](gen)
46  }
47}
48
49class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter {
50  def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_)
51  def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_))
52  def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1
53  val tageTaken = if (useSC) Bool() else UInt(0.W)
54  val scUsed    = if (useSC) Bool() else UInt(0.W)
55  val scPred    = if (useSC) Bool() else UInt(0.W)
56  // Suppose ctrbits of all tables are identical
57  val ctrs      = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
58  val sumAbs    = if (useSC) UInt(sumCtrBits.W) else UInt(0.W)
59}
60
61class TageMeta extends XSBundle with HasTageParameter {
62  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
63  val altDiffers = Bool()
64  val providerU = UInt(2.W)
65  val providerCtr = UInt(3.W)
66  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
67  val taken = Bool()
68  val scMeta = new SCMeta(EnableSC)
69}
70
71@chiselName
72class BranchPrediction extends XSBundle with HasIFUConst {
73  // val redirect = Bool()
74  val takens = UInt(PredictWidth.W)
75  // val jmpIdx = UInt(log2Up(PredictWidth).W)
76  val brMask = UInt(PredictWidth.W)
77  val jalMask = UInt(PredictWidth.W)
78  val targets = Vec(PredictWidth, UInt(VAddrBits.W))
79
80  // marks the last 2 bytes of this fetch packet
81  // val endsAtTheEndOfFirstBank = Bool()
82  // val endsAtTheEndOfLastBank = Bool()
83
84  // half RVI could only start at the end of a packet
85  val hasHalfRVI = Bool()
86
87
88  // assumes that only one of the two conditions could be true
89  def lastHalfRVIMask = Cat(hasHalfRVI.asUInt, 0.U((PredictWidth-1).W))
90
91  def lastHalfRVIClearMask = ~lastHalfRVIMask
92  // is taken from half RVI
93  def lastHalfRVITaken = takens(PredictWidth-1) && hasHalfRVI
94
95  def lastHalfRVIIdx = (PredictWidth-1).U
96  // should not be used if not lastHalfRVITaken
97  def lastHalfRVITarget = targets(PredictWidth-1)
98
99  def realTakens  = takens  & lastHalfRVIClearMask
100  def realBrMask  = brMask  & lastHalfRVIClearMask
101  def realJalMask = jalMask & lastHalfRVIClearMask
102
103  def brNotTakens = (~takens & realBrMask)
104  def sawNotTakenBr = VecInit((0 until PredictWidth).map(i =>
105                       (if (i == 0) false.B else ParallelORR(brNotTakens(i-1,0)))))
106  // def hasNotTakenBrs = (brNotTakens & LowerMaskFromLowest(realTakens)).orR
107  def unmaskedJmpIdx = ParallelPriorityEncoder(takens)
108  // if not taken before the half RVI inst
109  def saveHalfRVI = hasHalfRVI && !(ParallelORR(takens(PredictWidth-2,0)))
110  // could get PredictWidth-1 when only the first bank is valid
111  def jmpIdx = ParallelPriorityEncoder(realTakens)
112  // only used when taken
113  def target = {
114    val generator = new PriorityMuxGenerator[UInt]
115    generator.register(realTakens.asBools, targets, List.fill(PredictWidth)(None))
116    generator()
117  }
118  def taken = ParallelORR(realTakens)
119  def takenOnBr = taken && ParallelPriorityMux(realTakens, realBrMask.asBools)
120  def hasNotTakenBrs = Mux(taken, ParallelPriorityMux(realTakens, sawNotTakenBr), ParallelORR(brNotTakens))
121}
122
123class BpuMeta extends XSBundle with HasBPUParameter {
124  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
125  val ubtbHits = Bool()
126  val btbWriteWay = UInt(log2Up(BtbWays).W)
127  val btbHitJal = Bool()
128  val bimCtr = UInt(2.W)
129  val tageMeta = new TageMeta
130  // for global history
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  val predictor = if (BPUDebug) UInt(log2Up(4).W) else UInt(0.W) // Mark which component this prediction comes from {ubtb, btb, tage, loopPredictor}
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(PredictWidth.W)
152  val lastHalf = Bool()
153  val pd = Vec(PredictWidth, (new PreDecodeInfo))
154}
155
156class CfiUpdateInfo extends XSBundle with HasBPUParameter {
157  // from backend
158  val pc = UInt(VAddrBits.W)
159  // frontend -> backend -> frontend
160  val pd = new PreDecodeInfo
161  val rasSp = UInt(log2Up(RasSize).W)
162  val rasEntry = new RASEntry
163  val hist = new GlobalHistory
164  val predHist = new GlobalHistory
165  val specCnt = UInt(10.W)
166  // need pipeline update
167  val sawNotTakenBranch = Bool()
168  val predTaken = Bool()
169  val target = UInt(VAddrBits.W)
170  val taken = Bool()
171  val isMisPred = Bool()
172}
173
174// Dequeue DecodeWidth insts from Ibuffer
175class CtrlFlow extends XSBundle {
176  val instr = UInt(32.W)
177  val pc = UInt(VAddrBits.W)
178  val exceptionVec = ExceptionVec()
179  val intrVec = Vec(12, Bool())
180  val pd = new PreDecodeInfo
181  val pred_taken = Bool()
182  val crossPageIPFFix = Bool()
183  val ftqPtr = new FtqPtr
184  val ftqOffset = UInt(log2Up(PredictWidth).W)
185}
186
187class FtqEntry extends XSBundle {
188    // fetch pc, pc of each inst could be generated by concatenation
189    val ftqPC = UInt((VAddrBits.W))
190
191    // prediction metas
192    val hist = new GlobalHistory
193    val predHist = new GlobalHistory
194    val rasSp = UInt(log2Ceil(RasSize).W)
195    val rasTop = new RASEntry()
196    val metas = Vec(PredictWidth, new BpuMeta)
197
198    val cfiIsCall, cfiIsRet, cfiIsRVC = Bool()
199    val br_mask = Vec(PredictWidth, Bool())
200    val cfiIndex = ValidUndirectioned(UInt(log2Up(PredictWidth).W))
201    val specCnt = Vec(PredictWidth, UInt(10.W))
202    val valids = Vec(PredictWidth, Bool())
203
204    // backend update
205    val mispred = Vec(PredictWidth, Bool())
206    val jalr_target = UInt(VAddrBits.W)
207}
208
209
210
211class FPUCtrlSignals extends XSBundle {
212  val isAddSub = Bool() // swap23
213	val typeTagIn = UInt(2.W)
214	val typeTagOut = UInt(2.W)
215  val fromInt = Bool()
216  val wflags = Bool()
217  val fpWen = Bool()
218  val fmaCmd = UInt(2.W)
219  val div = Bool()
220  val sqrt = Bool()
221  val fcvt = Bool()
222  val typ = UInt(2.W)
223  val fmt = UInt(2.W)
224  val ren3 = Bool() //TODO: remove SrcType.fp
225}
226
227// Decode DecodeWidth insts at Decode Stage
228class CtrlSignals extends XSBundle {
229  val src1Type, src2Type, src3Type = SrcType()
230  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
231  val ldest = UInt(5.W)
232  val fuType = FuType()
233  val fuOpType = FuOpType()
234  val rfWen = Bool()
235  val fpWen = Bool()
236  val isXSTrap = Bool()
237  val noSpecExec = Bool()  // wait forward
238  val blockBackward  = Bool()  // block backward
239  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
240  val isRVF = Bool()
241  val selImm = SelImm()
242  val imm = UInt(ImmUnion.maxLen.W)
243  val commitType = CommitType()
244  val fpu = new FPUCtrlSignals
245
246  def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]) = {
247    val decoder = freechips.rocketchip.rocket.DecodeLogic(inst, XDecode.decodeDefault, table)
248    val signals =
249      Seq(src1Type, src2Type, src3Type, fuType, fuOpType, rfWen, fpWen,
250          isXSTrap, noSpecExec, blockBackward, flushPipe, isRVF, selImm)
251    signals zip decoder map { case(s, d) => s := d }
252    commitType := DontCare
253    this
254  }
255}
256
257class CfCtrl extends XSBundle {
258  val cf = new CtrlFlow
259  val ctrl = new CtrlSignals
260}
261
262class PerfDebugInfo extends XSBundle {
263  // val fetchTime = UInt(64.W)
264  val renameTime = UInt(64.W)
265  val dispatchTime = UInt(64.W)
266  val issueTime = UInt(64.W)
267  val writebackTime = UInt(64.W)
268  // val commitTime = UInt(64.W)
269}
270
271// Separate LSQ
272class LSIdx extends XSBundle {
273  val lqIdx = new LqPtr
274  val sqIdx = new SqPtr
275}
276
277// CfCtrl -> MicroOp at Rename Stage
278class MicroOp extends CfCtrl {
279  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
280  val src1State, src2State, src3State = SrcState()
281  val roqIdx = new RoqPtr
282  val lqIdx = new LqPtr
283  val sqIdx = new SqPtr
284  val diffTestDebugLrScValid = Bool()
285  val debugInfo = new PerfDebugInfo
286}
287
288class Redirect extends XSBundle {
289  val roqIdx = new RoqPtr
290  val ftqIdx = new FtqPtr
291  val ftqOffset = UInt(log2Up(PredictWidth).W)
292  val level = RedirectLevel()
293  val interrupt = Bool()
294  val cfiUpdate = new CfiUpdateInfo
295
296  def isUnconditional() = RedirectLevel.isUnconditional(level)
297  def flushItself() = RedirectLevel.flushItself(level)
298  def isException() = RedirectLevel.isException(level)
299}
300
301class Dp1ToDp2IO extends XSBundle {
302  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
303  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
304  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
305}
306
307class ReplayPregReq extends XSBundle {
308  // NOTE: set isInt and isFp both to 'false' when invalid
309  val isInt = Bool()
310  val isFp = Bool()
311  val preg = UInt(PhyRegIdxWidth.W)
312}
313
314class DebugBundle extends XSBundle{
315  val isMMIO = Bool()
316  val isPerfCnt = Bool()
317}
318
319class ExuInput extends XSBundle {
320  val uop = new MicroOp
321  val src1, src2, src3 = UInt((XLEN+1).W)
322}
323
324class ExuOutput extends XSBundle {
325  val uop = new MicroOp
326  val data = UInt((XLEN+1).W)
327  val fflags  = UInt(5.W)
328  val redirectValid = Bool()
329  val redirect = new Redirect
330  val debug = new DebugBundle
331}
332
333class ExternalInterruptIO extends XSBundle {
334  val mtip = Input(Bool())
335  val msip = Input(Bool())
336  val meip = Input(Bool())
337}
338
339class CSRSpecialIO extends XSBundle {
340  val exception = Flipped(ValidIO(new MicroOp))
341  val isInterrupt = Input(Bool())
342  val memExceptionVAddr = Input(UInt(VAddrBits.W))
343  val trapTarget = Output(UInt(VAddrBits.W))
344  val externalInterrupt = new ExternalInterruptIO
345  val interrupt = Output(Bool())
346}
347
348class RoqCommitInfo extends XSBundle {
349  val ldest = UInt(5.W)
350  val rfWen = Bool()
351  val fpWen = Bool()
352  val wflags = Bool()
353  val commitType = CommitType()
354  val pdest = UInt(PhyRegIdxWidth.W)
355  val old_pdest = UInt(PhyRegIdxWidth.W)
356  val lqIdx = new LqPtr
357  val sqIdx = new SqPtr
358  val ftqIdx = new FtqPtr
359  val ftqOffset = UInt(log2Up(PredictWidth).W)
360
361  // these should be optimized for synthesis verilog
362  val pc = UInt(VAddrBits.W)
363}
364
365class RoqCommitIO extends XSBundle {
366  val isWalk = Output(Bool())
367  val valid = Vec(CommitWidth, Output(Bool()))
368  val info = Vec(CommitWidth, Output(new RoqCommitInfo))
369
370  def hasWalkInstr = isWalk && valid.asUInt.orR
371  def hasCommitInstr = !isWalk && valid.asUInt.orR
372}
373
374class TlbFeedback extends XSBundle {
375  val roqIdx = new RoqPtr
376  val hit = Bool()
377}
378
379class FrontendToBackendIO extends XSBundle {
380  // to backend end
381  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
382  val fetchInfo = DecoupledIO(new FtqEntry)
383  // from backend
384  val redirect_cfiUpdate = Flipped(ValidIO(new Redirect))
385  val commit_cfiUpdate = Flipped(ValidIO(new FtqEntry))
386}
387
388class TlbCsrBundle extends XSBundle {
389  val satp = new Bundle {
390    val mode = UInt(4.W) // TODO: may change number to parameter
391    val asid = UInt(16.W)
392    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
393  }
394  val priv = new Bundle {
395    val mxr = Bool()
396    val sum = Bool()
397    val imode = UInt(2.W)
398    val dmode = UInt(2.W)
399  }
400
401  override def toPrintable: Printable = {
402    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
403    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
404  }
405}
406
407class SfenceBundle extends XSBundle {
408  val valid = Bool()
409  val bits = new Bundle {
410    val rs1 = Bool()
411    val rs2 = Bool()
412    val addr = UInt(VAddrBits.W)
413  }
414
415  override def toPrintable: Printable = {
416    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
417  }
418}
419