xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision f5c17053bf28e142efe8a623792e0439d57e9e78)
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
335110577fSZiyue Zhangimport xiangshan.backend.fu.vector.Bundles.{VType, Vl}
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}
385e7a1fcaSxiaofeibaoimport xiangshan.backend.issue.{IntScheduler, FpScheduler, 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))
9360f0c5aeSxiaofeibao  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth))
9460f0c5aeSxiaofeibao  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])
1375e7a1fcaSxiaofeibao  val fpScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[FpScheduler])
13847c01b71Sxiaofeibao-xjtu  val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler])
139618b89e6Slewislzh  val intCanCompress = intScheWbData.filter(_.bits.params.CanCompress)
140618b89e6Slewislzh  val i2vWbData = intScheWbData.filter(_.bits.params.writeVecRf)
141618b89e6Slewislzh  val f2vWbData = fpScheWbData.filter(_.bits.params.writeVecRf)
14247c01b71Sxiaofeibao-xjtu  val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu)
14385f51ecaSxiaofeibao-xjtu  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
14485f51ecaSxiaofeibao-xjtu    val valid = x.valid
14585f51ecaSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
14685f51ecaSxiaofeibao-xjtu    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
1475f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
148618b89e6Slewislzh    val isIntSche = intCanCompress.contains(x)
1495e7a1fcaSxiaofeibao    val isFpSche = fpScheWbData.contains(x)
15047c01b71Sxiaofeibao-xjtu    val isVfSche = vfScheWbData.contains(x)
15147c01b71Sxiaofeibao-xjtu    val isMemVload = memVloadWbData.contains(x)
152618b89e6Slewislzh    val isi2v = i2vWbData.contains(x)
153618b89e6Slewislzh    val isf2v = f2vWbData.contains(x)
154618b89e6Slewislzh    val canSameRobidxWbData = if(isVfSche) {
155618b89e6Slewislzh      i2vWbData ++ f2vWbData ++ vfScheWbData
156618b89e6Slewislzh    } else if(isi2v) {
157618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
158618b89e6Slewislzh    } else if (isf2v) {
159618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
160618b89e6Slewislzh    } else if (isIntSche) {
161618b89e6Slewislzh      intCanCompress ++ fpScheWbData
1625e7a1fcaSxiaofeibao    } else if (isFpSche) {
163618b89e6Slewislzh      intCanCompress ++ fpScheWbData
16447c01b71Sxiaofeibao-xjtu    }  else if (isMemVload) {
16547c01b71Sxiaofeibao-xjtu      memVloadWbData
16647c01b71Sxiaofeibao-xjtu    } else {
16747c01b71Sxiaofeibao-xjtu      Seq(x)
16847c01b71Sxiaofeibao-xjtu    }
16947c01b71Sxiaofeibao-xjtu    val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => {
17085f51ecaSxiaofeibao-xjtu      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
17185f51ecaSxiaofeibao-xjtu      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
17285f51ecaSxiaofeibao-xjtu    }).toSeq)
17341dbbdfdSsinceforYy    delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid)
17485f51ecaSxiaofeibao-xjtu    delayed
17585f51ecaSxiaofeibao-xjtu  }).toSeq
17685f51ecaSxiaofeibao-xjtu
17724519898SXuan Hu  private val exuPredecode = VecInit(
17883ba63b3SXuan Hu    delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
17924519898SXuan Hu  )
18024519898SXuan Hu
18183ba63b3SXuan Hu  private val exuRedirects: Seq[ValidIO[Redirect]] = delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => {
18224519898SXuan Hu    val out = Wire(Valid(new Redirect()))
18324519898SXuan Hu    out.valid := x.valid && x.bits.redirect.get.valid && x.bits.redirect.get.bits.cfiUpdate.isMisPred
18424519898SXuan Hu    out.bits := x.bits.redirect.get.bits
185a63155a6SXuan Hu    out.bits.debugIsCtrl := true.B
186a63155a6SXuan Hu    out.bits.debugIsMemVio := false.B
18724519898SXuan Hu    out
18883ba63b3SXuan Hu  }).toSeq
18924519898SXuan Hu
19024519898SXuan Hu  private val memViolation = io.fromMem.violation
19124519898SXuan Hu  val loadReplay = Wire(ValidIO(new Redirect))
1925f8b6c9eSsinceforYy  loadReplay.valid := GatedValidRegNext(memViolation.valid &&
19324519898SXuan Hu    !memViolation.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
19424519898SXuan Hu  )
19524519898SXuan Hu  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
196a63155a6SXuan Hu  loadReplay.bits.debugIsCtrl := false.B
197a63155a6SXuan Hu  loadReplay.bits.debugIsMemVio := true.B
19824519898SXuan Hu
1999477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.vld
20024519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.ptr.value
2015f8b6c9eSsinceforYy  redirectGen.io.redirectPcRead.data := pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(redirectGen.io.redirectPcRead.offset, redirectGen.io.redirectPcRead.vld))
2029477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.vld
20324519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.ptr.value
2045f8b6c9eSsinceforYy  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegEnable(redirectGen.io.memPredPcRead.offset, redirectGen.io.memPredPcRead.vld))
20524519898SXuan Hu
20624519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
2078241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
2089477429fSsinceforYy    pcMem.io.ren.get(pcMemIdx) := io.memLdPcRead(i).vld
20924519898SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
2105f8b6c9eSsinceforYy    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memLdPcRead(i).offset, io.memLdPcRead(i).vld))
21124519898SXuan Hu  }
21224519898SXuan Hu
213b133b458SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
2148241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
2159477429fSsinceforYy    pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).vld
216b133b458SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
2175f8b6c9eSsinceforYy    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).vld))
218b133b458SXuan Hu  }
219b133b458SXuan Hu
2204b0d80d8SXuan Hu  if (EnableStorePrefetchSMS) {
2214b0d80d8SXuan Hu    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
2229477429fSsinceforYy      pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).vld
2234b0d80d8SXuan Hu      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
2245f8b6c9eSsinceforYy      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).vld))
2254b0d80d8SXuan Hu    }
2264b0d80d8SXuan Hu  } else {
22783ba63b3SXuan Hu    io.memStPcRead.foreach(_.data := 0.U)
2284b0d80d8SXuan Hu  }
2294b0d80d8SXuan Hu
23024519898SXuan Hu  redirectGen.io.hartId := io.fromTop.hartId
23183ba63b3SXuan Hu  redirectGen.io.exuRedirect := exuRedirects.toSeq
2324b0d80d8SXuan Hu  redirectGen.io.exuOutPredecode := exuPredecode // guarded by exuRedirect.valid
23324519898SXuan Hu  redirectGen.io.loadReplay <> loadReplay
23424519898SXuan Hu
23524519898SXuan Hu  redirectGen.io.robFlush := s1_robFlushRedirect.valid
23624519898SXuan Hu
237ff7f931dSXuan Hu  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
2385f8b6c9eSsinceforYy  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
23924519898SXuan Hu  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
24024519898SXuan Hu  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
24124519898SXuan Hu  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
24224519898SXuan Hu  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
24324519898SXuan Hu  for (i <- 0 until CommitWidth) {
24424519898SXuan Hu    // why flushOut: instructions with flushPipe are not commited to frontend
24524519898SXuan Hu    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
24624519898SXuan Hu    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
2475f8b6c9eSsinceforYy    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
24824519898SXuan Hu    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
24924519898SXuan Hu  }
250ff7f931dSXuan Hu  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
251ff7f931dSXuan Hu  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
252ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
253ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
2549342624fSGao-Zeyu
2559342624fSGao-Zeyu  //jmp/brh
2569342624fSGao-Zeyu  for (i <- 0 until NumRedirect) {
257ff7f931dSXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).valid := exuRedirects(i).valid && exuRedirects(i).bits.cfiUpdate.isMisPred && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2586ce10964SXuan Hu    io.frontend.toFtq.ftqIdxAhead(i).bits := exuRedirects(i).bits.ftqIdx
2599342624fSGao-Zeyu  }
2609342624fSGao-Zeyu  //loadreplay
261ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
2629342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
2639342624fSGao-Zeyu  //exception
264ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
2659342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
26605cc2a4eSXuan Hu
26705cc2a4eSXuan Hu  io.frontend.canAccept := decode.io.canAccept
26805cc2a4eSXuan Hu
26924519898SXuan Hu  // Be careful here:
27024519898SXuan Hu  // T0: rob.io.flushOut, s0_robFlushRedirect
27124519898SXuan Hu  // T1: s1_robFlushRedirect, rob.io.exception.valid
27224519898SXuan Hu  // T2: csr.redirect.valid
27324519898SXuan Hu  // T3: csr.exception.valid
27424519898SXuan Hu  // T4: csr.trapTarget
27524519898SXuan Hu  // T5: ctrlBlock.trapTarget
27624519898SXuan Hu  // T6: io.frontend.toFtq.stage2Redirect.valid
27724519898SXuan Hu  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
27824519898SXuan Hu    s1_robFlushPc, // replay inst
279870f462dSXuan Hu    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
28024519898SXuan Hu  ), s1_robFlushRedirect.valid)
28124519898SXuan Hu  private val s2_csrIsXRet = io.robio.csr.isXRet
28224519898SXuan Hu  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
28324519898SXuan Hu  private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget
28424519898SXuan Hu
28524519898SXuan Hu  val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc)
286ff7f931dSXuan Hu  when (s6_flushFromRobValid) {
28724519898SXuan Hu    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
28874f21f21SsinceforYy    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
28924519898SXuan Hu  }
29024519898SXuan Hu
2916f483f86SXuan Hu  for (i <- 0 until DecodeWidth) {
2926f483f86SXuan Hu    gpaMem.io.fromIFU := io.frontend.fromIfu
2936f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
2946f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
2956f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
2966f483f86SXuan Hu  }
2976f483f86SXuan Hu
29824519898SXuan Hu  // vtype commit
29986727929Ssinsanction  decode.io.isResumeVType := rob.io.toDecode.isResumeVType
30081535d7bSsinsanction  decode.io.commitVType := rob.io.toDecode.commitVType
30181535d7bSsinsanction  decode.io.walkVType := rob.io.toDecode.walkVType
30224519898SXuan Hu
303bd3616acSZiyue Zhang  // spec vtype, from vtypegen to vtpebuffer
304bd3616acSZiyue Zhang  rob.io.fromDecode.lastSpecVType := decode.io.lastSpecVType
305bd3616acSZiyue Zhang  rob.io.fromDecode.specVtype := decode.io.specVtype
306bd3616acSZiyue Zhang
307e25c13faSXuan Hu  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
308f6458cc1SZiyue Zhang  decode.io.vtypeRedirect := s1_s3_redirect.valid
30924519898SXuan Hu
31024519898SXuan Hu  decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) =>
31124519898SXuan Hu    decodeIn.valid := frontendCf.valid
31224519898SXuan Hu    frontendCf.ready := decodeIn.ready
31324519898SXuan Hu    decodeIn.bits.connectCtrlFlow(frontendCf.bits)
31424519898SXuan Hu  }
31524519898SXuan Hu  decode.io.csrCtrl := RegNext(io.csrCtrl)
31624519898SXuan Hu  decode.io.intRat <> rat.io.intReadPorts
31724519898SXuan Hu  decode.io.fpRat <> rat.io.fpReadPorts
31824519898SXuan Hu  decode.io.vecRat <> rat.io.vecReadPorts
319368cbcecSxiaofeibao  decode.io.v0Rat <> rat.io.v0ReadPorts
320368cbcecSxiaofeibao  decode.io.vlRat <> rat.io.vlReadPorts
32124519898SXuan Hu  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
322870f462dSXuan Hu  decode.io.stallReason.in <> io.frontend.stallReason
32324519898SXuan Hu
324fa7f2c26STang Haojin  // snapshot check
325c4b56310SHaojin Tang  class CFIRobIdx extends Bundle {
326c4b56310SHaojin Tang    val robIdx = Vec(RenameWidth, new RobPtr)
327c4b56310SHaojin Tang    val isCFI = Vec(RenameWidth, Bool())
328c4b56310SHaojin Tang  }
329c4b56310SHaojin Tang  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
330c4b56310SHaojin Tang  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
331c4b56310SHaojin Tang  snpt.io.enq := genSnapshot
332c4b56310SHaojin Tang  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
333c4b56310SHaojin Tang  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
334fa7f2c26STang Haojin  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
335c4b56310SHaojin 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
336c4b56310SHaojin Tang  snpt.io.redirect := s1_s3_redirect.valid
337c4b56310SHaojin Tang  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
338c4b56310SHaojin Tang    val notCFIMask = snapshot.isCFI.map(~_)
33937d77575SzhanglyGit    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
34037d77575SzhanglyGit    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
34137d77575SzhanglyGit    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
342c4b56310SHaojin Tang  })
343a6742963SHaojin Tang  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
344c4b56310SHaojin Tang  snpt.io.flushVec := flushVecNext
345fa7f2c26STang Haojin
346fa7f2c26STang Haojin  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
347780712aaSxiaofeibao-xjtu    snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
348780712aaSxiaofeibao-xjtu      !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head)
349c61abc0cSXuan Hu  ).reduceTree(_ || _)
350c61abc0cSXuan Hu  val snptSelect = MuxCase(
351c61abc0cSXuan Hu    0.U(log2Ceil(RenameSnapshotNum).W),
352fa7f2c26STang Haojin    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
353780712aaSxiaofeibao-xjtu      (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
354780712aaSxiaofeibao-xjtu        !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx)
355c61abc0cSXuan Hu    )
356c61abc0cSXuan Hu  )
357fa7f2c26STang Haojin
358fa7f2c26STang Haojin  rob.io.snpt.snptEnq := DontCare
359fa7f2c26STang Haojin  rob.io.snpt.snptDeq := snpt.io.deq
360fa7f2c26STang Haojin  rob.io.snpt.useSnpt := useSnpt
361fa7f2c26STang Haojin  rob.io.snpt.snptSelect := snptSelect
362c4b56310SHaojin Tang  rob.io.snpt.flushVec := flushVecNext
363c4b56310SHaojin Tang  rat.io.snpt.snptEnq := genSnapshot
364fa7f2c26STang Haojin  rat.io.snpt.snptDeq := snpt.io.deq
365fa7f2c26STang Haojin  rat.io.snpt.useSnpt := useSnpt
366fa7f2c26STang Haojin  rat.io.snpt.snptSelect := snptSelect
367c4b56310SHaojin Tang  rat.io.snpt.flushVec := flushVec
368fa7f2c26STang Haojin
36924519898SXuan Hu  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
37024519898SXuan Hu  // fusion decoder
37124519898SXuan Hu  for (i <- 0 until DecodeWidth) {
37224519898SXuan Hu    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
37324519898SXuan Hu    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
37424519898SXuan Hu    if (i > 0) {
37524519898SXuan Hu      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
37624519898SXuan Hu    }
37724519898SXuan Hu  }
37824519898SXuan Hu
37924519898SXuan Hu  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
38024519898SXuan Hu
38124519898SXuan Hu  for (i <- 0 until RenameWidth) {
38224519898SXuan Hu    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
38324519898SXuan Hu      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
38424519898SXuan Hu
38524519898SXuan Hu    decodePipeRename(i).ready := rename.io.in(i).ready
38624519898SXuan Hu    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
38724519898SXuan Hu    rename.io.in(i).bits := decodePipeRename(i).bits
38824519898SXuan Hu  }
38924519898SXuan Hu
39024519898SXuan Hu  for (i <- 0 until RenameWidth - 1) {
39124519898SXuan Hu    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
39224519898SXuan Hu    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
39324519898SXuan Hu
39424519898SXuan Hu    // update the first RenameWidth - 1 instructions
39524519898SXuan Hu    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
39624519898SXuan Hu    when (fusionDecoder.io.out(i).valid) {
39724519898SXuan Hu      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
39824519898SXuan Hu      // TODO: remove this dirty code for ftq update
39924519898SXuan Hu      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
40024519898SXuan Hu      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
40124519898SXuan Hu      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
40224519898SXuan Hu      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
40324519898SXuan Hu      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
40424519898SXuan Hu      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
40524519898SXuan Hu      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
40624519898SXuan Hu      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
40724519898SXuan Hu      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
40824519898SXuan Hu      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
40924519898SXuan Hu    }
41024519898SXuan Hu
41124519898SXuan Hu  }
41224519898SXuan Hu
41324519898SXuan Hu  // memory dependency predict
41424519898SXuan Hu  // when decode, send fold pc to mdp
4159477429fSsinceforYy  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
41624519898SXuan Hu  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
41724519898SXuan Hu  for (i <- 0 until DecodeWidth) {
4189477429fSsinceforYy    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
41924519898SXuan Hu    mdpFlodPcVec(i) := Mux(
42024519898SXuan Hu      decode.io.out(i).fire,
42124519898SXuan Hu      decode.io.in(i).bits.foldpc,
42224519898SXuan Hu      rename.io.in(i).bits.foldpc
42324519898SXuan Hu    )
42424519898SXuan Hu  }
42524519898SXuan Hu
42624519898SXuan Hu  // currently, we only update mdp info when isReplay
42724519898SXuan Hu  memCtrl.io.redirect := s1_s3_redirect
42824519898SXuan Hu  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
42924519898SXuan Hu  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
43024519898SXuan Hu  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
4319477429fSsinceforYy  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
43224519898SXuan Hu  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
43324519898SXuan Hu  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
43424519898SXuan Hu
43524519898SXuan Hu  rat.io.redirect := s1_s3_redirect.valid
4366b102a39SHaojin Tang  rat.io.rabCommits := rob.io.rabCommits
437cda1c534Sxiaofeibao-xjtu  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
43824519898SXuan Hu  rat.io.intRenamePorts := rename.io.intRenamePorts
43924519898SXuan Hu  rat.io.fpRenamePorts := rename.io.fpRenamePorts
44024519898SXuan Hu  rat.io.vecRenamePorts := rename.io.vecRenamePorts
441368cbcecSxiaofeibao  rat.io.v0RenamePorts := rename.io.v0RenamePorts
442368cbcecSxiaofeibao  rat.io.vlRenamePorts := rename.io.vlRenamePorts
44324519898SXuan Hu
44424519898SXuan Hu  rename.io.redirect := s1_s3_redirect
4456b102a39SHaojin Tang  rename.io.rabCommits := rob.io.rabCommits
44624519898SXuan Hu  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
44724519898SXuan Hu    RegEnable(waittable2rename, decodeOut.fire)
44824519898SXuan Hu  }
44924519898SXuan Hu  rename.io.ssit := memCtrl.io.ssit2Rename
45024519898SXuan Hu  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
45124519898SXuan Hu  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
45224519898SXuan Hu  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
453368cbcecSxiaofeibao  rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data)))
454368cbcecSxiaofeibao  rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data)))
455dcf3a679STang Haojin  rename.io.int_need_free := rat.io.int_need_free
456dcf3a679STang Haojin  rename.io.int_old_pdest := rat.io.int_old_pdest
457dcf3a679STang Haojin  rename.io.fp_old_pdest := rat.io.fp_old_pdest
4583cf50307SZiyue Zhang  rename.io.vec_old_pdest := rat.io.vec_old_pdest
459368cbcecSxiaofeibao  rename.io.v0_old_pdest := rat.io.v0_old_pdest
460368cbcecSxiaofeibao  rename.io.vl_old_pdest := rat.io.vl_old_pdest
461b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
462b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
463b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
464368cbcecSxiaofeibao  rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get)
465368cbcecSxiaofeibao  rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get)
466d2b20d1aSTang Haojin  rename.io.stallReason.in <> decode.io.stallReason.out
467870f462dSXuan Hu  rename.io.snpt.snptEnq := DontCare
468870f462dSXuan Hu  rename.io.snpt.snptDeq := snpt.io.deq
469870f462dSXuan Hu  rename.io.snpt.useSnpt := useSnpt
470870f462dSXuan Hu  rename.io.snpt.snptSelect := snptSelect
471bb7e6e3aSxiaofeibao-xjtu  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
472c4b56310SHaojin Tang  rename.io.snpt.flushVec := flushVecNext
473c4b56310SHaojin Tang  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
474c4b56310SHaojin Tang  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
475870f462dSXuan Hu
476870f462dSXuan Hu  val renameOut = Wire(chiselTypeOf(rename.io.out))
477870f462dSXuan Hu  renameOut <> rename.io.out
478ac78003fSzhanglyGit  // pass all snapshot in the first element for correctness of blockBackward
479ac78003fSzhanglyGit  renameOut.tail.foreach(_.bits.snapshot := false.B)
480ac78003fSzhanglyGit  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
481ac78003fSzhanglyGit    false.B,
482ac78003fSzhanglyGit    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
483ac78003fSzhanglyGit  )
484ac78003fSzhanglyGit
485ac78003fSzhanglyGit  // pipeline between rename and dispatch
486*f5c17053Sxiaofeibao-xjtu  PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch")
48782674533Sxiaofeibao  dispatch.io.intIQValidNumVec := io.intIQValidNumVec
48882674533Sxiaofeibao  dispatch.io.fpIQValidNumVec := io.fpIQValidNumVec
489ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num
490ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num
491ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num
492ff3fcdf1Sxiaofeibao-xjtu  dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num
493ff3fcdf1Sxiaofeibao-xjtu
49424519898SXuan Hu  dispatch.io.hartId := io.fromTop.hartId
49524519898SXuan Hu  dispatch.io.redirect := s1_s3_redirect
49624519898SXuan Hu  dispatch.io.enqRob <> rob.io.enq
497d2b20d1aSTang Haojin  dispatch.io.robHead := rob.io.debugRobHead
498d2b20d1aSTang Haojin  dispatch.io.stallReason <> rename.io.stallReason.out
499d2b20d1aSTang Haojin  dispatch.io.lqCanAccept := io.lqCanAccept
500d2b20d1aSTang Haojin  dispatch.io.sqCanAccept := io.sqCanAccept
501d2b20d1aSTang Haojin  dispatch.io.robHeadNotReady := rob.io.headNotReady
502d2b20d1aSTang Haojin  dispatch.io.robFull := rob.io.robFull
5035f8b6c9eSsinceforYy  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
50424519898SXuan Hu
505ff3fcdf1Sxiaofeibao-xjtu  intDq0.io.enq <> dispatch.io.toIntDq0
506ff3fcdf1Sxiaofeibao-xjtu  intDq0.io.redirect <> s2_s4_redirect
507ff3fcdf1Sxiaofeibao-xjtu  intDq1.io.enq <> dispatch.io.toIntDq1
508ff3fcdf1Sxiaofeibao-xjtu  intDq1.io.redirect <> s2_s4_redirect
50924519898SXuan Hu
51024519898SXuan Hu  fpDq.io.enq <> dispatch.io.toFpDq
51124519898SXuan Hu  fpDq.io.redirect <> s2_s4_redirect
51224519898SXuan Hu
51360f0c5aeSxiaofeibao  vecDq.io.enq <> dispatch.io.toVecDq
51460f0c5aeSxiaofeibao  vecDq.io.redirect <> s2_s4_redirect
51560f0c5aeSxiaofeibao
51624519898SXuan Hu  lsDq.io.enq <> dispatch.io.toLsDq
51724519898SXuan Hu  lsDq.io.redirect <> s2_s4_redirect
51824519898SXuan Hu
519ff3fcdf1Sxiaofeibao-xjtu  io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq)
52060f0c5aeSxiaofeibao  io.toIssueBlock.fpUops <> fpDq.io.deq
52160f0c5aeSxiaofeibao  io.toIssueBlock.vfUops  <> vecDq.io.deq
52224519898SXuan Hu  io.toIssueBlock.memUops <> lsDq.io.deq
52324519898SXuan Hu  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
52424519898SXuan Hu  io.toIssueBlock.flush   <> s2_s4_redirect
52524519898SXuan Hu
5265f8b6c9eSsinceforYy  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
5273827c997SsinceforYy  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
5283827c997SsinceforYy  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
52924519898SXuan Hu
53024519898SXuan Hu  io.toDataPath.flush := s2_s4_redirect
53124519898SXuan Hu  io.toExuBlock.flush := s2_s4_redirect
53224519898SXuan Hu
53324519898SXuan Hu
53424519898SXuan Hu  rob.io.hartId := io.fromTop.hartId
53524519898SXuan Hu  rob.io.redirect := s1_s3_redirect
53624519898SXuan Hu  rob.io.writeback := delayedNotFlushedWriteBack
53785f51ecaSxiaofeibao-xjtu  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
5386f483f86SXuan Hu  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
53924519898SXuan Hu
54024519898SXuan Hu  io.redirect := s1_s3_redirect
54124519898SXuan Hu
54224519898SXuan Hu  // rob to int block
54324519898SXuan Hu  io.robio.csr <> rob.io.csr
54424519898SXuan Hu  // When wfi is disabled, it will not block ROB commit.
54524519898SXuan Hu  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
54624519898SXuan Hu  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
54724519898SXuan Hu
54824519898SXuan Hu  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
54924519898SXuan Hu
55024519898SXuan Hu  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
55124519898SXuan Hu  io.robio.exception := rob.io.exception
55224519898SXuan Hu  io.robio.exception.bits.pc := s1_robFlushPc
55324519898SXuan Hu
55424519898SXuan Hu  // rob to mem block
55524519898SXuan Hu  io.robio.lsq <> rob.io.lsq
55624519898SXuan Hu
557b7d9e8d5Sxiaofeibao-xjtu  io.debug_int_rat    .foreach(_ := rat.io.diff_int_rat.get)
558b7d9e8d5Sxiaofeibao-xjtu  io.debug_fp_rat     .foreach(_ := rat.io.diff_fp_rat.get)
559b7d9e8d5Sxiaofeibao-xjtu  io.debug_vec_rat    .foreach(_ := rat.io.diff_vec_rat.get)
560368cbcecSxiaofeibao  io.debug_v0_rat.foreach(_ := rat.io.diff_v0_rat.get)
561368cbcecSxiaofeibao  io.debug_vl_rat.foreach(_ := rat.io.diff_vl_rat.get)
56224519898SXuan Hu
56317b21f45SHaojin Tang  rob.io.debug_ls := io.robio.debug_ls
56417b21f45SHaojin Tang  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
56517b21f45SHaojin Tang  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
5666ce10964SXuan Hu  rob.io.debugEnqLsq := io.debugEnqLsq
5676ce10964SXuan Hu
56817b21f45SHaojin Tang  io.robio.robDeqPtr := rob.io.robDeqPtr
5698744445eSMaxpicca-Li
5707e4f0b19SZiyue-Zhang  // rob to backend
5717e4f0b19SZiyue-Zhang  io.robio.commitVType := rob.io.toDecode.commitVType
5727e4f0b19SZiyue-Zhang  // exu block to decode
573d8a50338SZiyue Zhang  decode.io.vsetvlVType := io.toDecode.vsetvlVType
5745110577fSZiyue Zhang  // backend to decode
5755110577fSZiyue Zhang  decode.io.vstart := io.toDecode.vstart
5765110577fSZiyue Zhang  // backend to rob
5775110577fSZiyue Zhang  rob.io.vstartIsZero := io.toDecode.vstart === 0.U
5787e4f0b19SZiyue-Zhang
57960ebee38STang Haojin  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
58060ebee38STang Haojin  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
58160ebee38STang Haojin  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
5827cf78eb2Shappy-lx  io.debugRolling := rob.io.debugRolling
58360ebee38STang Haojin
5845f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
5855f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull)
58660f0c5aeSxiaofeibao  io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull)
5875f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull)
58824519898SXuan Hu
58924519898SXuan Hu  val pfevent = Module(new PFEvent)
59024519898SXuan Hu  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
59124519898SXuan Hu  val csrevents = pfevent.io.hpmevent.slice(8,16)
59224519898SXuan Hu
59324519898SXuan Hu  val perfinfo = IO(new Bundle(){
59424519898SXuan Hu    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
59524519898SXuan Hu    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
59624519898SXuan Hu    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
59724519898SXuan Hu  })
59824519898SXuan Hu
59960f0c5aeSxiaofeibao  val perfFromUnits = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents)
6009a128342SHaoyuan Feng  val perfFromIO    = perfinfo.perfEventsEu0.map(x => ("perfEventsEu0", x.value)) ++
6019a128342SHaoyuan Feng                        perfinfo.perfEventsEu1.map(x => ("perfEventsEu1", x.value)) ++
6029a128342SHaoyuan Feng                        perfinfo.perfEventsRs.map(x => ("perfEventsRs", x.value))
6039a128342SHaoyuan Feng  val perfBlock     = Seq()
6049a128342SHaoyuan Feng  // let index = 0 be no event
6059a128342SHaoyuan Feng  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
6069a128342SHaoyuan Feng
6079a128342SHaoyuan Feng  if (printEventCoding) {
6089a128342SHaoyuan Feng    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
6099a128342SHaoyuan Feng      println("CtrlBlock perfEvents Set", name, inc, i)
6109a128342SHaoyuan Feng    }
6119a128342SHaoyuan Feng  }
6129a128342SHaoyuan Feng
6139a128342SHaoyuan Feng  val allPerfInc = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
6149a128342SHaoyuan Feng  val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
61524519898SXuan Hu  generatePerfEvent()
61624519898SXuan Hu}
61724519898SXuan Hu
61824519898SXuan Huclass CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
61924519898SXuan Hu  val fromTop = new Bundle {
62024519898SXuan Hu    val hartId = Input(UInt(8.W))
62124519898SXuan Hu  }
62224519898SXuan Hu  val toTop = new Bundle {
62324519898SXuan Hu    val cpuHalt = Output(Bool())
62424519898SXuan Hu  }
62524519898SXuan Hu  val frontend = Flipped(new FrontendToCtrlIO())
62624519898SXuan Hu  val toIssueBlock = new Bundle {
62724519898SXuan Hu    val flush = ValidIO(new Redirect)
62824519898SXuan Hu    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
62924519898SXuan Hu    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
63060f0c5aeSxiaofeibao    val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst))
63160f0c5aeSxiaofeibao    val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
63224519898SXuan Hu    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
63324519898SXuan Hu  }
63424519898SXuan Hu  val toDataPath = new Bundle {
63524519898SXuan Hu    val flush = ValidIO(new Redirect)
63624519898SXuan Hu  }
63724519898SXuan Hu  val toExuBlock = new Bundle {
63824519898SXuan Hu    val flush = ValidIO(new Redirect)
63924519898SXuan Hu  }
64082674533Sxiaofeibao  val intIQValidNumVec = Input(MixedVec(params.genIntIQValidNumBundle))
64182674533Sxiaofeibao  val fpIQValidNumVec = Input(MixedVec(params.genFpIQValidNumBundle))
64224519898SXuan Hu  val fromWB = new Bundle {
64324519898SXuan Hu    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
64424519898SXuan Hu  }
64524519898SXuan Hu  val redirect = ValidIO(new Redirect)
64624519898SXuan Hu  val fromMem = new Bundle {
647272ec6b1SHaojin Tang    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
64824519898SXuan Hu    val violation = Flipped(ValidIO(new Redirect))
64924519898SXuan Hu  }
65024519898SXuan Hu  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
65183ba63b3SXuan Hu  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
652b133b458SXuan Hu  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
6534b0d80d8SXuan Hu
65424519898SXuan Hu  val csrCtrl = Input(new CustomCSRCtrlIO)
65524519898SXuan Hu  val robio = new Bundle {
65624519898SXuan Hu    val csr = new RobCSRIO
65724519898SXuan Hu    val exception = ValidIO(new ExceptionInfo)
65824519898SXuan Hu    val lsq = new RobLsqIO
6596810d1e8Ssfencevma    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
6602326221cSXuan Hu    val debug_ls = Input(new DebugLSIO())
66117b21f45SHaojin Tang    val robHeadLsIssue = Input(Bool())
66217b21f45SHaojin Tang    val robDeqPtr = Output(new RobPtr)
6637e4f0b19SZiyue-Zhang    val commitVType = new Bundle {
6647e4f0b19SZiyue-Zhang      val vtype = Output(ValidIO(VType()))
6657e4f0b19SZiyue-Zhang      val hasVsetvl = Output(Bool())
6667e4f0b19SZiyue-Zhang    }
66724519898SXuan Hu  }
66824519898SXuan Hu
669d8a50338SZiyue Zhang  val toDecode = new Bundle {
670d8a50338SZiyue Zhang    val vsetvlVType = Input(VType())
6715110577fSZiyue Zhang    val vstart = Input(Vl())
672d8a50338SZiyue Zhang  }
673d8a50338SZiyue Zhang
67424519898SXuan Hu  val perfInfo = Output(new Bundle{
67524519898SXuan Hu    val ctrlInfo = new Bundle {
67624519898SXuan Hu      val robFull   = Bool()
67724519898SXuan Hu      val intdqFull = Bool()
67824519898SXuan Hu      val fpdqFull  = Bool()
67924519898SXuan Hu      val lsdqFull  = Bool()
68024519898SXuan Hu    }
68124519898SXuan Hu  })
682b7d9e8d5Sxiaofeibao-xjtu  val debug_int_rat     = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
683b7d9e8d5Sxiaofeibao-xjtu  val debug_fp_rat      = if (params.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
684d1e473c9Sxiaofeibao  val debug_vec_rat     = if (params.debugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None
685d1e473c9Sxiaofeibao  val debug_v0_rat      = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
686d1e473c9Sxiaofeibao  val debug_vl_rat      = if (params.debugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
68724519898SXuan Hu
688c61abc0cSXuan Hu  val sqCanAccept = Input(Bool())
689c61abc0cSXuan Hu  val lqCanAccept = Input(Bool())
6904b0d80d8SXuan Hu
6914b0d80d8SXuan Hu  val debugTopDown = new Bundle {
6924b0d80d8SXuan Hu    val fromRob = new RobCoreTopDownIO
6934b0d80d8SXuan Hu    val fromCore = new CoreDispatchTopDownIO
6944b0d80d8SXuan Hu  }
6954b0d80d8SXuan Hu  val debugRolling = new RobDebugRollingIO
6966ce10964SXuan Hu  val debugEnqLsq = Input(new LsqEnqIO)
69724519898SXuan Hu}
69824519898SXuan Hu
69924519898SXuan Huclass NamedIndexes(namedCnt: Seq[(String, Int)]) {
70024519898SXuan Hu  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
70124519898SXuan Hu
70224519898SXuan Hu  val maxIdx = namedCnt.map(_._2).sum
70324519898SXuan Hu  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
70424519898SXuan Hu    val begin = namedCnt.slice(0, i).map(_._2).sum
70524519898SXuan Hu    val end = begin + namedCnt(i)._2
70624519898SXuan Hu    (namedCnt(i)._1, (begin, end))
70724519898SXuan Hu  }.toMap
70824519898SXuan Hu
70924519898SXuan Hu  def apply(name: String): Seq[Int] = {
71024519898SXuan Hu    require(nameRangeMap.contains(name))
71124519898SXuan Hu    nameRangeMap(name)._1 until nameRangeMap(name)._2
71224519898SXuan Hu  }
71324519898SXuan Hu}
714