xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 579b9f28762b9000ab852a29357f1dcc0e1636a5)
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
137class FPUCtrlSignals extends XSBundle {
138	val tagIn = UInt(2.W)
139	val tagOut = UInt(2.W)
140}
141
142// Decode DecodeWidth insts at Decode Stage
143class CtrlSignals extends XSBundle {
144  val src1Type, src2Type, src3Type = SrcType()
145  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
146  val ldest = UInt(5.W)
147  val fuType = FuType()
148  val fuOpType = FuOpType()
149  val rfWen = Bool()
150  val fpWen = Bool()
151  val isXSTrap = Bool()
152  val noSpecExec = Bool()  // wait forward
153  val blockBackward  = Bool()  // block backward
154  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
155  val isRVF = Bool()
156  val imm = UInt(XLEN.W)
157  val commitType = CommitType()
158  val fpu = new FPUCtrlSignals
159}
160
161class CfCtrl extends XSBundle {
162  val cf = new CtrlFlow
163  val ctrl = new CtrlSignals
164  val brTag = new BrqPtr
165}
166
167// Load / Store Index
168//
169// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type.
170trait HasLSIdx { this: HasXSParameter =>
171  // Separate LSQ
172  val lqIdx = new LqPtr
173  val sqIdx = new SqPtr
174}
175
176class LSIdx extends XSBundle with HasLSIdx {}
177
178// CfCtrl -> MicroOp at Rename Stage
179class MicroOp extends CfCtrl with HasLSIdx {
180  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
181  val src1State, src2State, src3State = SrcState()
182  val roqIdx = new RoqPtr
183  val diffTestDebugLrScValid = Bool()
184}
185
186class Redirect extends XSBundle {
187  val roqIdx = new RoqPtr
188  val isException = Bool()
189  val isMisPred = Bool()
190  val isReplay = Bool()
191  val isFlushPipe = Bool()
192  val pc = UInt(VAddrBits.W)
193  val target = UInt(VAddrBits.W)
194  val brTag = new BrqPtr
195}
196
197class Dp1ToDp2IO extends XSBundle {
198  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
199  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
200  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
201}
202
203class ReplayPregReq extends XSBundle {
204  // NOTE: set isInt and isFp both to 'false' when invalid
205  val isInt = Bool()
206  val isFp = Bool()
207  val preg = UInt(PhyRegIdxWidth.W)
208}
209
210class DebugBundle extends XSBundle{
211  val isMMIO = Bool()
212}
213
214class ExuInput extends XSBundle {
215  val uop = new MicroOp
216  val src1, src2, src3 = UInt((XLEN+1).W)
217}
218
219class ExuOutput extends XSBundle {
220  val uop = new MicroOp
221  val data = UInt((XLEN+1).W)
222  val fflags  = new Fflags
223  val redirectValid = Bool()
224  val redirect = new Redirect
225  val brUpdate = new BranchUpdateInfo
226  val debug = new DebugBundle
227}
228
229class ExternalInterruptIO extends XSBundle {
230  val mtip = Input(Bool())
231  val msip = Input(Bool())
232  val meip = Input(Bool())
233}
234
235class CSRSpecialIO extends XSBundle {
236  val exception = Flipped(ValidIO(new MicroOp))
237  val isInterrupt = Input(Bool())
238  val memExceptionVAddr = Input(UInt(VAddrBits.W))
239  val trapTarget = Output(UInt(VAddrBits.W))
240  val externalInterrupt = new ExternalInterruptIO
241  val interrupt = Output(Bool())
242}
243
244//class ExuIO extends XSBundle {
245//  val in = Flipped(DecoupledIO(new ExuInput))
246//  val redirect = Flipped(ValidIO(new Redirect))
247//  val out = DecoupledIO(new ExuOutput)
248//  // for csr
249//  val csrOnly = new CSRSpecialIO
250//  val mcommit = Input(UInt(3.W))
251//}
252
253class RoqCommit extends XSBundle {
254  val uop = new MicroOp
255  val isWalk = Bool()
256}
257
258class TlbFeedback extends XSBundle {
259  val roqIdx = new RoqPtr
260  val hit = Bool()
261}
262
263class FrontendToBackendIO extends XSBundle {
264  // to backend end
265  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
266  // from backend
267  val redirect = Flipped(ValidIO(new Redirect))
268  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
269  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
270}
271
272class TlbCsrBundle extends XSBundle {
273  val satp = new Bundle {
274    val mode = UInt(4.W) // TODO: may change number to parameter
275    val asid = UInt(16.W)
276    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
277  }
278  val priv = new Bundle {
279    val mxr = Bool()
280    val sum = Bool()
281    val imode = UInt(2.W)
282    val dmode = UInt(2.W)
283  }
284
285  override def toPrintable: Printable = {
286    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
287    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
288  }
289}
290
291class SfenceBundle extends XSBundle {
292  val valid = Bool()
293  val bits = new Bundle {
294    val rs1 = Bool()
295    val rs2 = Bool()
296    val addr = UInt(VAddrBits.W)
297  }
298
299  override def toPrintable: Printable = {
300    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
301  }
302}
303