xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 37d77575ecbc45c170c8e9b1aed0e82f21bb4696)
124519898SXuan Hu/***************************************************************************************
224519898SXuan Hu* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
324519898SXuan Hu* Copyright (c) 2020-2021 Peng Cheng Laboratory
424519898SXuan Hu*
524519898SXuan Hu* XiangShan is licensed under Mulan PSL v2.
624519898SXuan Hu* You can use this software according to the terms and conditions of the Mulan PSL v2.
724519898SXuan Hu* You may obtain a copy of Mulan PSL v2 at:
824519898SXuan Hu*          http://license.coscl.org.cn/MulanPSL2
924519898SXuan Hu*
1024519898SXuan Hu* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
1124519898SXuan Hu* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1224519898SXuan Hu* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1324519898SXuan Hu*
1424519898SXuan Hu* See the Mulan PSL v2 for more details.
1524519898SXuan Hu***************************************************************************************/
1624519898SXuan Hu
1724519898SXuan Hupackage xiangshan.backend
1824519898SXuan Hu
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
2024519898SXuan Huimport chisel3._
2124519898SXuan Huimport chisel3.util._
2224519898SXuan Huimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
2324519898SXuan Huimport utility._
2424519898SXuan Huimport utils._
2524519898SXuan Huimport xiangshan.ExceptionNO._
2624519898SXuan Huimport xiangshan._
2724519898SXuan Huimport xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput}
282326221cSXuan Huimport xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator}
2924519898SXuan Huimport xiangshan.backend.datapath.DataConfig.VAddrData
3024519898SXuan Huimport xiangshan.backend.decode.{DecodeStage, FusionDecoder}
3183ba63b3SXuan Huimport xiangshan.backend.dispatch.{CoreDispatchTopDownIO, Dispatch, DispatchQueue}
3224519898SXuan Huimport xiangshan.backend.fu.PFEvent
3324519898SXuan Huimport xiangshan.backend.fu.vector.Bundles.VType
34870f462dSXuan Huimport xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator}
3583ba63b3SXuan Huimport xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr}
366ce10964SXuan Huimport xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
376ce10964SXuan Huimport xiangshan.mem.{LqPtr, LsqEnqIO}
3824519898SXuan Hu
3924519898SXuan Huclass CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
4024519898SXuan Hu  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
4124519898SXuan Hu  val redirect = Valid(new Redirect)
429342624fSGao-Zeyu  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
439342624fSGao-Zeyu  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
4424519898SXuan Hu}
4524519898SXuan Hu
4624519898SXuan Huclass CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
471ca4a39dSXuan Hu  override def shouldBeInlined: Boolean = false
481ca4a39dSXuan Hu
4924519898SXuan Hu  val rob = LazyModule(new Rob(params))
5024519898SXuan Hu
5124519898SXuan Hu  lazy val module = new CtrlBlockImp(this)(p, params)
5224519898SXuan Hu
5324519898SXuan Hu}
5424519898SXuan Hu
5524519898SXuan Huclass CtrlBlockImp(
5624519898SXuan Hu  override val wrapper: CtrlBlock
5724519898SXuan Hu)(implicit
5824519898SXuan Hu  p: Parameters,
5924519898SXuan Hu  params: BackendParams
6024519898SXuan Hu) extends LazyModuleImp(wrapper)
6124519898SXuan Hu  with HasXSParameter
6224519898SXuan Hu  with HasCircularQueuePtrHelper
6324519898SXuan Hu  with HasPerfEvents
6424519898SXuan Hu{
6524519898SXuan Hu  val pcMemRdIndexes = new NamedIndexes(Seq(
6624519898SXuan Hu    "exu"       -> params.numPcReadPort,
6724519898SXuan Hu    "redirect"  -> 1,
6824519898SXuan Hu    "memPred"   -> 1,
6924519898SXuan Hu    "robFlush"  -> 1,
7024519898SXuan Hu    "load"      -> params.LduCnt,
71b133b458SXuan Hu    "hybrid"    -> params.HyuCnt,
7283ba63b3SXuan Hu    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0)
7324519898SXuan Hu  ))
7424519898SXuan Hu
7524519898SXuan Hu  private val numPcMemReadForExu = params.numPcReadPort
7624519898SXuan Hu  private val numPcMemRead = pcMemRdIndexes.maxIdx
7724519898SXuan Hu
7824519898SXuan Hu  println(s"pcMem read num: $numPcMemRead")
7924519898SXuan Hu  println(s"pcMem read num for exu: $numPcMemReadForExu")
8024519898SXuan Hu
8124519898SXuan Hu  val io = IO(new CtrlBlockIO())
8224519898SXuan Hu
8324519898SXuan Hu  val decode = Module(new DecodeStage)
8424519898SXuan Hu  val fusionDecoder = Module(new FusionDecoder)
8524519898SXuan Hu  val rat = Module(new RenameTableWrapper)
8624519898SXuan Hu  val rename = Module(new Rename)
8724519898SXuan Hu  val dispatch = Module(new Dispatch)
8824519898SXuan Hu  val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth))
8924519898SXuan Hu  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth))
9024519898SXuan Hu  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
9124519898SXuan Hu  val redirectGen = Module(new RedirectGenerator)
9224519898SXuan Hu  private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC"))
9324519898SXuan Hu  private val rob = wrapper.rob.module
9424519898SXuan Hu  private val memCtrl = Module(new MemCtrl(params))
9524519898SXuan Hu
9624519898SXuan Hu  private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
9724519898SXuan Hu
9824519898SXuan Hu  private val s0_robFlushRedirect = rob.io.flushOut
9924519898SXuan Hu  private val s1_robFlushRedirect = Wire(Valid(new Redirect))
1003a9e5338SXuan Hu  s1_robFlushRedirect.valid := RegNext(s0_robFlushRedirect.valid, false.B)
10124519898SXuan Hu  s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid)
10224519898SXuan Hu
10324519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value
104b1e92023SsinceforYy  private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).getPc(RegEnable(s0_robFlushRedirect.bits.ftqOffset, s0_robFlushRedirect.valid))
10524519898SXuan Hu  private val s3_redirectGen = redirectGen.io.stage2Redirect
10624519898SXuan Hu  private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen)
10724519898SXuan Hu  private val s2_s4_pendingRedirectValid = RegInit(false.B)
10824519898SXuan Hu  when (s1_s3_redirect.valid) {
10924519898SXuan Hu    s2_s4_pendingRedirectValid := true.B
11024519898SXuan Hu  }.elsewhen (RegNext(io.frontend.toFtq.redirect.valid)) {
11124519898SXuan Hu    s2_s4_pendingRedirectValid := false.B
11224519898SXuan Hu  }
11324519898SXuan Hu
11424519898SXuan Hu  // Redirect will be RegNext at ExuBlocks and IssueBlocks
11524519898SXuan Hu  val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect)
11624519898SXuan Hu  val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect)
11724519898SXuan Hu
11824519898SXuan Hu  private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => {
11924519898SXuan Hu    val valid = x.valid
12024519898SXuan Hu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
12124519898SXuan Hu    val delayed = Wire(Valid(new ExuOutput(x.bits.params)))
12224519898SXuan Hu    delayed.valid := RegNext(valid && !killedByOlder)
12324519898SXuan Hu    delayed.bits := RegEnable(x.bits, x.valid)
12496e858baSXuan Hu    delayed.bits.debugInfo.writebackTime := GTimer()
12524519898SXuan Hu    delayed
12683ba63b3SXuan Hu  }).toSeq
12724519898SXuan Hu
12885f51ecaSxiaofeibao-xjtu  val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu)
12985f51ecaSxiaofeibao-xjtu  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
13085f51ecaSxiaofeibao-xjtu    val valid = x.valid
13185f51ecaSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
13285f51ecaSxiaofeibao-xjtu    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
13385f51ecaSxiaofeibao-xjtu    delayed.valid := RegNext(valid && !killedByOlder)
13485f51ecaSxiaofeibao-xjtu    val sameRobidxBools = VecInit(wbDataNoStd.map( wb => {
13585f51ecaSxiaofeibao-xjtu      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
13685f51ecaSxiaofeibao-xjtu      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
13785f51ecaSxiaofeibao-xjtu    }).toSeq)
13885f51ecaSxiaofeibao-xjtu    dontTouch(sameRobidxBools)
13985f51ecaSxiaofeibao-xjtu    delayed.bits := RegNext(PopCount(sameRobidxBools))
14085f51ecaSxiaofeibao-xjtu    delayed
14185f51ecaSxiaofeibao-xjtu  }).toSeq
14285f51ecaSxiaofeibao-xjtu
14324519898SXuan Hu  private val exuPredecode = VecInit(
14483ba63b3SXuan Hu    delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
14524519898SXuan Hu  )
14624519898SXuan Hu
14783ba63b3SXuan Hu  private val exuRedirects: Seq[ValidIO[Redirect]] = delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => {
14824519898SXuan Hu    val out = Wire(Valid(new Redirect()))
14924519898SXuan Hu    out.valid := x.valid && x.bits.redirect.get.valid && x.bits.redirect.get.bits.cfiUpdate.isMisPred
15024519898SXuan Hu    out.bits := x.bits.redirect.get.bits
151a63155a6SXuan Hu    out.bits.debugIsCtrl := true.B
152a63155a6SXuan Hu    out.bits.debugIsMemVio := false.B
15324519898SXuan Hu    out
15483ba63b3SXuan Hu  }).toSeq
15524519898SXuan Hu
15624519898SXuan Hu  private val memViolation = io.fromMem.violation
15724519898SXuan Hu  val loadReplay = Wire(ValidIO(new Redirect))
15824519898SXuan Hu  loadReplay.valid := RegNext(memViolation.valid &&
15924519898SXuan Hu    !memViolation.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
16024519898SXuan Hu  )
16124519898SXuan Hu  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
162a63155a6SXuan Hu  loadReplay.bits.debugIsCtrl := false.B
163a63155a6SXuan Hu  loadReplay.bits.debugIsMemVio := true.B
16424519898SXuan Hu
16524519898SXuan Hu  val pdestReverse = rob.io.commits.info.map(info => info.pdest).reverse
16624519898SXuan Hu
16724519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.ptr.value
16824519898SXuan Hu  redirectGen.io.redirectPcRead.data := pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegNext(redirectGen.io.redirectPcRead.offset))
16924519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.ptr.value
17024519898SXuan Hu  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegNext(redirectGen.io.memPredPcRead.offset))
17124519898SXuan Hu
17224519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
1738241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
17424519898SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
17524519898SXuan Hu    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegNext(io.memLdPcRead(i).offset))
17624519898SXuan Hu  }
17724519898SXuan Hu
178b133b458SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
1798241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
180b133b458SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
181b133b458SXuan Hu    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegNext(io.memHyPcRead(i).offset))
182b133b458SXuan Hu  }
183b133b458SXuan Hu
1844b0d80d8SXuan Hu  if (EnableStorePrefetchSMS) {
1854b0d80d8SXuan Hu    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
1864b0d80d8SXuan Hu      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
1874b0d80d8SXuan Hu      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegNext(io.memStPcRead(i).offset))
1884b0d80d8SXuan Hu    }
1894b0d80d8SXuan Hu  } else {
19083ba63b3SXuan Hu    io.memStPcRead.foreach(_.data := 0.U)
1914b0d80d8SXuan Hu  }
1924b0d80d8SXuan Hu
19324519898SXuan Hu  redirectGen.io.hartId := io.fromTop.hartId
19483ba63b3SXuan Hu  redirectGen.io.exuRedirect := exuRedirects.toSeq
1954b0d80d8SXuan Hu  redirectGen.io.exuOutPredecode := exuPredecode // guarded by exuRedirect.valid
19624519898SXuan Hu  redirectGen.io.loadReplay <> loadReplay
19724519898SXuan Hu
19824519898SXuan Hu  redirectGen.io.robFlush := s1_robFlushRedirect.valid
19924519898SXuan Hu
200ff7f931dSXuan Hu  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
201ff7f931dSXuan Hu  val s6_flushFromRobValid = RegNext(s5_flushFromRobValidAhead)
20224519898SXuan Hu  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
20324519898SXuan Hu  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
20424519898SXuan Hu  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
20524519898SXuan Hu  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
20624519898SXuan Hu  for (i <- 0 until CommitWidth) {
20724519898SXuan Hu    // why flushOut: instructions with flushPipe are not commited to frontend
20824519898SXuan Hu    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
20924519898SXuan Hu    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
21024519898SXuan Hu    io.frontend.toFtq.rob_commits(i).valid := RegNext(s1_isCommit)
21124519898SXuan Hu    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
21224519898SXuan Hu  }
213ff7f931dSXuan Hu  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
214ff7f931dSXuan Hu  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
215ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
216ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
2179342624fSGao-Zeyu
2189342624fSGao-Zeyu  //jmp/brh
2199342624fSGao-Zeyu  for (i <- 0 until NumRedirect) {
220ff7f931dSXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirects(i).valid && exuRedirects(i).bits.cfiUpdate.isMisPred && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2216ce10964SXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirects(i).bits.ftqIdx
2229342624fSGao-Zeyu  }
2239342624fSGao-Zeyu  //loadreplay
224ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2259342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
2269342624fSGao-Zeyu  //exception
227ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
2289342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
22924519898SXuan Hu  // Be careful here:
23024519898SXuan Hu  // T0: rob.io.flushOut, s0_robFlushRedirect
23124519898SXuan Hu  // T1: s1_robFlushRedirect, rob.io.exception.valid
23224519898SXuan Hu  // T2: csr.redirect.valid
23324519898SXuan Hu  // T3: csr.exception.valid
23424519898SXuan Hu  // T4: csr.trapTarget
23524519898SXuan Hu  // T5: ctrlBlock.trapTarget
23624519898SXuan Hu  // T6: io.frontend.toFtq.stage2Redirect.valid
23724519898SXuan Hu  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
23824519898SXuan Hu    s1_robFlushPc, // replay inst
239870f462dSXuan Hu    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
24024519898SXuan Hu  ), s1_robFlushRedirect.valid)
24124519898SXuan Hu  private val s2_csrIsXRet = io.robio.csr.isXRet
24224519898SXuan Hu  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
24324519898SXuan Hu  private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget
24424519898SXuan Hu
24524519898SXuan Hu  val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc)
246ff7f931dSXuan Hu  when (s6_flushFromRobValid) {
24724519898SXuan Hu    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
24874f21f21SsinceforYy    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
24924519898SXuan Hu  }
25024519898SXuan Hu
25124519898SXuan Hu  // vtype commit
25224519898SXuan Hu  decode.io.commitVType.bits := io.fromDataPath.vtype
25324519898SXuan Hu  decode.io.commitVType.valid := RegNext(rob.io.isVsetFlushPipe)
25424519898SXuan Hu
25524519898SXuan Hu  io.toDataPath.vtypeAddr := rob.io.vconfigPdest
25624519898SXuan Hu
2574c7680e0SXuan Hu  decode.io.walkVType := rob.io.toDecode.vtype
25824519898SXuan Hu
259e25c13faSXuan Hu  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
26024519898SXuan Hu
26124519898SXuan Hu  decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) =>
26224519898SXuan Hu    decodeIn.valid := frontendCf.valid
26324519898SXuan Hu    frontendCf.ready := decodeIn.ready
26424519898SXuan Hu    decodeIn.bits.connectCtrlFlow(frontendCf.bits)
26524519898SXuan Hu  }
26624519898SXuan Hu  decode.io.csrCtrl := RegNext(io.csrCtrl)
26724519898SXuan Hu  decode.io.intRat <> rat.io.intReadPorts
26824519898SXuan Hu  decode.io.fpRat <> rat.io.fpReadPorts
26924519898SXuan Hu  decode.io.vecRat <> rat.io.vecReadPorts
27024519898SXuan Hu  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
271870f462dSXuan Hu  decode.io.stallReason.in <> io.frontend.stallReason
27224519898SXuan Hu
273fa7f2c26STang Haojin  // snapshot check
274c4b56310SHaojin Tang  class CFIRobIdx extends Bundle {
275c4b56310SHaojin Tang    val robIdx = Vec(RenameWidth, new RobPtr)
276c4b56310SHaojin Tang    val isCFI = Vec(RenameWidth, Bool())
277c4b56310SHaojin Tang  }
278c4b56310SHaojin Tang  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
279c4b56310SHaojin Tang  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
280c4b56310SHaojin Tang  snpt.io.enq := genSnapshot
281c4b56310SHaojin Tang  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
282c4b56310SHaojin Tang  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
283fa7f2c26STang Haojin  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
284c4b56310SHaojin Tang    Cat(rob.io.commits.commitValid.zip(rob.io.commits.robIdx).map(x => x._1 && x._2 === snpt.io.snapshots(snpt.io.deqPtr.value).robIdx.head)).orR
285c4b56310SHaojin Tang  snpt.io.redirect := s1_s3_redirect.valid
286c4b56310SHaojin Tang  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
287c4b56310SHaojin Tang    val notCFIMask = snapshot.isCFI.map(~_)
288*37d77575SzhanglyGit    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
289*37d77575SzhanglyGit    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
290*37d77575SzhanglyGit    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
291c4b56310SHaojin Tang  })
292c4b56310SHaojin Tang  val flushVecNext = RegNext(flushVec, 0.U.asTypeOf(flushVec))
293c4b56310SHaojin Tang  snpt.io.flushVec := flushVecNext
294fa7f2c26STang Haojin
295fa7f2c26STang Haojin  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
296c4b56310SHaojin Tang    snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx).robIdx.head
297c61abc0cSXuan Hu  ).reduceTree(_ || _)
298c61abc0cSXuan Hu  val snptSelect = MuxCase(
299c61abc0cSXuan Hu    0.U(log2Ceil(RenameSnapshotNum).W),
300fa7f2c26STang Haojin    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
301c4b56310SHaojin Tang      (snpt.io.valids(idx) && s1_s3_redirect.bits.robIdx >= snpt.io.snapshots(idx).robIdx.head, idx)
302c61abc0cSXuan Hu    )
303c61abc0cSXuan Hu  )
304fa7f2c26STang Haojin
305fa7f2c26STang Haojin  rob.io.snpt.snptEnq := DontCare
306fa7f2c26STang Haojin  rob.io.snpt.snptDeq := snpt.io.deq
307fa7f2c26STang Haojin  rob.io.snpt.useSnpt := useSnpt
308fa7f2c26STang Haojin  rob.io.snpt.snptSelect := snptSelect
309c4b56310SHaojin Tang  rob.io.snpt.flushVec := flushVecNext
310c4b56310SHaojin Tang  rat.io.snpt.snptEnq := genSnapshot
311fa7f2c26STang Haojin  rat.io.snpt.snptDeq := snpt.io.deq
312fa7f2c26STang Haojin  rat.io.snpt.useSnpt := useSnpt
313fa7f2c26STang Haojin  rat.io.snpt.snptSelect := snptSelect
314c4b56310SHaojin Tang  rat.io.snpt.flushVec := flushVec
315fa7f2c26STang Haojin
31624519898SXuan Hu  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
31724519898SXuan Hu  // fusion decoder
31824519898SXuan Hu  for (i <- 0 until DecodeWidth) {
31924519898SXuan Hu    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
32024519898SXuan Hu    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
32124519898SXuan Hu    if (i > 0) {
32224519898SXuan Hu      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
32324519898SXuan Hu    }
32424519898SXuan Hu  }
32524519898SXuan Hu
32624519898SXuan Hu  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
32724519898SXuan Hu
32824519898SXuan Hu  for (i <- 0 until RenameWidth) {
32924519898SXuan Hu    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
33024519898SXuan Hu      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
33124519898SXuan Hu
33224519898SXuan Hu    decodePipeRename(i).ready := rename.io.in(i).ready
33324519898SXuan Hu    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
33424519898SXuan Hu    rename.io.in(i).bits := decodePipeRename(i).bits
33524519898SXuan Hu  }
33624519898SXuan Hu
33724519898SXuan Hu  for (i <- 0 until RenameWidth - 1) {
33824519898SXuan Hu    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
33924519898SXuan Hu    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
34024519898SXuan Hu
34124519898SXuan Hu    // update the first RenameWidth - 1 instructions
34224519898SXuan Hu    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
34324519898SXuan Hu    when (fusionDecoder.io.out(i).valid) {
34424519898SXuan Hu      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
34524519898SXuan Hu      // TODO: remove this dirty code for ftq update
34624519898SXuan Hu      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
34724519898SXuan Hu      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
34824519898SXuan Hu      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
34924519898SXuan Hu      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
35024519898SXuan Hu      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
35124519898SXuan Hu      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
35224519898SXuan Hu      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
35324519898SXuan Hu      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
35424519898SXuan Hu      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
35524519898SXuan Hu      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
35624519898SXuan Hu    }
35724519898SXuan Hu
35824519898SXuan Hu  }
35924519898SXuan Hu
36024519898SXuan Hu  // memory dependency predict
36124519898SXuan Hu  // when decode, send fold pc to mdp
36224519898SXuan Hu  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
36324519898SXuan Hu  for (i <- 0 until DecodeWidth) {
36424519898SXuan Hu    mdpFlodPcVec(i) := Mux(
36524519898SXuan Hu      decode.io.out(i).fire,
36624519898SXuan Hu      decode.io.in(i).bits.foldpc,
36724519898SXuan Hu      rename.io.in(i).bits.foldpc
36824519898SXuan Hu    )
36924519898SXuan Hu  }
37024519898SXuan Hu
37124519898SXuan Hu  // currently, we only update mdp info when isReplay
37224519898SXuan Hu  memCtrl.io.redirect := s1_s3_redirect
37324519898SXuan Hu  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
37424519898SXuan Hu  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
37524519898SXuan Hu  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
37624519898SXuan Hu  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
37724519898SXuan Hu  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
37824519898SXuan Hu
37924519898SXuan Hu  rat.io.redirect := s1_s3_redirect.valid
3806b102a39SHaojin Tang  rat.io.rabCommits := rob.io.rabCommits
381cda1c534Sxiaofeibao-xjtu  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
38224519898SXuan Hu  rat.io.intRenamePorts := rename.io.intRenamePorts
38324519898SXuan Hu  rat.io.fpRenamePorts := rename.io.fpRenamePorts
38424519898SXuan Hu  rat.io.vecRenamePorts := rename.io.vecRenamePorts
38524519898SXuan Hu
38624519898SXuan Hu  rename.io.redirect := s1_s3_redirect
3876b102a39SHaojin Tang  rename.io.rabCommits := rob.io.rabCommits
38824519898SXuan Hu  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
38924519898SXuan Hu    RegEnable(waittable2rename, decodeOut.fire)
39024519898SXuan Hu  }
39124519898SXuan Hu  rename.io.ssit := memCtrl.io.ssit2Rename
39224519898SXuan Hu  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
39324519898SXuan Hu  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
39424519898SXuan Hu  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
395dcf3a679STang Haojin  rename.io.int_need_free := rat.io.int_need_free
396dcf3a679STang Haojin  rename.io.int_old_pdest := rat.io.int_old_pdest
397dcf3a679STang Haojin  rename.io.fp_old_pdest := rat.io.fp_old_pdest
3983cf50307SZiyue Zhang  rename.io.vec_old_pdest := rat.io.vec_old_pdest
399b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
400b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
401b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
402b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vconfig_rat.foreach(_ := rat.io.debug_vconfig_rat.get)
403d2b20d1aSTang Haojin  rename.io.stallReason.in <> decode.io.stallReason.out
404870f462dSXuan Hu  rename.io.snpt.snptEnq := DontCare
405870f462dSXuan Hu  rename.io.snpt.snptDeq := snpt.io.deq
406870f462dSXuan Hu  rename.io.snpt.useSnpt := useSnpt
407870f462dSXuan Hu  rename.io.snpt.snptSelect := snptSelect
408c3f16425Sxiaofeibao-xjtu  rename.io.robIsEmpty := rob.io.enq.isEmpty
409c4b56310SHaojin Tang  rename.io.snpt.flushVec := flushVecNext
410c4b56310SHaojin Tang  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
411c4b56310SHaojin Tang  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
412870f462dSXuan Hu
413870f462dSXuan Hu  val renameOut = Wire(chiselTypeOf(rename.io.out))
414870f462dSXuan Hu  renameOut <> rename.io.out
4159faa51afSxiaofeibao-xjtu  dispatch.io.fromRename <> renameOut
416c3f16425Sxiaofeibao-xjtu  renameOut.zip(dispatch.io.recv).map{case (rename,recv) => rename.ready := recv}
417c3f16425Sxiaofeibao-xjtu  dispatch.io.fromRenameIsFp := rename.io.toDispatchIsFp
418c3f16425Sxiaofeibao-xjtu  dispatch.io.fromRenameIsInt := rename.io.toDispatchIsInt
41924519898SXuan Hu  dispatch.io.hartId := io.fromTop.hartId
42024519898SXuan Hu  dispatch.io.redirect := s1_s3_redirect
42124519898SXuan Hu  dispatch.io.enqRob <> rob.io.enq
422d2b20d1aSTang Haojin  dispatch.io.robHead := rob.io.debugRobHead
423d2b20d1aSTang Haojin  dispatch.io.stallReason <> rename.io.stallReason.out
424d2b20d1aSTang Haojin  dispatch.io.lqCanAccept := io.lqCanAccept
425d2b20d1aSTang Haojin  dispatch.io.sqCanAccept := io.sqCanAccept
426d2b20d1aSTang Haojin  dispatch.io.robHeadNotReady := rob.io.headNotReady
427d2b20d1aSTang Haojin  dispatch.io.robFull := rob.io.robFull
42824519898SXuan Hu  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
42924519898SXuan Hu
43024519898SXuan Hu  intDq.io.enq <> dispatch.io.toIntDq
43124519898SXuan Hu  intDq.io.redirect <> s2_s4_redirect
43224519898SXuan Hu
43324519898SXuan Hu  fpDq.io.enq <> dispatch.io.toFpDq
43424519898SXuan Hu  fpDq.io.redirect <> s2_s4_redirect
43524519898SXuan Hu
43624519898SXuan Hu  lsDq.io.enq <> dispatch.io.toLsDq
43724519898SXuan Hu  lsDq.io.redirect <> s2_s4_redirect
43824519898SXuan Hu
43924519898SXuan Hu  io.toIssueBlock.intUops <> intDq.io.deq
44024519898SXuan Hu  io.toIssueBlock.vfUops  <> fpDq.io.deq
44124519898SXuan Hu  io.toIssueBlock.memUops <> lsDq.io.deq
44224519898SXuan Hu  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
44324519898SXuan Hu  io.toIssueBlock.flush   <> s2_s4_redirect
44424519898SXuan Hu
44524519898SXuan Hu  pcMem.io.wen.head   := RegNext(io.frontend.fromFtq.pc_mem_wen)
4463827c997SsinceforYy  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
4473827c997SsinceforYy  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
44824519898SXuan Hu
44924519898SXuan Hu  private val jumpPcVec         : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
45024519898SXuan Hu  io.toIssueBlock.pcVec := jumpPcVec
45124519898SXuan Hu
45224519898SXuan Hu  io.toDataPath.flush := s2_s4_redirect
45324519898SXuan Hu  io.toExuBlock.flush := s2_s4_redirect
45424519898SXuan Hu
45524519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("exu").zipWithIndex) {
45624519898SXuan Hu    pcMem.io.raddr(pcMemIdx) := intDq.io.deqNext(i).ftqPtr.value
45724519898SXuan Hu    jumpPcVec(i) := pcMem.io.rdata(pcMemIdx).getPc(RegNext(intDq.io.deqNext(i).ftqOffset))
45824519898SXuan Hu  }
45924519898SXuan Hu
46024519898SXuan Hu  val dqOuts = Seq(io.toIssueBlock.intUops) ++ Seq(io.toIssueBlock.vfUops) ++ Seq(io.toIssueBlock.memUops)
46124519898SXuan Hu  dqOuts.zipWithIndex.foreach { case (dqOut, dqIdx) =>
46224519898SXuan Hu    dqOut.map(_.bits.pc).zipWithIndex.map{ case (pc, portIdx) =>
46324519898SXuan Hu      if(params.allSchdParams(dqIdx).numPcReadPort > 0){
46424519898SXuan Hu        val realJumpPcVec = jumpPcVec.drop(params.allSchdParams.take(dqIdx).map(_.numPcReadPort).sum).take(params.allSchdParams(dqIdx).numPcReadPort)
46524519898SXuan Hu        pc := realJumpPcVec(portIdx)
46624519898SXuan Hu      }
46724519898SXuan Hu    }
46824519898SXuan Hu  }
46924519898SXuan Hu
47024519898SXuan Hu  rob.io.hartId := io.fromTop.hartId
47124519898SXuan Hu  rob.io.redirect := s1_s3_redirect
47224519898SXuan Hu  rob.io.writeback := delayedNotFlushedWriteBack
47385f51ecaSxiaofeibao-xjtu  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
47424519898SXuan Hu
47524519898SXuan Hu  io.redirect := s1_s3_redirect
47624519898SXuan Hu
47724519898SXuan Hu  // rob to int block
47824519898SXuan Hu  io.robio.csr <> rob.io.csr
47924519898SXuan Hu  // When wfi is disabled, it will not block ROB commit.
48024519898SXuan Hu  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
48124519898SXuan Hu  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
48224519898SXuan Hu
48324519898SXuan Hu  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
48424519898SXuan Hu
48524519898SXuan Hu  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
48624519898SXuan Hu  io.robio.exception := rob.io.exception
48724519898SXuan Hu  io.robio.exception.bits.pc := s1_robFlushPc
48824519898SXuan Hu
48924519898SXuan Hu  // rob to mem block
49024519898SXuan Hu  io.robio.lsq <> rob.io.lsq
49124519898SXuan Hu
492b7d9e8d5Sxiaofeibao-xjtu  io.debug_int_rat    .foreach(_ := rat.io.diff_int_rat.get)
493b7d9e8d5Sxiaofeibao-xjtu  io.debug_fp_rat     .foreach(_ := rat.io.diff_fp_rat.get)
494b7d9e8d5Sxiaofeibao-xjtu  io.debug_vec_rat    .foreach(_ := rat.io.diff_vec_rat.get)
495b7d9e8d5Sxiaofeibao-xjtu  io.debug_vconfig_rat.foreach(_ := rat.io.diff_vconfig_rat.get)
49624519898SXuan Hu
49717b21f45SHaojin Tang  rob.io.debug_ls := io.robio.debug_ls
49817b21f45SHaojin Tang  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
49917b21f45SHaojin Tang  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
5006ce10964SXuan Hu  rob.io.debugEnqLsq := io.debugEnqLsq
5016ce10964SXuan Hu
50217b21f45SHaojin Tang  io.robio.robDeqPtr := rob.io.robDeqPtr
5038744445eSMaxpicca-Li
50460ebee38STang Haojin  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
50560ebee38STang Haojin  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
50660ebee38STang Haojin  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
5077cf78eb2Shappy-lx  io.debugRolling := rob.io.debugRolling
50860ebee38STang Haojin
50924519898SXuan Hu  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
51024519898SXuan Hu  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
51124519898SXuan Hu  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
51224519898SXuan Hu  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
51324519898SXuan Hu
51424519898SXuan Hu  val pfevent = Module(new PFEvent)
51524519898SXuan Hu  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
51624519898SXuan Hu  val csrevents = pfevent.io.hpmevent.slice(8,16)
51724519898SXuan Hu
51824519898SXuan Hu  val perfinfo = IO(new Bundle(){
51924519898SXuan Hu    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
52024519898SXuan Hu    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
52124519898SXuan Hu    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
52224519898SXuan Hu  })
52324519898SXuan Hu
5249a128342SHaoyuan Feng  val perfFromUnits = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerfEvents)
5259a128342SHaoyuan Feng  val perfFromIO    = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++
5269a128342SHaoyuan Feng                        perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++
5279a128342SHaoyuan Feng                        perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value))
5289a128342SHaoyuan Feng  val perfBlock     = Seq()
5299a128342SHaoyuan Feng  // let index = 0 be no event
5309a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
5319a128342SHaoyuan Feng
5329a128342SHaoyuan Feng  if (printEventCoding) {
5339a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
5349a128342SHaoyuan Feng      println("CtrlBlock perfEvents Set", name, inc, i)
5359a128342SHaoyuan Feng    }
5369a128342SHaoyuan Feng  }
5379a128342SHaoyuan Feng
5389a128342SHaoyuan Feng  val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
5399a128342SHaoyuan Feng  val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
54024519898SXuan Hu  generatePerfEvent()
54124519898SXuan Hu}
54224519898SXuan Hu
54324519898SXuan Huclass CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
54424519898SXuan Hu  val fromTop = new Bundle {
54524519898SXuan Hu    val hartId = Input(UInt(8.W))
54624519898SXuan Hu  }
54724519898SXuan Hu  val toTop = new Bundle {
54824519898SXuan Hu    val cpuHalt = Output(Bool())
54924519898SXuan Hu  }
55024519898SXuan Hu  val frontend = Flipped(new FrontendToCtrlIO())
55124519898SXuan Hu  val toIssueBlock = new Bundle {
55224519898SXuan Hu    val flush = ValidIO(new Redirect)
55324519898SXuan Hu    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
55424519898SXuan Hu    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
55524519898SXuan Hu    val vfUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
55624519898SXuan Hu    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
55724519898SXuan Hu    val pcVec = Output(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
55824519898SXuan Hu  }
55924519898SXuan Hu  val fromDataPath = new Bundle{
56024519898SXuan Hu    val vtype = Input(new VType)
56124519898SXuan Hu  }
56224519898SXuan Hu  val toDataPath = new Bundle {
56324519898SXuan Hu    val vtypeAddr = Output(UInt(PhyRegIdxWidth.W))
56424519898SXuan Hu    val flush = ValidIO(new Redirect)
56524519898SXuan Hu  }
56624519898SXuan Hu  val toExuBlock = new Bundle {
56724519898SXuan Hu    val flush = ValidIO(new Redirect)
56824519898SXuan Hu  }
56924519898SXuan Hu  val fromWB = new Bundle {
57024519898SXuan Hu    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
57124519898SXuan Hu  }
57224519898SXuan Hu  val redirect = ValidIO(new Redirect)
57324519898SXuan Hu  val fromMem = new Bundle {
574272ec6b1SHaojin Tang    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
57524519898SXuan Hu    val violation = Flipped(ValidIO(new Redirect))
57624519898SXuan Hu  }
57724519898SXuan Hu  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
57883ba63b3SXuan Hu  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
579b133b458SXuan Hu  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
5804b0d80d8SXuan Hu
58124519898SXuan Hu  val csrCtrl = Input(new CustomCSRCtrlIO)
58224519898SXuan Hu  val robio = new Bundle {
58324519898SXuan Hu    val csr = new RobCSRIO
58424519898SXuan Hu    val exception = ValidIO(new ExceptionInfo)
58524519898SXuan Hu    val lsq = new RobLsqIO
5866810d1e8Ssfencevma    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
5872326221cSXuan Hu    val debug_ls = Input(new DebugLSIO())
58817b21f45SHaojin Tang    val robHeadLsIssue = Input(Bool())
58917b21f45SHaojin Tang    val robDeqPtr = Output(new RobPtr)
59024519898SXuan Hu  }
59124519898SXuan Hu
59224519898SXuan Hu  val perfInfo = Output(new Bundle{
59324519898SXuan Hu    val ctrlInfo = new Bundle {
59424519898SXuan Hu      val robFull   = Bool()
59524519898SXuan Hu      val intdqFull = Bool()
59624519898SXuan Hu      val fpdqFull  = Bool()
59724519898SXuan Hu      val lsdqFull  = Bool()
59824519898SXuan Hu    }
59924519898SXuan Hu  })
600b7d9e8d5Sxiaofeibao-xjtu  val debug_int_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
601b7d9e8d5Sxiaofeibao-xjtu  val debug_fp_rat      = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
602b7d9e8d5Sxiaofeibao-xjtu  val debug_vec_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
603b7d9e8d5Sxiaofeibao-xjtu  val debug_vconfig_rat = if (params.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None // TODO: use me
60424519898SXuan Hu
605c61abc0cSXuan Hu  val sqCanAccept = Input(Bool())
606c61abc0cSXuan Hu  val lqCanAccept = Input(Bool())
6074b0d80d8SXuan Hu
6084b0d80d8SXuan Hu  val debugTopDown = new Bundle {
6094b0d80d8SXuan Hu    val fromRob = new RobCoreTopDownIO
6104b0d80d8SXuan Hu    val fromCore = new CoreDispatchTopDownIO
6114b0d80d8SXuan Hu  }
6124b0d80d8SXuan Hu  val debugRolling = new RobDebugRollingIO
6136ce10964SXuan Hu  val debugEnqLsq = Input(new LsqEnqIO)
61424519898SXuan Hu}
61524519898SXuan Hu
61624519898SXuan Huclass NamedIndexes(namedCnt: Seq[(String, Int)]) {
61724519898SXuan Hu  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
61824519898SXuan Hu
61924519898SXuan Hu  val maxIdx = namedCnt.map(_._2).sum
62024519898SXuan Hu  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
62124519898SXuan Hu    val begin = namedCnt.slice(0, i).map(_._2).sum
62224519898SXuan Hu    val end = begin + namedCnt(i)._2
62324519898SXuan Hu    (namedCnt(i)._1, (begin, end))
62424519898SXuan Hu  }.toMap
62524519898SXuan Hu
62624519898SXuan Hu  def apply(name: String): Seq[Int] = {
62724519898SXuan Hu    require(nameRangeMap.contains(name))
62824519898SXuan Hu    nameRangeMap(name)._1 until nameRangeMap(name)._2
62924519898SXuan Hu  }
63024519898SXuan Hu}
631