xref: /XiangShan/src/main/scala/xiangshan/backend/Bundles.scala (revision 4daa5bf3c3f27e7fd090866d52405b21e107eb8d)
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(LoadDependencyWidth.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    val dirtyVs         = Bool()
188
189    val eliminatedMove  = Bool()
190    // Take snapshot at this CFI inst
191    val snapshot        = Bool()
192    val debugInfo       = new PerfDebugInfo
193    val storeSetHit     = Bool() // inst has been allocated an store set
194    val waitForRobIdx   = new RobPtr // store set predicted previous store robIdx
195    // Load wait is needed
196    // load inst will not be executed until former store (predicted by mdp) addr calcuated
197    val loadWaitBit     = Bool()
198    // If (loadWaitBit && loadWaitStrict), strict load wait is needed
199    // load inst will not be executed until ALL former store addr calcuated
200    val loadWaitStrict  = Bool()
201    val ssid            = UInt(SSIDWidth.W)
202    // Todo
203    val lqIdx = new LqPtr
204    val sqIdx = new SqPtr
205    // debug module
206    val singleStep      = Bool()
207    // schedule
208    val replayInst      = Bool()
209
210    val debug_fuType    = OptionWrapper(backendParams.debugEn, FuType())
211
212    def getDebugFuType: UInt = debug_fuType.getOrElse(fuType)
213
214    def isLUI: Bool = this.fuType === FuType.alu.U && (this.selImm === SelImm.IMM_U || this.selImm === SelImm.IMM_LUI32)
215    def isLUI32: Bool = this.selImm === SelImm.IMM_LUI32
216    def isWFI: Bool = this.fuType === FuType.csr.U && fuOpType === CSROpType.wfi
217
218    def isSvinvalBegin(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && !flush
219    def isSvinval(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.sfence && !flush
220    def isSvinvalEnd(flush: Bool) = FuType.isFence(fuType) && fuOpType === FenceOpType.nofence && flush
221
222    def isHls: Bool = {
223      fuType === FuType.ldu.U && LSUOpType.isHlv(fuOpType) || fuType === FuType.stu.U && LSUOpType.isHsv(fuOpType)
224    }
225
226    def srcIsReady: Vec[Bool] = {
227      VecInit(this.srcType.zip(this.srcState).map {
228        case (t, s) => SrcType.isNotReg(t) || SrcState.isReady(s)
229      })
230    }
231
232    def clearExceptions(
233      exceptionBits: Seq[Int] = Seq(),
234      flushPipe    : Boolean = false,
235      replayInst   : Boolean = false
236    ): DynInst = {
237      this.exceptionVec.zipWithIndex.filterNot(x => exceptionBits.contains(x._2)).foreach(_._1 := false.B)
238      if (!flushPipe) { this.flushPipe := false.B }
239      if (!replayInst) { this.replayInst := false.B }
240      this
241    }
242
243    def needWriteRf: Bool = (rfWen && ldest =/= 0.U) || fpWen || vecWen
244  }
245
246  trait BundleSource {
247    var wakeupSource = "undefined"
248    var idx = 0
249  }
250
251  /**
252    *
253    * @param pregIdxWidth index width of preg
254    * @param exuIndices exu indices of wakeup bundle
255    */
256  sealed abstract class IssueQueueWakeUpBaseBundle(pregIdxWidth: Int, val exuIndices: Seq[Int])(implicit p: Parameters) extends XSBundle {
257    val rfWen = Bool()
258    val fpWen = Bool()
259    val vecWen = Bool()
260    val pdest = UInt(pregIdxWidth.W)
261
262    /**
263      * @param successor Seq[(psrc, srcType)]
264      * @return Seq[if wakeup psrc]
265      */
266    def wakeUp(successor: Seq[(UInt, UInt)], valid: Bool): Seq[Bool] = {
267      successor.map { case (thatPsrc, srcType) =>
268        val pdestMatch = pdest === thatPsrc
269        pdestMatch && (
270          SrcType.isFp(srcType) && this.fpWen ||
271            SrcType.isXp(srcType) && this.rfWen ||
272            SrcType.isVp(srcType) && this.vecWen
273          ) && valid
274      }
275    }
276    def wakeUpFromIQ(successor: Seq[(UInt, UInt)]): Seq[Bool] = {
277      successor.map { case (thatPsrc, srcType) =>
278        val pdestMatch = pdest === thatPsrc
279        pdestMatch && (
280          SrcType.isFp(srcType) && this.fpWen ||
281            SrcType.isXp(srcType) && this.rfWen ||
282            SrcType.isVp(srcType) && this.vecWen
283          )
284      }
285    }
286
287    def hasOnlyOneSource: Boolean = exuIndices.size == 1
288
289    def hasMultiSources: Boolean = exuIndices.size > 1
290
291    def isWBWakeUp = this.isInstanceOf[IssueQueueWBWakeUpBundle]
292
293    def isIQWakeUp = this.isInstanceOf[IssueQueueIQWakeUpBundle]
294
295    def exuIdx: Int = {
296      require(hasOnlyOneSource)
297      this.exuIndices.head
298    }
299  }
300
301  class IssueQueueWBWakeUpBundle(exuIndices: Seq[Int], backendParams: BackendParams)(implicit p: Parameters) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, exuIndices) {
302
303  }
304
305  class IssueQueueIQWakeUpBundle(
306    exuIdx: Int,
307    backendParams: BackendParams,
308    copyWakeupOut: Boolean = false,
309    copyNum: Int = 0
310  )(implicit p: Parameters) extends IssueQueueWakeUpBaseBundle(backendParams.pregIdxWidth, Seq(exuIdx)) {
311    val loadDependency = Vec(LoadPipelineWidth, UInt(LoadDependencyWidth.W))
312    val is0Lat = Bool()
313    val params = backendParams.allExuParams.filter(_.exuIdx == exuIdx).head
314    val pdestCopy  = OptionWrapper(copyWakeupOut, Vec(copyNum, UInt(params.wbPregIdxWidth.W)))
315    val rfWenCopy  = OptionWrapper(copyWakeupOut && params.needIntWen, Vec(copyNum, Bool()))
316    val fpWenCopy  = OptionWrapper(copyWakeupOut && params.needFpWen, Vec(copyNum, Bool()))
317    val vecWenCopy = OptionWrapper(copyWakeupOut && params.needVecWen, Vec(copyNum, Bool()))
318    val loadDependencyCopy = OptionWrapper(copyWakeupOut && params.isIQWakeUpSink, Vec(copyNum, Vec(LoadPipelineWidth, UInt(LoadDependencyWidth.W))))
319    def fromExuInput(exuInput: ExuInput, l2ExuVecs: Vec[UInt]): Unit = {
320      this.rfWen := exuInput.rfWen.getOrElse(false.B)
321      this.fpWen := exuInput.fpWen.getOrElse(false.B)
322      this.vecWen := exuInput.vecWen.getOrElse(false.B)
323      this.pdest := exuInput.pdest
324    }
325
326    def fromExuInput(exuInput: ExuInput): Unit = {
327      this.rfWen := exuInput.rfWen.getOrElse(false.B)
328      this.fpWen := exuInput.fpWen.getOrElse(false.B)
329      this.vecWen := exuInput.vecWen.getOrElse(false.B)
330      this.pdest := exuInput.pdest
331    }
332  }
333
334  class VPUCtrlSignals(implicit p: Parameters) extends XSBundle {
335    // vtype
336    val vill      = Bool()
337    val vma       = Bool()    // 1: agnostic, 0: undisturbed
338    val vta       = Bool()    // 1: agnostic, 0: undisturbed
339    val vsew      = VSew()
340    val vlmul     = VLmul()   // 1/8~8      --> -3~3
341
342    val vm        = Bool()    // 0: need v0.t
343    val vstart    = Vl()
344
345    // float rounding mode
346    val frm       = Frm()
347    // scalar float instr and vector float reduction
348    val fpu       = Fpu()
349    // vector fix int rounding mode
350    val vxrm      = Vxrm()
351    // vector uop index, exclude other non-vector uop
352    val vuopIdx   = UopIdx()
353    val lastUop   = Bool()
354    // maybe used if data dependancy
355    val vmask     = UInt(MaskSrcData().dataWidth.W)
356    val vl        = Vl()
357
358    // vector load/store
359    val nf        = Nf()
360    val veew      = VEew()
361
362    val isReverse = Bool() // vrsub, vrdiv
363    val isExt     = Bool()
364    val isNarrow  = Bool()
365    val isDstMask = Bool() // vvm, vvvm, mmm
366    val isOpMask  = Bool() // vmand, vmnand
367    val isMove    = Bool() // vmv.s.x, vmv.v.v, vmv.v.x, vmv.v.i
368
369    def vtype: VType = {
370      val res = Wire(VType())
371      res.illegal := this.vill
372      res.vma     := this.vma
373      res.vta     := this.vta
374      res.vsew    := this.vsew
375      res.vlmul   := this.vlmul
376      res
377    }
378
379    def vconfig: VConfig = {
380      val res = Wire(VConfig())
381      res.vtype := this.vtype
382      res.vl    := this.vl
383      res
384    }
385
386    def connectVType(source: VType): Unit = {
387      this.vill  := source.illegal
388      this.vma   := source.vma
389      this.vta   := source.vta
390      this.vsew  := source.vsew
391      this.vlmul := source.vlmul
392    }
393  }
394
395  // DynInst --[IssueQueue]--> DataPath
396  class IssueQueueIssueBundle(
397    iqParams: IssueBlockParams,
398    val exuParams: ExeUnitParams,
399  )(implicit
400    p: Parameters
401  ) extends Bundle {
402    private val rfReadDataCfgSet: Seq[Set[DataConfig]] = exuParams.getRfReadDataCfgSet
403
404    val rf: MixedVec[MixedVec[RfReadPortWithConfig]] = Flipped(MixedVec(
405      rfReadDataCfgSet.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 getFpRfReadValidBundle(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 getVfRfReadValidBundle(issueValid: Bool): Seq[ValidIO[RfReadPortWithConfig]] = {
435      rf.zip(srcType).map {
436        case (rfRd: MixedVec[RfReadPortWithConfig], t: UInt) =>
437          makeValid(issueValid, rfRd.head)
438      }.toSeq
439    }
440
441    def getIntRfWriteValidBundle(issueValid: Bool) = {
442
443    }
444  }
445
446  class OGRespBundle(implicit p:Parameters, params: IssueBlockParams) extends XSBundle {
447    val issueQueueParams = this.params
448    val og0resp = Valid(new EntryDeqRespBundle)
449    val og1resp = Valid(new EntryDeqRespBundle)
450  }
451
452  class fuBusyRespBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
453    val respType = RSFeedbackType() // update credit if needs replay
454    val rfWen = Bool() // TODO: use params to identify IntWB/VfWB
455    val fuType = FuType()
456  }
457
458  class WbFuBusyTableWriteBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
459    private val intCertainLat = params.intLatencyCertain
460    private val fpCertainLat = params.fpLatencyCertain
461    private val vfCertainLat = params.vfLatencyCertain
462    private val intLat = params.intLatencyValMax
463    private val fpLat = params.fpLatencyValMax
464    private val vfLat = params.vfLatencyValMax
465
466    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
467    val fpWbBusyTable = OptionWrapper(fpCertainLat, UInt((fpLat + 1).W))
468    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
469    val intDeqRespSet = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
470    val fpDeqRespSet = OptionWrapper(fpCertainLat, UInt((fpLat + 1).W))
471    val vfDeqRespSet = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
472  }
473
474  class WbFuBusyTableReadBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
475    private val intCertainLat = params.intLatencyCertain
476    private val fpCertainLat = params.fpLatencyCertain
477    private val vfCertainLat = params.vfLatencyCertain
478    private val intLat = params.intLatencyValMax
479    private val fpLat = params.fpLatencyValMax
480    private val vfLat = params.vfLatencyValMax
481
482    val intWbBusyTable = OptionWrapper(intCertainLat, UInt((intLat + 1).W))
483    val fpWbBusyTable = OptionWrapper(fpCertainLat, UInt((fpLat + 1).W))
484    val vfWbBusyTable = OptionWrapper(vfCertainLat, UInt((vfLat + 1).W))
485  }
486
487  class WbConflictBundle(val params: ExeUnitParams)(implicit p: Parameters) extends XSBundle {
488    private val intCertainLat = params.intLatencyCertain
489    private val fpCertainLat = params.fpLatencyCertain
490    private val vfCertainLat = params.vfLatencyCertain
491
492    val intConflict = OptionWrapper(intCertainLat, Bool())
493    val fpConflict = OptionWrapper(fpCertainLat, Bool())
494    val vfConflict = OptionWrapper(vfCertainLat, Bool())
495  }
496
497  class ImmInfo extends Bundle {
498    val imm = UInt(32.W)
499    val immType = SelImm()
500  }
501
502  // DataPath --[ExuInput]--> Exu
503  class ExuInput(val params: ExeUnitParams, copyWakeupOut:Boolean = false, copyNum:Int = 0)(implicit p: Parameters) extends XSBundle {
504    val fuType        = FuType()
505    val fuOpType      = FuOpType()
506    val src           = Vec(params.numRegSrc, UInt(params.dataBitsMax.W))
507    val imm           = UInt(32.W)
508    val robIdx        = new RobPtr
509    val iqIdx         = UInt(log2Up(MemIQSizeMax).W)// Only used by store yet
510    val isFirstIssue  = Bool()                      // Only used by store yet
511    val pdestCopy  = OptionWrapper(copyWakeupOut, Vec(copyNum, UInt(params.wbPregIdxWidth.W)))
512    val rfWenCopy  = OptionWrapper(copyWakeupOut && params.needIntWen, Vec(copyNum, Bool()))
513    val fpWenCopy  = OptionWrapper(copyWakeupOut && params.needFpWen, Vec(copyNum, Bool()))
514    val vecWenCopy = OptionWrapper(copyWakeupOut && params.needVecWen, Vec(copyNum, Bool()))
515    val loadDependencyCopy = OptionWrapper(copyWakeupOut && params.isIQWakeUpSink, Vec(copyNum, Vec(LoadPipelineWidth, UInt(LoadDependencyWidth.W))))
516    val pdest         = UInt(params.wbPregIdxWidth.W)
517    val rfWen         = if (params.needIntWen)    Some(Bool())                        else None
518    val fpWen         = if (params.needFpWen)     Some(Bool())                        else None
519    val vecWen        = if (params.needVecWen)    Some(Bool())                        else None
520    val fpu           = if (params.writeFflags)   Some(new FPUCtrlSignals)            else None
521    val vpu           = if (params.needVPUCtrl)   Some(new VPUCtrlSignals)            else None
522    val flushPipe     = if (params.flushPipe)     Some(Bool())                        else None
523    val pc            = if (params.needPc)        Some(UInt(VAddrData().dataWidth.W)) else None
524    val preDecode     = if (params.hasPredecode)  Some(new PreDecodeInfo)             else None
525    val ftqIdx        = if (params.needPc || params.replayInst || params.hasStoreAddrFu)
526                                                  Some(new FtqPtr)                    else None
527    val ftqOffset     = if (params.needPc || params.replayInst || params.hasStoreAddrFu)
528                                                  Some(UInt(log2Up(PredictWidth).W))  else None
529    val predictInfo   = if (params.needPdInfo)  Some(new Bundle {
530      val target = UInt(VAddrData().dataWidth.W)
531      val taken = Bool()
532    }) else None
533    val loadWaitBit    = OptionWrapper(params.hasLoadExu, Bool())
534    val waitForRobIdx  = OptionWrapper(params.hasLoadExu, new RobPtr) // store set predicted previous store robIdx
535    val storeSetHit    = OptionWrapper(params.hasLoadExu, Bool()) // inst has been allocated an store set
536    val loadWaitStrict = OptionWrapper(params.hasLoadExu, Bool()) // load inst will not be executed until ALL former store addr calcuated
537    val ssid           = OptionWrapper(params.hasLoadExu, UInt(SSIDWidth.W))
538    val sqIdx = if (params.hasMemAddrFu || params.hasStdFu) Some(new SqPtr) else None
539    val lqIdx = if (params.hasMemAddrFu) Some(new LqPtr) else None
540    val dataSources = Vec(params.numRegSrc, DataSource())
541    val l1ExuOH = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, ExuOH()))
542    val srcTimer = OptionWrapper(params.isIQWakeUpSink, Vec(params.numRegSrc, UInt(3.W)))
543    val loadDependency = OptionWrapper(params.isIQWakeUpSink, Vec(LoadPipelineWidth, UInt(LoadDependencyWidth.W)))
544
545    val perfDebugInfo = new PerfDebugInfo()
546
547    def exuIdx = this.params.exuIdx
548
549    def needCancel(og0CancelOH: UInt, og1CancelOH: UInt) : Bool = {
550      if (params.isIQWakeUpSink) {
551        require(
552          og0CancelOH.getWidth == l1ExuOH.get.head.getWidth,
553          s"cancelVecSize: {og0: ${og0CancelOH.getWidth}, og1: ${og1CancelOH.getWidth}}"
554        )
555        val l1Cancel: Bool = l1ExuOH.get.zip(srcTimer.get).map {
556          case(exuOH: UInt, srcTimer: UInt) =>
557            (exuOH & og0CancelOH).orR && srcTimer === 1.U
558        }.reduce(_ | _)
559        l1Cancel
560      } else {
561        false.B
562      }
563    }
564
565    def getFpWen = {
566      if (params.writeFpRf) this.fpWen
567      else None
568    }
569
570    def getVfWen = {
571      if(params.writeVecRf) this.vecWen
572      else None
573    }
574
575    def fromIssueBundle(source: IssueQueueIssueBundle): Unit = {
576      // src is assigned to rfReadData
577      this.fuType        := source.common.fuType
578      this.fuOpType      := source.common.fuOpType
579      this.imm           := source.common.imm
580      this.robIdx        := source.common.robIdx
581      this.pdest         := source.common.pdest
582      this.isFirstIssue  := source.common.isFirstIssue // Only used by mem debug log
583      this.iqIdx         := source.common.iqIdx        // Only used by mem feedback
584      this.dataSources   := source.common.dataSources
585      this.l1ExuOH       .foreach(_ := source.common.l1ExuOH.get)
586      this.rfWen         .foreach(_ := source.common.rfWen.get)
587      this.fpWen         .foreach(_ := source.common.fpWen.get)
588      this.vecWen        .foreach(_ := source.common.vecWen.get)
589      this.fpu           .foreach(_ := source.common.fpu.get)
590      this.vpu           .foreach(_ := source.common.vpu.get)
591      this.flushPipe     .foreach(_ := source.common.flushPipe.get)
592      this.pc            .foreach(_ := source.common.pc.get)
593      this.preDecode     .foreach(_ := source.common.preDecode.get)
594      this.ftqIdx        .foreach(_ := source.common.ftqIdx.get)
595      this.ftqOffset     .foreach(_ := source.common.ftqOffset.get)
596      this.predictInfo   .foreach(_ := source.common.predictInfo.get)
597      this.loadWaitBit   .foreach(_ := source.common.loadWaitBit.get)
598      this.waitForRobIdx .foreach(_ := source.common.waitForRobIdx.get)
599      this.storeSetHit   .foreach(_ := source.common.storeSetHit.get)
600      this.loadWaitStrict.foreach(_ := source.common.loadWaitStrict.get)
601      this.ssid          .foreach(_ := source.common.ssid.get)
602      this.lqIdx         .foreach(_ := source.common.lqIdx.get)
603      this.sqIdx         .foreach(_ := source.common.sqIdx.get)
604      this.srcTimer      .foreach(_ := source.common.srcTimer.get)
605      this.loadDependency.foreach(_ := source.common.loadDependency.get.map(_ << 1))
606    }
607  }
608
609  // ExuInput --[FuncUnit]--> ExuOutput
610  class ExuOutput(
611    val params: ExeUnitParams,
612  )(implicit
613    val p: Parameters
614  ) extends Bundle with BundleSource with HasXSParameter {
615    val data         = UInt(params.dataBitsMax.W)
616    val pdest        = UInt(params.wbPregIdxWidth.W)
617    val robIdx       = new RobPtr
618    val intWen       = if (params.needIntWen)   Some(Bool())                  else None
619    val fpWen        = if (params.needFpWen)    Some(Bool())                  else None
620    val vecWen       = if (params.needVecWen)   Some(Bool())                  else None
621    val redirect     = if (params.hasRedirect)  Some(ValidIO(new Redirect))   else None
622    val fflags       = if (params.writeFflags)  Some(UInt(5.W))               else None
623    val wflags       = if (params.writeFflags)  Some(Bool())                  else None
624    val vxsat        = if (params.writeVxsat)   Some(Bool())                  else None
625    val exceptionVec = if (params.exceptionOut.nonEmpty) Some(ExceptionVec()) else None
626    val flushPipe    = if (params.flushPipe)    Some(Bool())                  else None
627    val replay       = if (params.replayInst)   Some(Bool())                  else None
628    val lqIdx        = if (params.hasLoadFu)    Some(new LqPtr())             else None
629    val sqIdx        = if (params.hasStoreAddrFu || params.hasStdFu)
630                                                Some(new SqPtr())             else None
631    val trigger      = if (params.trigger)      Some(new TriggerCf)           else None
632    // uop info
633    val predecodeInfo = if(params.hasPredecode) Some(new PreDecodeInfo) else None
634    // vldu used only
635    val vls = OptionWrapper(params.hasVLoadFu, new Bundle {
636      val vpu = new VPUCtrlSignals
637      val oldVdPsrc = UInt(PhyRegIdxWidth.W)
638      val vdIdx = UInt(3.W)
639      val vdIdxInField = UInt(3.W)
640      val isIndexed = Bool()
641      val isMasked = Bool()
642    })
643    val debug = new DebugBundle
644    val debugInfo = new PerfDebugInfo
645  }
646
647  // ExuOutput + DynInst --> WriteBackBundle
648  class WriteBackBundle(val params: PregWB, backendParams: BackendParams)(implicit p: Parameters) extends Bundle with BundleSource {
649    val rfWen = Bool()
650    val fpWen = Bool()
651    val vecWen = Bool()
652    val pdest = UInt(params.pregIdxWidth(backendParams).W)
653    val data = UInt(params.dataWidth.W)
654    val robIdx = new RobPtr()(p)
655    val flushPipe = Bool()
656    val replayInst = Bool()
657    val redirect = ValidIO(new Redirect)
658    val fflags = UInt(5.W)
659    val vxsat = Bool()
660    val exceptionVec = ExceptionVec()
661    val debug = new DebugBundle
662    val debugInfo = new PerfDebugInfo
663
664    this.wakeupSource = s"WB(${params.toString})"
665
666    def fromExuOutput(source: ExuOutput) = {
667      this.rfWen  := source.intWen.getOrElse(false.B)
668      this.fpWen  := source.fpWen.getOrElse(false.B)
669      this.vecWen := source.vecWen.getOrElse(false.B)
670      this.pdest  := source.pdest
671      this.data   := source.data
672      this.robIdx := source.robIdx
673      this.flushPipe := source.flushPipe.getOrElse(false.B)
674      this.replayInst := source.replay.getOrElse(false.B)
675      this.redirect := source.redirect.getOrElse(0.U.asTypeOf(this.redirect))
676      this.fflags := source.fflags.getOrElse(0.U.asTypeOf(this.fflags))
677      this.vxsat := source.vxsat.getOrElse(0.U.asTypeOf(this.vxsat))
678      this.exceptionVec := source.exceptionVec.getOrElse(0.U.asTypeOf(this.exceptionVec))
679      this.debug := source.debug
680      this.debugInfo := source.debugInfo
681    }
682
683    def asIntRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
684      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, backendParams.getPregParams(IntData()).addrWidth)))
685      rfWrite.wen := this.rfWen && fire
686      rfWrite.addr := this.pdest
687      rfWrite.data := this.data
688      rfWrite.intWen := this.rfWen
689      rfWrite.fpWen := false.B
690      rfWrite.vecWen := false.B
691      rfWrite
692    }
693
694    def asFpRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
695      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, backendParams.getPregParams(FpData()).addrWidth)))
696      rfWrite.wen := this.fpWen && fire
697      rfWrite.addr := this.pdest
698      rfWrite.data := this.data
699      rfWrite.intWen := false.B
700      rfWrite.fpWen := this.fpWen
701      rfWrite.vecWen := false.B
702      rfWrite
703    }
704
705    def asVfRfWriteBundle(fire: Bool): RfWritePortWithConfig = {
706      val rfWrite = Wire(Output(new RfWritePortWithConfig(this.params.dataCfg, backendParams.getPregParams(VecData()).addrWidth)))
707      rfWrite.wen := this.vecWen && fire
708      rfWrite.addr := this.pdest
709      rfWrite.data := this.data
710      rfWrite.intWen := false.B
711      rfWrite.fpWen := false.B
712      rfWrite.vecWen := this.vecWen
713      rfWrite
714    }
715  }
716
717  // ExuOutput --> ExuBypassBundle --[DataPath]-->ExuInput
718  //                                /
719  //     [IssueQueue]--> ExuInput --
720  class ExuBypassBundle(
721    val params: ExeUnitParams,
722  )(implicit
723    val p: Parameters
724  ) extends Bundle {
725    val data  = UInt(params.dataBitsMax.W)
726    val pdest = UInt(params.wbPregIdxWidth.W)
727  }
728
729  class ExceptionInfo(implicit p: Parameters) extends XSBundle {
730    val pc = UInt(VAddrData().dataWidth.W)
731    val instr = UInt(32.W)
732    val commitType = CommitType()
733    val exceptionVec = ExceptionVec()
734    val gpaddr = UInt(GPAddrBits.W)
735    val singleStep = Bool()
736    val crossPageIPFFix = Bool()
737    val isInterrupt = Bool()
738    val isHls = Bool()
739    val vls = Bool()
740    val trigger  = new TriggerCf
741  }
742
743  object UopIdx {
744    def apply()(implicit p: Parameters): UInt = UInt(log2Up(p(XSCoreParamsKey).MaxUopSize + 1).W)
745  }
746
747  object FuLatency {
748    def apply(): UInt = UInt(width.W)
749
750    def width = 4 // 0~15 // Todo: assosiate it with FuConfig
751  }
752
753  object ExuOH {
754    def apply(exuNum: Int): UInt = UInt(exuNum.W)
755
756    def apply()(implicit p: Parameters): UInt = UInt(width.W)
757
758    def width(implicit p: Parameters): Int = p(XSCoreParamsKey).backendParams.numExu
759  }
760
761  object ExuVec {
762    def apply(exuNum: Int): Vec[Bool] = Vec(exuNum, Bool())
763
764    def apply()(implicit p: Parameters): Vec[Bool] = Vec(width, Bool())
765
766    def width(implicit p: Parameters): Int = p(XSCoreParamsKey).backendParams.numExu
767  }
768
769  class CancelSignal(implicit p: Parameters) extends XSBundle {
770    val rfWen = Bool()
771    val fpWen = Bool()
772    val vecWen = Bool()
773    val pdest = UInt(PhyRegIdxWidth.W)
774
775    def needCancel(srcType: UInt, psrc: UInt, valid: Bool): Bool = {
776      val pdestMatch = pdest === psrc
777      pdestMatch && (
778        SrcType.isFp(srcType) && !this.rfWen ||
779          SrcType.isXp(srcType) && this.rfWen ||
780          SrcType.isVp(srcType) && !this.rfWen
781        ) && valid
782    }
783  }
784
785  class MemExuInput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
786    val uop = new DynInst
787    val src = if (isVector) Vec(5, UInt(VLEN.W)) else Vec(3, UInt(XLEN.W))
788    val iqIdx = UInt(log2Up(MemIQSizeMax).W)
789    val isFirstIssue = Bool()
790
791    def src_rs1 = src(0)
792    def src_stride = src(1)
793    def src_vs3 = src(2)
794    def src_mask = if (isVector) src(3) else 0.U
795    def src_vl = if (isVector) src(4) else 0.U
796  }
797
798  class MemExuOutput(isVector: Boolean = false)(implicit p: Parameters) extends XSBundle {
799    val uop = new DynInst
800    val data = if (isVector) UInt(VLEN.W) else UInt(XLEN.W)
801    val mask = if (isVector) Some(UInt(VLEN.W)) else None
802    val vdIdx = if (isVector) Some(UInt(3.W)) else None // TODO: parameterize width
803    val vdIdxInField = if (isVector) Some(UInt(3.W)) else None
804    val debug = new DebugBundle
805
806    def isVls = FuType.isVls(uop.fuType)
807  }
808
809  class MemMicroOpRbExt(implicit p: Parameters) extends XSBundle {
810    val uop = new DynInst
811    val flag = UInt(1.W)
812  }
813
814  object LoadShouldCancel {
815    def apply(loadDependency: Option[Seq[UInt]], ldCancel: Seq[LoadCancelIO]): Bool = {
816      val ld1Cancel = loadDependency.map(_.zip(ldCancel.map(_.ld1Cancel)).map { case (dep, cancel) => cancel && dep(0)}.reduce(_ || _))
817      val ld2Cancel = loadDependency.map(_.zip(ldCancel.map(_.ld2Cancel)).map { case (dep, cancel) => cancel && dep(1)}.reduce(_ || _))
818      ld1Cancel.map(_ || ld2Cancel.get).getOrElse(false.B)
819    }
820  }
821}
822