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