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