xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 0053432d77c1c37cfecd64fcac345619640e0578)
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
162// CfCtrl -> MicroOp at Rename Stage
163class MicroOp extends CfCtrl with HasRoqIdx {
164  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
165  val src1State, src2State, src3State = SrcState()
166  val lsroqIdx = UInt(LsroqIdxWidth.W)
167  val diffTestDebugLrScValid = Bool()
168}
169
170class Redirect extends XSBundle with HasRoqIdx {
171  val isException = Bool()
172  val isMisPred = Bool()
173  val isReplay = Bool()
174  val isFlushPipe = Bool()
175  val pc = UInt(VAddrBits.W)
176  val target = UInt(VAddrBits.W)
177  val brTag = new BrqPtr
178}
179
180class Dp1ToDp2IO extends XSBundle {
181  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
182  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
183  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
184}
185
186class ReplayPregReq extends XSBundle {
187  // NOTE: set isInt and isFp both to 'false' when invalid
188  val isInt = Bool()
189  val isFp = Bool()
190  val preg = UInt(PhyRegIdxWidth.W)
191}
192
193class DebugBundle extends XSBundle{
194  val isMMIO = Bool()
195}
196
197class ExuInput extends XSBundle {
198  val uop = new MicroOp
199  val src1, src2, src3 = UInt(XLEN.W)
200}
201
202class ExuOutput extends XSBundle {
203  val uop = new MicroOp
204  val data = UInt(XLEN.W)
205  val redirectValid = Bool()
206  val redirect = new Redirect
207  val brUpdate = new BranchUpdateInfo
208  val debug = new DebugBundle
209}
210
211class ExuIO extends XSBundle {
212  val in = Flipped(DecoupledIO(new ExuInput))
213  val redirect = Flipped(ValidIO(new Redirect))
214  val out = DecoupledIO(new ExuOutput)
215  // for csr
216  val exception = Flipped(ValidIO(new MicroOp))
217  // for Lsu
218  val dmem = new SimpleBusUC
219  val mcommit = Input(UInt(3.W))
220}
221
222class RoqCommit extends XSBundle {
223  val uop = new MicroOp
224  val isWalk = Bool()
225}
226
227class TlbFeedback extends XSBundle with HasRoqIdx{
228  val hit = Bool()
229}
230
231class FrontendToBackendIO extends XSBundle {
232  // to backend end
233  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
234  // from backend
235  val redirect = Flipped(ValidIO(new Redirect))
236  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
237  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
238}
239
240class TlbCsrBundle extends XSBundle {
241  val satp = new Bundle {
242    val mode = UInt(4.W) // TODO: may change number to parameter
243    val asid = UInt(16.W)
244    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
245  }
246  val priv = new Bundle {
247    val mxr = Bool()
248    val sum = Bool()
249    val imode = UInt(2.W)
250    val dmode = UInt(2.W)
251  }
252
253  override def toPrintable: Printable = {
254    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
255    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
256  }
257}
258
259class SfenceBundle extends XSBundle {
260  val valid = Bool()
261  val bits = new Bundle {
262    val rs1 = Bool()
263    val rs2 = Bool()
264    val addr = UInt(VAddrBits.W)
265  }
266
267  override def toPrintable: Printable = {
268    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
269  }
270}
271