xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 7cef916fd7e520a6be806e455ad3594f17cb5a7b)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import bus.simplebus._
6import xiangshan.backend.brq.BrqPtr
7import xiangshan.backend.rename.FreeListPtr
8import xiangshan.frontend.PreDecodeInfo
9import xiangshan.frontend.HasBPUParameter
10import xiangshan.frontend.HasTageParameter
11
12// Fetch FetchWidth x 32-bit insts from Icache
13class FetchPacket extends XSBundle {
14  val instrs = Vec(PredictWidth, UInt(32.W))
15  val mask = UInt(PredictWidth.W)
16  // val pc = UInt(VAddrBits.W)
17  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
18  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
19  val brInfo = Vec(PredictWidth, new BranchInfo)
20  val pd = Vec(PredictWidth, new PreDecodeInfo)
21  val ipf = Bool()
22  val crossPageIPFFix = Bool()
23}
24
25class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
26  val valid = Bool()
27  val bits = gen.cloneType.asInstanceOf[T]
28  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
29}
30
31object ValidUndirectioned {
32  def apply[T <: Data](gen: T) = {
33    new ValidUndirectioned[T](gen)
34  }
35}
36
37class TageMeta extends XSBundle with HasTageParameter {
38  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
39  val altDiffers = Bool()
40  val providerU = UInt(2.W)
41  val providerCtr = UInt(3.W)
42  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
43}
44
45class BranchPrediction extends XSBundle {
46  val redirect = Bool()
47  val taken = Bool()
48  val jmpIdx = UInt(log2Up(PredictWidth).W)
49  val hasNotTakenBrs = Bool()
50  val target = UInt(VAddrBits.W)
51  val saveHalfRVI = Bool()
52  val takenOnBr = Bool()
53}
54
55class BranchInfo extends XSBundle with HasBPUParameter {
56  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
57  val ubtbHits = Bool()
58  val btbWriteWay = UInt(log2Up(BtbWays).W)
59  val btbHitJal = Bool()
60  val bimCtr = UInt(2.W)
61  val histPtr = UInt(log2Up(ExtHistoryLength).W)
62  val predHistPtr = UInt(log2Up(ExtHistoryLength).W)
63  val tageMeta = new TageMeta
64  val rasSp = UInt(log2Up(RasSize).W)
65  val rasTopCtr = UInt(8.W)
66  val rasToqAddr = UInt(VAddrBits.W)
67  val fetchIdx = UInt(log2Up(PredictWidth).W)
68  val specCnt = UInt(10.W)
69  val sawNotTakenBranch = Bool()
70
71  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
72  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
73  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
74
75  def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
76    this.histPtr := histPtr
77    this.tageMeta := tageMeta
78    this.rasSp := rasSp
79    this.rasTopCtr := rasTopCtr
80    this.asUInt
81  }
82  def size = 0.U.asTypeOf(this).getWidth
83  def fromUInt(x: UInt) = x.asTypeOf(this)
84}
85
86class Predecode extends XSBundle {
87  val isFetchpcEqualFirstpc = Bool()
88  val mask = UInt((FetchWidth*2).W)
89  val pd = Vec(FetchWidth*2, (new PreDecodeInfo))
90}
91
92class BranchUpdateInfo extends XSBundle {
93  // from backend
94  val pc = UInt(VAddrBits.W)
95  val pnpc = UInt(VAddrBits.W)
96  val target = UInt(VAddrBits.W)
97  val brTarget = UInt(VAddrBits.W)
98  val taken = Bool()
99  val fetchIdx = UInt(log2Up(FetchWidth*2).W)
100  val isMisPred = Bool()
101  val brTag = new BrqPtr
102
103  // frontend -> backend -> frontend
104  val pd = new PreDecodeInfo
105  val brInfo = new BranchInfo
106}
107
108// Dequeue DecodeWidth insts from Ibuffer
109class CtrlFlow extends XSBundle {
110  val instr = UInt(32.W)
111  val pc = UInt(VAddrBits.W)
112  val exceptionVec = Vec(16, Bool())
113  val intrVec = Vec(12, Bool())
114  val brUpdate = new BranchUpdateInfo
115  val crossPageIPFFix = Bool()
116}
117
118// Decode DecodeWidth insts at Decode Stage
119class CtrlSignals extends XSBundle {
120  val src1Type, src2Type, src3Type = SrcType()
121  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
122  val ldest = UInt(5.W)
123  val fuType = FuType()
124  val fuOpType = FuOpType()
125  val rfWen = Bool()
126  val fpWen = Bool()
127  val isXSTrap = Bool()
128  val noSpecExec = Bool()  // This inst can not be speculated
129  val isBlocked  = Bool()  // This inst requires pipeline to be blocked
130  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
131  val isRVF = Bool()
132  val imm = UInt(XLEN.W)
133  val commitType = CommitType()
134}
135
136class CfCtrl extends XSBundle {
137  val cf = new CtrlFlow
138  val ctrl = new CtrlSignals
139  val brTag = new BrqPtr
140}
141
142trait HasRoqIdx { this: HasXSParameter =>
143  val roqIdx = UInt(RoqIdxWidth.W)
144
145  def isAfter(thatIdx: UInt): Bool = {
146    Mux(
147      this.roqIdx.head(1) === thatIdx.head(1),
148      this.roqIdx.tail(1) > thatIdx.tail(1),
149      this.roqIdx.tail(1) < thatIdx.tail(1)
150    )
151  }
152
153  def isAfter[ T<: HasRoqIdx ](that: T): Bool = {
154    isAfter(that.roqIdx)
155  }
156
157  def needFlush(redirect: Valid[Redirect]): Bool = {
158    redirect.valid && (redirect.bits.isException || redirect.bits.isFlushPipe || this.isAfter(redirect.bits.roqIdx)) // TODO: need check by JiaWei
159  }
160}
161
162class PerfDebugInfo extends XSBundle {
163  // val fetchTime = UInt(64.W)
164  val renameTime = UInt(64.W)
165  val dispatchTime = UInt(64.W)
166  val issueTime = UInt(64.W)
167  val writebackTime = UInt(64.W)
168  // val commitTime = UInt(64.W)
169}
170
171// CfCtrl -> MicroOp at Rename Stage
172class MicroOp extends CfCtrl with HasRoqIdx {
173  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
174  val src1State, src2State, src3State = SrcState()
175  val lsroqIdx = UInt(LsroqIdxWidth.W)
176  val diffTestDebugLrScValid = Bool()
177  val debugInfo = new PerfDebugInfo
178}
179
180class Redirect extends XSBundle with HasRoqIdx {
181  val isException = Bool()
182  val isMisPred = Bool()
183  val isReplay = Bool()
184  val isFlushPipe = Bool()
185  val pc = UInt(VAddrBits.W)
186  val target = UInt(VAddrBits.W)
187  val brTag = new BrqPtr
188}
189
190class Dp1ToDp2IO extends XSBundle {
191  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
192  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
193  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
194}
195
196class ReplayPregReq extends XSBundle {
197  // NOTE: set isInt and isFp both to 'false' when invalid
198  val isInt = Bool()
199  val isFp = Bool()
200  val preg = UInt(PhyRegIdxWidth.W)
201}
202
203class DebugBundle extends XSBundle{
204  val isMMIO = Bool()
205}
206
207class ExuInput extends XSBundle {
208  val uop = new MicroOp
209  val src1, src2, src3 = UInt(XLEN.W)
210}
211
212class ExuOutput extends XSBundle {
213  val uop = new MicroOp
214  val data = UInt(XLEN.W)
215  val redirectValid = Bool()
216  val redirect = new Redirect
217  val brUpdate = new BranchUpdateInfo
218  val debug = new DebugBundle
219}
220
221class ExuIO extends XSBundle {
222  val in = Flipped(DecoupledIO(new ExuInput))
223  val redirect = Flipped(ValidIO(new Redirect))
224  val out = DecoupledIO(new ExuOutput)
225  // for csr
226  val exception = Flipped(ValidIO(new MicroOp))
227  // for Lsu
228  val dmem = new SimpleBusUC
229  val mcommit = Input(UInt(3.W))
230}
231
232class RoqCommit extends XSBundle {
233  val uop = new MicroOp
234  val isWalk = Bool()
235}
236
237class TlbFeedback extends XSBundle with HasRoqIdx{
238  val hit = Bool()
239}
240
241class FrontendToBackendIO extends XSBundle {
242  // to backend end
243  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
244  // from backend
245  val redirect = Flipped(ValidIO(new Redirect))
246  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
247  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
248}
249
250class TlbCsrBundle extends XSBundle {
251  val satp = new Bundle {
252    val mode = UInt(4.W) // TODO: may change number to parameter
253    val asid = UInt(16.W)
254    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
255  }
256  val priv = new Bundle {
257    val mxr = Bool()
258    val sum = Bool()
259    val imode = UInt(2.W)
260    val dmode = UInt(2.W)
261  }
262
263  override def toPrintable: Printable = {
264    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
265    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
266  }
267}
268
269class SfenceBundle extends XSBundle {
270  val valid = Bool()
271  val bits = new Bundle {
272    val rs1 = Bool()
273    val rs2 = Bool()
274    val addr = UInt(VAddrBits.W)
275  }
276
277  override def toPrintable: Printable = {
278    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
279  }
280}
281