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