xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 4b3d9f67355a9945cd5eca46929b89c130c43c26)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import xiangshan.backend.brq.BrqPtr
6import xiangshan.backend.fu.fpu.Fflags
7import xiangshan.backend.rename.FreeListPtr
8import xiangshan.backend.roq.RoqPtr
9import xiangshan.mem.{LqPtr, SqPtr}
10import xiangshan.frontend.PreDecodeInfo
11import xiangshan.frontend.HasBPUParameter
12import xiangshan.frontend.HasTageParameter
13import scala.math.max
14
15// Fetch FetchWidth x 32-bit insts from Icache
16class FetchPacket extends XSBundle {
17  val instrs = Vec(PredictWidth, UInt(32.W))
18  val mask = UInt(PredictWidth.W)
19  // val pc = UInt(VAddrBits.W)
20  val pc = Vec(PredictWidth, UInt(VAddrBits.W))
21  val pnpc = Vec(PredictWidth, UInt(VAddrBits.W))
22  val brInfo = Vec(PredictWidth, new BranchInfo)
23  val pd = Vec(PredictWidth, new PreDecodeInfo)
24  val ipf = Bool()
25  val crossPageIPFFix = Bool()
26  val predTaken = Bool()
27}
28
29class ValidUndirectioned[T <: Data](gen: T) extends Bundle {
30  val valid = Bool()
31  val bits = gen.cloneType.asInstanceOf[T]
32  override def cloneType = new ValidUndirectioned(gen).asInstanceOf[this.type]
33}
34
35object ValidUndirectioned {
36  def apply[T <: Data](gen: T) = {
37    new ValidUndirectioned[T](gen)
38  }
39}
40
41class SCMeta(val useSC: Boolean) extends XSBundle with HasTageParameter {
42  def maxVal = 8 * ((1 << TageCtrBits) - 1) + SCTableInfo.map{case (_,cb,_) => (1 << cb) - 1}.reduce(_+_)
43  def minVal = -(8 * (1 << TageCtrBits) + SCTableInfo.map{case (_,cb,_) => 1 << cb}.reduce(_+_))
44  def sumCtrBits = max(log2Ceil(-minVal), log2Ceil(maxVal+1)) + 1
45  val tageTaken = if (useSC) Bool() else UInt(0.W)
46  val scUsed    = if (useSC) Bool() else UInt(0.W)
47  val scPred    = if (useSC) Bool() else UInt(0.W)
48  // Suppose ctrbits of all tables are identical
49  val ctrs      = if (useSC) Vec(SCNTables, SInt(SCCtrBits.W)) else Vec(SCNTables, SInt(0.W))
50  val sumAbs    = if (useSC) UInt(sumCtrBits.W) else UInt(0.W)
51}
52
53class TageMeta extends XSBundle with HasTageParameter {
54  val provider = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
55  val altDiffers = Bool()
56  val providerU = UInt(2.W)
57  val providerCtr = UInt(3.W)
58  val allocate = ValidUndirectioned(UInt(log2Ceil(TageNTables).W))
59  val taken = Bool()
60  val scMeta = new SCMeta(EnableSC)
61}
62
63class BranchPrediction extends XSBundle {
64  val redirect = Bool()
65  val taken = Bool()
66  val jmpIdx = UInt(log2Up(PredictWidth).W)
67  val hasNotTakenBrs = Bool()
68  val target = UInt(VAddrBits.W)
69  val saveHalfRVI = Bool()
70  val takenOnBr = Bool()
71}
72
73class BranchInfo extends XSBundle with HasBPUParameter {
74  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
75  val ubtbHits = Bool()
76  val btbWriteWay = UInt(log2Up(BtbWays).W)
77  val btbHitJal = Bool()
78  val bimCtr = UInt(2.W)
79  val histPtr = UInt(log2Up(ExtHistoryLength).W)
80  val predHistPtr = UInt(log2Up(ExtHistoryLength).W)
81  val tageMeta = new TageMeta
82  val rasSp = UInt(log2Up(RasSize).W)
83  val rasTopCtr = UInt(8.W)
84  val rasToqAddr = UInt(VAddrBits.W)
85  val fetchIdx = UInt(log2Up(PredictWidth).W)
86  val specCnt = UInt(10.W)
87  val sawNotTakenBranch = Bool()
88
89  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
90  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
91  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
92
93  def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
94    this.histPtr := histPtr
95    this.tageMeta := tageMeta
96    this.rasSp := rasSp
97    this.rasTopCtr := rasTopCtr
98    this.asUInt
99  }
100  def size = 0.U.asTypeOf(this).getWidth
101  def fromUInt(x: UInt) = x.asTypeOf(this)
102}
103
104class Predecode extends XSBundle {
105  val isFetchpcEqualFirstpc = Bool()
106  val mask = UInt((FetchWidth*2).W)
107  val pd = Vec(FetchWidth*2, (new PreDecodeInfo))
108}
109
110class BranchUpdateInfo extends XSBundle {
111  // from backend
112  val pc = UInt(VAddrBits.W)
113  val pnpc = UInt(VAddrBits.W)
114  val target = UInt(VAddrBits.W)
115  val brTarget = UInt(VAddrBits.W)
116  val taken = Bool()
117  val fetchIdx = UInt(log2Up(FetchWidth*2).W)
118  val isMisPred = Bool()
119  val brTag = new BrqPtr
120
121  // frontend -> backend -> frontend
122  val pd = new PreDecodeInfo
123  val brInfo = new BranchInfo
124}
125
126// Dequeue DecodeWidth insts from Ibuffer
127class CtrlFlow extends XSBundle {
128  val instr = UInt(32.W)
129  val pc = UInt(VAddrBits.W)
130  val exceptionVec = Vec(16, Bool())
131  val intrVec = Vec(12, Bool())
132  val brUpdate = new BranchUpdateInfo
133  val crossPageIPFFix = Bool()
134}
135
136// Decode DecodeWidth insts at Decode Stage
137class CtrlSignals extends XSBundle {
138  val src1Type, src2Type, src3Type = SrcType()
139  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
140  val ldest = UInt(5.W)
141  val fuType = FuType()
142  val fuOpType = FuOpType()
143  val rfWen = Bool()
144  val fpWen = Bool()
145  val isXSTrap = Bool()
146  val noSpecExec = Bool()  // wait forward
147  val blockBackward  = Bool()  // block backward
148  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
149  val isRVF = Bool()
150  val imm = UInt(XLEN.W)
151  val commitType = CommitType()
152}
153
154class CfCtrl extends XSBundle {
155  val cf = new CtrlFlow
156  val ctrl = new CtrlSignals
157  val brTag = new BrqPtr
158}
159
160// Load / Store Index
161//
162// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type.
163trait HasLSIdx { this: HasXSParameter =>
164  // Separate LSQ
165  val lqIdx = new LqPtr
166  val sqIdx = new SqPtr
167}
168
169class LSIdx extends XSBundle with HasLSIdx {}
170
171// CfCtrl -> MicroOp at Rename Stage
172class MicroOp extends CfCtrl with HasLSIdx {
173  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
174  val src1State, src2State, src3State = SrcState()
175  val roqIdx = new RoqPtr
176  val diffTestDebugLrScValid = Bool()
177}
178
179class Redirect extends XSBundle {
180  val roqIdx = new RoqPtr
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+1).W)
210}
211
212class ExuOutput extends XSBundle {
213  val uop = new MicroOp
214  val data = UInt((XLEN+1).W)
215  val fflags  = new Fflags
216  val redirectValid = Bool()
217  val redirect = new Redirect
218  val brUpdate = new BranchUpdateInfo
219  val debug = new DebugBundle
220}
221
222class ExternalInterruptIO extends XSBundle {
223  val mtip = Input(Bool())
224  val msip = Input(Bool())
225  val meip = Input(Bool())
226}
227
228class CSRSpecialIO extends XSBundle {
229  val exception = Flipped(ValidIO(new MicroOp))
230  val isInterrupt = Input(Bool())
231  val memExceptionVAddr = Input(UInt(VAddrBits.W))
232  val trapTarget = Output(UInt(VAddrBits.W))
233  val externalInterrupt = new ExternalInterruptIO
234  val interrupt = Output(Bool())
235}
236
237//class ExuIO extends XSBundle {
238//  val in = Flipped(DecoupledIO(new ExuInput))
239//  val redirect = Flipped(ValidIO(new Redirect))
240//  val out = DecoupledIO(new ExuOutput)
241//  // for csr
242//  val csrOnly = new CSRSpecialIO
243//  val mcommit = Input(UInt(3.W))
244//}
245
246class RoqCommit extends XSBundle {
247  val uop = new MicroOp
248  val isWalk = Bool()
249}
250
251class TlbFeedback extends XSBundle {
252  val roqIdx = new RoqPtr
253  val hit = Bool()
254}
255
256class FrontendToBackendIO extends XSBundle {
257  // to backend end
258  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
259  // from backend
260  val redirect = Flipped(ValidIO(new Redirect))
261  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
262  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
263}
264
265class TlbCsrBundle extends XSBundle {
266  val satp = new Bundle {
267    val mode = UInt(4.W) // TODO: may change number to parameter
268    val asid = UInt(16.W)
269    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
270  }
271  val priv = new Bundle {
272    val mxr = Bool()
273    val sum = Bool()
274    val imode = UInt(2.W)
275    val dmode = UInt(2.W)
276  }
277
278  override def toPrintable: Printable = {
279    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
280    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
281  }
282}
283
284class SfenceBundle extends XSBundle {
285  val valid = Bool()
286  val bits = new Bundle {
287    val rs1 = Bool()
288    val rs2 = Bool()
289    val addr = UInt(VAddrBits.W)
290  }
291
292  override def toPrintable: Printable = {
293    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
294  }
295}
296