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