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