xref: /XiangShan/src/main/scala/xiangshan/backend/issue/IssueQueue.scala (revision 402837879bf6bd3f78695deeea5e29429b72c1d0)
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  val enqEntryOldestSel = Wire(Vec(params.numDeq, ValidIO(UInt(params.numEnq.W))))
235  val othersEntryOldestSel = Wire(Vec(params.numDeq, ValidIO(UInt((params.numEntries - params.numEnq).W))))
236  val subDeqPolicyRequest = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
237  val canIssueMergeAllBusy = Wire(Vec(params.numDeq, UInt(params.numEntries.W)))
238
239  /**
240    * Connection of [[entries]]
241    */
242  entries.io match { case entriesIO: EntriesIO =>
243    entriesIO.flush <> io.flush
244    entriesIO.wakeUpFromWB := io.wakeupFromWB
245    entriesIO.wakeUpFromIQ := io.wakeupFromIQ
246    entriesIO.og0Cancel := io.og0Cancel
247    entriesIO.og1Cancel := io.og1Cancel
248    entriesIO.ldCancel := io.ldCancel
249    entriesIO.enq.zipWithIndex.foreach { case (enq: ValidIO[EntryBundle], i) =>
250      enq.valid := s0_doEnqSelValidVec(i)
251      val numLsrc = s0_enqBits(i).srcType.size.min(enq.bits.status.srcType.size)
252      for(j <- 0 until numLsrc) {
253        enq.bits.status.srcState(j) := s0_enqBits(i).srcState(j) |
254                                       wakeupEnqSrcStateBypassFromWB(i)(j) |
255                                       wakeupEnqSrcStateBypassFromIQ(i)(j)
256        enq.bits.status.psrc(j) := s0_enqBits(i).psrc(j)
257        enq.bits.status.srcType(j) := s0_enqBits(i).srcType(j)
258        enq.bits.status.dataSources(j).value := Mux(wakeupEnqSrcStateBypassFromIQ(i)(j).asBool, DataSource.forward, s0_enqBits(i).dataSource(j).value)
259        enq.bits.payload.debugInfo.enqRsTime := GTimer()
260      }
261      enq.bits.status.fuType := s0_enqBits(i).fuType
262      enq.bits.status.robIdx := s0_enqBits(i).robIdx
263      enq.bits.status.uopIdx.foreach(_ := s0_enqBits(i).uopIdx)
264      enq.bits.status.issueTimer := "b11".U
265      enq.bits.status.deqPortIdx := 0.U
266      enq.bits.status.issued := false.B
267      enq.bits.status.firstIssue := false.B
268      enq.bits.status.blocked := false.B
269      enq.bits.status.srcWakeUpL1ExuOH match {
270        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
271          case ((exuOH, wakeUpByIQOH), srcIdx) =>
272            when(wakeUpByIQOH.asUInt.orR) {
273              exuOH := Mux1H(wakeUpByIQOH, io.wakeupFromIQ.toSeq.map(x => MathUtils.IntToOH(x.bits.exuIdx).U(backendParams.numExu.W)))
274            }.otherwise {
275              exuOH := s0_enqBits(i).l1ExuOH(srcIdx)
276            }
277        }
278        case None =>
279      }
280      enq.bits.status.srcTimer match {
281        case Some(value) => value.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
282          case ((timer, wakeUpByIQOH), srcIdx) =>
283            when(wakeUpByIQOH.asUInt.orR) {
284              timer := 1.U.asTypeOf(timer)
285            }.otherwise {
286              timer := Mux(s0_enqBits(i).dataSource(srcIdx).value === DataSource.bypass, 2.U.asTypeOf(timer), 0.U.asTypeOf(timer))
287            }
288        }
289        case None =>
290      }
291      enq.bits.status.srcLoadDependency.foreach(_.zip(srcWakeUpEnqByIQMatrix(i)).zipWithIndex.foreach {
292        case ((dep, wakeUpByIQOH), srcIdx) =>
293          dep := Mux(wakeUpByIQOH.asUInt.orR, Mux1H(wakeUpByIQOH, shiftedWakeupLoadDependencyByIQVec), 0.U.asTypeOf(dep))
294      })
295      enq.bits.imm := s0_enqBits(i).imm
296      enq.bits.payload := s0_enqBits(i)
297    }
298    entriesIO.deq.zipWithIndex.foreach { case (deq, i) =>
299      deq.enqEntryOldestSel := enqEntryOldestSel(i)
300      deq.othersEntryOldestSel := othersEntryOldestSel(i)
301      deq.subDeqPolicyRequest := { if (subDeqPolicies(i).nonEmpty) subDeqPolicyRequest(i) else 0.U }
302      deq.subDeqSelOH := subDeqSelOHVec(i).getOrElse(Seq(0.U, 0.U))
303      deq.finalDeqSelOH.valid := finalDeqSelValidVec(i)
304      deq.finalDeqSelOH.bits := finalDeqSelOHVec(i)
305    }
306    entriesIO.deqResp.zipWithIndex.foreach { case (deqResp, i) =>
307      deqResp.valid := io.deqResp(i).valid
308      deqResp.bits.robIdx := io.deqResp(i).bits.robIdx
309      deqResp.bits.uopIdx := io.deqResp(i).bits.uopIdx
310      deqResp.bits.dataInvalidSqIdx := io.deqResp(i).bits.dataInvalidSqIdx
311      deqResp.bits.respType := io.deqResp(i).bits.respType
312      deqResp.bits.rfWen := io.deqResp(i).bits.rfWen
313      deqResp.bits.fuType := io.deqResp(i).bits.fuType
314    }
315    entriesIO.og0Resp.zipWithIndex.foreach { case (og0Resp, i) =>
316      og0Resp.valid := io.og0Resp(i).valid
317      og0Resp.bits.robIdx := io.og0Resp(i).bits.robIdx
318      og0Resp.bits.uopIdx := io.og0Resp(i).bits.uopIdx
319      og0Resp.bits.dataInvalidSqIdx := io.og0Resp(i).bits.dataInvalidSqIdx
320      og0Resp.bits.respType := io.og0Resp(i).bits.respType
321      og0Resp.bits.rfWen := io.og0Resp(i).bits.rfWen
322      og0Resp.bits.fuType := io.og0Resp(i).bits.fuType
323    }
324    entriesIO.og1Resp.zipWithIndex.foreach { case (og1Resp, i) =>
325      og1Resp.valid := io.og1Resp(i).valid
326      og1Resp.bits.robIdx := io.og1Resp(i).bits.robIdx
327      og1Resp.bits.uopIdx := io.og1Resp(i).bits.uopIdx
328      og1Resp.bits.dataInvalidSqIdx := io.og1Resp(i).bits.dataInvalidSqIdx
329      og1Resp.bits.respType := io.og1Resp(i).bits.respType
330      og1Resp.bits.rfWen := io.og1Resp(i).bits.rfWen
331      og1Resp.bits.fuType := io.og1Resp(i).bits.fuType
332    }
333    entriesIO.finalIssueResp.foreach(_.zipWithIndex.foreach { case (finalIssueResp, i) =>
334      finalIssueResp := io.finalIssueResp.get(i)
335    })
336    entriesIO.memAddrIssueResp.foreach(_.zipWithIndex.foreach { case (memAddrIssueResp, i) =>
337      memAddrIssueResp := io.memAddrIssueResp.get(i)
338    })
339    transEntryDeqVec := entriesIO.transEntryDeqVec
340    deqEntryVec := entriesIO.deq.map(_.deqEntry)
341    fuTypeVec := entriesIO.fuType
342    transSelVec := entriesIO.transSelVec
343  }
344
345
346  s0_enqSelValidVec := s0_enqValidVec.zip(io.enq).map{ case (enqValid, enq) => enqValid && enq.ready}
347
348  protected val commonAccept: UInt = Cat(fuTypeVec.map(fuType =>
349    Cat(commonFuCfgs.map(_.fuType.U === fuType)).orR
350  ).reverse)
351
352  // if deq port can accept the uop
353  protected val canAcceptVec: Seq[UInt] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
354    Cat(fuTypeVec.map(fuType => Cat(fuCfgs.map(_.fuType.U === fuType)).orR).reverse).asUInt
355  }
356
357  protected val deqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
358    fuTypeVec.map(fuType =>
359      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR) // C+E0    C+E1
360  }
361
362  canIssueMergeAllBusy.zipWithIndex.foreach { case (merge, i) =>
363    val mergeFuBusy = {
364      if (fuBusyTableWrite(i).nonEmpty) canIssueVec.asUInt & (~fuBusyTableMask(i))
365      else canIssueVec.asUInt
366    }
367    val mergeIntWbBusy = {
368      if (intWbBusyTableRead(i).nonEmpty) mergeFuBusy & (~intWbBusyTableMask(i))
369      else mergeFuBusy
370    }
371    val mergeVfWbBusy = {
372      if (vfWbBusyTableRead(i).nonEmpty) mergeIntWbBusy & (~vfWbBusyTableMask(i))
373      else mergeIntWbBusy
374    }
375    merge := mergeVfWbBusy
376  }
377
378  subDeqPolicyRequest.zipWithIndex.foreach { case (req, i) =>
379    req := canIssueMergeAllBusy(i) & VecInit(deqCanAcceptVec(i)).asUInt
380  }
381
382  subDeqPolicies.zipWithIndex.foreach { case (dpOption: Option[DeqPolicy], i) =>
383    if (dpOption.nonEmpty) {
384      val dp = dpOption.get
385      dp.io.request             := subDeqPolicyRequest(i)
386      subDeqSelValidVec(i).get  := dp.io.deqSelOHVec.map(oh => oh.valid)
387      subDeqSelOHVec(i).get     := dp.io.deqSelOHVec.map(oh => oh.bits)
388    }
389  }
390
391  protected val enqCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
392    io.enq.map(_.bits.fuType).map(fuType =>
393      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR) // C+E0    C+E1
394  }
395
396  protected val transCanAcceptVec: Seq[IndexedSeq[Bool]] = deqFuCfgs.map { fuCfgs: Seq[FuConfig] =>
397    transEntryDeqVec.map(_.bits.status.fuType).zip(transEntryDeqVec.map(_.valid)).map{ case (fuType, valid) =>
398      Cat(fuCfgs.map(_.fuType.U === fuType)).asUInt.orR && valid }
399  }
400
401  enqEntryOldestSel.zipWithIndex.foreach { case (sel, deqIdx) =>
402    sel := NewAgeDetector(numEntries = params.numEnq,
403        enq = VecInit(enqCanAcceptVec(deqIdx).zip(s0_doEnqSelValidVec).map{ case (doCanAccept, valid) => doCanAccept && valid }),
404        clear = VecInit(clearVec.take(params.numEnq)),
405        canIssue = canIssueMergeAllBusy(deqIdx)(params.numEnq-1, 0)
406      )
407  }
408
409  othersEntryOldestSel.zipWithIndex.foreach { case (sel, deqIdx) =>
410    sel := AgeDetector(numEntries = params.numEntries - params.numEnq,
411        enq = VecInit(transCanAcceptVec(deqIdx).zip(transSelVec).map{ case(doCanAccept, transSel) => Mux(doCanAccept, transSel, 0.U)}),
412        deq = VecInit(clearVec.drop(params.numEnq)).asUInt,
413        canIssue = canIssueMergeAllBusy(deqIdx)(params.numEntries-1, params.numEnq)
414      )
415  }
416
417  finalDeqSelValidVec.head := othersEntryOldestSel.head.valid || enqEntryOldestSel.head.valid || subDeqSelValidVec.head.getOrElse(Seq(false.B)).head
418  finalDeqSelOHVec.head := Mux(othersEntryOldestSel.head.valid, Cat(othersEntryOldestSel.head.bits, 0.U((params.numEnq).W)),
419                            Mux(enqEntryOldestSel.head.valid, Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldestSel.head.bits),
420                              subDeqSelOHVec.head.getOrElse(Seq(0.U)).head))
421
422  if (params.numDeq == 2) {
423    params.getFuCfgs.contains(FuConfig.FakeHystaCfg) match {
424      case true =>
425        finalDeqSelValidVec(1) := false.B
426        finalDeqSelOHVec(1) := 0.U.asTypeOf(finalDeqSelOHVec(1))
427      case false =>
428        val chooseOthersOldest = othersEntryOldestSel(1).valid && Cat(othersEntryOldestSel(1).bits, 0.U((params.numEnq).W)) =/= finalDeqSelOHVec.head
429        val chooseEnqOldest = enqEntryOldestSel(1).valid && Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldestSel(1).bits) =/= finalDeqSelOHVec.head
430        val choose1stSub = subDeqSelOHVec(1).getOrElse(Seq(0.U)).head =/= finalDeqSelOHVec.head
431
432        finalDeqSelValidVec(1) := MuxCase(subDeqSelValidVec(1).getOrElse(Seq(false.B)).last, Seq(
433          (chooseOthersOldest) -> othersEntryOldestSel(1).valid,
434          (chooseEnqOldest) -> enqEntryOldestSel(1).valid,
435          (choose1stSub) -> subDeqSelValidVec(1).getOrElse(Seq(false.B)).head)
436        )
437        finalDeqSelOHVec(1) := MuxCase(subDeqSelOHVec(1).getOrElse(Seq(0.U)).last, Seq(
438          (chooseOthersOldest) -> Cat(othersEntryOldestSel(1).bits, 0.U((params.numEnq).W)),
439          (chooseEnqOldest) -> Cat(0.U((params.numEntries-params.numEnq).W), enqEntryOldestSel(1).bits),
440          (choose1stSub) -> subDeqSelOHVec(1).getOrElse(Seq(0.U)).head)
441        )
442    }
443  }
444
445  //fuBusyTable
446  fuBusyTableWrite.zip(fuBusyTableRead).zipWithIndex.foreach { case ((busyTableWrite: Option[FuBusyTableWrite], busyTableRead: Option[FuBusyTableRead]), i) =>
447    if(busyTableWrite.nonEmpty) {
448      val btwr = busyTableWrite.get
449      val btrd = busyTableRead.get
450      btwr.io.in.deqResp := io.deqResp(i)
451      btwr.io.in.og0Resp := io.og0Resp(i)
452      btwr.io.in.og1Resp := io.og1Resp(i)
453      btrd.io.in.fuBusyTable := btwr.io.out.fuBusyTable
454      btrd.io.in.fuTypeRegVec := fuTypeVec
455      fuBusyTableMask(i) := btrd.io.out.fuBusyTableMask
456    }
457    else {
458      fuBusyTableMask(i) := 0.U(params.numEntries.W)
459    }
460  }
461
462  //wbfuBusyTable write
463  intWbBusyTableWrite.zip(intWbBusyTableOut).zip(intDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
464    if(busyTableWrite.nonEmpty) {
465      val btwr = busyTableWrite.get
466      val bt = busyTable.get
467      val dq = deqResp.get
468      btwr.io.in.deqResp := io.deqResp(i)
469      btwr.io.in.og0Resp := io.og0Resp(i)
470      btwr.io.in.og1Resp := io.og1Resp(i)
471      bt := btwr.io.out.fuBusyTable
472      dq := btwr.io.out.deqRespSet
473    }
474  }
475
476  vfWbBusyTableWrite.zip(vfWbBusyTableOut).zip(vfDeqRespSetOut).zipWithIndex.foreach { case (((busyTableWrite: Option[FuBusyTableWrite], busyTable: Option[UInt]), deqResp), i) =>
477    if (busyTableWrite.nonEmpty) {
478      val btwr = busyTableWrite.get
479      val bt = busyTable.get
480      val dq = deqResp.get
481      btwr.io.in.deqResp := io.deqResp(i)
482      btwr.io.in.og0Resp := io.og0Resp(i)
483      btwr.io.in.og1Resp := io.og1Resp(i)
484      bt := btwr.io.out.fuBusyTable
485      dq := btwr.io.out.deqRespSet
486    }
487  }
488
489  //wbfuBusyTable read
490  intWbBusyTableRead.zip(intWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
491    if(busyTableRead.nonEmpty) {
492      val btrd = busyTableRead.get
493      val bt = busyTable.get
494      btrd.io.in.fuBusyTable := bt
495      btrd.io.in.fuTypeRegVec := fuTypeVec
496      intWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
497    }
498    else {
499      intWbBusyTableMask(i) := 0.U(params.numEntries.W)
500    }
501  }
502  vfWbBusyTableRead.zip(vfWbBusyTableIn).zipWithIndex.foreach { case ((busyTableRead: Option[FuBusyTableRead], busyTable: Option[UInt]), i) =>
503    if (busyTableRead.nonEmpty) {
504      val btrd = busyTableRead.get
505      val bt = busyTable.get
506      btrd.io.in.fuBusyTable := bt
507      btrd.io.in.fuTypeRegVec := fuTypeVec
508      vfWbBusyTableMask(i) := btrd.io.out.fuBusyTableMask
509    }
510    else {
511      vfWbBusyTableMask(i) := 0.U(params.numEntries.W)
512    }
513  }
514
515  wakeUpQueues.zipWithIndex.foreach { case (wakeUpQueueOption, i) =>
516    val og0RespEach = io.og0Resp(i)
517    val og1RespEach = io.og1Resp(i)
518    wakeUpQueueOption.foreach {
519      wakeUpQueue =>
520        val flush = Wire(new WakeupQueueFlush)
521        flush.redirect := io.flush
522        flush.ldCancel := io.ldCancel
523        flush.og0Fail := io.og0Resp(i).valid && RSFeedbackType.isBlocked(io.og0Resp(i).bits.respType)
524        flush.og1Fail := io.og1Resp(i).valid && RSFeedbackType.isBlocked(io.og1Resp(i).bits.respType)
525        wakeUpQueue.io.flush := flush
526        wakeUpQueue.io.enq.valid := io.deq(i).fire && !io.deq(i).bits.common.needCancel(io.og0Cancel, io.og1Cancel) && {
527          io.deq(i).bits.common.rfWen.getOrElse(false.B) && io.deq(i).bits.common.pdest =/= 0.U ||
528          io.deq(i).bits.common.fpWen.getOrElse(false.B) ||
529          io.deq(i).bits.common.vecWen.getOrElse(false.B)
530        }
531        wakeUpQueue.io.enq.bits.uop := io.deq(i).bits.common
532        wakeUpQueue.io.enq.bits.lat := getDeqLat(i, io.deq(i).bits.common.fuType)
533        wakeUpQueue.io.og0IssueFail := flush.og0Fail
534        wakeUpQueue.io.og1IssueFail := flush.og1Fail
535    }
536  }
537
538  io.deq.zipWithIndex.foreach { case (deq, i) =>
539    deq.valid                := finalDeqSelValidVec(i)
540    deq.bits.addrOH          := finalDeqSelOHVec(i)
541    deq.bits.common.isFirstIssue := deqFirstIssueVec(i)
542    deq.bits.common.iqIdx    := OHToUInt(finalDeqSelOHVec(i))
543    deq.bits.common.fuType   := deqEntryVec(i).bits.payload.fuType
544    deq.bits.common.fuOpType := deqEntryVec(i).bits.payload.fuOpType
545    deq.bits.common.rfWen.foreach(_ := deqEntryVec(i).bits.payload.rfWen)
546    deq.bits.common.fpWen.foreach(_ := deqEntryVec(i).bits.payload.fpWen)
547    deq.bits.common.vecWen.foreach(_ := deqEntryVec(i).bits.payload.vecWen)
548    deq.bits.common.flushPipe.foreach(_ := deqEntryVec(i).bits.payload.flushPipe)
549    deq.bits.common.pdest := deqEntryVec(i).bits.payload.pdest
550    deq.bits.common.robIdx := deqEntryVec(i).bits.payload.robIdx
551    deq.bits.common.dataSources.zip(finalDataSources(i)).zipWithIndex.foreach {
552      case ((sink, source), srcIdx) =>
553        sink.value := Mux(
554          SrcType.isXp(deqEntryVec(i).bits.payload.srcType(srcIdx)) && deqEntryVec(i).bits.payload.psrc(srcIdx) === 0.U,
555          DataSource.none,
556          source.value
557        )
558    }
559    if (deq.bits.common.l1ExuOH.size > 0) {
560      if (params.hasIQWakeUp) {
561        deq.bits.common.l1ExuOH := finalWakeUpL1ExuOH.get(i)
562      } else {
563        deq.bits.common.l1ExuOH := deqEntryVec(i).bits.payload.l1ExuOH.take(deq.bits.common.l1ExuOH.length)
564      }
565    }
566    deq.bits.common.srcTimer.foreach(_ := finalSrcTimer.get(i))
567    deq.bits.common.loadDependency.foreach(_ := deqEntryVec(i).bits.status.mergedLoadDependency.get)
568    deq.bits.common.deqLdExuIdx.foreach(_ := params.backendParam.getLdExuIdx(deq.bits.exuParams).U)
569    deq.bits.common.src := DontCare
570    deq.bits.common.preDecode.foreach(_ := deqEntryVec(i).bits.payload.preDecodeInfo)
571
572    deq.bits.rf.zip(deqEntryVec(i).bits.payload.psrc).foreach { case (rf, psrc) =>
573      rf.foreach(_.addr := psrc) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
574    }
575    deq.bits.rf.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (rf, srcType) =>
576      rf.foreach(_.srcType := srcType) // psrc in payload array can be pregIdx of IntRegFile or VfRegFile
577    }
578    deq.bits.srcType.zip(deqEntryVec(i).bits.payload.srcType).foreach { case (sink, source) =>
579      sink := source
580    }
581    deq.bits.immType := deqEntryVec(i).bits.payload.selImm
582
583    if (params.inIntSchd && params.AluCnt > 0) {
584      // dirty code for lui+addi(w) fusion
585      val isLuiAddiFusion = deqEntryVec(i).bits.payload.isLUI32
586      val luiImm = Cat(deqEntryVec(i).bits.payload.lsrc(1), deqEntryVec(i).bits.payload.lsrc(0), deqEntryVec(i).bits.imm(ImmUnion.maxLen - 1, 0))
587      deq.bits.common.imm := Mux(isLuiAddiFusion, ImmUnion.LUI32.toImm32(luiImm), deqEntryVec(i).bits.imm)
588    }
589    else if (params.inMemSchd && params.LduCnt > 0) {
590      // dirty code for fused_lui_load
591      val isLuiLoadFusion = SrcType.isNotReg(deqEntryVec(i).bits.payload.srcType(0)) && FuType.isLoad(deqEntryVec(i).bits.payload.fuType)
592      deq.bits.common.imm := Mux(isLuiLoadFusion, Imm_LUI_LOAD().getLuiImm(deqEntryVec(i).bits.payload), deqEntryVec(i).bits.imm)
593    }
594    else {
595      deq.bits.common.imm := deqEntryVec(i).bits.imm
596    }
597
598    deq.bits.common.perfDebugInfo := deqEntryVec(i).bits.payload.debugInfo
599    deq.bits.common.perfDebugInfo.selectTime := GTimer()
600    deq.bits.common.perfDebugInfo.issueTime := GTimer() + 1.U
601  }
602
603  private val ldCancels = io.fromCancelNetwork.map(in =>
604    LoadShouldCancel(in.bits.common.loadDependency, io.ldCancel)
605  )
606  private val fromCancelNetworkShift = WireDefault(io.fromCancelNetwork)
607  fromCancelNetworkShift.zip(io.fromCancelNetwork).foreach {
608    case (shifted, original) =>
609      original.ready := shifted.ready // this will not cause combinational loop
610      shifted.bits.common.loadDependency.foreach(
611        _ := original.bits.common.loadDependency.get.map(_ << 1)
612      )
613  }
614  io.deqDelay.zip(fromCancelNetworkShift).zip(ldCancels).foreach { case ((deqDly, deq), ldCancel) =>
615    NewPipelineConnect(
616      deq, deqDly, deqDly.valid,
617      deq.bits.common.robIdx.needFlush(io.flush) || ldCancel,
618      Option("Scheduler2DataPathPipe")
619    )
620  }
621  dontTouch(io.deqDelay)
622  io.wakeupToIQ.zipWithIndex.foreach { case (wakeup, i) =>
623    if (wakeUpQueues(i).nonEmpty && finalWakeUpL1ExuOH.nonEmpty) {
624      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
625      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits, finalWakeUpL1ExuOH.get(i))
626      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
627    } else if (wakeUpQueues(i).nonEmpty) {
628      wakeup.valid := wakeUpQueues(i).get.io.deq.valid
629      wakeup.bits.fromExuInput(wakeUpQueues(i).get.io.deq.bits)
630      wakeup.bits.loadDependency := wakeUpQueues(i).get.io.deq.bits.loadDependency.getOrElse(0.U.asTypeOf(wakeup.bits.loadDependency))
631    } else {
632      wakeup.valid := false.B
633      wakeup.bits := 0.U.asTypeOf(wakeup.bits)
634    }
635  }
636
637  // Todo: better counter implementation
638  private val enqHasValid = validVec.take(params.numEnq).reduce(_ | _)
639  private val enqEntryValidCnt = PopCount(validVec.take(params.numEnq))
640  private val othersValidCnt = PopCount(validVec.drop(params.numEnq))
641  io.status.leftVec(0) := validVec.drop(params.numEnq).reduce(_ & _)
642  for (i <- 0 until params.numEnq) {
643    io.status.leftVec(i + 1) := othersValidCnt === (params.numEntries - params.numEnq - (i + 1)).U
644  }
645  io.enq.foreach(_.ready := !Cat(io.status.leftVec).orR || !enqHasValid) // Todo: more efficient implementation
646  io.status.empty := !Cat(validVec).orR
647  io.status.full := Cat(io.status.leftVec).orR
648  io.status.validCnt := PopCount(validVec)
649
650  protected def getDeqLat(deqPortIdx: Int, fuType: UInt) : UInt = {
651    Mux1H(fuLatencyMaps(deqPortIdx) map { case (k, v) => (k.U === fuType, v.U) })
652  }
653
654  // issue perf counter
655  // enq count
656  XSPerfAccumulate("enq_valid_cnt", PopCount(io.enq.map(_.fire)))
657  XSPerfAccumulate("enq_fire_cnt", PopCount(io.enq.map(_.fire)))
658  // valid count
659  XSPerfHistogram("enq_entry_valid_cnt", enqEntryValidCnt, true.B, 0, params.numEnq + 1)
660  XSPerfHistogram("other_entry_valid_cnt", othersValidCnt, true.B, 0, params.numEntries - params.numEnq + 1)
661  XSPerfHistogram("valid_cnt", PopCount(validVec), true.B, 0, params.numEntries + 1)
662  // only split when more than 1 func type
663  if (params.getFuCfgs.size > 0) {
664    for (t <- FuType.functionNameMap.keys) {
665      val fuName = FuType.functionNameMap(t)
666      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
667        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)
668      }
669    }
670  }
671  // ready instr count
672  private val readyEntriesCnt = PopCount(validVec.zip(canIssueVec).map(x => x._1 && x._2))
673  XSPerfHistogram("ready_cnt", readyEntriesCnt, true.B, 0, params.numEntries + 1)
674  // only split when more than 1 func type
675  if (params.getFuCfgs.size > 0) {
676    for (t <- FuType.functionNameMap.keys) {
677      val fuName = FuType.functionNameMap(t)
678      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
679        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)
680      }
681    }
682  }
683
684  // deq instr count
685  XSPerfAccumulate("issue_instr_pre_count", PopCount(io.deq.map(_.valid)))
686  XSPerfHistogram("issue_instr_pre_count_hist", PopCount(io.deq.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
687  XSPerfAccumulate("issue_instr_count", PopCount(io.deqDelay.map(_.valid)))
688  XSPerfHistogram("issue_instr_count_hist", PopCount(io.deqDelay.map(_.valid)), true.B, 0, params.numDeq + 1, 1)
689
690  // deq instr data source count
691  XSPerfAccumulate("issue_datasource_reg", 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)) })
693  }.reduce(_ +& _))
694  XSPerfAccumulate("issue_datasource_bypass", 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)) })
696  }.reduce(_ +& _))
697  XSPerfAccumulate("issue_datasource_forward", 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)) })
699  }.reduce(_ +& _))
700  XSPerfAccumulate("issue_datasource_noreg", io.deq.map{ deq =>
701    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
702  }.reduce(_ +& _))
703
704  XSPerfHistogram("issue_datasource_reg_hist", 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)) })
706  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
707  XSPerfHistogram("issue_datasource_bypass_hist", 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)) })
709  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
710  XSPerfHistogram("issue_datasource_forward_hist", 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)) })
712  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
713  XSPerfHistogram("issue_datasource_noreg_hist", io.deq.map{ deq =>
714    PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) })
715  }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
716
717  // deq instr data source count for each futype
718  for (t <- FuType.functionNameMap.keys) {
719    val fuName = FuType.functionNameMap(t)
720    if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
721      XSPerfAccumulate(s"issue_datasource_reg_futype_${fuName}", io.deq.map{ deq =>
722        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 })
723      }.reduce(_ +& _))
724      XSPerfAccumulate(s"issue_datasource_bypass_futype_${fuName}", io.deq.map{ deq =>
725        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 })
726      }.reduce(_ +& _))
727      XSPerfAccumulate(s"issue_datasource_forward_futype_${fuName}", io.deq.map{ deq =>
728        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 })
729      }.reduce(_ +& _))
730      XSPerfAccumulate(s"issue_datasource_noreg_futype_${fuName}", io.deq.map{ deq =>
731        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
732      }.reduce(_ +& _))
733
734      XSPerfHistogram(s"issue_datasource_reg_hist_futype_${fuName}", io.deq.map{ deq =>
735        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 })
736      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
737      XSPerfHistogram(s"issue_datasource_bypass_hist_futype_${fuName}", io.deq.map{ deq =>
738        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 })
739      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
740      XSPerfHistogram(s"issue_datasource_forward_hist_futype_${fuName}", io.deq.map{ deq =>
741        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 })
742      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
743      XSPerfHistogram(s"issue_datasource_noreg_hist_futype_${fuName}", io.deq.map{ deq =>
744        PopCount(deq.bits.common.dataSources.zipWithIndex.map{ case (ds, j) => deq.valid && SrcType.isNotReg(deq.bits.srcType(j)) && deq.bits.common.fuType === t.U })
745      }.reduce(_ +& _), true.B, 0, params.numDeq * params.numRegSrc + 1, 1)
746    }
747  }
748
749  // cancel instr count
750  if (params.hasIQWakeUp) {
751    val cancelVec: Vec[Bool] = entries.io.cancel.get
752    XSPerfAccumulate("cancel_instr_count", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)))
753    XSPerfHistogram("cancel_instr_hist", PopCount(validVec.zip(cancelVec).map(x => x._1 & x._2)), true.B, 0, params.numEntries, 1)
754    for (t <- FuType.functionNameMap.keys) {
755      val fuName = FuType.functionNameMap(t)
756      if (params.getFuCfgs.map(_.fuType == t).reduce(_ | _)) {
757        XSPerfAccumulate(s"cancel_instr_count_futype_${fuName}", PopCount(validVec.zip(cancelVec).zip(fuTypeVec).map{ case ((x, y), fu) => x & y & fu === t.U }))
758        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)
759      }
760    }
761  }
762}
763
764class IssueQueueJumpBundle extends Bundle {
765  val pc = UInt(VAddrData().dataWidth.W)
766}
767
768class IssueQueueLoadBundle(implicit p: Parameters) extends XSBundle {
769  val fastMatch = UInt(backendParams.LduCnt.W)
770  val fastImm = UInt(12.W)
771}
772
773class IssueQueueIntIO()(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO
774
775class IssueQueueIntImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
776  extends IssueQueueImp(wrapper)
777{
778  io.suggestName("none")
779  override lazy val io = IO(new IssueQueueIntIO).suggestName("io")
780
781  if(params.needPc) {
782    entries.io.enq.zipWithIndex.foreach { case (entriesEnq, i) =>
783      entriesEnq.bits.status.pc.foreach(_ := io.enq(i).bits.pc)
784    }
785  }
786
787  io.deq.zipWithIndex.foreach{ case (deq, i) => {
788    deq.bits.common.pc.foreach(_ := deqEntryVec(i).bits.status.pc.get)
789    deq.bits.common.preDecode.foreach(_ := deqEntryVec(i).bits.payload.preDecodeInfo)
790    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
791    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
792    deq.bits.common.predictInfo.foreach(x => {
793      x.target := DontCare
794      x.taken := deqEntryVec(i).bits.payload.pred_taken
795    })
796    // for std
797    deq.bits.common.sqIdx.foreach(_ := deqEntryVec(i).bits.payload.sqIdx)
798    // for i2f
799    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
800  }}
801}
802
803class IssueQueueVfImp(override val wrapper: IssueQueue)(implicit p: Parameters, iqParams: IssueBlockParams)
804  extends IssueQueueImp(wrapper)
805{
806  s0_enqBits.foreach{ x =>
807    x.srcType(3) := SrcType.vp // v0: mask src
808    x.srcType(4) := SrcType.vp // vl&vtype
809  }
810  io.deq.zipWithIndex.foreach{ case (deq, i) => {
811    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
812    deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
813    deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
814    deq.bits.common.vpu.foreach(_.lastUop := deqEntryVec(i).bits.payload.lastUop)
815  }}
816}
817
818class IssueQueueMemBundle(implicit p: Parameters, params: IssueBlockParams) extends Bundle {
819  val feedbackIO = Flipped(Vec(params.numDeq, new MemRSFeedbackIO))
820  val checkWait = new Bundle {
821    val stIssuePtr = Input(new SqPtr)
822    val memWaitUpdateReq = Flipped(new MemWaitUpdateReq)
823  }
824  val loadFastMatch = Output(Vec(params.LduCnt, new IssueQueueLoadBundle))
825
826  // vector
827  val sqDeqPtr = OptionWrapper(params.isVecMemIQ, Input(new SqPtr))
828  val lqDeqPtr = OptionWrapper(params.isVecMemIQ, Input(new LqPtr))
829}
830
831class IssueQueueMemIO(implicit p: Parameters, params: IssueBlockParams) extends IssueQueueIO {
832  val memIO = Some(new IssueQueueMemBundle)
833}
834
835class IssueQueueMemAddrImp(override val wrapper: IssueQueue)(implicit p: Parameters, params: IssueBlockParams)
836  extends IssueQueueImp(wrapper) with HasCircularQueuePtrHelper {
837
838  require(params.StdCnt == 0 && (params.LduCnt + params.StaCnt + params.HyuCnt + params.VlduCnt) > 0, "IssueQueueMemAddrImp can only be instance of MemAddr IQ, " +
839    s"StdCnt: ${params.StdCnt}, LduCnt: ${params.LduCnt}, StaCnt: ${params.StaCnt}, HyuCnt: ${params.HyuCnt}")
840  println(s"[IssueQueueMemAddrImp] StdCnt: ${params.StdCnt}, LduCnt: ${params.LduCnt}, StaCnt: ${params.StaCnt}, HyuCnt: ${params.HyuCnt}")
841
842  io.suggestName("none")
843  override lazy val io = IO(new IssueQueueMemIO).suggestName("io")
844  private val memIO = io.memIO.get
845
846  memIO.loadFastMatch := 0.U.asTypeOf(memIO.loadFastMatch) // TODO: is still needed?
847
848  for (i <- io.enq.indices) {
849    val blockNotReleased = isAfter(io.enq(i).bits.sqIdx, memIO.checkWait.stIssuePtr)
850    val storeAddrWaitForIsIssuing = VecInit((0 until StorePipelineWidth).map(i => {
851      memIO.checkWait.memWaitUpdateReq.robIdx(i).valid &&
852        memIO.checkWait.memWaitUpdateReq.robIdx(i).bits.value === io.enq(i).bits.waitForRobIdx.value
853    })).asUInt.orR && !io.enq(i).bits.loadWaitStrict // is waiting for store addr ready
854    s0_enqBits(i).loadWaitBit := io.enq(i).bits.loadWaitBit && !storeAddrWaitForIsIssuing && blockNotReleased
855    // when have vpu
856    if (params.VlduCnt > 0 || params.VstuCnt > 0) {
857      s0_enqBits(i).srcType(3) := SrcType.vp // v0: mask src
858      s0_enqBits(i).srcType(4) := SrcType.vp // vl&vtype
859    }
860  }
861
862  for (i <- entries.io.enq.indices) {
863    entries.io.enq(i).bits.status match { case enqData =>
864      enqData.blocked := false.B // s0_enqBits(i).loadWaitBit
865      enqData.mem.get.strictWait := s0_enqBits(i).loadWaitStrict
866      enqData.mem.get.waitForStd := false.B
867      enqData.mem.get.waitForRobIdx := s0_enqBits(i).waitForRobIdx
868      enqData.mem.get.waitForSqIdx := 0.U.asTypeOf(enqData.mem.get.waitForSqIdx) // generated by sq, will be updated later
869      enqData.mem.get.sqIdx := s0_enqBits(i).sqIdx
870    }
871
872    entries.io.fromMem.get.slowResp.zipWithIndex.foreach { case (slowResp, i) =>
873      slowResp.valid                 := memIO.feedbackIO(i).feedbackSlow.valid
874      slowResp.bits.robIdx           := memIO.feedbackIO(i).feedbackSlow.bits.robIdx
875      slowResp.bits.uopIdx           := DontCare
876      slowResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackSlow.bits.hit, RSFeedbackType.fuIdle, RSFeedbackType.feedbackInvalid)
877      slowResp.bits.dataInvalidSqIdx := memIO.feedbackIO(i).feedbackSlow.bits.dataInvalidSqIdx
878      slowResp.bits.rfWen := DontCare
879      slowResp.bits.fuType := DontCare
880    }
881
882    entries.io.fromMem.get.fastResp.zipWithIndex.foreach { case (fastResp, i) =>
883      fastResp.valid                 := memIO.feedbackIO(i).feedbackFast.valid
884      fastResp.bits.robIdx           := memIO.feedbackIO(i).feedbackFast.bits.robIdx
885      fastResp.bits.uopIdx           := DontCare
886      fastResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackFast.bits.hit, RSFeedbackType.fuIdle, memIO.feedbackIO(i).feedbackFast.bits.sourceType)
887      fastResp.bits.dataInvalidSqIdx := 0.U.asTypeOf(fastResp.bits.dataInvalidSqIdx)
888      fastResp.bits.rfWen := DontCare
889      fastResp.bits.fuType := DontCare
890    }
891
892    entries.io.fromMem.get.memWaitUpdateReq := memIO.checkWait.memWaitUpdateReq
893    entries.io.fromMem.get.stIssuePtr := memIO.checkWait.stIssuePtr
894  }
895
896  io.deq.zipWithIndex.foreach { case (deq, i) =>
897    deq.bits.common.loadWaitBit.foreach(_ := deqEntryVec(i).bits.payload.loadWaitBit)
898    deq.bits.common.waitForRobIdx.foreach(_ := deqEntryVec(i).bits.payload.waitForRobIdx)
899    deq.bits.common.storeSetHit.foreach(_ := deqEntryVec(i).bits.payload.storeSetHit)
900    deq.bits.common.loadWaitStrict.foreach(_ := deqEntryVec(i).bits.payload.loadWaitStrict)
901    deq.bits.common.ssid.foreach(_ := deqEntryVec(i).bits.payload.ssid)
902    deq.bits.common.sqIdx.get := deqEntryVec(i).bits.payload.sqIdx
903    deq.bits.common.lqIdx.get := deqEntryVec(i).bits.payload.lqIdx
904    deq.bits.common.ftqIdx.foreach(_ := deqEntryVec(i).bits.payload.ftqPtr)
905    deq.bits.common.ftqOffset.foreach(_ := deqEntryVec(i).bits.payload.ftqOffset)
906    // when have vpu
907    if (params.VlduCnt > 0 || params.VstuCnt > 0) {
908      deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
909      deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
910    }
911  }
912}
913
914class IssueQueueVecMemImp(override val wrapper: IssueQueue)(implicit p: Parameters, params: IssueBlockParams)
915  extends IssueQueueImp(wrapper) with HasCircularQueuePtrHelper {
916
917  require((params.VstdCnt + params.VlduCnt + params.VstaCnt) > 0, "IssueQueueVecMemImp can only be instance of VecMem IQ")
918
919  io.suggestName("none")
920  override lazy val io = IO(new IssueQueueMemIO).suggestName("io")
921  private val memIO = io.memIO.get
922
923  def selectOldUop(robIdx: Seq[RobPtr], uopIdx: Seq[UInt], valid: Seq[Bool]): Vec[Bool] = {
924    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))))
925    val resultOnehot = VecInit((0 until robIdx.length).map(i => Cat((0 until robIdx.length).map(j =>
926      (if (j < i) !valid(j) || compareVec(i)(j)
927      else if (j == i) valid(i)
928      else !valid(j) || !compareVec(j)(i))
929    )).andR))
930    resultOnehot
931  }
932
933  val robIdxVec = entries.io.robIdx.get
934  val uopIdxVec = entries.io.uopIdx.get
935  val allEntryOldestOH = selectOldUop(robIdxVec, uopIdxVec, validVec)
936
937  finalDeqSelValidVec.head := (allEntryOldestOH.asUInt & canIssueVec.asUInt).orR
938  finalDeqSelOHVec.head := allEntryOldestOH.asUInt & canIssueVec.asUInt
939
940  if (params.isVecMemAddrIQ) {
941    s0_enqBits.foreach{ x =>
942      x.srcType(3) := SrcType.vp // v0: mask src
943      x.srcType(4) := SrcType.vp // vl&vtype
944    }
945
946    for (i <- io.enq.indices) {
947      s0_enqBits(i).loadWaitBit := false.B
948    }
949
950    for (i <- entries.io.enq.indices) {
951      entries.io.enq(i).bits.status match { case enqData =>
952        enqData.blocked := false.B // s0_enqBits(i).loadWaitBit
953        enqData.mem.get.strictWait := s0_enqBits(i).loadWaitStrict
954        enqData.mem.get.waitForStd := false.B
955        enqData.mem.get.waitForRobIdx := s0_enqBits(i).waitForRobIdx
956        enqData.mem.get.waitForSqIdx := 0.U.asTypeOf(enqData.mem.get.waitForSqIdx) // generated by sq, will be updated later
957        enqData.mem.get.sqIdx := s0_enqBits(i).sqIdx
958      }
959
960      entries.io.fromMem.get.slowResp.zipWithIndex.foreach { case (slowResp, i) =>
961        slowResp.valid                 := memIO.feedbackIO(i).feedbackSlow.valid
962        slowResp.bits.robIdx           := memIO.feedbackIO(i).feedbackSlow.bits.robIdx
963        slowResp.bits.uopIdx           := DontCare
964        slowResp.bits.respType         := Mux(memIO.feedbackIO(i).feedbackSlow.bits.hit, RSFeedbackType.fuIdle, RSFeedbackType.feedbackInvalid)
965        slowResp.bits.dataInvalidSqIdx := memIO.feedbackIO(i).feedbackSlow.bits.dataInvalidSqIdx
966        slowResp.bits.rfWen := DontCare
967        slowResp.bits.fuType := DontCare
968      }
969
970      entries.io.fromMem.get.fastResp.zipWithIndex.foreach { case (fastResp, i) =>
971        fastResp.valid                 := memIO.feedbackIO(i).feedbackFast.valid
972        fastResp.bits.robIdx           := memIO.feedbackIO(i).feedbackFast.bits.robIdx
973        fastResp.bits.uopIdx           := DontCare
974        fastResp.bits.respType         := memIO.feedbackIO(i).feedbackFast.bits.sourceType
975        fastResp.bits.dataInvalidSqIdx := 0.U.asTypeOf(fastResp.bits.dataInvalidSqIdx)
976        fastResp.bits.rfWen := DontCare
977        fastResp.bits.fuType := DontCare
978      }
979
980      entries.io.fromMem.get.memWaitUpdateReq := memIO.checkWait.memWaitUpdateReq
981      entries.io.fromMem.get.stIssuePtr := memIO.checkWait.stIssuePtr
982    }
983  }
984
985  for (i <- entries.io.enq.indices) {
986    entries.io.enq(i).bits.status match { case enqData =>
987      enqData.vecMem.get.sqIdx := s0_enqBits(i).sqIdx
988      enqData.vecMem.get.lqIdx := s0_enqBits(i).lqIdx
989    }
990  }
991
992  entries.io.fromLsq.get.sqDeqPtr := memIO.sqDeqPtr.get
993  entries.io.fromLsq.get.lqDeqPtr := memIO.lqDeqPtr.get
994
995  io.deq.zipWithIndex.foreach { case (deq, i) =>
996    deq.bits.common.sqIdx.foreach(_ := deqEntryVec(i).bits.payload.sqIdx)
997    deq.bits.common.lqIdx.foreach(_ := deqEntryVec(i).bits.payload.lqIdx)
998    if (params.isVecLdAddrIQ) {
999      deq.bits.common.ftqIdx.get := deqEntryVec(i).bits.payload.ftqPtr
1000      deq.bits.common.ftqOffset.get := deqEntryVec(i).bits.payload.ftqOffset
1001    }
1002    deq.bits.common.fpu.foreach(_ := deqEntryVec(i).bits.payload.fpu)
1003    deq.bits.common.vpu.foreach(_ := deqEntryVec(i).bits.payload.vpu)
1004    deq.bits.common.vpu.foreach(_.vuopIdx := deqEntryVec(i).bits.payload.uopIdx)
1005    deq.bits.common.vpu.foreach(_.lastUop := deqEntryVec(i).bits.payload.lastUop)
1006  }
1007}
1008