xref: /XiangShan/src/main/scala/xiangshan/backend/Bundles.scala (revision e63b0a03bf743ee0f232c7a1ce7c16c34538d31b)
1package xiangshan.backend
2
3import chipsalliance.rocketchip.config.Parameters
4import chisel3._
5import chisel3.util.BitPat.bitPatToUInt
6import chisel3.util._
7import utils.OptionWrapper
8import xiangshan._
9import xiangshan.backend.datapath.DataConfig._
10import xiangshan.backend.datapath.DataSource
11import xiangshan.backend.datapath.WbConfig.WbConfig
12import xiangshan.backend.decode.{ImmUnion, XDecode}
13import xiangshan.backend.exu.ExeUnitParams
14import xiangshan.backend.fu.FuType
15import xiangshan.backend.fu.fpu.Bundles.Frm
16import xiangshan.backend.fu.vector.Bundles.{Category, Nf, VConfig, VLmul, VSew, VType, Vl, Vxrm}
17import xiangshan.backend.issue.{IssueBlockParams, IssueQueueDeqRespBundle, IssueQueueJumpBundle, SchedulerType, StatusArrayDeqRespBundle}
18import xiangshan.backend.regfile.{RfReadPortWithConfig, RfWritePortWithConfig}
19import xiangshan.backend.rob.RobPtr
20import xiangshan.frontend._
21import xiangshan.mem.{LqPtr, SqPtr}
22
23object Bundles {
24
25  // frontend -> backend
26  class StaticInst(implicit p: Parameters) extends XSBundle {
27    val instr           = UInt(32.W)
28    val pc              = UInt(VAddrBits.W)
29    val foldpc          = UInt(MemPredPCWidth.W)
30    val exceptionVec    = ExceptionVec()
31    val trigger         = new TriggerCf
32    val preDecodeInfo   = new PreDecodeInfo
33    val pred_taken      = Bool()
34    val crossPageIPFFix = Bool()
35    val ftqPtr          = new FtqPtr
36    val ftqOffset       = UInt(log2Up(PredictWidth).W)
37
38    def connectCtrlFlow(source: CtrlFlow): Unit = {
39      this.instr            := source.instr
40      this.pc               := source.pc
41      this.foldpc           := source.foldpc
42      this.exceptionVec     := source.exceptionVec
43      this.trigger          := source.trigger
44      this.preDecodeInfo    := source.pd
45      this.pred_taken       := source.pred_taken
46      this.crossPageIPFFix  := source.crossPageIPFFix
47      this.ftqPtr           := source.ftqPtr
48      this.ftqOffset        := source.ftqOffset
49    }
50  }
51
52  // StaticInst --[Decode]--> DecodedInst
53  class DecodedInst(implicit p: Parameters) extends XSBundle {
54    def numSrc = backendParams.numSrc
55    // passed from StaticInst
56    val instr           = UInt(32.W)
57    val pc              = UInt(VAddrBits.W)
58    val foldpc          = UInt(MemPredPCWidth.W)
59    val exceptionVec    = ExceptionVec()
60    val trigger         = new TriggerCf
61    val preDecodeInfo   = new PreDecodeInfo
62    val pred_taken      = Bool()
63    val crossPageIPFFix = Bool()
64    val ftqPtr          = new FtqPtr
65    val ftqOffset       = UInt(log2Up(PredictWidth).W)
66    // decoded
67    val srcType       = Vec(numSrc, SrcType())
68    val lsrc          = Vec(numSrc, UInt(6.W))
69    val ldest         = UInt(6.W)
70    val fuType        = FuType()
71    val fuOpType      = FuOpType()
72    val rfWen         = Bool()
73    val fpWen         = Bool()
74    val vecWen        = Bool()
75    val isXSTrap      = Bool()
76    val waitForward   = Bool() // no speculate execution
77    val blockBackward = Bool()
78    val flushPipe     = Bool() // This inst will flush all the pipe when commit, like exception but can commit
79    val selImm        = SelImm()
80    val imm           = UInt(ImmUnion.maxLen.W)
81    val fpu           = new FPUCtrlSignals
82    val vpu           = new VPUCtrlSignals
83    val isMove        = Bool()
84    val uopIdx        = UInt(5.W)
85    val uopSplitType  = UopSplitType()
86    val isVset        = Bool()
87    val firstUop      = Bool()
88    val lastUop       = Bool()
89    val numUops       = UInt(log2Up(MaxUopSize).W) // rob need this
90    val commitType    = CommitType() // Todo: remove it
91
92    private def allSignals = srcType.take(3) ++ Seq(fuType, fuOpType, rfWen, fpWen, vecWen,
93      isXSTrap, waitForward, blockBackward, flushPipe, uopSplitType, selImm)
94
95    def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]): DecodedInst = {
96      val decoder: Seq[UInt] = ListLookup(
97        inst, XDecode.decodeDefault.map(bitPatToUInt),
98        table.map{ case (pat, pats) => (pat, pats.map(bitPatToUInt)) }.toArray
99      )
100      allSignals zip decoder foreach { case (s, d) => s := d }
101      this
102    }
103
104    def isSoftPrefetch: Bool = {
105      fuType === FuType.alu.U && fuOpType === ALUOpType.or && selImm === SelImm.IMM_I && ldest === 0.U
106    }
107
108    def connectStaticInst(source: StaticInst): Unit = {
109      for ((name, data) <- this.elements) {
110        if (source.elements.contains(name)) {
111          data := source.elements(name)
112        }
113      }
114    }
115  }
116
117  // DecodedInst --[Rename]--> DynInst
118  class DynInst(implicit p: Parameters) extends XSBundle {
119    def numSrc          = backendParams.numSrc
120    // passed from StaticInst
121    val instr           = UInt(32.W)
122    val pc              = UInt(VAddrBits.W)
123    val foldpc          = UInt(MemPredPCWidth.W)
124    val exceptionVec    = ExceptionVec()
125    val trigger         = new TriggerCf
126    val preDecodeInfo   = new PreDecodeInfo
127    val pred_taken      = Bool()
128    val crossPageIPFFix = Bool()
129    val ftqPtr          = new FtqPtr
130    val ftqOffset       = UInt(log2Up(PredictWidth).W)
131    // passed from DecodedInst
132    val srcType         = Vec(numSrc, SrcType())
133    val lsrc            = Vec(numSrc, UInt(6.W))
134    val ldest           = UInt(6.W)
135    val fuType          = FuType()
136    val fuOpType        = FuOpType()
137    val rfWen           = Bool()
138    val fpWen           = Bool()
139    val vecWen          = Bool()
140    val isXSTrap        = Bool()
141    val waitForward     = Bool() // no speculate execution
142    val blockBackward   = Bool()
143    val flushPipe       = Bool() // This inst will flush all the pipe when commit, like exception but can commit
144    val selImm          = SelImm()
145    val imm             = UInt(XLEN.W) // Todo: check if it need minimized
146    val fpu             = new FPUCtrlSignals
147    val vpu             = new VPUCtrlSignals
148    val isMove          = Bool()
149    val uopIdx          = UInt(5.W)
150    val isVset          = Bool()
151    val firstUop        = Bool()
152    val lastUop         = Bool()
153    val numUops         = UInt(log2Up(MaxUopSize).W) // rob need this
154    val commitType      = CommitType()
155    // rename
156    val srcState        = Vec(numSrc, SrcState())
157    val psrc            = Vec(numSrc, UInt(PhyRegIdxWidth.W))
158    val pdest           = UInt(PhyRegIdxWidth.W)
159    val oldPdest        = UInt(PhyRegIdxWidth.W)
160    val robIdx          = new RobPtr
161
162    val eliminatedMove  = Bool()
163    val debugInfo       = new PerfDebugInfo
164    val storeSetHit     = Bool() // inst has been allocated an store set
165    val waitForRobIdx   = new RobPtr // store set predicted previous store robIdx
166    // Load wait is needed
167    // load inst will not be executed until former store (predicted by mdp) addr calcuated
168    val loadWaitBit     = Bool()
169    // If (loadWaitBit && loadWaitStrict), strict load wait is needed
170    // load inst will not be executed until ALL former store addr calcuated
171    val loadWaitStrict  = Bool()
172    val ssid            = UInt(SSIDWidth.W)
173    // Todo
174    val lqIdx = new LqPtr
175    val sqIdx = new SqPtr
176    // debug module
177    val singleStep      = Bool()
178    // schedule
179    val replayInst      = Bool()
180
181    def isLUI: Bool = this.fuType === FuType.alu.U && this.selImm === SelImm.IMM_U
182    def isWFI: Bool = this.fuType === FuType.csr.U && fuOpType === CSROpType.wfi
183
184    def isSvinvalBegin(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && !flush
185    def isSvinval(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.sfence && !flush
186    def isSvinvalEnd(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && flush
187
188    def srcIsReady: Vec[Bool] = {
189      VecInit(this.srcType.zip(this.srcState).map {
190        case (t, s) => SrcType.isNotReg(t) || SrcState.isReady(s)
191      })
192    }
193
194    def clearExceptions(
195      exceptionBits: Seq[Int] = Seq(),
196      flushPipe    : Boolean = false,
197      replayInst   : Boolean = false
198    ): DynInst = {
199      this.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B)
200      if (!flushPipe) { this.flushPipe := false.B }
201      if (!replayInst) { this.replayInst := false.B }
202      this
203    }
204
205    def needWriteRf: Bool = (rfWen && ldest =/= 0.U) || fpWen || vecWen
206  }
207
208  trait BundleSource {
209    var wakeupSource = "undefined"
210    var idx = 0
211  }
212
213  /**
214    *
215    * @param pregIdxWidth index width of preg
216    * @param exuIndices exu indices of wakeup bundle
217    */
218  sealed abstract class IssueQueueWakeUpBaseBundle(pregIdxWidth: Int, val exuIndices: Seq[Int]) extends Bundle {
219    val rfWen = Bool()
220    val fpWen = Bool()
221    val vecWen = Bool()
222    val pdest = UInt(pregIdxWidth.W)
223
224    /**
225      * @param successor Seq[(psrc, srcType)]
226      * @return Seq[if wakeup psrc]
227      */
228    def wakeUp(successor: Seq[(UInt, UInt)], valid: Bool): Seq[Bool] = {
229      successor.map { case (thatPsrc, srcType) =>
230        val pdestMatch = pdest === thatPsrc
231        pdestMatch && (
232          SrcType.isFp(srcType) && this.fpWen ||
233            SrcType.isXp(srcType) && this.rfWen ||
234            SrcType.isVp(srcType) && this.vecWen
235          ) && valid
236      }
237    }
238
239    def hasOnlyOneSource: Boolean = exuIndices.size == 1
240
241    def hasMultiSources: Boolean = exuIndices.size > 1
242
243    def isWBWakeUp = this.isInstanceOf[IssueQueueWBWakeUpBundle]
244
245    def isIQWakeUp = this.isInstanceOf[IssueQueueIQWakeUpBundle]
246
247    def exuIdx: Int = {
248      require(hasOnlyOneSource)
249      this.exuIndices.head
250    }
251  }
252
253  class IssueQueueWBWakeUpBundle(exuIndices: Seq[Int], backendParams: BackendParams) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, exuIndices) {
254
255  }
256
257  class IssueQueueIQWakeUpBundle(exuIdx: Int, backendParams: BackendParams) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, Seq(exuIdx)) {
258    val l2ExuVec: Vec[Bool] = ExuVec(backendParams.numExu)
259
260    def fromExuInput(exuInput: ExuInput, l2ExuVecs: Vec[Vec[Bool]]): Unit = {
261      this.rfWen := exuInput.rfWen.getOrElse(false.B)
262      this.fpWen := exuInput.fpWen.getOrElse(false.B)
263      this.vecWen := exuInput.vecWen.getOrElse(false.B)
264      this.pdest := exuInput.pdest
265      this.l2ExuVec := l2ExuVecs.reduce { (x: Vec[Bool], y: Vec[Bool]) => VecInit((x.asUInt | y.asUInt).asBools) }
266    }
267
268    def fromExuInput(exuInput: ExuInput): Unit = {
269      this.rfWen := exuInput.rfWen.getOrElse(false.B)
270      this.fpWen := exuInput.fpWen.getOrElse(false.B)
271      this.vecWen := exuInput.vecWen.getOrElse(false.B)
272      this.pdest := exuInput.pdest
273      this.l2ExuVec.foreach(_ := false.B)
274    }
275  }
276
277  /**
278    * This bundle is used to set srcState as NotReady.
279    * @param cancelSeq cancel stage seq
280    */
281  class IssueQueueCancelBundle(val exuIdx: Int, cancelSeq: Seq[String]) extends Bundle {
282    val cancelVec: Vec[Bool] = Vec(cancelSeq.size, Bool())
283
284    def apply(cancelStage: String): Bool = {
285      this.cancelVec(cancelSeq.indexOf(cancelStage.toUpperCase))
286    }
287  }
288
289  class VPUCtrlSignals(implicit p: Parameters) extends XSBundle {
290    // vtype
291    val vill      = Bool()
292    val vma       = Bool()    // 1: agnostic, 0: undisturbed
293    val vta       = Bool()    // 1: agnostic, 0: undisturbed
294    val vsew      = VSew()
295    val vlmul     = VLmul()   // 1/8~8      --> -3~3
296
297    val vm        = Bool()    // 0: need v0.t
298    val vstart    = Vl()
299
300    // float rounding mode
301    val frm       = Frm()
302    // vector fix int rounding mode
303    val vxrm      = Vxrm()
304    // vector uop index, exclude other non-vector uop
305    val vuopIdx   = UopIdx()
306    // maybe used if data dependancy
307    val vmask     = UInt(MaskSrcData().dataWidth.W)
308    val vl        = Vl()
309
310    // vector load/store
311    val nf        = Nf()
312
313    val needScalaSrc       = Bool()
314    val permImmTruncate    = Bool() // opivi
315
316    val isReverse = Bool() // vrsub, vrdiv
317    val isExt     = Bool()
318    val isNarrow  = Bool()
319    val isDstMask = Bool() // vvm, vvvm, mmm
320    val isMove    = Bool() // vmv.s.x, vmv.v.v, vmv.v.x, vmv.v.i
321
322    def vtype: VType = {
323      val res = Wire(VType())
324      res.illegal := this.vill
325      res.vma     := this.vma
326      res.vta     := this.vta
327      res.vsew    := this.vsew
328      res.vlmul   := this.vlmul
329      res
330    }
331
332    def vconfig: VConfig = {
333      val res = Wire(VConfig())
334      res.vtype := this.vtype
335      res.vl    := this.vl
336      res
337    }
338  }
339
340  // DynInst --[IssueQueue]--> DataPath
341  class IssueQueueIssueBundle(
342    iqParams: IssueBlockParams,
343    exuParams: ExeUnitParams,
344    addrWidth: Int,
345    vaddrBits: Int
346  )(implicit
347    p: Parameters
348  ) extends Bundle {
349    private val rfReadDataCfgSet: Seq[Set[DataConfig]] = exuParams.getRfReadDataCfgSet
350
351    val rf: MixedVec[MixedVec[RfReadPortWithConfig]] = Flipped(MixedVec(
352      rfReadDataCfgSet.map((set: Set[DataConfig]) =>
353        MixedVec(set.map((x: DataConfig) => new RfReadPortWithConfig(x, addrWidth)).toSeq)
354      )
355    ))
356
357    val srcType = Vec(exuParams.numRegSrc, SrcType()) // used to select imm or reg data
358    val immType = SelImm()                         // used to select imm extractor
359    val common = new ExuInput(exuParams)
360    val jmp = if (exuParams.needPc) Some(Flipped(new IssueQueueJumpBundle)) else None
361    val addrOH = UInt(iqParams.numEntries.W)
362
363    def exuIdx = exuParams.exuIdx
364    def getSource: SchedulerType = exuParams.getWBSource
365    def getIntWbBusyBundle = common.rfWen.toSeq
366    def getVfWbBusyBundle = common.getVfWen.toSeq
367    def getIntRfReadBundle: Seq[RfReadPortWithConfig] = rf.flatten.filter(_.readInt)
368    def getVfRfReadBundle: Seq[RfReadPortWithConfig] = rf.flatten.filter(_.readVf)
369  }
370
371  class OGRespBundle(implicit p:Parameters, params: IssueBlockParams) extends XSBundle {
372    val issueQueueParams = this.params
373    val og0resp = Valid(new StatusArrayDeqRespBundle)
374    val og1resp = Valid(new StatusArrayDeqRespBundle)
375  }
376
377  class fuBusyRespBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
378    val respType = RSFeedbackType() // update credit if needs replay
379    val rfWen = Bool() // TODO: use params to identify IntWB/VfWB
380    val fuType = FuType()
381  }
382
383  class WbFuBusyTableWriteBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
384    private val intCertainLat = params.intLatencyCertain
385    private val vfCertainLat = params.vfLatencyCertain
386    private val intLat = params.intLatencyValMax
387    private val vfLat = params.vfLatencyValMax
388
389    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
390    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
391    val intDeqRespSet = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
392    val vfDeqRespSet = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
393  }
394
395  class WbFuBusyTableReadBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
396    private val intCertainLat = params.intLatencyCertain
397    private val vfCertainLat = params.vfLatencyCertain
398    private val intLat = params.intLatencyValMax
399    private val vfLat = params.vfLatencyValMax
400
401    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
402    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
403  }
404
405  class WbConflictBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
406    private val intCertainLat = params.intLatencyCertain
407    private val vfCertainLat = params.vfLatencyCertain
408
409    val intConflict = OptionWrapper(intCertainLat, Bool())
410    val vfConflict = OptionWrapper(vfCertainLat, Bool())
411  }
412
413  // DataPath --[ExuInput]--> Exu
414  class ExuInput(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
415    val fuType        = FuType()
416    val fuOpType      = FuOpType()
417    val src           = Vec(params.numRegSrc, UInt(params.dataBitsMax.W))
418    val imm           = UInt(XLEN.W)
419    val robIdx        = new RobPtr
420    val iqIdx         = UInt(log2Up(MemIQSizeMax).W)// Only used by store yet
421    val isFirstIssue  = Bool()                      // Only used by store yet
422    val pdest         = UInt(params.wbPregIdxWidth.W)
423    val rfWen         = if (params.writeIntRf)    Some(Bool())                        else None
424    val fpWen         = if (params.writeFpRf)     Some(Bool())                        else None
425    val vecWen        = if (params.writeVecRf)    Some(Bool())                        else None
426    val fpu           = if (params.needFPUCtrl)   Some(new FPUCtrlSignals)            else None
427    val vpu           = if (params.needVPUCtrl)   Some(new VPUCtrlSignals)            else None
428    val flushPipe     = if (params.flushPipe)     Some(Bool())                        else None
429    val pc            = if (params.needPc)        Some(UInt(VAddrData().dataWidth.W)) else None
430    val jalrTarget    = if (params.hasJmpFu)      Some(UInt(VAddrData().dataWidth.W)) else None
431    val preDecode     = if (params.hasPredecode)  Some(new PreDecodeInfo)             else None
432    val ftqIdx        = if (params.needPc || params.replayInst)
433                                                  Some(new FtqPtr)                    else None
434    val ftqOffset     = if (params.needPc || params.replayInst)
435                                                  Some(UInt(log2Up(PredictWidth).W))  else None
436    val predictInfo   = if (params.hasPredecode)  Some(new Bundle {
437      val target = UInt(VAddrData().dataWidth.W)
438      val taken = Bool()
439    }) else None
440    val sqIdx = if (params.hasMemAddrFu || params.hasStdFu) Some(new SqPtr) else None
441    val lqIdx = if (params.hasMemAddrFu) Some(new LqPtr) else None
442    val dataSources = Vec(params.numRegSrc, DataSource())
443    val l1ExuVec = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, ExuVec()))
444    val l2ExuVec = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, ExuVec()))
445    val srcTimer = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, UInt(3.W)))
446
447    def exuIdx = this.params.exuIdx
448
449    def needCancel(og0CancelVec: Vec[Bool], og1CancelVec: Vec[Bool]) : Bool = {
450      if (params.isIQWakeUpSink) {
451        require(
452          og0CancelVec.size == l1ExuVec.get.head.size && og1CancelVec.size == l2ExuVec.get.head.size,
453          s"cancelVecSize: {og0: ${og0CancelVec.size}, og1: ${og1CancelVec.size}}"
454        )
455        val l1Cancel: Bool = l1ExuVec.get.zip(srcTimer.get).map {
456          case(exuOH: Vec[Bool], srcTimer: UInt) =>
457            (exuOH.asUInt & og0CancelVec.asUInt).orR && srcTimer === 1.U
458        }.reduce(_ | _)
459        // Todo: remove this
460        val l2Cancel: Bool = l2ExuVec.get.zip(srcTimer.get).map {
461          case(exuOH: Vec[Bool], srcTimer: UInt) =>
462            (exuOH.asUInt & og1CancelVec.asUInt).orR && srcTimer === 2.U
463        }.reduce(_ | _)
464        l1Cancel | l2Cancel
465      } else {
466        false.B
467      }
468    }
469
470    def getVfWen = {
471      if (params.writeFpRf) this.fpWen
472      else if(params.writeVecRf) this.vecWen
473      else None
474    }
475
476    def fromIssueBundle(source: IssueQueueIssueBundle): Unit = {
477      // src is assigned to rfReadData
478      this.fuType       := source.common.fuType
479      this.fuOpType     := source.common.fuOpType
480      this.imm          := source.common.imm
481      this.robIdx       := source.common.robIdx
482      this.pdest        := source.common.pdest
483      this.isFirstIssue := source.common.isFirstIssue // Only used by mem debug log
484      this.iqIdx        := source.common.iqIdx        // Only used by mem feedback
485      this.dataSources  := source.common.dataSources
486      this.rfWen        .foreach(_ := source.common.rfWen.get)
487      this.fpWen        .foreach(_ := source.common.fpWen.get)
488      this.vecWen       .foreach(_ := source.common.vecWen.get)
489      this.fpu          .foreach(_ := source.common.fpu.get)
490      this.vpu          .foreach(_ := source.common.vpu.get)
491      this.flushPipe    .foreach(_ := source.common.flushPipe.get)
492      this.pc           .foreach(_ := source.jmp.get.pc)
493      this.jalrTarget   .foreach(_ := source.jmp.get.target)
494      this.preDecode    .foreach(_ := source.common.preDecode.get)
495      this.ftqIdx       .foreach(_ := source.common.ftqIdx.get)
496      this.ftqOffset    .foreach(_ := source.common.ftqOffset.get)
497      this.predictInfo  .foreach(_ := source.common.predictInfo.get)
498      this.lqIdx        .foreach(_ := source.common.lqIdx.get)
499      this.sqIdx        .foreach(_ := source.common.sqIdx.get)
500      this.l1ExuVec     .foreach(_ := source.common.l1ExuVec.get)
501      this.l2ExuVec     .foreach(_ := source.common.l2ExuVec.get)
502      this.srcTimer     .foreach(_ := source.common.srcTimer.get)
503    }
504  }
505
506  // ExuInput --[FuncUnit]--> ExuOutput
507  class ExuOutput(
508    val params: ExeUnitParams,
509  )(implicit
510    val p: Parameters
511  ) extends Bundle with BundleSource with HasXSParameter {
512    val data         = UInt(params.dataBitsMax.W)
513    val pdest        = UInt(params.wbPregIdxWidth.W)
514    val robIdx       = new RobPtr
515    val intWen       = if (params.writeIntRf)   Some(Bool())                  else None
516    val fpWen        = if (params.writeFpRf)    Some(Bool())                  else None
517    val vecWen       = if (params.writeVecRf)   Some(Bool())                  else None
518    val redirect     = if (params.hasRedirect)  Some(ValidIO(new Redirect))   else None
519    val fflags       = if (params.writeFflags)  Some(UInt(5.W))               else None
520    val vxsat        = if (params.writeVxsat)   Some(Bool())                  else None
521    val exceptionVec = if (params.exceptionOut.nonEmpty) Some(ExceptionVec()) else None
522    val flushPipe    = if (params.flushPipe)    Some(Bool())                  else None
523    val replay       = if (params.replayInst)   Some(Bool())                  else None
524    val lqIdx        = if (params.hasLoadFu)    Some(new LqPtr())             else None
525    val sqIdx        = if (params.hasStoreAddrFu || params.hasStdFu)
526                                                Some(new SqPtr())             else None
527    val ftqIdx       = if (params.needPc || params.replayInst)
528                                                Some(new FtqPtr)                    else None
529    val ftqOffset    = if (params.needPc || params.replayInst)
530                                                Some(UInt(log2Up(PredictWidth).W))  else None
531    // uop info
532    val predecodeInfo = if(params.hasPredecode) Some(new PreDecodeInfo) else None
533    val debug = new DebugBundle
534    val debugInfo = new PerfDebugInfo
535  }
536
537  // ExuOutput + DynInst --> WriteBackBundle
538  class WriteBackBundle(val params: WbConfig)(implicit p: Parameters) extends Bundle with BundleSource {
539    val rfWen = Bool()
540    val fpWen = Bool()
541    val vecWen = Bool()
542    val pdest = UInt(params.pregIdxWidth.W)
543    val data = UInt(params.dataWidth.W)
544    val robIdx = new RobPtr()(p)
545    val flushPipe = Bool()
546    val replayInst = Bool()
547    val redirect = ValidIO(new Redirect)
548    val fflags = UInt(5.W)
549    val vxsat = Bool()
550    val exceptionVec = ExceptionVec()
551    val debug = new DebugBundle
552    val debugInfo = new PerfDebugInfo
553
554    this.wakeupSource = s"WB(${params.toString})"
555
556    def fromExuOutput(source: ExuOutput) = {
557      this.rfWen  := source.intWen.getOrElse(false.B)
558      this.fpWen  := source.fpWen.getOrElse(false.B)
559      this.vecWen := source.vecWen.getOrElse(false.B)
560      this.pdest  := source.pdest
561      this.data   := source.data
562      this.robIdx := source.robIdx
563      this.flushPipe := source.flushPipe.getOrElse(false.B)
564      this.replayInst := source.replay.getOrElse(false.B)
565      this.redirect := source.redirect.getOrElse(0.U.asTypeOf(this.redirect))
566      this.fflags := source.fflags.getOrElse(0.U.asTypeOf(this.fflags))
567      this.vxsat := source.vxsat.getOrElse(0.U.asTypeOf(this.vxsat))
568      this.exceptionVec := source.exceptionVec.getOrElse(0.U.asTypeOf(this.exceptionVec))
569      this.debug := source.debug
570      this.debugInfo := source.debugInfo
571    }
572
573    def asIntRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
574      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, this.params.pregIdxWidth)))
575      rfWrite.wen := this.rfWen && fire
576      rfWrite.addr := this.pdest
577      rfWrite.data := this.data
578      rfWrite.intWen := this.rfWen
579      rfWrite.fpWen := false.B
580      rfWrite.vecWen := false.B
581      rfWrite
582    }
583
584    def asVfRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
585      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, this.params.pregIdxWidth)))
586      rfWrite.wen := (this.fpWen || this.vecWen) && fire
587      rfWrite.addr := this.pdest
588      rfWrite.data := this.data
589      rfWrite.intWen := false.B
590      rfWrite.fpWen := this.fpWen
591      rfWrite.vecWen := this.vecWen
592      rfWrite
593    }
594  }
595
596  // ExuOutput --> ExuBypassBundle --[DataPath]-->ExuInput
597  //                                /
598  //     [IssueQueue]--> ExuInput --
599  class ExuBypassBundle(
600    val params: ExeUnitParams,
601  )(implicit
602    val p: Parameters
603  ) extends Bundle {
604    val data  = UInt(params.dataBitsMax.W)
605    val pdest = UInt(params.wbPregIdxWidth.W)
606  }
607
608  class ExceptionInfo extends Bundle {
609    val pc = UInt(VAddrData().dataWidth.W)
610    val instr = UInt(32.W)
611    val commitType = CommitType()
612    val exceptionVec = ExceptionVec()
613    val singleStep = Bool()
614    val crossPageIPFFix = Bool()
615    val isInterrupt = Bool()
616  }
617
618  object UopIdx {
619    def apply()(implicit p: Parameters): UInt = UInt(log2Up(p(XSCoreParamsKey).MaxUopSize + 1).W)
620  }
621
622  object FuLatency {
623    def apply(): UInt = UInt(width.W)
624
625    def width = 4 // 0~15 // Todo: assosiate it with FuConfig
626  }
627
628  object ExuVec {
629    def apply(exuNum: Int): Vec[Bool] = Vec(exuNum, Bool())
630
631    def apply()(implicit p: Parameters): Vec[Bool] = Vec(width, Bool())
632
633    def width(implicit p: Parameters): Int = p(XSCoreParamsKey).backendParams.numExu
634  }
635
636  class MemExuInput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
637    val uop = new DynInst
638    val src = if (isVector) Vec(5, UInt(VLEN.W)) else Vec(3, UInt(XLEN.W))
639    val iqIdx = UInt(log2Up(MemIQSizeMax).W)
640    val isFirstIssue = Bool()
641  }
642
643  class MemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
644    val uop = new DynInst
645    val data = if (isVector) UInt(VLEN.W) else UInt(XLEN.W)
646    val debug = new DebugBundle
647  }
648
649  class MemMicroOpRbExt(implicit p: Parameters) extends XSBundle {
650    val uop = new DynInst
651    val flag = UInt(1.W)
652  }
653}
654