xref: /XiangShan/src/main/scala/xiangshan/backend/Bundles.scala (revision 49f433de6df515ccb8595055741a1826ae5e0e37)
1package xiangshan.backend
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util.BitPat.bitPatToUInt
6import chisel3.util._
7import utils.BundleUtils.makeValid
8import utils.OptionWrapper
9import xiangshan._
10import xiangshan.backend.datapath.DataConfig._
11import xiangshan.backend.datapath.DataSource
12import xiangshan.backend.datapath.WbConfig.PregWB
13import xiangshan.backend.decode.{ImmUnion, XDecode}
14import xiangshan.backend.exu.ExeUnitParams
15import xiangshan.backend.fu.FuType
16import xiangshan.backend.fu.fpu.Bundles.Frm
17import xiangshan.backend.fu.vector.Bundles._
18import xiangshan.backend.issue.{IssueBlockParams, IssueQueueDeqRespBundle, SchedulerType}
19import xiangshan.backend.issue.EntryBundles._
20import xiangshan.backend.regfile.{RfReadPortWithConfig, RfWritePortWithConfig}
21import xiangshan.backend.rob.RobPtr
22import xiangshan.frontend._
23import xiangshan.mem.{LqPtr, SqPtr}
24
25object Bundles {
26  /**
27   * Connect Same Name Port like bundleSource := bundleSinkBudle.
28   *
29   * There is no limit to the number of ports on both sides.
30   *
31   * Don't forget to connect the remaining ports!
32   */
33  def connectSamePort (bundleSource: Bundle, bundleSink: Bundle):Unit = {
34    bundleSource.elements.foreach { case (name, data) =>
35      if (bundleSink.elements.contains(name))
36        data := bundleSink.elements(name)
37    }
38  }
39  // frontend -> backend
40  class StaticInst(implicit p: Parameters) extends XSBundle {
41    val instr           = UInt(32.W)
42    val pc              = UInt(VAddrBits.W)
43    val foldpc          = UInt(MemPredPCWidth.W)
44    val exceptionVec    = ExceptionVec()
45    val trigger         = new TriggerCf
46    val preDecodeInfo   = new PreDecodeInfo
47    val pred_taken      = Bool()
48    val crossPageIPFFix = Bool()
49    val ftqPtr          = new FtqPtr
50    val ftqOffset       = UInt(log2Up(PredictWidth).W)
51
52    def connectCtrlFlow(source: CtrlFlow): Unit = {
53      this.instr            := source.instr
54      this.pc               := source.pc
55      this.foldpc           := source.foldpc
56      this.exceptionVec     := source.exceptionVec
57      this.trigger          := source.trigger
58      this.preDecodeInfo    := source.pd
59      this.pred_taken       := source.pred_taken
60      this.crossPageIPFFix  := source.crossPageIPFFix
61      this.ftqPtr           := source.ftqPtr
62      this.ftqOffset        := source.ftqOffset
63    }
64  }
65
66  // StaticInst --[Decode]--> DecodedInst
67  class DecodedInst(implicit p: Parameters) extends XSBundle {
68    def numSrc = backendParams.numSrc
69    // passed from StaticInst
70    val instr           = UInt(32.W)
71    val pc              = UInt(VAddrBits.W)
72    val foldpc          = UInt(MemPredPCWidth.W)
73    val exceptionVec    = ExceptionVec()
74    val trigger         = new TriggerCf
75    val preDecodeInfo   = new PreDecodeInfo
76    val pred_taken      = Bool()
77    val crossPageIPFFix = Bool()
78    val ftqPtr          = new FtqPtr
79    val ftqOffset       = UInt(log2Up(PredictWidth).W)
80    // decoded
81    val srcType         = Vec(numSrc, SrcType())
82    val lsrc            = Vec(numSrc, UInt(6.W))
83    val ldest           = UInt(6.W)
84    val fuType          = FuType()
85    val fuOpType        = FuOpType()
86    val rfWen           = Bool()
87    val fpWen           = Bool()
88    val vecWen          = Bool()
89    val isXSTrap        = Bool()
90    val waitForward     = Bool() // no speculate execution
91    val blockBackward   = Bool()
92    val flushPipe       = Bool() // This inst will flush all the pipe when commit, like exception but can commit
93    val canRobCompress  = Bool()
94    val selImm          = SelImm()
95    val imm             = UInt(ImmUnion.maxLen.W)
96    val fpu             = new FPUCtrlSignals
97    val vpu             = new VPUCtrlSignals
98    val vlsInstr        = Bool()
99    val wfflags         = Bool()
100    val isMove          = Bool()
101    val uopIdx          = UopIdx()
102    val uopSplitType    = UopSplitType()
103    val isVset          = Bool()
104    val firstUop        = Bool()
105    val lastUop         = Bool()
106    val numUops         = UInt(log2Up(MaxUopSize).W) // rob need this
107    val numWB           = UInt(log2Up(MaxUopSize).W) // rob need this
108    val commitType      = CommitType() // Todo: remove it
109
110    val debug_fuType    = OptionWrapper(backendParams.debugEn, FuType())
111
112    private def allSignals = srcType.take(3) ++ Seq(fuType, fuOpType, rfWen, fpWen, vecWen,
113      isXSTrap, waitForward, blockBackward, flushPipe, canRobCompress, uopSplitType, selImm)
114
115    def decode(inst: UInt, table: Iterable[(BitPat, List[BitPat])]): DecodedInst = {
116      val decoder: Seq[UInt] = ListLookup(
117        inst, XDecode.decodeDefault.map(bitPatToUInt),
118        table.map{ case (pat, pats) => (pat, pats.map(bitPatToUInt)) }.toArray
119      )
120      allSignals zip decoder foreach { case (s, d) => s := d }
121      debug_fuType.foreach(_ := fuType)
122      this
123    }
124
125    def isSoftPrefetch: Bool = {
126      fuType === FuType.alu.U && fuOpType === ALUOpType.or && selImm === SelImm.IMM_I && ldest === 0.U
127    }
128
129    def connectStaticInst(source: StaticInst): Unit = {
130      for ((name, data) <- this.elements) {
131        if (source.elements.contains(name)) {
132          data := source.elements(name)
133        }
134      }
135    }
136  }
137
138  // DecodedInst --[Rename]--> DynInst
139  class DynInst(implicit p: Parameters) extends XSBundle {
140    def numSrc          = backendParams.numSrc
141    // passed from StaticInst
142    val instr           = UInt(32.W)
143    val pc              = UInt(VAddrBits.W)
144    val foldpc          = UInt(MemPredPCWidth.W)
145    val exceptionVec    = ExceptionVec()
146    val trigger         = new TriggerCf
147    val preDecodeInfo   = new PreDecodeInfo
148    val pred_taken      = Bool()
149    val crossPageIPFFix = Bool()
150    val ftqPtr          = new FtqPtr
151    val ftqOffset       = UInt(log2Up(PredictWidth).W)
152    // passed from DecodedInst
153    val srcType         = Vec(numSrc, SrcType())
154    val ldest           = UInt(6.W)
155    val fuType          = FuType()
156    val fuOpType        = FuOpType()
157    val rfWen           = Bool()
158    val fpWen           = Bool()
159    val vecWen          = Bool()
160    val isXSTrap        = Bool()
161    val waitForward     = Bool() // no speculate execution
162    val blockBackward   = Bool()
163    val flushPipe       = Bool() // This inst will flush all the pipe when commit, like exception but can commit
164    val canRobCompress  = Bool()
165    val selImm          = SelImm()
166    val imm             = UInt(32.W)
167    val fpu             = new FPUCtrlSignals
168    val vpu             = new VPUCtrlSignals
169    val vlsInstr        = Bool()
170    val wfflags         = Bool()
171    val isMove          = Bool()
172    val uopIdx          = UopIdx()
173    val isVset          = Bool()
174    val firstUop        = Bool()
175    val lastUop         = Bool()
176    val numUops         = UInt(log2Up(MaxUopSize).W) // rob need this
177    val numWB           = UInt(log2Up(MaxUopSize).W) // rob need this
178    val commitType      = CommitType()
179    // rename
180    val srcState        = Vec(numSrc, SrcState())
181    val srcLoadDependency  = Vec(numSrc, Vec(LoadPipelineWidth, UInt(3.W)))
182    val psrc            = Vec(numSrc, UInt(PhyRegIdxWidth.W))
183    val pdest           = UInt(PhyRegIdxWidth.W)
184    val robIdx          = new RobPtr
185    val instrSize       = UInt(log2Ceil(RenameWidth + 1).W)
186    val dirtyFs         = Bool()
187
188    val eliminatedMove  = Bool()
189    // Take snapshot at this CFI inst
190    val snapshot        = Bool()
191    val debugInfo       = new PerfDebugInfo
192    val storeSetHit     = Bool() // inst has been allocated an store set
193    val waitForRobIdx   = new RobPtr // store set predicted previous store robIdx
194    // Load wait is needed
195    // load inst will not be executed until former store (predicted by mdp) addr calcuated
196    val loadWaitBit     = Bool()
197    // If (loadWaitBit && loadWaitStrict), strict load wait is needed
198    // load inst will not be executed until ALL former store addr calcuated
199    val loadWaitStrict  = Bool()
200    val ssid            = UInt(SSIDWidth.W)
201    // Todo
202    val lqIdx = new LqPtr
203    val sqIdx = new SqPtr
204    // debug module
205    val singleStep      = Bool()
206    // schedule
207    val replayInst      = Bool()
208
209    val debug_fuType    = OptionWrapper(backendParams.debugEn, FuType())
210
211    def getDebugFuType: UInt = debug_fuType.getOrElse(fuType)
212
213    def isLUI: Bool = this.fuType === FuType.alu.U && (this.selImm === SelImm.IMM_U || this.selImm === SelImm.IMM_LUI32)
214    def isLUI32: Bool = this.selImm === SelImm.IMM_LUI32
215    def isWFI: Bool = this.fuType === FuType.csr.U && fuOpType === CSROpType.wfi
216
217    def isSvinvalBegin(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && !flush
218    def isSvinval(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.sfence && !flush
219    def isSvinvalEnd(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && flush
220
221    def srcIsReady: Vec[Bool] = {
222      VecInit(this.srcType.zip(this.srcState).map {
223        case (t, s) => SrcType.isNotReg(t) || SrcState.isReady(s)
224      })
225    }
226
227    def clearExceptions(
228      exceptionBits: Seq[Int] = Seq(),
229      flushPipe    : Boolean = false,
230      replayInst   : Boolean = false
231    ): DynInst = {
232      this.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B)
233      if (!flushPipe) { this.flushPipe := false.B }
234      if (!replayInst) { this.replayInst := false.B }
235      this
236    }
237
238    def needWriteRf: Bool = (rfWen && ldest =/= 0.U) || fpWen || vecWen
239  }
240
241  trait BundleSource {
242    var wakeupSource = "undefined"
243    var idx = 0
244  }
245
246  /**
247    *
248    * @param pregIdxWidth index width of preg
249    * @param exuIndices exu indices of wakeup bundle
250    */
251  sealed abstract class IssueQueueWakeUpBaseBundle(pregIdxWidth: Int, val exuIndices: Seq[Int]) extends Bundle {
252    val rfWen = Bool()
253    val fpWen = Bool()
254    val vecWen = Bool()
255    val pdest = UInt(pregIdxWidth.W)
256
257    /**
258      * @param successor Seq[(psrc, srcType)]
259      * @return Seq[if wakeup psrc]
260      */
261    def wakeUp(successor: Seq[(UInt, UInt)], valid: Bool): Seq[Bool] = {
262      successor.map { case (thatPsrc, srcType) =>
263        val pdestMatch = pdest === thatPsrc
264        pdestMatch && (
265          SrcType.isFp(srcType) && this.fpWen ||
266            SrcType.isXp(srcType) && this.rfWen ||
267            SrcType.isVp(srcType) && this.vecWen
268          ) && valid
269      }
270    }
271    def wakeUpFromIQ(successor: Seq[(UInt, UInt)]): Seq[Bool] = {
272      successor.map { case (thatPsrc, srcType) =>
273        val pdestMatch = pdest === thatPsrc
274        pdestMatch && (
275          SrcType.isFp(srcType) && this.fpWen ||
276            SrcType.isXp(srcType) && this.rfWen ||
277            SrcType.isVp(srcType) && this.vecWen
278          )
279      }
280    }
281
282    def hasOnlyOneSource: Boolean = exuIndices.size == 1
283
284    def hasMultiSources: Boolean = exuIndices.size > 1
285
286    def isWBWakeUp = this.isInstanceOf[IssueQueueWBWakeUpBundle]
287
288    def isIQWakeUp = this.isInstanceOf[IssueQueueIQWakeUpBundle]
289
290    def exuIdx: Int = {
291      require(hasOnlyOneSource)
292      this.exuIndices.head
293    }
294  }
295
296  class IssueQueueWBWakeUpBundle(exuIndices: Seq[Int], backendParams: BackendParams) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, exuIndices) {
297
298  }
299
300class IssueQueueIQWakeUpBundle(
301  exuIdx: Int,
302  backendParams: BackendParams,
303  copyWakeupOut: Boolean = false,
304  copyNum: Int = 0
305) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, Seq(exuIdx)) {
306    val loadDependency = Vec(backendParams.LduCnt + backendParams.HyuCnt, UInt(3.W))
307    val is0Lat = Bool()
308    val params = backendParams.allExuParams.filter(_.exuIdx == exuIdx).head
309    val pdestCopy  = OptionWrapper(copyWakeupOut, Vec(copyNum, UInt(params.wbPregIdxWidth.W)))
310    val rfWenCopy  = OptionWrapper(copyWakeupOut && params.needIntWen, Vec(copyNum, Bool()))
311    val fpWenCopy  = OptionWrapper(copyWakeupOut && params.needFpWen, Vec(copyNum, Bool()))
312    val vecWenCopy = OptionWrapper(copyWakeupOut && params.needVecWen, Vec(copyNum, Bool()))
313    val loadDependencyCopy = OptionWrapper(copyWakeupOut && params.isIQWakeUpSink, Vec(copyNum,Vec(backendParams.LdExuCnt, UInt(3.W))))
314    def fromExuInput(exuInput: ExuInput, l2ExuVecs: Vec[UInt]): Unit = {
315      this.rfWen := exuInput.rfWen.getOrElse(false.B)
316      this.fpWen := exuInput.fpWen.getOrElse(false.B)
317      this.vecWen := exuInput.vecWen.getOrElse(false.B)
318      this.pdest := exuInput.pdest
319    }
320
321    def fromExuInput(exuInput: ExuInput): Unit = {
322      this.rfWen := exuInput.rfWen.getOrElse(false.B)
323      this.fpWen := exuInput.fpWen.getOrElse(false.B)
324      this.vecWen := exuInput.vecWen.getOrElse(false.B)
325      this.pdest := exuInput.pdest
326    }
327  }
328
329  class VPUCtrlSignals(implicit p: Parameters) extends XSBundle {
330    // vtype
331    val vill      = Bool()
332    val vma       = Bool()    // 1: agnostic, 0: undisturbed
333    val vta       = Bool()    // 1: agnostic, 0: undisturbed
334    val vsew      = VSew()
335    val vlmul     = VLmul()   // 1/8~8      --> -3~3
336
337    val vm        = Bool()    // 0: need v0.t
338    val vstart    = Vl()
339
340    // float rounding mode
341    val frm       = Frm()
342    // scalar float instr and vector float reduction
343    val fpu       = Fpu()
344    // vector fix int rounding mode
345    val vxrm      = Vxrm()
346    // vector uop index, exclude other non-vector uop
347    val vuopIdx   = UopIdx()
348    val lastUop   = Bool()
349    // maybe used if data dependancy
350    val vmask     = UInt(MaskSrcData().dataWidth.W)
351    val vl        = Vl()
352
353    // vector load/store
354    val nf        = Nf()
355    val veew      = VEew()
356
357    val isReverse = Bool() // vrsub, vrdiv
358    val isExt     = Bool()
359    val isNarrow  = Bool()
360    val isDstMask = Bool() // vvm, vvvm, mmm
361    val isOpMask  = Bool() // vmand, vmnand
362    val isMove    = Bool() // vmv.s.x, vmv.v.v, vmv.v.x, vmv.v.i
363
364    def vtype: VType = {
365      val res = Wire(VType())
366      res.illegal := this.vill
367      res.vma     := this.vma
368      res.vta     := this.vta
369      res.vsew    := this.vsew
370      res.vlmul   := this.vlmul
371      res
372    }
373
374    def vconfig: VConfig = {
375      val res = Wire(VConfig())
376      res.vtype := this.vtype
377      res.vl    := this.vl
378      res
379    }
380
381    def connectVType(source: VType): Unit = {
382      this.vill  := source.illegal
383      this.vma   := source.vma
384      this.vta   := source.vta
385      this.vsew  := source.vsew
386      this.vlmul := source.vlmul
387    }
388  }
389
390  // DynInst --[IssueQueue]--> DataPath
391  class IssueQueueIssueBundle(
392    iqParams: IssueBlockParams,
393    val exuParams: ExeUnitParams,
394  )(implicit
395    p: Parameters
396  ) extends Bundle {
397    private val rfReadDataCfgSet: Seq[Set[DataConfig]] = exuParams.getRfReadDataCfgSet
398    // check which set both have fp and vec and remove fp
399    private val rfReadDataCfgSetFilterFp = rfReadDataCfgSet.map((set: Set[DataConfig]) =>
400      if (set.contains(FpData()) && set.contains(VecData())) set.filter(_ != FpData())
401      else set
402    )
403
404    val rf: MixedVec[MixedVec[RfReadPortWithConfig]] = Flipped(MixedVec(
405      rfReadDataCfgSetFilterFp.map((set: Set[DataConfig]) =>
406        MixedVec(set.map((x: DataConfig) => new RfReadPortWithConfig(x, exuParams.rdPregIdxWidth)).toSeq)
407      )
408    ))
409
410    val srcType = Vec(exuParams.numRegSrc, SrcType()) // used to select imm or reg data
411    val immType = SelImm()                         // used to select imm extractor
412    val common = new ExuInput(exuParams)
413    val addrOH = UInt(iqParams.numEntries.W)
414
415    def exuIdx = exuParams.exuIdx
416    def getSource: SchedulerType = exuParams.getWBSource
417    def getIntWbBusyBundle = common.rfWen.toSeq
418    def getVfWbBusyBundle = common.getVfWen.toSeq
419
420    def getIntRfReadValidBundle(issueValid: Bool): Seq[ValidIO[RfReadPortWithConfig]] = {
421      rf.zip(srcType).map {
422        case (rfRd: MixedVec[RfReadPortWithConfig], t: UInt) =>
423          makeValid(issueValid, rfRd.head)
424      }.toSeq
425    }
426
427    def getVfRfReadValidBundle(issueValid: Bool): Seq[ValidIO[RfReadPortWithConfig]] = {
428      rf.zip(srcType).map {
429        case (rfRd: MixedVec[RfReadPortWithConfig], t: UInt) =>
430          makeValid(issueValid, rfRd.head)
431      }.toSeq
432    }
433
434    def getIntRfWriteValidBundle(issueValid: Bool) = {
435
436    }
437  }
438
439  class OGRespBundle(implicit p:Parameters, params: IssueBlockParams) extends XSBundle {
440    val issueQueueParams = this.params
441    val og0resp = Valid(new EntryDeqRespBundle)
442    val og1resp = Valid(new EntryDeqRespBundle)
443  }
444
445  class fuBusyRespBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
446    val respType = RSFeedbackType() // update credit if needs replay
447    val rfWen = Bool() // TODO: use params to identify IntWB/VfWB
448    val fuType = FuType()
449  }
450
451  class WbFuBusyTableWriteBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
452    private val intCertainLat = params.intLatencyCertain
453    private val vfCertainLat = params.vfLatencyCertain
454    private val intLat = params.intLatencyValMax
455    private val vfLat = params.vfLatencyValMax
456
457    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
458    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
459    val intDeqRespSet = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
460    val vfDeqRespSet = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
461  }
462
463  class WbFuBusyTableReadBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
464    private val intCertainLat = params.intLatencyCertain
465    private val vfCertainLat = params.vfLatencyCertain
466    private val intLat = params.intLatencyValMax
467    private val vfLat = params.vfLatencyValMax
468
469    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
470    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
471  }
472
473  class WbConflictBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
474    private val intCertainLat = params.intLatencyCertain
475    private val vfCertainLat = params.vfLatencyCertain
476
477    val intConflict = OptionWrapper(intCertainLat, Bool())
478    val vfConflict = OptionWrapper(vfCertainLat, Bool())
479  }
480
481  class ImmInfo extends Bundle {
482    val imm = UInt(32.W)
483    val immType = SelImm()
484  }
485
486  // DataPath --[ExuInput]--> Exu
487  class ExuInput(val params: ExeUnitParams, copyWakeupOut:Boolean = false, copyNum:Int = 0)(implicit p: Parameters) extends XSBundle {
488    val fuType        = FuType()
489    val fuOpType      = FuOpType()
490    val src           = Vec(params.numRegSrc, UInt(params.dataBitsMax.W))
491    val imm           = UInt(32.W)
492    val robIdx        = new RobPtr
493    val iqIdx         = UInt(log2Up(MemIQSizeMax).W)// Only used by store yet
494    val isFirstIssue  = Bool()                      // Only used by store yet
495    val pdestCopy  = OptionWrapper(copyWakeupOut, Vec(copyNum, UInt(params.wbPregIdxWidth.W)))
496    val rfWenCopy  = OptionWrapper(copyWakeupOut && params.needIntWen, Vec(copyNum, Bool()))
497    val fpWenCopy  = OptionWrapper(copyWakeupOut && params.needFpWen, Vec(copyNum, Bool()))
498    val vecWenCopy = OptionWrapper(copyWakeupOut && params.needVecWen, Vec(copyNum, Bool()))
499    val loadDependencyCopy = OptionWrapper(copyWakeupOut && params.isIQWakeUpSink, Vec(copyNum,Vec(LoadPipelineWidth, UInt(3.W))))
500    val pdest         = UInt(params.wbPregIdxWidth.W)
501    val rfWen         = if (params.needIntWen)    Some(Bool())                        else None
502    val fpWen         = if (params.needFpWen)     Some(Bool())                        else None
503    val vecWen        = if (params.needVecWen)    Some(Bool())                        else None
504    val fpu           = if (params.writeFflags)   Some(new FPUCtrlSignals)            else None
505    val vpu           = if (params.needVPUCtrl)   Some(new VPUCtrlSignals)            else None
506    val flushPipe     = if (params.flushPipe)     Some(Bool())                        else None
507    val pc            = if (params.needPc)        Some(UInt(VAddrData().dataWidth.W)) else None
508    val preDecode     = if (params.hasPredecode)  Some(new PreDecodeInfo)             else None
509    val ftqIdx        = if (params.needPc || params.replayInst || params.hasStoreAddrFu)
510                                                  Some(new FtqPtr)                    else None
511    val ftqOffset     = if (params.needPc || params.replayInst || params.hasStoreAddrFu)
512                                                  Some(UInt(log2Up(PredictWidth).W))  else None
513    val predictInfo   = if (params.needPdInfo)  Some(new Bundle {
514      val target = UInt(VAddrData().dataWidth.W)
515      val taken = Bool()
516    }) else None
517    val loadWaitBit    = OptionWrapper(params.hasLoadExu, Bool())
518    val waitForRobIdx  = OptionWrapper(params.hasLoadExu, new RobPtr) // store set predicted previous store robIdx
519    val storeSetHit    = OptionWrapper(params.hasLoadExu, Bool()) // inst has been allocated an store set
520    val loadWaitStrict = OptionWrapper(params.hasLoadExu, Bool()) // load inst will not be executed until ALL former store addr calcuated
521    val ssid           = OptionWrapper(params.hasLoadExu, UInt(SSIDWidth.W))
522    val sqIdx = if (params.hasMemAddrFu || params.hasStdFu) Some(new SqPtr) else None
523    val lqIdx = if (params.hasMemAddrFu) Some(new LqPtr) else None
524    val dataSources = Vec(params.numRegSrc, DataSource())
525    val l1ExuOH = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, ExuOH()))
526    val srcTimer = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, UInt(3.W)))
527    val loadDependency = OptionWrapper(params.isIQWakeUpSink, Vec(LoadPipelineWidth, UInt(3.W)))
528
529    val perfDebugInfo = new PerfDebugInfo()
530
531    def exuIdx = this.params.exuIdx
532
533    def needCancel(og0CancelOH: UInt, og1CancelOH: UInt) : Bool = {
534      if (params.isIQWakeUpSink) {
535        require(
536          og0CancelOH.getWidth == l1ExuOH.get.head.getWidth,
537          s"cancelVecSize: {og0: ${og0CancelOH.getWidth}, og1: ${og1CancelOH.getWidth}}"
538        )
539        val l1Cancel: Bool = l1ExuOH.get.zip(srcTimer.get).map {
540          case(exuOH: UInt, srcTimer: UInt) =>
541            (exuOH & og0CancelOH).orR && srcTimer === 1.U
542        }.reduce(_ | _)
543        l1Cancel
544      } else {
545        false.B
546      }
547    }
548
549    def getVfWen = {
550      if (params.writeFpRf) this.fpWen
551      else if(params.writeVecRf) this.vecWen
552      else None
553    }
554
555    def fromIssueBundle(source: IssueQueueIssueBundle): Unit = {
556      // src is assigned to rfReadData
557      this.fuType        := source.common.fuType
558      this.fuOpType      := source.common.fuOpType
559      this.imm           := source.common.imm
560      this.robIdx        := source.common.robIdx
561      this.pdest         := source.common.pdest
562      this.isFirstIssue  := source.common.isFirstIssue // Only used by mem debug log
563      this.iqIdx         := source.common.iqIdx        // Only used by mem feedback
564      this.dataSources   := source.common.dataSources
565      this.l1ExuOH       .foreach(_ := source.common.l1ExuOH.get)
566      this.rfWen         .foreach(_ := source.common.rfWen.get)
567      this.fpWen         .foreach(_ := source.common.fpWen.get)
568      this.vecWen        .foreach(_ := source.common.vecWen.get)
569      this.fpu           .foreach(_ := source.common.fpu.get)
570      this.vpu           .foreach(_ := source.common.vpu.get)
571      this.flushPipe     .foreach(_ := source.common.flushPipe.get)
572      this.pc            .foreach(_ := source.common.pc.get)
573      this.preDecode     .foreach(_ := source.common.preDecode.get)
574      this.ftqIdx        .foreach(_ := source.common.ftqIdx.get)
575      this.ftqOffset     .foreach(_ := source.common.ftqOffset.get)
576      this.predictInfo   .foreach(_ := source.common.predictInfo.get)
577      this.loadWaitBit   .foreach(_ := source.common.loadWaitBit.get)
578      this.waitForRobIdx .foreach(_ := source.common.waitForRobIdx.get)
579      this.storeSetHit   .foreach(_ := source.common.storeSetHit.get)
580      this.loadWaitStrict.foreach(_ := source.common.loadWaitStrict.get)
581      this.ssid          .foreach(_ := source.common.ssid.get)
582      this.lqIdx         .foreach(_ := source.common.lqIdx.get)
583      this.sqIdx         .foreach(_ := source.common.sqIdx.get)
584      this.srcTimer      .foreach(_ := source.common.srcTimer.get)
585      this.loadDependency.foreach(_ := source.common.loadDependency.get.map(_ << 1))
586    }
587  }
588
589  // ExuInput --[FuncUnit]--> ExuOutput
590  class ExuOutput(
591    val params: ExeUnitParams,
592  )(implicit
593    val p: Parameters
594  ) extends Bundle with BundleSource with HasXSParameter {
595    val data         = UInt(params.dataBitsMax.W)
596    val pdest        = UInt(params.wbPregIdxWidth.W)
597    val robIdx       = new RobPtr
598    val intWen       = if (params.needIntWen)   Some(Bool())                  else None
599    val fpWen        = if (params.needFpWen)    Some(Bool())                  else None
600    val vecWen       = if (params.needVecWen)   Some(Bool())                  else None
601    val redirect     = if (params.hasRedirect)  Some(ValidIO(new Redirect))   else None
602    val fflags       = if (params.writeFflags)  Some(UInt(5.W))               else None
603    val wflags       = if (params.writeFflags)  Some(Bool())                  else None
604    val vxsat        = if (params.writeVxsat)   Some(Bool())                  else None
605    val exceptionVec = if (params.exceptionOut.nonEmpty) Some(ExceptionVec()) else None
606    val flushPipe    = if (params.flushPipe)    Some(Bool())                  else None
607    val replay       = if (params.replayInst)   Some(Bool())                  else None
608    val lqIdx        = if (params.hasLoadFu)    Some(new LqPtr())             else None
609    val sqIdx        = if (params.hasStoreAddrFu || params.hasStdFu)
610                                                Some(new SqPtr())             else None
611    val trigger      = if (params.trigger)      Some(new TriggerCf)           else None
612    // uop info
613    val predecodeInfo = if(params.hasPredecode) Some(new PreDecodeInfo) else None
614    // vldu used only
615    val vls = OptionWrapper(params.hasVLoadFu, new Bundle {
616      val vpu = new VPUCtrlSignals
617      val oldVdPsrc = UInt(PhyRegIdxWidth.W)
618      val vdIdx = UInt(3.W)
619      val vdIdxInField = UInt(3.W)
620      val isIndexed = Bool()
621      val isMasked = Bool()
622    })
623    val debug = new DebugBundle
624    val debugInfo = new PerfDebugInfo
625  }
626
627  // ExuOutput + DynInst --> WriteBackBundle
628  class WriteBackBundle(val params: PregWB, backendParams: BackendParams)(implicit p: Parameters) extends Bundle with BundleSource {
629    val rfWen = Bool()
630    val fpWen = Bool()
631    val vecWen = Bool()
632    val pdest = UInt(params.pregIdxWidth(backendParams).W)
633    val data = UInt(params.dataWidth.W)
634    val robIdx = new RobPtr()(p)
635    val flushPipe = Bool()
636    val replayInst = Bool()
637    val redirect = ValidIO(new Redirect)
638    val fflags = UInt(5.W)
639    val vxsat = Bool()
640    val exceptionVec = ExceptionVec()
641    val debug = new DebugBundle
642    val debugInfo = new PerfDebugInfo
643
644    this.wakeupSource = s"WB(${params.toString})"
645
646    def fromExuOutput(source: ExuOutput) = {
647      this.rfWen  := source.intWen.getOrElse(false.B)
648      this.fpWen  := source.fpWen.getOrElse(false.B)
649      this.vecWen := source.vecWen.getOrElse(false.B)
650      this.pdest  := source.pdest
651      this.data   := source.data
652      this.robIdx := source.robIdx
653      this.flushPipe := source.flushPipe.getOrElse(false.B)
654      this.replayInst := source.replay.getOrElse(false.B)
655      this.redirect := source.redirect.getOrElse(0.U.asTypeOf(this.redirect))
656      this.fflags := source.fflags.getOrElse(0.U.asTypeOf(this.fflags))
657      this.vxsat := source.vxsat.getOrElse(0.U.asTypeOf(this.vxsat))
658      this.exceptionVec := source.exceptionVec.getOrElse(0.U.asTypeOf(this.exceptionVec))
659      this.debug := source.debug
660      this.debugInfo := source.debugInfo
661    }
662
663    def asIntRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
664      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, backendParams.getPregParams(IntData()).addrWidth)))
665      rfWrite.wen := this.rfWen && fire
666      rfWrite.addr := this.pdest
667      rfWrite.data := this.data
668      rfWrite.intWen := this.rfWen
669      rfWrite.fpWen := false.B
670      rfWrite.vecWen := false.B
671      rfWrite
672    }
673
674    def asVfRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
675      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, backendParams.getPregParams(VecData()).addrWidth)))
676      rfWrite.wen := (this.fpWen || this.vecWen) && fire
677      rfWrite.addr := this.pdest
678      rfWrite.data := this.data
679      rfWrite.intWen := false.B
680      rfWrite.fpWen := this.fpWen
681      rfWrite.vecWen := this.vecWen
682      rfWrite
683    }
684  }
685
686  // ExuOutput --> ExuBypassBundle --[DataPath]-->ExuInput
687  //                                /
688  //     [IssueQueue]--> ExuInput --
689  class ExuBypassBundle(
690    val params: ExeUnitParams,
691  )(implicit
692    val p: Parameters
693  ) extends Bundle {
694    val data  = UInt(params.dataBitsMax.W)
695    val pdest = UInt(params.wbPregIdxWidth.W)
696  }
697
698  class ExceptionInfo(implicit p: Parameters) extends Bundle {
699    val pc = UInt(VAddrData().dataWidth.W)
700    val instr = UInt(32.W)
701    val commitType = CommitType()
702    val exceptionVec = ExceptionVec()
703    val singleStep = Bool()
704    val crossPageIPFFix = Bool()
705    val isInterrupt = Bool()
706    val vls = Bool()
707    val trigger  = new TriggerCf
708  }
709
710  object UopIdx {
711    def apply()(implicit p: Parameters): UInt = UInt(log2Up(p(XSCoreParamsKey).MaxUopSize + 1).W)
712  }
713
714  object FuLatency {
715    def apply(): UInt = UInt(width.W)
716
717    def width = 4 // 0~15 // Todo: assosiate it with FuConfig
718  }
719
720  object ExuOH {
721    def apply(exuNum: Int): UInt = UInt(exuNum.W)
722
723    def apply()(implicit p: Parameters): UInt = UInt(width.W)
724
725    def width(implicit p: Parameters): Int = p(XSCoreParamsKey).backendParams.numExu
726  }
727
728  object ExuVec {
729    def apply(exuNum: Int): Vec[Bool] = Vec(exuNum, Bool())
730
731    def apply()(implicit p: Parameters): Vec[Bool] = Vec(width, Bool())
732
733    def width(implicit p: Parameters): Int = p(XSCoreParamsKey).backendParams.numExu
734  }
735
736  class CancelSignal(implicit p: Parameters) extends XSBundle {
737    val rfWen = Bool()
738    val fpWen = Bool()
739    val vecWen = Bool()
740    val pdest = UInt(PhyRegIdxWidth.W)
741
742    def needCancel(srcType: UInt, psrc: UInt, valid: Bool): Bool = {
743      val pdestMatch = pdest === psrc
744      pdestMatch && (
745        SrcType.isFp(srcType) && !this.rfWen ||
746          SrcType.isXp(srcType) && this.rfWen ||
747          SrcType.isVp(srcType) && !this.rfWen
748        ) && valid
749    }
750  }
751
752  class MemExuInput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
753    val uop = new DynInst
754    val src = if (isVector) Vec(5, UInt(VLEN.W)) else Vec(3, UInt(XLEN.W))
755    val iqIdx = UInt(log2Up(MemIQSizeMax).W)
756    val isFirstIssue = Bool()
757
758    def src_rs1 = src(0)
759    def src_stride = src(1)
760    def src_vs3 = src(2)
761    def src_mask = if (isVector) src(3) else 0.U
762    def src_vl = if (isVector) src(4) else 0.U
763  }
764
765  class MemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
766    val uop = new DynInst
767    val data = if (isVector) UInt(VLEN.W) else UInt(XLEN.W)
768    val mask = if (isVector) Some(UInt(VLEN.W)) else None
769    val vdIdx = if (isVector) Some(UInt(3.W)) else None // TODO: parameterize width
770    val vdIdxInField = if (isVector) Some(UInt(3.W)) else None
771    val debug = new DebugBundle
772
773    def isVls = FuType.isVls(uop.fuType)
774  }
775
776  class MemMicroOpRbExt(implicit p: Parameters) extends XSBundle {
777    val uop = new DynInst
778    val flag = UInt(1.W)
779  }
780
781  object LoadShouldCancel {
782    def apply(loadDependency: Option[Seq[UInt]], ldCancel: Seq[LoadCancelIO]): Bool = {
783      val ld1Cancel = loadDependency.map(_.zip(ldCancel.map(_.ld1Cancel)).map { case (dep, cancel) => cancel && dep(1)}.reduce(_ || _))
784      val ld2Cancel = loadDependency.map(_.zip(ldCancel.map(_.ld2Cancel)).map { case (dep, cancel) => cancel && dep(2)}.reduce(_ || _))
785      ld1Cancel.map(_ || ld2Cancel.get).getOrElse(false.B)
786    }
787  }
788}
789