xref: /XiangShan/src/main/scala/xiangshan/Bundle.scala (revision 3fce4f48e1f4c43253ff1758dbd05b8b71627990)
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 sum       = if (useSC) SInt(sumCtrBits.W) else SInt(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 scMeta = new SCMeta(EnableSC)
60}
61
62class BranchPrediction extends XSBundle {
63  val redirect = Bool()
64  val taken = Bool()
65  val jmpIdx = UInt(log2Up(PredictWidth).W)
66  val hasNotTakenBrs = Bool()
67  val target = UInt(VAddrBits.W)
68  val saveHalfRVI = Bool()
69  val takenOnBr = Bool()
70}
71
72class BranchInfo extends XSBundle with HasBPUParameter {
73  val ubtbWriteWay = UInt(log2Up(UBtbWays).W)
74  val ubtbHits = Bool()
75  val btbWriteWay = UInt(log2Up(BtbWays).W)
76  val btbHitJal = Bool()
77  val bimCtr = UInt(2.W)
78  val histPtr = UInt(log2Up(ExtHistoryLength).W)
79  val predHistPtr = UInt(log2Up(ExtHistoryLength).W)
80  val tageMeta = new TageMeta
81  val rasSp = UInt(log2Up(RasSize).W)
82  val rasTopCtr = UInt(8.W)
83  val rasToqAddr = UInt(VAddrBits.W)
84  val fetchIdx = UInt(log2Up(PredictWidth).W)
85  val specCnt = UInt(10.W)
86  val sawNotTakenBranch = Bool()
87
88  val debug_ubtb_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
89  val debug_btb_cycle  = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
90  val debug_tage_cycle = if (EnableBPUTimeRecord) UInt(64.W) else UInt(0.W)
91
92  def apply(histPtr: UInt, tageMeta: TageMeta, rasSp: UInt, rasTopCtr: UInt) = {
93    this.histPtr := histPtr
94    this.tageMeta := tageMeta
95    this.rasSp := rasSp
96    this.rasTopCtr := rasTopCtr
97    this.asUInt
98  }
99  def size = 0.U.asTypeOf(this).getWidth
100  def fromUInt(x: UInt) = x.asTypeOf(this)
101}
102
103class Predecode extends XSBundle {
104  val isFetchpcEqualFirstpc = Bool()
105  val mask = UInt((FetchWidth*2).W)
106  val pd = Vec(FetchWidth*2, (new PreDecodeInfo))
107}
108
109class BranchUpdateInfo extends XSBundle {
110  // from backend
111  val pc = UInt(VAddrBits.W)
112  val pnpc = UInt(VAddrBits.W)
113  val target = UInt(VAddrBits.W)
114  val brTarget = UInt(VAddrBits.W)
115  val taken = Bool()
116  val fetchIdx = UInt(log2Up(FetchWidth*2).W)
117  val isMisPred = Bool()
118  val brTag = new BrqPtr
119
120  // frontend -> backend -> frontend
121  val pd = new PreDecodeInfo
122  val brInfo = new BranchInfo
123}
124
125// Dequeue DecodeWidth insts from Ibuffer
126class CtrlFlow extends XSBundle {
127  val instr = UInt(32.W)
128  val pc = UInt(VAddrBits.W)
129  val exceptionVec = Vec(16, Bool())
130  val intrVec = Vec(12, Bool())
131  val brUpdate = new BranchUpdateInfo
132  val crossPageIPFFix = Bool()
133}
134
135// Decode DecodeWidth insts at Decode Stage
136class CtrlSignals extends XSBundle {
137  val src1Type, src2Type, src3Type = SrcType()
138  val lsrc1, lsrc2, lsrc3 = UInt(5.W)
139  val ldest = UInt(5.W)
140  val fuType = FuType()
141  val fuOpType = FuOpType()
142  val rfWen = Bool()
143  val fpWen = Bool()
144  val isXSTrap = Bool()
145  val noSpecExec = Bool()  // This inst can not be speculated
146  val isBlocked  = Bool()  // This inst requires pipeline to be blocked
147  val flushPipe  = Bool()  // This inst will flush all the pipe when commit, like exception but can commit
148  val isRVF = Bool()
149  val imm = UInt(XLEN.W)
150  val commitType = CommitType()
151}
152
153class CfCtrl extends XSBundle {
154  val cf = new CtrlFlow
155  val ctrl = new CtrlSignals
156  val brTag = new BrqPtr
157}
158
159// Load / Store Index
160//
161// When using unified lsroq, lsIdx serves as lsroqIdx,
162// while separated lq and sq is used, lsIdx consists of lqIdx, sqIdx and l/s type.
163// All lsroqIdx will be replaced by new lsIdx in the future.
164trait HasLSIdx { this: HasXSParameter =>
165
166  // if(EnableUnifiedLSQ){
167  // Unified LSQ
168  val lsroqIdx = UInt(LsroqIdxWidth.W)
169  // } else {
170  // Separate LSQ
171  val lqIdx = new LqPtr
172  val sqIdx = new SqPtr
173}
174
175class LSIdx extends XSBundle with HasLSIdx {}
176
177// CfCtrl -> MicroOp at Rename Stage
178class MicroOp extends CfCtrl with HasLSIdx {
179  val psrc1, psrc2, psrc3, pdest, old_pdest = UInt(PhyRegIdxWidth.W)
180  val src1State, src2State, src3State = SrcState()
181  val roqIdx = new RoqPtr
182  val diffTestDebugLrScValid = Bool()
183}
184
185class Redirect extends XSBundle {
186  val roqIdx = new RoqPtr
187  val isException = Bool()
188  val isMisPred = Bool()
189  val isReplay = Bool()
190  val isFlushPipe = Bool()
191  val pc = UInt(VAddrBits.W)
192  val target = UInt(VAddrBits.W)
193  val brTag = new BrqPtr
194}
195
196class Dp1ToDp2IO extends XSBundle {
197  val intDqToDp2 = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
198  val fpDqToDp2 = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new MicroOp))
199  val lsDqToDp2 = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new MicroOp))
200}
201
202class ReplayPregReq extends XSBundle {
203  // NOTE: set isInt and isFp both to 'false' when invalid
204  val isInt = Bool()
205  val isFp = Bool()
206  val preg = UInt(PhyRegIdxWidth.W)
207}
208
209class DebugBundle extends XSBundle{
210  val isMMIO = Bool()
211}
212
213class ExuInput extends XSBundle {
214  val uop = new MicroOp
215  val src1, src2, src3 = UInt(XLEN.W)
216}
217
218class ExuOutput extends XSBundle {
219  val uop = new MicroOp
220  val data = UInt(XLEN.W)
221  val fflags  = new Fflags
222  val redirectValid = Bool()
223  val redirect = new Redirect
224  val brUpdate = new BranchUpdateInfo
225  val debug = new DebugBundle
226}
227
228class ExternalInterruptIO extends XSBundle {
229  val mtip = Input(Bool())
230  val msip = Input(Bool())
231  val meip = Input(Bool())
232}
233
234class CSRSpecialIO extends XSBundle {
235  val exception = Flipped(ValidIO(new MicroOp))
236  val isInterrupt = Input(Bool())
237  val memExceptionVAddr = Input(UInt(VAddrBits.W))
238  val trapTarget = Output(UInt(VAddrBits.W))
239  val externalInterrupt = new ExternalInterruptIO
240  val interrupt = Output(Bool())
241}
242
243class ExuIO extends XSBundle {
244  val in = Flipped(DecoupledIO(new ExuInput))
245  val redirect = Flipped(ValidIO(new Redirect))
246  val out = DecoupledIO(new ExuOutput)
247  // for csr
248  val csrOnly = new CSRSpecialIO
249  val mcommit = Input(UInt(3.W))
250}
251
252class RoqCommit extends XSBundle {
253  val uop = new MicroOp
254  val isWalk = Bool()
255}
256
257class TlbFeedback extends XSBundle {
258  val roqIdx = new RoqPtr
259  val hit = Bool()
260}
261
262class FrontendToBackendIO extends XSBundle {
263  // to backend end
264  val cfVec = Vec(DecodeWidth, DecoupledIO(new CtrlFlow))
265  // from backend
266  val redirect = Flipped(ValidIO(new Redirect))
267  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
268  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
269  val sfence = Input(new SfenceBundle)
270  val tlbCsrIO = Input(new TlbCsrBundle)
271}
272
273class TlbCsrBundle extends XSBundle {
274  val satp = new Bundle {
275    val mode = UInt(4.W) // TODO: may change number to parameter
276    val asid = UInt(16.W)
277    val ppn  = UInt(44.W) // just use PAddrBits - 3 - vpnnLen
278  }
279  val priv = new Bundle {
280    val mxr = Bool()
281    val sum = Bool()
282    val imode = UInt(2.W)
283    val dmode = UInt(2.W)
284  }
285
286  override def toPrintable: Printable = {
287    p"Satp mode:0x${Hexadecimal(satp.mode)} asid:0x${Hexadecimal(satp.asid)} ppn:0x${Hexadecimal(satp.ppn)} " +
288    p"Priv mxr:${priv.mxr} sum:${priv.sum} imode:${priv.imode} dmode:${priv.dmode}"
289  }
290}
291
292class SfenceBundle extends XSBundle {
293  val valid = Bool()
294  val bits = new Bundle {
295    val rs1 = Bool()
296    val rs2 = Bool()
297    val addr = UInt(VAddrBits.W)
298  }
299
300  override def toPrintable: Printable = {
301    p"valid:0x${Hexadecimal(valid)} rs1:${bits.rs1} rs2:${bits.rs2} addr:${Hexadecimal(bits.addr)}"
302  }
303}
304