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