xref: /XiangShan/src/main/scala/xiangshan/backend/issue/IssueQueue.scala (revision 670870b33c2943a64ae73a18de995f772b364dd8)
1package xiangshan.backend.issue
2
3import org.chipsalliance.cde.config.Parameters
4import chisel3._
5import chisel3.util._
6import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
7import utility.{GTimer, HasCircularQueuePtrHelper}
8import utils._
9import xiangshan._
10import xiangshan.backend.Bundles._
11import xiangshan.backend.decode.{ImmUnion, Imm_LUI_LOAD}
12import xiangshan.backend.datapath.DataConfig._
13import xiangshan.backend.datapath.DataSource
14import xiangshan.backend.fu.{FuConfig, FuType}
15import xiangshan.mem.{MemWaitUpdateReq, SqPtr}
16import xiangshan.backend.datapath.NewPipelineConnect
17
18class IssueQueue(params: IssueBlockParams)(implicit p: Parameters) extends LazyModule with HasXSParameter {
19  override def shouldBeInlined: Boolean = false
20
21  implicit val iqParams = params
22  lazy val module: IssueQueueImp = iqParams.schdType match {
23    case IntScheduler() => new IssueQueueIntImp(this)
24    case VfScheduler() => new IssueQueueVfImp(this)
25    case MemScheduler() => if (iqParams.StdCnt == 0) new IssueQueueMemAddrImp(this)
26      else new IssueQueueIntImp(this)
27    case _ => null
28  }
29}
30
31class IssueQueueStatusBundle(numEnq: Int) extends Bundle {
32  val empty = Output(Bool())
33  val full = Output(Bool())
34  val leftVec = Output(Vec(numEnq + 1, Bool()))
35}
36
37class IssueQueueDeqRespBundle(implicit p:Parameters, params: IssueBlockParams) extends EntryDeqRespBundle
38
39class IssueQueueIO()(implicit p: Parameters, params: IssueBlockParams) extends XSBundle {
40  // Inputs
41  val flush = Flipped(ValidIO(new Redirect))
42  val enq = Vec(params.numEnq, Flipped(DecoupledIO(new DynInst)))
43
44  val deqResp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
45  val og0Resp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
46  val og1Resp = Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle)))
47  val finalIssueResp = OptionWrapper(params.LduCnt > 0, Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle))))
48  val memAddrIssueResp = OptionWrapper(params.LduCnt > 0, Vec(params.numDeq, Flipped(ValidIO(new IssueQueueDeqRespBundle))))
49  val wbBusyTableRead = Input(params.genWbFuBusyTableReadBundle())
50  val wbBusyTableWrite = Output(params.genWbFuBusyTableWriteBundle())
51  val wakeupFromWB: MixedVec[ValidIO[IssueQueueWBWakeUpBundle]] = Flipped(params.genWBWakeUpSinkValidBundle)
52  val wakeupFromIQ: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = Flipped(params.genIQWakeUpSinkValidBundle)
53  val og0Cancel = Input(ExuOH(backendParams.numExu))
54  val og1Cancel = Input(ExuOH(backendParams.numExu))
55  val ldCancel = Vec(backendParams.LduCnt + backendParams.HyuCnt, Flipped(new LoadCancelIO))
56
57  // Outputs
58  val deq: MixedVec[DecoupledIO[IssueQueueIssueBundle]] = params.genIssueDecoupledBundle
59  val wakeupToIQ: MixedVec[ValidIO[IssueQueueIQWakeUpBundle]] = params.genIQWakeUpSourceValidBundle
60  val status = Output(new IssueQueueStatusBundle(params.numEnq))
61  // val statusNext = Output(new IssueQueueStatusBundle(params.numEnq))
62
63  val fromCancelNetwork = Flipped(params.genIssueDecoupledBundle)
64  val deqDelay: MixedVec[DecoupledIO[IssueQueueIssueBundle]] = params.genIssueDecoupledBundle// = deq.cloneType
65  def allWakeUp = wakeupFromWB ++ wakeupFromIQ
66}
67
68class IssueQueueImp(override val wrapper: IssueQueue)(implicit p: Parameters, val params: IssueBlockParams)
69  extends LazyModuleImp(wrapper)
70  with HasXSParameter {
71
72  println(s"[IssueQueueImp] ${params.getIQName} wakeupFromWB(${io.wakeupFromWB.size}), " +
73    s"wakeup exu in(${params.wakeUpInExuSources.size}): ${params.wakeUpInExuSources.map(_.name).mkString("{",",","}")}, " +
74    s"wakeup exu out(${params.wakeUpOutExuSources.size}): ${params.wakeUpOutExuSources.map(_.name).mkString("{",",","}")}, " +
75    s"numEntries: ${params.numEntries}, numRegSrc: ${params.numRegSrc}")
76
77  require(params.numExu <= 2, "IssueQueue has not supported more than 2 deq ports")
78  val deqFuCfgs     : Seq[Seq[FuConfig]] = params.exuBlockParams.map(_.fuConfigs)
79  val allDeqFuCfgs  : Seq[FuConfig] = params.exuBlockParams.flatMap(_.fuConfigs)
80  val fuCfgsCnt     : Map[FuConfig, Int] = allDeqFuCfgs.groupBy(x => x).map { case (cfg, cfgSeq) => (cfg, cfgSeq.length) }
81  val commonFuCfgs  : Seq[FuConfig] = fuCfgsCnt.filter(_._2 > 1).keys.toSeq
82  val fuLatencyMaps : Seq[Map[FuType.OHType, Int]] = params.exuBlockParams.map(x => x.fuLatencyMap)
83
84  println(s"[IssueQueueImp] ${params.getIQName} fuLatencyMaps: ${fuLatencyMaps}")
85  println(s"[IssueQueueImp] ${params.getIQName} commonFuCfgs: ${commonFuCfgs.map(_.name)}")
86  lazy val io = IO(new IssueQueueIO())
87  dontTouch(io.deq)
88  dontTouch(io.deqResp)
89  // Modules
90
91  val entries = Module(new Entries)
92  val subDeqPolicies  = deqFuCfgs.map(x => if (x.nonEmpty) Some(Module(new DeqPolicy())) else None)
93  val fuBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.latencyValMax > 0, Module(new FuBusyTableWrite(x.fuLatencyMap))) }
94  val fuBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.latencyValMax > 0, Module(new FuBusyTableRead(x.fuLatencyMap))) }
95  val intWbBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.intLatencyCertain, Module(new FuBusyTableWrite(x.intFuLatencyMap))) }
96  val intWbBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.intLatencyCertain, Module(new FuBusyTableRead(x.intFuLatencyMap))) }
97  val vfWbBusyTableWrite = params.exuBlockParams.map { case x => OptionWrapper(x.vfLatencyCertain, Module(new FuBusyTableWrite(x.vfFuLatencyMap))) }
98  val vfWbBusyTableRead = params.exuBlockParams.map { case x => OptionWrapper(x.vfLatencyCertain, Module(new FuBusyTableRead(x.vfFuLatencyMap))) }
99
100  class WakeupQueueFlush extends Bundle {
101    val redirect = ValidIO(new Redirect)
102    val ldCancel = Vec(backendParams.LduCnt + backendParams.HyuCnt, new LoadCancelIO)
103    val og0Fail = Output(Bool())
104    val og1Fail = Output(Bool())
105  }
106
107  private def flushFunc(exuInput: ExuInput, flush: WakeupQueueFlush, stage: Int): Bool = {
108    val redirectFlush = exuInput.robIdx.needFlush(flush.redirect)
109    val loadDependencyFlush = LoadShouldCancel(exuInput.loadDependency, flush.ldCancel)
110    val ogFailFlush = stage match {
111      case 1 => flush.og0Fail
112      case 2 => flush.og1Fail
113      case _ => false.B
114    }
115    redirectFlush || loadDependencyFlush || ogFailFlush
116  }
117
118  private def modificationFunc(exuInput: ExuInput): ExuInput = {
119    val newExuInput = WireDefault(exuInput)
120    newExuInput.loadDependency match {
121      case Some(deps) => deps.zip(exuInput.loadDependency.get).foreach(x => x._1 := x._2 << 1)
122      case None =>
123    }
124    newExuInput
125  }
126
127  val wakeUpQueues: Seq[Option[MultiWakeupQueue[ExuInput, WakeupQueueFlush]]] = params.exuBlockParams.map { x => OptionWrapper(x.isIQWakeUpSource, Module(
128    new MultiWakeupQueue(new ExuInput(x), new WakeupQueueFlush, x.fuLatancySet, flushFunc, modificationFunc)
129  ))}
130
131  val intWbBusyTableIn = io.wbBusyTableRead.map(_.intWbBusyTable)
132  val vfWbBusyTableIn = io.wbBusyTableRead.map(_.vfWbBusyTable)
133  val intWbBusyTableOut = io.wbBusyTableWrite.map(_.intWbBusyTable)
134  val vfWbBusyTableOut = io.wbBusyTableWrite.map(_.vfWbBusyTable)
135  val intDeqRespSetOut = io.wbBusyTableWrite.map(_.intDeqRespSet)
136  val vfDeqRespSetOut = io.wbBusyTableWrite.map(_.vfDeqRespSet)
137  val fuBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
138  val intWbBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
139  val vfWbBusyTableMask = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
140  val s0_enqValidVec = io.enq.map(_.valid)
141  val s0_enqSelValidVec = Wire(Vec(params.numEnq, Bool()))
142  val s0_enqNotFlush = !io.flush.valid
143  val s0_enqBits = WireInit(VecInit(io.enq.map(_.bits)))
144  val s0_doEnqSelValidVec = s0_enqSelValidVec.map(_ && s0_enqNotFlush) //enqValid && notFlush && enqReady
145
146
147  // One deq port only need one special deq policy
148  val subDeqSelValidVec: Seq[Option[Vec[Bool]]] = subDeqPolicies.map(_.map(_ => Wire(Vec(params.numDeq, Bool()))))
149  val subDeqSelOHVec: Seq[Option[Vec[UInt]]] = subDeqPolicies.map(_.map(_ => Wire(Vec(params.numDeq, UInt(params.numEntries.W)))))
150
151  val finalDeqSelValidVec = Wire(Vec(params.numDeq, Bool()))
152  val finalDeqSelOHVec    = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
153  val finalDeqOH: IndexedSeq[UInt] = (finalDeqSelValidVec zip finalDeqSelOHVec).map { case (valid, oh) =>
154    Mux(valid, oh, 0.U)
155  }
156  val finalDeqMask: UInt = finalDeqOH.reduce(_ | _)
157
158  val deqRespVec = io.deqResp
159
160  val validVec = VecInit(entries.io.valid.asBools)
161  val canIssueVec = VecInit(entries.io.canIssue.asBools)
162  val clearVec = VecInit(entries.io.clear.asBools)
163  val deqFirstIssueVec = VecInit(entries.io.deq.map(_.isFirstIssue))
164
165  val dataSources: Vec[Vec[DataSource]] = entries.io.dataSources
166  val finalDataSources: Vec[Vec[DataSource]] = VecInit(finalDeqOH.map(oh => Mux1H(oh, dataSources)))
167  // (entryIdx)(srcIdx)(exuIdx)
168  val wakeUpL1ExuOH: Option[Vec[Vec[UInt]]] = entries.io.srcWakeUpL1ExuOH
169  val srcTimer: Option[Vec[Vec[UInt]]] = entries.io.srcTimer
170
171  // (deqIdx)(srcIdx)(exuIdx)
172  val finalWakeUpL1ExuOH: Option[Vec[Vec[UInt]]] = wakeUpL1ExuOH.map(x => VecInit(finalDeqOH.map(oh => Mux1H(oh, x))))
173  val finalSrcTimer = srcTimer.map(x => VecInit(finalDeqOH.map(oh => Mux1H(oh, x))))
174
175  val wakeupEnqSrcStateBypassFromWB: Vec[Vec[UInt]] = Wire(Vec(io.enq.size, Vec(io.enq.head.bits.srcType.size, SrcState())))
176  val wakeupEnqSrcStateBypassFromIQ: Vec[Vec[UInt]] = Wire(Vec(io.enq.size, Vec(io.enq.head.bits.srcType.size, SrcState())))
177  val srcWakeUpEnqByIQMatrix = Wire(Vec(params.numEnq, Vec(params.numRegSrc, Vec(params.numWakeupFromIQ, Bool()))))
178
179  val shiftedWakeupLoadDependencyByIQVec = Wire(Vec(params.numWakeupFromIQ, Vec(LoadPipelineWidth, UInt(3.W))))
180  shiftedWakeupLoadDependencyByIQVec
181    .zip(io.wakeupFromIQ.map(_.bits.loadDependency))
182    .zip(params.wakeUpInExuSources.map(_.name)).foreach {
183    case ((deps, originalDeps), name) => deps.zip(originalDeps).zipWithIndex.foreach {
184      case ((dep, originalDep), deqPortIdx) =>
185        if (name.contains("LDU") && name.replace("LDU", "").toInt == deqPortIdx)
186          dep := (originalDep << 1).asUInt | 1.U
187        else
188          dep := originalDep << 1
189    }
190  }
191
192  for (i <- io.enq.indices) {
193    for (j <- s0_enqBits(i).srcType.indices) {
194      wakeupEnqSrcStateBypassFromWB(i)(j) := Cat(
195        io.wakeupFromWB.map(x => x.bits.wakeUp(Seq((s0_enqBits(i).psrc(j), s0_enqBits(i).srcType(j))), x.valid).head).toSeq
196      ).orR
197    }
198  }
199
200  for (i <- io.enq.indices) {
201    val numLsrc = s0_enqBits(i).srcType.size.min(entries.io.enq(i).bits.status.srcType.size)
202    for (j <- s0_enqBits(i).srcType.indices) {
203      val ldTransCancel = if (params.numWakeupFromIQ > 0 && j < numLsrc) Mux(
204        srcWakeUpEnqByIQMatrix(i)(j).asUInt.orR,
205        Mux1H(srcWakeUpEnqByIQMatrix(i)(j), io.wakeupFromIQ.map(_.bits.loadDependency).map(dep => LoadShouldCancel(Some(dep), io.ldCancel)).toSeq),
206        false.B
207      ) else false.B
208      wakeupEnqSrcStateBypassFromIQ(i)(j) := Cat(
209        io.wakeupFromIQ.map(x => x.bits.wakeUp(Seq((s0_enqBits(i).psrc(j), s0_enqBits(i).srcType(j))), x.valid).head).toSeq
210      ).orR && !ldTransCancel
211    }
212  }
213
214  srcWakeUpEnqByIQMatrix.zipWithIndex.foreach { case (wakeups: Vec[Vec[Bool]], i) =>
215    if (io.wakeupFromIQ.isEmpty) {
216      wakeups := 0.U.asTypeOf(wakeups)
217    } else {
218      val wakeupVec: IndexedSeq[IndexedSeq[Bool]] = io.wakeupFromIQ.map((bundle: ValidIO[IssueQueueIQWakeUpBundle]) =>
219        bundle.bits.wakeUp(s0_enqBits(i).psrc.take(params.numRegSrc) zip s0_enqBits(i).srcType.take(params.numRegSrc), bundle.valid)
220      ).toIndexedSeq.transpose
221      wakeups := wakeupVec.map(x => VecInit(x))
222    }
223  }
224
225  val fuTypeVec = Wire(Vec(params.numEntries, FuType()))
226  val transEntryDeqVec = Wire(Vec(params.numEnq, ValidIO(new EntryBundle)))
227  val deqEntryVec = Wire(Vec(params.numDeq, ValidIO(new EntryBundle)))
228  val transSelVec = Wire(Vec(params.numEnq, UInt((params.numEntries-params.numEnq).W)))
229
230  /**
231    * Connection of [[entries]]
232    */
233  entries.io match { case entriesIO: EntriesIO =>
234    entriesIO.flush <> io.flush
235    entriesIO.wakeUpFromWB := io.wakeupFromWB
236    entriesIO.wakeUpFromIQ := io.wakeupFromIQ
237    entriesIO.og0Cancel := io.og0Cancel
238    entriesIO.og1Cancel := io.og1Cancel
239    entriesIO.ldCancel := io.ldCancel
240    entriesIO.enq.zipWithIndex.foreach { case (enq: ValidIO[EntryBundle], i) =>
241      enq.valid := s0_doEnqSelValidVec(i)
242      val numLsrc = s0_enqBits(i).srcType.size.min(enq.bits.status.srcType.size)
243      for(j <- 0 until numLsrc) {
244        enq.bits.status.srcState(j) := s0_enqBits(i).srcState(j) |
245                                       wakeupEnqSrcStateBypassFromWB(i)(j) |
246                                       wakeupEnqSrcStateBypassFromIQ(i)(j)
247        enq.bits.status.psrc(j) := s0_enqBits(i).psrc(j)
248        enq.bits.status.srcType(j) := s0_enqBits(i).srcType(j)
249        enq.bits.status.dataSources(j).value := Mux(wakeupEnqSrcStateBypassFromIQ(i)(j).asBool, DataSource.forward, s0_enqBits(i).dataSource(j).value)
250        enq.bits.payload.debugInfo.enqRsTime := GTimer()
251      }
252      enq.bits.status.fuType := s0_enqBits(i).fuType
253      enq.bits.status.robIdx := s0_enqBits(i).robIdx
254      enq.bits.status.issueTimer := "b11".U
255      enq.bits.status.deqPortIdx := 0.U
256      enq.bits.status.issued := false.B
257      enq.bits.status.firstIssue := false.B
258      enq.bits.status.blocked := false.B
259      enq.bits.status.srcWakeUpL1ExuOH match {
260        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
261          case ((exuOH, wakeUpByIQOH), srcIdx) =>
262            when(wakeUpByIQOH.asUInt.orR) {
263              exuOH := Mux1H(wakeUpByIQOH, io.wakeupFromIQ.toSeq.map(x => MathUtils.IntToOH(x.bits.exuIdx).U(backendParams.numExu.W)))
264            }.otherwise {
265              exuOH := s0_enqBits(i).l1ExuOH(srcIdx)
266            }
267        }
268        case None =>
269      }
270      enq.bits.status.srcTimer match {
271        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
272          case ((timer, wakeUpByIQOH), srcIdx) =>
273            when(wakeUpByIQOH.asUInt.orR) {
274              timer := 1.U.asTypeOf(timer)
275            }.otherwise {
276              timer := Mux(s0_enqBits(i).dataSource(srcIdx).value === DataSource.bypass, 2.U.asTypeOf(timer), 0.U.asTypeOf(timer))
277            }
278        }
279        case None =>
280      }
281      enq.bits.status.srcLoadDependency.foreach(_.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
282        case ((dep, wakeUpByIQOH), srcIdx) =>
283          dep := Mux(wakeUpByIQOH.asUInt.orR, Mux1H(wakeUpByIQOH, shiftedWakeupLoadDependencyByIQVec), 0.U.asTypeOf(dep))
284      })
285      enq.bits.imm := s0_enqBits(i).imm
286      enq.bits.payload := s0_enqBits(i)
287    }
288    entriesIO.deq.zipWithIndex.foreach { case (deq, i) =>
289      deq.deqSelOH.valid := finalDeqSelValidVec(i)
290      deq.deqSelOH.bits := finalDeqSelOHVec(i)
291    }
292    entriesIO.deqResp.zipWithIndex.foreach { case (deqResp, i) =>
293      deqResp.valid := io.deqResp(i).valid
294      deqResp.bits.robIdx := io.deqResp(i).bits.robIdx
295      deqResp.bits.dataInvalidSqIdx := io.deqResp(i).bits.dataInvalidSqIdx
296      deqResp.bits.respType := io.deqResp(i).bits.respType
297      deqResp.bits.rfWen := io.deqResp(i).bits.rfWen
298      deqResp.bits.fuType := io.deqResp(i).bits.fuType
299    }
300    entriesIO.og0Resp.zipWithIndex.foreach { case (og0Resp, i) =>
301      og0Resp.valid := io.og0Resp(i).valid
302      og0Resp.bits.robIdx := io.og0Resp(i).bits.robIdx
303      og0Resp.bits.dataInvalidSqIdx := io.og0Resp(i).bits.dataInvalidSqIdx
304      og0Resp.bits.respType := io.og0Resp(i).bits.respType
305      og0Resp.bits.rfWen := io.og0Resp(i).bits.rfWen
306      og0Resp.bits.fuType := io.og0Resp(i).bits.fuType
307    }
308    entriesIO.og1Resp.zipWithIndex.foreach { case (og1Resp, i) =>
309      og1Resp.valid := io.og1Resp(i).valid
310      og1Resp.bits.robIdx := io.og1Resp(i).bits.robIdx
311      og1Resp.bits.dataInvalidSqIdx := io.og1Resp(i).bits.dataInvalidSqIdx
312      og1Resp.bits.respType := io.og1Resp(i).bits.respType
313      og1Resp.bits.rfWen := io.og1Resp(i).bits.rfWen
314      og1Resp.bits.fuType := io.og1Resp(i).bits.fuType
315    }
316    entriesIO.finalIssueResp.foreach(_.zipWithIndex.foreach { case (finalIssueResp, i) =>
317      finalIssueResp := io.finalIssueResp.get(i)
318    })
319    entriesIO.memAddrIssueResp.foreach(_.zipWithIndex.foreach { case (memAddrIssueResp, i) =>
320      memAddrIssueResp := io.memAddrIssueResp.get(i)
321    })
322    transEntryDeqVec := entriesIO.transEntryDeqVec
323    deqEntryVec := entriesIO.deqEntry
324    fuTypeVec := entriesIO.fuType
325    transSelVec := entriesIO.transSelVec
326  }
327
328
329  s0_enqSelValidVec := s0_enqValidVec.zip(io.enq).map{ case (enqValid, enq) => enqValid && enq.ready}
330
331  protected val commonAccept: UInt = Cat(fuTypeVec.map(fuType =>
332    Cat(commonFuCfgs.map(_.fuType.U === fuType)).orR
333  ).reverse)
334
335  // if deq port can accept the uop
336  protected val canAcceptVec: Seq[UInt] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
337    Cat(fuTypeVec.map(fuType => Cat(fuCfgs.map(_.fuType.U === fuType)).orR).reverse).asUInt
338  }
339
340  protected val deqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
341    fuTypeVec.map(fuType =>
342      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR) // C+E0    C+E1
343  }
344
345  subDeqPolicies.zipWithIndex.foreach { case (dpOption: Option[DeqPolicy], i) =>
346    if (dpOption.nonEmpty) {
347      val dp = dpOption.get
348      dp.io.request             := canIssueVec.asUInt & VecInit(deqCanAcceptVec(i)).asUInt & (~fuBusyTableMask(i)).asUInt & (~intWbBusyTableMask(i)).asUInt & (~vfWbBusyTableMask(i)).asUInt
349      subDeqSelValidVec(i).get  := dp.io.deqSelOHVec.map(oh => oh.valid)
350      subDeqSelOHVec(i).get     := dp.io.deqSelOHVec.map(oh => oh.bits)
351    }
352  }
353
354  protected val enqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
355    io.enq.map(_.bits.fuType).map(fuType =>
356      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR) // C+E0    C+E1
357  }
358
359  protected val transCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
360    transEntryDeqVec.map(_.bits.status.fuType).zip(transEntryDeqVec.map(_.valid)).map{ case (fuType, valid) =>
361      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR && valid }
362  }
363
364  val enqEntryOldest = (0 until params.numDeq).map {
365    case deqIdx =>
366      NewAgeDetector(numEntries = params.numEnq,
367        enq = VecInit(enqCanAcceptVec(deqIdx).zip(s0_doEnqSelValidVec).map{ case (doCanAccept, valid) => doCanAccept && valid }),
368        clear = VecInit(clearVec.take(params.numEnq)),
369        canIssue = VecInit(canIssueVec.take(params.numEnq)).asUInt & ((~fuBusyTableMask(deqIdx)).asUInt & (~intWbBusyTableMask(deqIdx)).asUInt & (~vfWbBusyTableMask(deqIdx)).asUInt)(params.numEnq-1, 0)
370      )
371  }
372
373  val othersEntryOldest = (0 until params.numDeq).map {
374    case deqIdx =>
375      AgeDetector(numEntries = params.numEntries - params.numEnq,
376        enq = VecInit(transCanAcceptVec(deqIdx).zip(transSelVec).map{ case(doCanAccept, transSel) => Mux(doCanAccept, transSel, 0.U)}),
377        deq = VecInit(clearVec.drop(params.numEnq)).asUInt,
378        canIssue = VecInit(canIssueVec.drop(params.numEnq)).asUInt & ((~fuBusyTableMask(deqIdx)).asUInt & (~intWbBusyTableMask(deqIdx)).asUInt & (~vfWbBusyTableMask(deqIdx)).asUInt)(params.numEntries-1, params.numEnq)
379      )
380  }
381
382  finalDeqSelValidVec.head := othersEntryOldest.head.valid || enqEntryOldest.head.valid || subDeqSelValidVec.head.getOrElse(Seq(false.B)).head
383  finalDeqSelOHVec.head := Mux(othersEntryOldest.head.valid, Cat(othersEntryOldest.head.bits, 0.U((params.numEnq).W)),
384                            Mux(enqEntryOldest.head.valid, Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldest.head.bits),
385                              subDeqSelOHVec.head.getOrElse(Seq(0.U)).head))
386
387  if (params.numDeq == 2) {
388    val chooseOthersOldest = othersEntryOldest(1).valid && Cat(othersEntryOldest(1).bits, 0.U((params.numEnq).W)) =/= finalDeqSelOHVec.head
389    val chooseEnqOldest = enqEntryOldest(1).valid && Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldest(1).bits) =/= finalDeqSelOHVec.head
390    val choose1stSub = subDeqSelOHVec(1).getOrElse(Seq(0.U)).head =/= finalDeqSelOHVec.head
391
392    finalDeqSelValidVec(1) := MuxCase(subDeqSelValidVec(1).getOrElse(Seq(false.B)).last, Seq(
393      (chooseOthersOldest) -> othersEntryOldest(1).valid,
394      (chooseEnqOldest) -> enqEntryOldest(1).valid,
395      (choose1stSub) -> subDeqSelValidVec(1).getOrElse(Seq(false.B)).head)
396    )
397    finalDeqSelOHVec(1) := MuxCase(subDeqSelOHVec(1).getOrElse(Seq(0.U)).last, Seq(
398      (chooseOthersOldest) -> Cat(othersEntryOldest(1).bits, 0.U((params.numEnq).W)),
399      (chooseEnqOldest) -> Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldest(1).bits),
400      (choose1stSub) -> subDeqSelOHVec(1).getOrElse(Seq(0.U)).head)
401    )
402  }
403
404  //fuBusyTable
405  fuBusyTableWrite.zip(fuBusyTableRead).zipWithIndex.foreach { case ((busyTableWrite: Option[FuBusyTableWrite], busyTableRead: Option[FuBusyTableRead]), i) =>
406    if(busyTableWrite.nonEmpty) {
407      val btwr = busyTableWrite.get
408      val btrd = busyTableRead.get
409      btwr.io.in.deqResp := io.deqResp(i)
410      btwr.io.in.og0Resp := io.og0Resp(i)
411      btwr.io.in.og1Resp := io.og1Resp(i)
412      btrd.io.in.fuBusyTable := btwr.io.out.fuBusyTable
413      btrd.io.in.fuTypeRegVec := fuTypeVec
414      fuBusyTableMask(i) := btrd.io.out.fuBusyTableMask
415    }
416    else {
417      fuBusyTableMask(i) := 0.U(params.numEntries.W)
418    }
419  }
420
421  //wbfuBusyTable write
422  intWbBusyTableWrite.zip(intWbBusyTableOut).zip(intDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
423    if(busyTableWrite.nonEmpty) {
424      val btwr = busyTableWrite.get
425      val bt = busyTable.get
426      val dq = deqResp.get
427      btwr.io.in.deqResp := io.deqResp(i)
428      btwr.io.in.og0Resp := io.og0Resp(i)
429      btwr.io.in.og1Resp := io.og1Resp(i)
430      bt := btwr.io.out.fuBusyTable
431      dq := btwr.io.out.deqRespSet
432    }
433  }
434
435  vfWbBusyTableWrite.zip(vfWbBusyTableOut).zip(vfDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
436    if (busyTableWrite.nonEmpty) {
437      val btwr = busyTableWrite.get
438      val bt = busyTable.get
439      val dq = deqResp.get
440      btwr.io.in.deqResp := io.deqResp(i)
441      btwr.io.in.og0Resp := io.og0Resp(i)
442      btwr.io.in.og1Resp := io.og1Resp(i)
443      bt := btwr.io.out.fuBusyTable
444      dq := btwr.io.out.deqRespSet
445    }
446  }
447
448  //wbfuBusyTable read
449  intWbBusyTableRead.zip(intWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
450    if(busyTableRead.nonEmpty) {
451      val btrd = busyTableRead.get
452      val bt = busyTable.get
453      btrd.io.in.fuBusyTable := bt
454      btrd.io.in.fuTypeRegVec := fuTypeVec
455      intWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
456    }
457    else {
458      intWbBusyTableMask(i) := 0.U(params.numEntries.W)
459    }
460  }
461  vfWbBusyTableRead.zip(vfWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
462    if (busyTableRead.nonEmpty) {
463      val btrd = busyTableRead.get
464      val bt = busyTable.get
465      btrd.io.in.fuBusyTable := bt
466      btrd.io.in.fuTypeRegVec := fuTypeVec
467      vfWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
468    }
469    else {
470      vfWbBusyTableMask(i) := 0.U(params.numEntries.W)
471    }
472  }
473
474  wakeUpQueues.zipWithIndex.foreach { case (wakeUpQueueOption, i) =>
475    val og0RespEach = io.og0Resp(i)
476    val og1RespEach = io.og1Resp(i)
477    wakeUpQueueOption.foreach {
478      wakeUpQueue =>
479        val flush = Wire(new WakeupQueueFlush)
480        flush.redirect := io.flush
481        flush.ldCancel := io.ldCancel
482        flush.og0Fail := io.og0Resp(i).valid && RSFeedbackType.isBlocked(io.og0Resp(i).bits.respType)
483        flush.og1Fail := io.og1Resp(i).valid && RSFeedbackType.isBlocked(io.og1Resp(i).bits.respType)
484        wakeUpQueue.io.flush := flush
485        wakeUpQueue.io.enq.valid := io.deq(i).fire && !io.deq(i).bits.common.needCancel(io.og0Cancel, io.og1Cancel) && {
486          io.deq(i).bits.common.rfWen.getOrElse(false.B) && io.deq(i).bits.common.pdest =/= 0.U ||
487          io.deq(i).bits.common.fpWen.getOrElse(false.B) ||
488          io.deq(i).bits.common.vecWen.getOrElse(false.B)
489        }
490        wakeUpQueue.io.enq.bits.uop := io.deq(i).bits.common
491        wakeUpQueue.io.enq.bits.lat := getDeqLat(i, io.deq(i).bits.common.fuType)
492        wakeUpQueue.io.og0IssueFail := flush.og0Fail
493        wakeUpQueue.io.og1IssueFail := flush.og1Fail
494    }
495  }
496
497  io.deq.zipWithIndex.foreach { case (deq, i) =>
498    deq.valid                := finalDeqSelValidVec(i)
499    deq.bits.addrOH          := finalDeqSelOHVec(i)
500    deq.bits.common.isFirstIssue := deqFirstIssueVec(i)
501    deq.bits.common.iqIdx    := OHToUInt(finalDeqSelOHVec(i))
502    deq.bits.common.fuType   := deqEntryVec(i).bits.payload.fuType
503    deq.bits.common.fuOpType := deqEntryVec(i).bits.payload.fuOpType
504    deq.bits.common.rfWen.foreach(_ := deqEntryVec(i).bits.payload.rfWen)
505    deq.bits.common.fpWen.foreach(_ := deqEntryVec(i).bits.payload.fpWen)
506    deq.bits.common.vecWen.foreach(_ := deqEntryVec(i).bits.payload.vecWen)
507    deq.bits.common.flushPipe.foreach(_ := deqEntryVec(i).bits.payload.flushPipe)
508    deq.bits.common.pdest := deqEntryVec(i).bits.payload.pdest
509    deq.bits.common.robIdx := deqEntryVec(i).bits.payload.robIdx
510    deq.bits.common.imm := deqEntryVec(i).bits.imm
511    deq.bits.common.dataSources.zip(finalDataSources(i)).zipWithIndex.foreach {
512      case ((sink, source), srcIdx) =>
513        sink.value := Mux(
514          SrcType.isXp(deqEntryVec(i).bits.payload.srcType(srcIdx)) && deqEntryVec(i).bits.payload.psrc(srcIdx) === 0.U,
515          DataSource.none,
516          source.value
517        )
518    }
519    if (deq.bits.common.l1ExuOH.size > 0) {
520      if (params.hasIQWakeUp) {
521        deq.bits.common.l1ExuOH := finalWakeUpL1ExuOH.get(i)
522      } else {
523        deq.bits.common.l1ExuOH := deqEntryVec(i).bits.payload.l1ExuOH.take(deq.bits.common.l1ExuOH.length)
524      }
525    }
526    deq.bits.common.srcTimer.foreach(_ := finalSrcTimer.get(i))
527    deq.bits.common.loadDependency.foreach(_ := deqEntryVec(i).bits.status.mergedLoadDependency.get)
528    deq.bits.common.deqPortIdx.foreach(_ := i.U)
529    deq.bits.common.src := DontCare
530
531    deq.bits.rf.zip(deqEntryVec(i).bits.payload.psrc).foreach { case (rf, psrc) =>
532      rf.foreach(_.addr := psrc) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
533    }
534    deq.bits.rf.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (rf, srcType) =>
535      rf.foreach(_.srcType := srcType) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
536    }
537    deq.bits.srcType.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (sink, source) =>
538      sink := source
539    }
540    deq.bits.immType := deqEntryVec(i).bits.payload.selImm
541
542    // dirty code for lui+addi(w) fusion
543    when (deqEntryVec(i).bits.payload.isLUI32) {
544      val lui_imm = Cat(deqEntryVec(i).bits.payload.lsrc(1), deqEntryVec(i).bits.payload.lsrc(0), deqEntryVec(i).bits.imm(ImmUnion.maxLen - 1, 0))
545      deq.bits.common.imm := ImmUnion.LUI32.toImm32(lui_imm)
546    }
547
548    // dirty code for fused_lui_load
549    when (SrcType.isImm(deqEntryVec(i).bits.payload.srcType(0)) && deqEntryVec(i).bits.payload.fuType === FuType.ldu.U) {
550      deq.bits.common.imm := Imm_LUI_LOAD().getLuiImm(deqEntryVec(i).bits.payload)
551    }
552
553    deq.bits.common.perfDebugInfo := deqEntryVec(i).bits.payload.debugInfo
554    deq.bits.common.perfDebugInfo.selectTime := GTimer()
555    deq.bits.common.perfDebugInfo.issueTime := GTimer() + 1.U
556  }
557
558  private val ldCancels = io.fromCancelNetwork.map(in =>
559    LoadShouldCancel(in.bits.common.loadDependency, io.ldCancel)
560  )
561  private val fromCancelNetworkShift = WireDefault(io.fromCancelNetwork)
562  fromCancelNetworkShift.zip(io.fromCancelNetwork).foreach {
563    case (shifted, original) =>
564      original.ready := shifted.ready // this will not cause combinational loop
565      shifted.bits.common.loadDependency.foreach(
566        _ := original.bits.common.loadDependency.get.map(_ << 1)
567      )
568  }
569  io.deqDelay.zip(fromCancelNetworkShift).zip(ldCancels).foreach { case ((deqDly, deq), ldCancel) =>
570    NewPipelineConnect(
571      deq, deqDly, deqDly.valid,
572      deq.bits.common.robIdx.needFlush(io.flush) || ldCancel,
573      Option("Scheduler2DataPathPipe")
574    )
575  }
576  dontTouch(io.deqDelay)
577  io.wakeupToIQ.zipWithIndex.foreach { case (wakeup, i) =>
578    if (wakeUpQueues(i).nonEmpty && finalWakeUpL1ExuOH.nonEmpty) {
579      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
580      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits, finalWakeUpL1ExuOH.get(i))
581      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
582    } else if (wakeUpQueues(i).nonEmpty) {
583      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
584      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits)
585      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
586    } else {
587      wakeup.valid := false.B
588      wakeup.bits := 0.U.asTypeOf(wakeup.bits)
589    }
590  }
591
592  // Todo: better counter implementation
593  private val enqHasValid = validVec.take(params.numEnq).reduce(_ | _)
594  private val enqEntryValidCnt = PopCount(validVec.take(params.numEnq))
595  private val othersValidCnt = PopCount(validVec.drop(params.numEnq))
596  io.status.leftVec(0) := validVec.drop(params.numEnq).reduce(_ & _)
597  for (i <- 0 until params.numEnq) {
598    io.status.leftVec(i + 1) := othersValidCnt === (params.numEntries - params.numEnq - (i + 1)).U
599  }
600  io.enq.foreach(_.ready := !Cat(io.status.leftVec).orR || !enqHasValid) // Todo: more efficient implementation
601  io.status.empty := !Cat(validVec).orR
602  io.status.full := Cat(io.status.leftVec).orR
603
604  protected def getDeqLat(deqPortIdx: Int, fuType: UInt) : UInt = {
605    Mux1H(fuLatencyMaps(deqPortIdx) map { case (k, v) => (k.U === fuType, v.U) })
606  }
607
608  // issue perf counter
609  // enq count
610  XSPerfAccumulate("enq_valid_cnt", PopCount(io.enq.map(_.fire)))
611  XSPerfAccumulate("enq_fire_cnt", PopCount(io.enq.map(_.fire)))
612  // valid count
613  XSPerfHistogram("enq_entry_valid_cnt", enqEntryValidCnt, true.B, 0, params.numEnq + 1)
614  XSPerfHistogram("other_entry_valid_cnt", othersValidCnt, true.B, 0, params.numEntries - params.numEnq + 1)
615  XSPerfHistogram("valid_cnt", PopCount(validVec), true.B, 0, params.numEntries + 1)
616  // ready instr count
617  private val readyEntriesCnt = PopCount(validVec.zip(canIssueVec).map(x => x._1 && x._2))
618  XSPerfHistogram("ready_cnt", readyEntriesCnt, true.B, 0, params.numEntries + 1)
619  // only split when more than 1 func type
620  if (params.getFuCfgs.size > 0) {
621    for (t <- FuType.functionNameMap.keys) {
622      val fuName = FuType.functionNameMap(t)
623      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
624        XSPerfHistogram(s"ready_cnt_hist_futype_${fuName}", PopCount(validVec.zip(canIssueVec).zip(fuTypeVec).map { case ((v, c), fu) => v && c && fu === t.U }), true.B, 0, params.numEntries, 1)
625      }
626    }
627  }
628
629  // deq instr count
630  XSPerfAccumulate("issue_instr_pre_count", PopCount(io.deq.map(_.valid)))
631  XSPerfHistogram("issue_instr_pre_count_hist", PopCount(io.deq.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
632  XSPerfAccumulate("issue_instr_count", PopCount(io.deqDelay.map(_.valid)))
633  XSPerfHistogram("issue_instr_count_hist", PopCount(io.deqDelay.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
634
635  // deq instr data source count
636  XSPerfAccumulate("issue_datasource_reg", io.deq.map{ deq =>
637    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) })
638  }.reduce(_ +& _))
639  XSPerfAccumulate("issue_datasource_bypass", io.deq.map{ deq =>
640    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) })
641  }.reduce(_ +& _))
642  XSPerfAccumulate("issue_datasource_forward", io.deq.map{ deq =>
643    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) })
644  }.reduce(_ +& _))
645  XSPerfAccumulate("issue_datasource_noreg", io.deq.map{ deq =>
646    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
647  }.reduce(_ +& _))
648
649  XSPerfHistogram("issue_datasource_reg_hist", io.deq.map{ deq =>
650    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) })
651  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
652  XSPerfHistogram("issue_datasource_bypass_hist", io.deq.map{ deq =>
653    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) })
654  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
655  XSPerfHistogram("issue_datasource_forward_hist", io.deq.map{ deq =>
656    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) })
657  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
658  XSPerfHistogram("issue_datasource_noreg_hist", io.deq.map{ deq =>
659    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
660  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
661
662  // deq instr data source count for each futype
663  for (t <- FuType.functionNameMap.keys) {
664    val fuName = FuType.functionNameMap(t)
665    if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
666      XSPerfAccumulate(s"issue_datasource_reg_futype_${fuName}", io.deq.map{ deq =>
667        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
668      }.reduce(_ +& _))
669      XSPerfAccumulate(s"issue_datasource_bypass_futype_${fuName}", io.deq.map{ deq =>
670        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
671      }.reduce(_ +& _))
672      XSPerfAccumulate(s"issue_datasource_forward_futype_${fuName}", io.deq.map{ deq =>
673        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
674      }.reduce(_ +& _))
675      XSPerfAccumulate(s"issue_datasource_noreg_futype_${fuName}", io.deq.map{ deq =>
676        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
677      }.reduce(_ +& _))
678
679      XSPerfHistogram(s"issue_datasource_reg_hist_futype_${fuName}", io.deq.map{ deq =>
680        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.reg && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
681      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
682      XSPerfHistogram(s"issue_datasource_bypass_hist_futype_${fuName}", io.deq.map{ deq =>
683        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.bypass && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
684      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
685      XSPerfHistogram(s"issue_datasource_forward_hist_futype_${fuName}", io.deq.map{ deq =>
686        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && ds.value === DataSource.forward && !SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
687      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
688      XSPerfHistogram(s"issue_datasource_noreg_hist_futype_${fuName}", io.deq.map{ deq =>
689        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
690      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
691    }
692  }
693
694  // cancel instr count
695  if (params.hasIQWakeUp) {
696    val cancelVec: Vec[Bool] = entries.io.cancel.get
697    XSPerfAccumulate("cancel_instr_count", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)))
698    XSPerfHistogram("cancel_instr_hist", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)), true.B, 0, params.numEntries, 1)
699    for (t <- FuType.functionNameMap.keys) {
700      val fuName = FuType.functionNameMap(t)
701      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
702        XSPerfAccumulate(s"cancel_instr_count_futype_${fuName}", PopCount(validVec.zip(cancelVec).zip(fuTypeVec).map{ case ((x, y), fu) => x & y & fu === t.U }))
703        XSPerfHistogram(s"cancel_instr_hist_futype_${fuName}", PopCount(validVec.zip(cancelVec).zip(fuTypeVec).map{ case ((x, y), fu) => x & y & fu === t.U }), true.B, 0, params.numEntries, 1)
704      }
705    }
706  }
707}
708
709class IssueQueueJumpBundle extends Bundle {
710  val pc = UInt(VAddrData().dataWidth.W)
711}
712
713class IssueQueueLoadBundle(implicit p: Parameters) extends XSBundle {
714  val fastMatch = UInt(backendParams.LduCnt.W)
715  val fastImm = UInt(12.W)
716}
717
718class IssueQueueIntIO()(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO
719
720class IssueQueueIntImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
721  extends IssueQueueImp(wrapper)
722{
723  io.suggestName("none")
724  override lazy val io = IO(new IssueQueueIntIO).suggestName("io")
725
726  if(params.needPc) {
727    entries.io.enq.zipWithIndex.foreach { case (entriesEnq, i) =>
728      entriesEnq.bits.status.pc.foreach(_ := io.enq(i).bits.pc)
729    }
730  }
731
732  io.deq.zipWithIndex.foreach{ case (deq, i) => {
733    deq.bits.common.pc.foreach(_ := deqEntryVec(i).bits.status.pc.get)
734    deq.bits.common.preDecode.foreach(_ := deqEntryVec(i).bits.payload.preDecodeInfo)
735    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
736    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
737    deq.bits.common.predictInfo.foreach(x => {
738      x.target := DontCare
739      x.taken := deqEntryVec(i).bits.payload.pred_taken
740    })
741    // for std
742    deq.bits.common.sqIdx.foreach(_ := deqEntryVec(i).bits.payload.sqIdx)
743    // for i2f
744    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
745  }}
746}
747
748class IssueQueueVfImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
749  extends IssueQueueImp(wrapper)
750{
751  s0_enqBits.foreach{ x =>
752    x.srcType(3) := SrcType.vp // v0: mask src
753    x.srcType(4) := SrcType.vp // vl&vtype
754  }
755  io.deq.zipWithIndex.foreach{ case (deq, i) => {
756    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
757    deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
758    deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
759  }}
760}
761
762class IssueQueueMemBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
763  val feedbackIO = Flipped(Vec(params.numDeq, new MemRSFeedbackIO))
764  val checkWait = new Bundle {
765    val stIssuePtr = Input(new SqPtr)
766    val memWaitUpdateReq = Flipped(new MemWaitUpdateReq)
767  }
768  val loadFastMatch = Output(Vec(params.LduCnt, new IssueQueueLoadBundle))
769}
770
771class IssueQueueMemIO(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO {
772  val memIO = Some(new IssueQueueMemBundle)
773}
774
775class IssueQueueMemAddrImp(override val wrapper: IssueQueue)(implicit p: Parameters, params: IssueBlockParams)
776  extends IssueQueueImp(wrapper) with HasCircularQueuePtrHelper {
777
778  require(params.StdCnt == 0 && (params.LduCnt + params.StaCnt + params.HyuCnt + params.VlduCnt) > 0, "IssueQueueMemAddrImp can only be instance of MemAddr IQ, " +
779    s"StdCnt: ${params.StdCnt}, LduCnt: ${params.LduCnt}, StaCnt: ${params.StaCnt}, HyuCnt: ${params.HyuCnt}")
780
781  io.suggestName("none")
782  override lazy val io = IO(new IssueQueueMemIO).suggestName("io")
783  private val memIO = io.memIO.get
784
785  memIO.loadFastMatch := 0.U.asTypeOf(memIO.loadFastMatch) // TODO: is still needed?
786
787  for (i <- io.enq.indices) {
788    s0_enqBits(i).loadWaitBit := false.B
789  }
790
791  for (i <- entries.io.enq.indices) {
792    entries.io.enq(i).bits.status match { case enqData =>
793      enqData.blocked := false.B // s0_enqBits(i).loadWaitBit
794      enqData.mem.get.strictWait := s0_enqBits(i).loadWaitStrict
795      enqData.mem.get.waitForStd := false.B
796      enqData.mem.get.waitForRobIdx := s0_enqBits(i).waitForRobIdx
797      enqData.mem.get.waitForSqIdx := 0.U.asTypeOf(enqData.mem.get.waitForSqIdx) // generated by sq, will be updated later
798      enqData.mem.get.sqIdx := s0_enqBits(i).sqIdx
799    }
800
801    entries.io.fromMem.get.slowResp.zipWithIndex.foreach { case (slowResp, i) =>
802      slowResp.valid                 := memIO.feedbackIO(i).feedbackSlow.valid
803      slowResp.bits.robIdx           := memIO.feedbackIO(i).feedbackSlow.bits.robIdx
804      slowResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackSlow.bits.hit, RSFeedbackType.fuIdle, RSFeedbackType.feedbackInvalid)
805      slowResp.bits.dataInvalidSqIdx := memIO.feedbackIO(i).feedbackSlow.bits.dataInvalidSqIdx
806      slowResp.bits.rfWen := DontCare
807      slowResp.bits.fuType := DontCare
808    }
809
810    entries.io.fromMem.get.fastResp.zipWithIndex.foreach { case (fastResp, i) =>
811      fastResp.valid                 := memIO.feedbackIO(i).feedbackFast.valid
812      fastResp.bits.robIdx           := memIO.feedbackIO(i).feedbackFast.bits.robIdx
813      fastResp.bits.respType         := memIO.feedbackIO(i).feedbackFast.bits.sourceType
814      fastResp.bits.dataInvalidSqIdx := 0.U.asTypeOf(fastResp.bits.dataInvalidSqIdx)
815      fastResp.bits.rfWen := DontCare
816      fastResp.bits.fuType := DontCare
817    }
818
819    entries.io.fromMem.get.memWaitUpdateReq := memIO.checkWait.memWaitUpdateReq
820    entries.io.fromMem.get.stIssuePtr := memIO.checkWait.stIssuePtr
821  }
822
823  io.deq.zipWithIndex.foreach { case (deq, i) =>
824    deq.bits.common.sqIdx.get := deqEntryVec(i).bits.payload.sqIdx
825    deq.bits.common.lqIdx.get := deqEntryVec(i).bits.payload.lqIdx
826    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
827    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
828  }
829}