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