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