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