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