xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 60f0c5ae701c71b6347e32d54543e5e7858f2038)
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}
3847c01b71Sxiaofeibao-xjtuimport xiangshan.backend.issue.{IntScheduler, VfScheduler, MemScheduler}
3924519898SXuan Hu
4024519898SXuan Huclass CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
4124519898SXuan Hu  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
4224519898SXuan Hu  val redirect = Valid(new Redirect)
439342624fSGao-Zeyu  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
449342624fSGao-Zeyu  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
4524519898SXuan Hu}
4624519898SXuan Hu
4724519898SXuan Huclass CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
481ca4a39dSXuan Hu  override def shouldBeInlined: Boolean = false
491ca4a39dSXuan Hu
5024519898SXuan Hu  val rob = LazyModule(new Rob(params))
5124519898SXuan Hu
5224519898SXuan Hu  lazy val module = new CtrlBlockImp(this)(p, params)
5324519898SXuan Hu
546f483f86SXuan Hu  val gpaMem = LazyModule(new GPAMem())
5524519898SXuan Hu}
5624519898SXuan Hu
5724519898SXuan Huclass CtrlBlockImp(
5824519898SXuan Hu  override val wrapper: CtrlBlock
5924519898SXuan Hu)(implicit
6024519898SXuan Hu  p: Parameters,
6124519898SXuan Hu  params: BackendParams
6224519898SXuan Hu) extends LazyModuleImp(wrapper)
6324519898SXuan Hu  with HasXSParameter
6424519898SXuan Hu  with HasCircularQueuePtrHelper
6524519898SXuan Hu  with HasPerfEvents
6624519898SXuan Hu{
6724519898SXuan Hu  val pcMemRdIndexes = new NamedIndexes(Seq(
6824519898SXuan Hu    "redirect"  -> 1,
6924519898SXuan Hu    "memPred"   -> 1,
7024519898SXuan Hu    "robFlush"  -> 1,
7124519898SXuan Hu    "load"      -> params.LduCnt,
72b133b458SXuan Hu    "hybrid"    -> params.HyuCnt,
7383ba63b3SXuan Hu    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0)
7424519898SXuan Hu  ))
7524519898SXuan Hu
7624519898SXuan Hu  private val numPcMemReadForExu = params.numPcReadPort
7724519898SXuan Hu  private val numPcMemRead = pcMemRdIndexes.maxIdx
7824519898SXuan Hu
7929dbac5aSsinsanction  // now pcMem read for exu is moved to PcTargetMem (OG0)
8024519898SXuan Hu  println(s"pcMem read num: $numPcMemRead")
8124519898SXuan Hu  println(s"pcMem read num for exu: $numPcMemReadForExu")
8224519898SXuan Hu
8324519898SXuan Hu  val io = IO(new CtrlBlockIO())
8424519898SXuan Hu
856f483f86SXuan Hu  val gpaMem = wrapper.gpaMem.module
8624519898SXuan Hu  val decode = Module(new DecodeStage)
8724519898SXuan Hu  val fusionDecoder = Module(new FusionDecoder)
8824519898SXuan Hu  val rat = Module(new RenameTableWrapper)
8924519898SXuan Hu  val rename = Module(new Rename)
9024519898SXuan Hu  val dispatch = Module(new Dispatch)
91c1e19666Sxiaofeibao-xjtu  val intDq0 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 0))
92c1e19666Sxiaofeibao-xjtu  val intDq1 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 1))
93*60f0c5aeSxiaofeibao  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth))
94*60f0c5aeSxiaofeibao  val vecDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth))
9524519898SXuan Hu  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
9624519898SXuan Hu  val redirectGen = Module(new RedirectGenerator)
979477429fSsinceforYy  private def hasRen: Boolean = true
989477429fSsinceforYy  private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC", hasRen = hasRen))
9924519898SXuan Hu  private val rob = wrapper.rob.module
10024519898SXuan Hu  private val memCtrl = Module(new MemCtrl(params))
10124519898SXuan Hu
10224519898SXuan Hu  private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
10324519898SXuan Hu
10424519898SXuan Hu  private val s0_robFlushRedirect = rob.io.flushOut
10524519898SXuan Hu  private val s1_robFlushRedirect = Wire(Valid(new Redirect))
1065f8b6c9eSsinceforYy  s1_robFlushRedirect.valid := GatedValidRegNext(s0_robFlushRedirect.valid, false.B)
10724519898SXuan Hu  s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid)
10824519898SXuan Hu
1099477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.valid
11024519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value
111b1e92023SsinceforYy  private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).getPc(RegEnable(s0_robFlushRedirect.bits.ftqOffset, s0_robFlushRedirect.valid))
11224519898SXuan Hu  private val s3_redirectGen = redirectGen.io.stage2Redirect
11324519898SXuan Hu  private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen)
11424519898SXuan Hu  private val s2_s4_pendingRedirectValid = RegInit(false.B)
11524519898SXuan Hu  when (s1_s3_redirect.valid) {
11624519898SXuan Hu    s2_s4_pendingRedirectValid := true.B
1175f8b6c9eSsinceforYy  }.elsewhen (GatedValidRegNext(io.frontend.toFtq.redirect.valid)) {
11824519898SXuan Hu    s2_s4_pendingRedirectValid := false.B
11924519898SXuan Hu  }
12024519898SXuan Hu
12124519898SXuan Hu  // Redirect will be RegNext at ExuBlocks and IssueBlocks
12224519898SXuan Hu  val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect)
12324519898SXuan Hu  val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect)
12424519898SXuan Hu
12524519898SXuan Hu  private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => {
12624519898SXuan Hu    val valid = x.valid
12724519898SXuan Hu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
12824519898SXuan Hu    val delayed = Wire(Valid(new ExuOutput(x.bits.params)))
1295f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
13024519898SXuan Hu    delayed.bits := RegEnable(x.bits, x.valid)
13196e858baSXuan Hu    delayed.bits.debugInfo.writebackTime := GTimer()
13224519898SXuan Hu    delayed
13383ba63b3SXuan Hu  }).toSeq
13424519898SXuan Hu
13585f51ecaSxiaofeibao-xjtu  val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu)
13647c01b71Sxiaofeibao-xjtu  val intScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[IntScheduler])
13747c01b71Sxiaofeibao-xjtu  val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler])
13847c01b71Sxiaofeibao-xjtu  val writeFpVecWbData = io.fromWB.wbData.filter(x => x.bits.params.writeFpRf || x.bits.params.writeVecRf)
13947c01b71Sxiaofeibao-xjtu  val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu)
14085f51ecaSxiaofeibao-xjtu  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
14185f51ecaSxiaofeibao-xjtu    val valid = x.valid
14285f51ecaSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
14385f51ecaSxiaofeibao-xjtu    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
1445f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
14547c01b71Sxiaofeibao-xjtu    val isIntSche = intScheWbData.contains(x)
14647c01b71Sxiaofeibao-xjtu    val isVfSche = vfScheWbData.contains(x)
14747c01b71Sxiaofeibao-xjtu    val isMemVload = memVloadWbData.contains(x)
148e031d9a7Sxiaofeibao-xjtu    val canSameRobidxWbData = if (isIntSche || isVfSche) {
149e031d9a7Sxiaofeibao-xjtu      intScheWbData ++ vfScheWbData
15047c01b71Sxiaofeibao-xjtu    } else if (isMemVload) {
15147c01b71Sxiaofeibao-xjtu      memVloadWbData
15247c01b71Sxiaofeibao-xjtu    } else {
15347c01b71Sxiaofeibao-xjtu      Seq(x)
15447c01b71Sxiaofeibao-xjtu    }
15547c01b71Sxiaofeibao-xjtu    val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => {
15685f51ecaSxiaofeibao-xjtu      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
15785f51ecaSxiaofeibao-xjtu      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
15885f51ecaSxiaofeibao-xjtu    }).toSeq)
15941dbbdfdSsinceforYy    delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid)
16085f51ecaSxiaofeibao-xjtu    delayed
16185f51ecaSxiaofeibao-xjtu  }).toSeq
16285f51ecaSxiaofeibao-xjtu
16324519898SXuan Hu  private val exuPredecode = VecInit(
16483ba63b3SXuan Hu    delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
16524519898SXuan Hu  )
16624519898SXuan Hu
16783ba63b3SXuan Hu  private val exuRedirects: Seq[ValidIO[Redirect]] = delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => {
16824519898SXuan Hu    val out = Wire(Valid(new Redirect()))
16924519898SXuan Hu    out.valid := x.valid && x.bits.redirect.get.valid && x.bits.redirect.get.bits.cfiUpdate.isMisPred
17024519898SXuan Hu    out.bits := x.bits.redirect.get.bits
171a63155a6SXuan Hu    out.bits.debugIsCtrl := true.B
172a63155a6SXuan Hu    out.bits.debugIsMemVio := false.B
17324519898SXuan Hu    out
17483ba63b3SXuan Hu  }).toSeq
17524519898SXuan Hu
17624519898SXuan Hu  private val memViolation = io.fromMem.violation
17724519898SXuan Hu  val loadReplay = Wire(ValidIO(new Redirect))
1785f8b6c9eSsinceforYy  loadReplay.valid := GatedValidRegNext(memViolation.valid &&
17924519898SXuan Hu    !memViolation.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
18024519898SXuan Hu  )
18124519898SXuan Hu  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
182a63155a6SXuan Hu  loadReplay.bits.debugIsCtrl := false.B
183a63155a6SXuan Hu  loadReplay.bits.debugIsMemVio := true.B
18424519898SXuan Hu
1859477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.vld
18624519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.ptr.value
1875f8b6c9eSsinceforYy  redirectGen.io.redirectPcRead.data := pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(redirectGen.io.redirectPcRead.offset, redirectGen.io.redirectPcRead.vld))
1889477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.vld
18924519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.ptr.value
1905f8b6c9eSsinceforYy  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegEnable(redirectGen.io.memPredPcRead.offset, redirectGen.io.memPredPcRead.vld))
19124519898SXuan Hu
19224519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
1938241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
1949477429fSsinceforYy    pcMem.io.ren.get(pcMemIdx) := io.memLdPcRead(i).vld
19524519898SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
1965f8b6c9eSsinceforYy    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memLdPcRead(i).offset, io.memLdPcRead(i).vld))
19724519898SXuan Hu  }
19824519898SXuan Hu
199b133b458SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
2008241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
2019477429fSsinceforYy    pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).vld
202b133b458SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
2035f8b6c9eSsinceforYy    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).vld))
204b133b458SXuan Hu  }
205b133b458SXuan Hu
2064b0d80d8SXuan Hu  if (EnableStorePrefetchSMS) {
2074b0d80d8SXuan Hu    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
2089477429fSsinceforYy      pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).vld
2094b0d80d8SXuan Hu      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
2105f8b6c9eSsinceforYy      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).vld))
2114b0d80d8SXuan Hu    }
2124b0d80d8SXuan Hu  } else {
21383ba63b3SXuan Hu    io.memStPcRead.foreach(_.data := 0.U)
2144b0d80d8SXuan Hu  }
2154b0d80d8SXuan Hu
21624519898SXuan Hu  redirectGen.io.hartId := io.fromTop.hartId
21783ba63b3SXuan Hu  redirectGen.io.exuRedirect := exuRedirects.toSeq
2184b0d80d8SXuan Hu  redirectGen.io.exuOutPredecode := exuPredecode // guarded by exuRedirect.valid
21924519898SXuan Hu  redirectGen.io.loadReplay <> loadReplay
22024519898SXuan Hu
22124519898SXuan Hu  redirectGen.io.robFlush := s1_robFlushRedirect.valid
22224519898SXuan Hu
223ff7f931dSXuan Hu  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
2245f8b6c9eSsinceforYy  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
22524519898SXuan Hu  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
22624519898SXuan Hu  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
22724519898SXuan Hu  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
22824519898SXuan Hu  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
22924519898SXuan Hu  for (i <- 0 until CommitWidth) {
23024519898SXuan Hu    // why flushOut: instructions with flushPipe are not commited to frontend
23124519898SXuan Hu    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
23224519898SXuan Hu    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
2335f8b6c9eSsinceforYy    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
23424519898SXuan Hu    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
23524519898SXuan Hu  }
236ff7f931dSXuan Hu  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
237ff7f931dSXuan Hu  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
238ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
239ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
2409342624fSGao-Zeyu
2419342624fSGao-Zeyu  //jmp/brh
2429342624fSGao-Zeyu  for (i <- 0 until NumRedirect) {
243ff7f931dSXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirects(i).valid && exuRedirects(i).bits.cfiUpdate.isMisPred && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2446ce10964SXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirects(i).bits.ftqIdx
2459342624fSGao-Zeyu  }
2469342624fSGao-Zeyu  //loadreplay
247ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2489342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
2499342624fSGao-Zeyu  //exception
250ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
2519342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
25205cc2a4eSXuan Hu
25305cc2a4eSXuan Hu  io.frontend.canAccept := decode.io.canAccept
25405cc2a4eSXuan Hu
25524519898SXuan Hu  // Be careful here:
25624519898SXuan Hu  // T0: rob.io.flushOut, s0_robFlushRedirect
25724519898SXuan Hu  // T1: s1_robFlushRedirect, rob.io.exception.valid
25824519898SXuan Hu  // T2: csr.redirect.valid
25924519898SXuan Hu  // T3: csr.exception.valid
26024519898SXuan Hu  // T4: csr.trapTarget
26124519898SXuan Hu  // T5: ctrlBlock.trapTarget
26224519898SXuan Hu  // T6: io.frontend.toFtq.stage2Redirect.valid
26324519898SXuan Hu  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
26424519898SXuan Hu    s1_robFlushPc, // replay inst
265870f462dSXuan Hu    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
26624519898SXuan Hu  ), s1_robFlushRedirect.valid)
26724519898SXuan Hu  private val s2_csrIsXRet = io.robio.csr.isXRet
26824519898SXuan Hu  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
26924519898SXuan Hu  private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget
27024519898SXuan Hu
27124519898SXuan Hu  val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc)
272ff7f931dSXuan Hu  when (s6_flushFromRobValid) {
27324519898SXuan Hu    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
27474f21f21SsinceforYy    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
27524519898SXuan Hu  }
27624519898SXuan Hu
2776f483f86SXuan Hu  for (i <- 0 until DecodeWidth) {
2786f483f86SXuan Hu    gpaMem.io.fromIFU := io.frontend.fromIfu
2796f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
2806f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
2816f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
2826f483f86SXuan Hu  }
2836f483f86SXuan Hu
28424519898SXuan Hu  // vtype commit
28586727929Ssinsanction  decode.io.isResumeVType := rob.io.toDecode.isResumeVType
28681535d7bSsinsanction  decode.io.commitVType := rob.io.toDecode.commitVType
28781535d7bSsinsanction  decode.io.walkVType := rob.io.toDecode.walkVType
28824519898SXuan Hu
289e25c13faSXuan Hu  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
29024519898SXuan Hu
29124519898SXuan Hu  decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) =>
29224519898SXuan Hu    decodeIn.valid := frontendCf.valid
29324519898SXuan Hu    frontendCf.ready := decodeIn.ready
29424519898SXuan Hu    decodeIn.bits.connectCtrlFlow(frontendCf.bits)
29524519898SXuan Hu  }
29624519898SXuan Hu  decode.io.csrCtrl := RegNext(io.csrCtrl)
29724519898SXuan Hu  decode.io.intRat <> rat.io.intReadPorts
29824519898SXuan Hu  decode.io.fpRat <> rat.io.fpReadPorts
29924519898SXuan Hu  decode.io.vecRat <> rat.io.vecReadPorts
30024519898SXuan Hu  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
301870f462dSXuan Hu  decode.io.stallReason.in <> io.frontend.stallReason
30224519898SXuan Hu
303fa7f2c26STang Haojin  // snapshot check
304c4b56310SHaojin Tang  class CFIRobIdx extends Bundle {
305c4b56310SHaojin Tang    val robIdx = Vec(RenameWidth, new RobPtr)
306c4b56310SHaojin Tang    val isCFI = Vec(RenameWidth, Bool())
307c4b56310SHaojin Tang  }
308c4b56310SHaojin Tang  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
309c4b56310SHaojin Tang  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
310c4b56310SHaojin Tang  snpt.io.enq := genSnapshot
311c4b56310SHaojin Tang  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
312c4b56310SHaojin Tang  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
313fa7f2c26STang Haojin  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
314c4b56310SHaojin 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
315c4b56310SHaojin Tang  snpt.io.redirect := s1_s3_redirect.valid
316c4b56310SHaojin Tang  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
317c4b56310SHaojin Tang    val notCFIMask = snapshot.isCFI.map(~_)
31837d77575SzhanglyGit    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
31937d77575SzhanglyGit    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
32037d77575SzhanglyGit    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
321c4b56310SHaojin Tang  })
322a6742963SHaojin Tang  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
323c4b56310SHaojin Tang  snpt.io.flushVec := flushVecNext
324fa7f2c26STang Haojin
325fa7f2c26STang Haojin  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
326780712aaSxiaofeibao-xjtu    snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
327780712aaSxiaofeibao-xjtu      !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head)
328c61abc0cSXuan Hu  ).reduceTree(_ || _)
329c61abc0cSXuan Hu  val snptSelect = MuxCase(
330c61abc0cSXuan Hu    0.U(log2Ceil(RenameSnapshotNum).W),
331fa7f2c26STang Haojin    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
332780712aaSxiaofeibao-xjtu      (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
333780712aaSxiaofeibao-xjtu        !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx)
334c61abc0cSXuan Hu    )
335c61abc0cSXuan Hu  )
336fa7f2c26STang Haojin
337fa7f2c26STang Haojin  rob.io.snpt.snptEnq := DontCare
338fa7f2c26STang Haojin  rob.io.snpt.snptDeq := snpt.io.deq
339fa7f2c26STang Haojin  rob.io.snpt.useSnpt := useSnpt
340fa7f2c26STang Haojin  rob.io.snpt.snptSelect := snptSelect
341c4b56310SHaojin Tang  rob.io.snpt.flushVec := flushVecNext
342c4b56310SHaojin Tang  rat.io.snpt.snptEnq := genSnapshot
343fa7f2c26STang Haojin  rat.io.snpt.snptDeq := snpt.io.deq
344fa7f2c26STang Haojin  rat.io.snpt.useSnpt := useSnpt
345fa7f2c26STang Haojin  rat.io.snpt.snptSelect := snptSelect
346c4b56310SHaojin Tang  rat.io.snpt.flushVec := flushVec
347fa7f2c26STang Haojin
34824519898SXuan Hu  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
34924519898SXuan Hu  // fusion decoder
35024519898SXuan Hu  for (i <- 0 until DecodeWidth) {
35124519898SXuan Hu    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
35224519898SXuan Hu    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
35324519898SXuan Hu    if (i > 0) {
35424519898SXuan Hu      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
35524519898SXuan Hu    }
35624519898SXuan Hu  }
35724519898SXuan Hu
35824519898SXuan Hu  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
35924519898SXuan Hu
36024519898SXuan Hu  for (i <- 0 until RenameWidth) {
36124519898SXuan Hu    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
36224519898SXuan Hu      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
36324519898SXuan Hu
36424519898SXuan Hu    decodePipeRename(i).ready := rename.io.in(i).ready
36524519898SXuan Hu    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
36624519898SXuan Hu    rename.io.in(i).bits := decodePipeRename(i).bits
36724519898SXuan Hu  }
36824519898SXuan Hu
36924519898SXuan Hu  for (i <- 0 until RenameWidth - 1) {
37024519898SXuan Hu    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
37124519898SXuan Hu    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
37224519898SXuan Hu
37324519898SXuan Hu    // update the first RenameWidth - 1 instructions
37424519898SXuan Hu    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
37524519898SXuan Hu    when (fusionDecoder.io.out(i).valid) {
37624519898SXuan Hu      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
37724519898SXuan Hu      // TODO: remove this dirty code for ftq update
37824519898SXuan Hu      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
37924519898SXuan Hu      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
38024519898SXuan Hu      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
38124519898SXuan Hu      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
38224519898SXuan Hu      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
38324519898SXuan Hu      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
38424519898SXuan Hu      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
38524519898SXuan Hu      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
38624519898SXuan Hu      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
38724519898SXuan Hu      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
38824519898SXuan Hu    }
38924519898SXuan Hu
39024519898SXuan Hu  }
39124519898SXuan Hu
39224519898SXuan Hu  // memory dependency predict
39324519898SXuan Hu  // when decode, send fold pc to mdp
3949477429fSsinceforYy  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
39524519898SXuan Hu  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
39624519898SXuan Hu  for (i <- 0 until DecodeWidth) {
3979477429fSsinceforYy    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
39824519898SXuan Hu    mdpFlodPcVec(i) := Mux(
39924519898SXuan Hu      decode.io.out(i).fire,
40024519898SXuan Hu      decode.io.in(i).bits.foldpc,
40124519898SXuan Hu      rename.io.in(i).bits.foldpc
40224519898SXuan Hu    )
40324519898SXuan Hu  }
40424519898SXuan Hu
40524519898SXuan Hu  // currently, we only update mdp info when isReplay
40624519898SXuan Hu  memCtrl.io.redirect := s1_s3_redirect
40724519898SXuan Hu  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
40824519898SXuan Hu  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
40924519898SXuan Hu  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
4109477429fSsinceforYy  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
41124519898SXuan Hu  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
41224519898SXuan Hu  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
41324519898SXuan Hu
41424519898SXuan Hu  rat.io.redirect := s1_s3_redirect.valid
4156b102a39SHaojin Tang  rat.io.rabCommits := rob.io.rabCommits
416cda1c534Sxiaofeibao-xjtu  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
41724519898SXuan Hu  rat.io.intRenamePorts := rename.io.intRenamePorts
41824519898SXuan Hu  rat.io.fpRenamePorts := rename.io.fpRenamePorts
41924519898SXuan Hu  rat.io.vecRenamePorts := rename.io.vecRenamePorts
42024519898SXuan Hu
42124519898SXuan Hu  rename.io.redirect := s1_s3_redirect
4226b102a39SHaojin Tang  rename.io.rabCommits := rob.io.rabCommits
42324519898SXuan Hu  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
42424519898SXuan Hu    RegEnable(waittable2rename, decodeOut.fire)
42524519898SXuan Hu  }
42624519898SXuan Hu  rename.io.ssit := memCtrl.io.ssit2Rename
42724519898SXuan Hu  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
42824519898SXuan Hu  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
42924519898SXuan Hu  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
430dcf3a679STang Haojin  rename.io.int_need_free := rat.io.int_need_free
431dcf3a679STang Haojin  rename.io.int_old_pdest := rat.io.int_old_pdest
432dcf3a679STang Haojin  rename.io.fp_old_pdest := rat.io.fp_old_pdest
4333cf50307SZiyue Zhang  rename.io.vec_old_pdest := rat.io.vec_old_pdest
434b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
435b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
436b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
437b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vconfig_rat.foreach(_ := rat.io.debug_vconfig_rat.get)
438d2b20d1aSTang Haojin  rename.io.stallReason.in <> decode.io.stallReason.out
439870f462dSXuan Hu  rename.io.snpt.snptEnq := DontCare
440870f462dSXuan Hu  rename.io.snpt.snptDeq := snpt.io.deq
441870f462dSXuan Hu  rename.io.snpt.useSnpt := useSnpt
442870f462dSXuan Hu  rename.io.snpt.snptSelect := snptSelect
443bb7e6e3aSxiaofeibao-xjtu  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
444c4b56310SHaojin Tang  rename.io.snpt.flushVec := flushVecNext
445c4b56310SHaojin Tang  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
446c4b56310SHaojin Tang  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
447870f462dSXuan Hu
448870f462dSXuan Hu  val renameOut = Wire(chiselTypeOf(rename.io.out))
449870f462dSXuan Hu  renameOut <> rename.io.out
450ac78003fSzhanglyGit  // pass all snapshot in the first element for correctness of blockBackward
451ac78003fSzhanglyGit  renameOut.tail.foreach(_.bits.snapshot := false.B)
452ac78003fSzhanglyGit  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
453ac78003fSzhanglyGit    false.B,
454ac78003fSzhanglyGit    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
455ac78003fSzhanglyGit  )
456ac78003fSzhanglyGit
457ac78003fSzhanglyGit
458ac78003fSzhanglyGit  // pipeline between rename and dispatch
459ac78003fSzhanglyGit   for (i <- 0 until RenameWidth) {
460ac78003fSzhanglyGit     PipelineConnect(renameOut(i), dispatch.io.fromRename(i), dispatch.io.recv(i), s1_s3_redirect.valid)
461ac78003fSzhanglyGit   }
462ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.IQValidNumVec := io.IQValidNumVec
463ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num
464ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num
465ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num
466ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num
467ff3fcdf1Sxiaofeibao-xjtu
46824519898SXuan Hu  dispatch.io.hartId := io.fromTop.hartId
46924519898SXuan Hu  dispatch.io.redirect := s1_s3_redirect
47024519898SXuan Hu  dispatch.io.enqRob <> rob.io.enq
471d2b20d1aSTang Haojin  dispatch.io.robHead := rob.io.debugRobHead
472d2b20d1aSTang Haojin  dispatch.io.stallReason <> rename.io.stallReason.out
473d2b20d1aSTang Haojin  dispatch.io.lqCanAccept := io.lqCanAccept
474d2b20d1aSTang Haojin  dispatch.io.sqCanAccept := io.sqCanAccept
475d2b20d1aSTang Haojin  dispatch.io.robHeadNotReady := rob.io.headNotReady
476d2b20d1aSTang Haojin  dispatch.io.robFull := rob.io.robFull
4775f8b6c9eSsinceforYy  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
47824519898SXuan Hu
479ff3fcdf1Sxiaofeibao-xjtu  intDq0.io.enq <> dispatch.io.toIntDq0
480ff3fcdf1Sxiaofeibao-xjtu  intDq0.io.redirect <> s2_s4_redirect
481ff3fcdf1Sxiaofeibao-xjtu  intDq1.io.enq <> dispatch.io.toIntDq1
482ff3fcdf1Sxiaofeibao-xjtu  intDq1.io.redirect <> s2_s4_redirect
48324519898SXuan Hu
48424519898SXuan Hu  fpDq.io.enq <> dispatch.io.toFpDq
48524519898SXuan Hu  fpDq.io.redirect <> s2_s4_redirect
48624519898SXuan Hu
487*60f0c5aeSxiaofeibao  vecDq.io.enq <> dispatch.io.toVecDq
488*60f0c5aeSxiaofeibao  vecDq.io.redirect <> s2_s4_redirect
489*60f0c5aeSxiaofeibao
49024519898SXuan Hu  lsDq.io.enq <> dispatch.io.toLsDq
49124519898SXuan Hu  lsDq.io.redirect <> s2_s4_redirect
49224519898SXuan Hu
493ff3fcdf1Sxiaofeibao-xjtu  io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq)
494*60f0c5aeSxiaofeibao  io.toIssueBlock.fpUops <> fpDq.io.deq
495*60f0c5aeSxiaofeibao  io.toIssueBlock.vfUops  <> vecDq.io.deq
49624519898SXuan Hu  io.toIssueBlock.memUops <> lsDq.io.deq
49724519898SXuan Hu  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
49824519898SXuan Hu  io.toIssueBlock.flush   <> s2_s4_redirect
49924519898SXuan Hu
5005f8b6c9eSsinceforYy  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
5013827c997SsinceforYy  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
5023827c997SsinceforYy  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
50324519898SXuan Hu
50424519898SXuan Hu  io.toDataPath.flush := s2_s4_redirect
50524519898SXuan Hu  io.toExuBlock.flush := s2_s4_redirect
50624519898SXuan Hu
50724519898SXuan Hu
50824519898SXuan Hu  rob.io.hartId := io.fromTop.hartId
50924519898SXuan Hu  rob.io.redirect := s1_s3_redirect
51024519898SXuan Hu  rob.io.writeback := delayedNotFlushedWriteBack
51185f51ecaSxiaofeibao-xjtu  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
5126f483f86SXuan Hu  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
51324519898SXuan Hu
51424519898SXuan Hu  io.redirect := s1_s3_redirect
51524519898SXuan Hu
51624519898SXuan Hu  // rob to int block
51724519898SXuan Hu  io.robio.csr <> rob.io.csr
51824519898SXuan Hu  // When wfi is disabled, it will not block ROB commit.
51924519898SXuan Hu  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
52024519898SXuan Hu  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
52124519898SXuan Hu
52224519898SXuan Hu  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
52324519898SXuan Hu
52424519898SXuan Hu  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
52524519898SXuan Hu  io.robio.exception := rob.io.exception
52624519898SXuan Hu  io.robio.exception.bits.pc := s1_robFlushPc
52724519898SXuan Hu
52824519898SXuan Hu  // rob to mem block
52924519898SXuan Hu  io.robio.lsq <> rob.io.lsq
53024519898SXuan Hu
531b7d9e8d5Sxiaofeibao-xjtu  io.debug_int_rat    .foreach(_ := rat.io.diff_int_rat.get)
532b7d9e8d5Sxiaofeibao-xjtu  io.debug_fp_rat     .foreach(_ := rat.io.diff_fp_rat.get)
533b7d9e8d5Sxiaofeibao-xjtu  io.debug_vec_rat    .foreach(_ := rat.io.diff_vec_rat.get)
534b7d9e8d5Sxiaofeibao-xjtu  io.debug_vconfig_rat.foreach(_ := rat.io.diff_vconfig_rat.get)
53524519898SXuan Hu
53617b21f45SHaojin Tang  rob.io.debug_ls := io.robio.debug_ls
53717b21f45SHaojin Tang  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
53817b21f45SHaojin Tang  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
5396ce10964SXuan Hu  rob.io.debugEnqLsq := io.debugEnqLsq
5406ce10964SXuan Hu
54117b21f45SHaojin Tang  io.robio.robDeqPtr := rob.io.robDeqPtr
5428744445eSMaxpicca-Li
5437e4f0b19SZiyue-Zhang  // rob to backend
5447e4f0b19SZiyue-Zhang  io.robio.commitVType := rob.io.toDecode.commitVType
5457e4f0b19SZiyue-Zhang  // exu block to decode
5467e4f0b19SZiyue-Zhang  decode.io.vsetvlVType := io.robio.vsetvlVType
5477e4f0b19SZiyue-Zhang
54860ebee38STang Haojin  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
54960ebee38STang Haojin  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
55060ebee38STang Haojin  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
5517cf78eb2Shappy-lx  io.debugRolling := rob.io.debugRolling
55260ebee38STang Haojin
5535f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
5545f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull)
555*60f0c5aeSxiaofeibao  io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull)
5565f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull)
55724519898SXuan Hu
55824519898SXuan Hu  val pfevent = Module(new PFEvent)
55924519898SXuan Hu  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
56024519898SXuan Hu  val csrevents = pfevent.io.hpmevent.slice(8,16)
56124519898SXuan Hu
56224519898SXuan Hu  val perfinfo = IO(new Bundle(){
56324519898SXuan Hu    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
56424519898SXuan Hu    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
56524519898SXuan Hu    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
56624519898SXuan Hu  })
56724519898SXuan Hu
568*60f0c5aeSxiaofeibao  val perfFromUnits = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents)
5699a128342SHaoyuan Feng  val perfFromIO    = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++
5709a128342SHaoyuan Feng                        perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++
5719a128342SHaoyuan Feng                        perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value))
5729a128342SHaoyuan Feng  val perfBlock     = Seq()
5739a128342SHaoyuan Feng  // let index = 0 be no event
5749a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
5759a128342SHaoyuan Feng
5769a128342SHaoyuan Feng  if (printEventCoding) {
5779a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
5789a128342SHaoyuan Feng      println("CtrlBlock perfEvents Set", name, inc, i)
5799a128342SHaoyuan Feng    }
5809a128342SHaoyuan Feng  }
5819a128342SHaoyuan Feng
5829a128342SHaoyuan Feng  val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
5839a128342SHaoyuan Feng  val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
58424519898SXuan Hu  generatePerfEvent()
58524519898SXuan Hu}
58624519898SXuan Hu
58724519898SXuan Huclass CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
58824519898SXuan Hu  val fromTop = new Bundle {
58924519898SXuan Hu    val hartId = Input(UInt(8.W))
59024519898SXuan Hu  }
59124519898SXuan Hu  val toTop = new Bundle {
59224519898SXuan Hu    val cpuHalt = Output(Bool())
59324519898SXuan Hu  }
59424519898SXuan Hu  val frontend = Flipped(new FrontendToCtrlIO())
59524519898SXuan Hu  val toIssueBlock = new Bundle {
59624519898SXuan Hu    val flush = ValidIO(new Redirect)
59724519898SXuan Hu    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
59824519898SXuan Hu    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
599*60f0c5aeSxiaofeibao    val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst))
600*60f0c5aeSxiaofeibao    val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
60124519898SXuan Hu    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
60224519898SXuan Hu  }
60324519898SXuan Hu  val toDataPath = new Bundle {
60424519898SXuan Hu    val flush = ValidIO(new Redirect)
60524519898SXuan Hu  }
60624519898SXuan Hu  val toExuBlock = new Bundle {
60724519898SXuan Hu    val flush = ValidIO(new Redirect)
60824519898SXuan Hu  }
609c1e19666Sxiaofeibao-xjtu  val IQValidNumVec = Input(MixedVec(params.genIQValidNumBundle))
61024519898SXuan Hu  val fromWB = new Bundle {
61124519898SXuan Hu    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
61224519898SXuan Hu  }
61324519898SXuan Hu  val redirect = ValidIO(new Redirect)
61424519898SXuan Hu  val fromMem = new Bundle {
615272ec6b1SHaojin Tang    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
61624519898SXuan Hu    val violation = Flipped(ValidIO(new Redirect))
61724519898SXuan Hu  }
61824519898SXuan Hu  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
61983ba63b3SXuan Hu  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
620b133b458SXuan Hu  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
6214b0d80d8SXuan Hu
62224519898SXuan Hu  val csrCtrl = Input(new CustomCSRCtrlIO)
62324519898SXuan Hu  val robio = new Bundle {
62424519898SXuan Hu    val csr = new RobCSRIO
62524519898SXuan Hu    val exception = ValidIO(new ExceptionInfo)
62624519898SXuan Hu    val lsq = new RobLsqIO
6276810d1e8Ssfencevma    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
6282326221cSXuan Hu    val debug_ls = Input(new DebugLSIO())
62917b21f45SHaojin Tang    val robHeadLsIssue = Input(Bool())
63017b21f45SHaojin Tang    val robDeqPtr = Output(new RobPtr)
6317e4f0b19SZiyue-Zhang    val vsetvlVType = Input(VType())
6327e4f0b19SZiyue-Zhang    val commitVType = new Bundle {
6337e4f0b19SZiyue-Zhang      val vtype = Output(ValidIO(VType()))
6347e4f0b19SZiyue-Zhang      val hasVsetvl = Output(Bool())
6357e4f0b19SZiyue-Zhang    }
63624519898SXuan Hu  }
63724519898SXuan Hu
63824519898SXuan Hu  val perfInfo = Output(new Bundle{
63924519898SXuan Hu    val ctrlInfo = new Bundle {
64024519898SXuan Hu      val robFull   = Bool()
64124519898SXuan Hu      val intdqFull = Bool()
64224519898SXuan Hu      val fpdqFull  = Bool()
64324519898SXuan Hu      val lsdqFull  = Bool()
64424519898SXuan Hu    }
64524519898SXuan Hu  })
646b7d9e8d5Sxiaofeibao-xjtu  val debug_int_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
647b7d9e8d5Sxiaofeibao-xjtu  val debug_fp_rat      = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
648b7d9e8d5Sxiaofeibao-xjtu  val debug_vec_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
649b7d9e8d5Sxiaofeibao-xjtu  val debug_vconfig_rat = if (params.debugEn) Some(Output(UInt(PhyRegIdxWidth.W))) else None // TODO: use me
65024519898SXuan Hu
651c61abc0cSXuan Hu  val sqCanAccept = Input(Bool())
652c61abc0cSXuan Hu  val lqCanAccept = Input(Bool())
6534b0d80d8SXuan Hu
6544b0d80d8SXuan Hu  val debugTopDown = new Bundle {
6554b0d80d8SXuan Hu    val fromRob = new RobCoreTopDownIO
6564b0d80d8SXuan Hu    val fromCore = new CoreDispatchTopDownIO
6574b0d80d8SXuan Hu  }
6584b0d80d8SXuan Hu  val debugRolling = new RobDebugRollingIO
6596ce10964SXuan Hu  val debugEnqLsq = Input(new LsqEnqIO)
66024519898SXuan Hu}
66124519898SXuan Hu
66224519898SXuan Huclass NamedIndexes(namedCnt: Seq[(String, Int)]) {
66324519898SXuan Hu  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
66424519898SXuan Hu
66524519898SXuan Hu  val maxIdx = namedCnt.map(_._2).sum
66624519898SXuan Hu  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
66724519898SXuan Hu    val begin = namedCnt.slice(0, i).map(_._2).sum
66824519898SXuan Hu    val end = begin + namedCnt(i)._2
66924519898SXuan Hu    (namedCnt(i)._1, (begin, end))
67024519898SXuan Hu  }.toMap
67124519898SXuan Hu
67224519898SXuan Hu  def apply(name: String): Seq[Int] = {
67324519898SXuan Hu    require(nameRangeMap.contains(name))
67424519898SXuan Hu    nameRangeMap(name)._1 until nameRangeMap(name)._2
67524519898SXuan Hu  }
67624519898SXuan Hu}
677