xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision bbb5025804d15e99c33b7cdb625687af6aed8720)
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._
270a7d1d5cSxiaofeibaoimport xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput, ExuVec, StaticInst, TrapInstInfo}
282326221cSXuan Huimport xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator}
290a7d1d5cSxiaofeibaoimport xiangshan.backend.datapath.DataConfig.{FpData, IntData, V0Data, VAddrData, VecData, VlData}
3024519898SXuan Huimport xiangshan.backend.decode.{DecodeStage, FusionDecoder}
31914bbc86Sxiaofeibao-xjtuimport xiangshan.backend.dispatch.{CoreDispatchTopDownIO}
320a7d1d5cSxiaofeibaoimport xiangshan.backend.dispatch.NewDispatch
335110577fSZiyue Zhangimport xiangshan.backend.fu.vector.Bundles.{VType, Vl}
3415ed99a7SXuan Huimport xiangshan.backend.fu.wrapper.CSRToDecode
35870f462dSXuan Huimport xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator}
3683ba63b3SXuan Huimport xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr}
376ce10964SXuan Huimport xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
380a7d1d5cSxiaofeibaoimport xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr}
3915ed99a7SXuan Huimport xiangshan.backend.issue.{FpScheduler, IntScheduler, MemScheduler, VfScheduler}
404907ec88Schengguanghuiimport xiangshan.backend.trace._
4124519898SXuan Hu
4224519898SXuan Huclass CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
4324519898SXuan Hu  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
4424519898SXuan Hu  val redirect = Valid(new Redirect)
459342624fSGao-Zeyu  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
469342624fSGao-Zeyu  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
4724519898SXuan Hu}
4824519898SXuan Hu
4924519898SXuan Huclass CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
501ca4a39dSXuan Hu  override def shouldBeInlined: Boolean = false
511ca4a39dSXuan Hu
5224519898SXuan Hu  val rob = LazyModule(new Rob(params))
5324519898SXuan Hu
5424519898SXuan Hu  lazy val module = new CtrlBlockImp(this)(p, params)
5524519898SXuan Hu
566f483f86SXuan Hu  val gpaMem = LazyModule(new GPAMem())
5724519898SXuan Hu}
5824519898SXuan Hu
5924519898SXuan Huclass CtrlBlockImp(
6024519898SXuan Hu  override val wrapper: CtrlBlock
6124519898SXuan Hu)(implicit
6224519898SXuan Hu  p: Parameters,
6324519898SXuan Hu  params: BackendParams
6424519898SXuan Hu) extends LazyModuleImp(wrapper)
6524519898SXuan Hu  with HasXSParameter
6624519898SXuan Hu  with HasCircularQueuePtrHelper
6724519898SXuan Hu  with HasPerfEvents
6885a8d7caSZehao Liu  with HasCriticalErrors
6924519898SXuan Hu{
7024519898SXuan Hu  val pcMemRdIndexes = new NamedIndexes(Seq(
7124519898SXuan Hu    "redirect"  -> 1,
7224519898SXuan Hu    "memPred"   -> 1,
7324519898SXuan Hu    "robFlush"  -> 1,
74c37914a4Sxiaofeibao    "bjuPc"     -> params.BrhCnt,
75c37914a4Sxiaofeibao    "bjuTarget" -> params.BrhCnt,
7624519898SXuan Hu    "load"      -> params.LduCnt,
77b133b458SXuan Hu    "hybrid"    -> params.HyuCnt,
784907ec88Schengguanghui    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0),
794907ec88Schengguanghui    "trace"     -> TraceGroupNum
8024519898SXuan Hu  ))
8124519898SXuan Hu
8224519898SXuan Hu  private val numPcMemRead = pcMemRdIndexes.maxIdx
8324519898SXuan Hu
8429dbac5aSsinsanction  // now pcMem read for exu is moved to PcTargetMem (OG0)
8524519898SXuan Hu  println(s"pcMem read num: $numPcMemRead")
8624519898SXuan Hu
8724519898SXuan Hu  val io = IO(new CtrlBlockIO())
8824519898SXuan Hu
890a7d1d5cSxiaofeibao  val dispatch = Module(new NewDispatch)
906f483f86SXuan Hu  val gpaMem = wrapper.gpaMem.module
9124519898SXuan Hu  val decode = Module(new DecodeStage)
9224519898SXuan Hu  val fusionDecoder = Module(new FusionDecoder)
9324519898SXuan Hu  val rat = Module(new RenameTableWrapper)
9424519898SXuan Hu  val rename = Module(new Rename)
9524519898SXuan Hu  val redirectGen = Module(new RedirectGenerator)
969477429fSsinceforYy  private def hasRen: Boolean = true
979477429fSsinceforYy  private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC", hasRen = hasRen))
9824519898SXuan Hu  private val rob = wrapper.rob.module
9924519898SXuan Hu  private val memCtrl = Module(new MemCtrl(params))
10024519898SXuan Hu
10124519898SXuan Hu  private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
10224519898SXuan Hu
10324519898SXuan Hu  private val s0_robFlushRedirect = rob.io.flushOut
10424519898SXuan Hu  private val s1_robFlushRedirect = Wire(Valid(new Redirect))
1055f8b6c9eSsinceforYy  s1_robFlushRedirect.valid := GatedValidRegNext(s0_robFlushRedirect.valid, false.B)
10624519898SXuan Hu  s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid)
10724519898SXuan Hu
1089477429fSsinceforYy  pcMem.io.ren.get(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.valid
10924519898SXuan Hu  pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value
110a2fa0ad9Sxiaofeibao  private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).startAddr + (RegEnable(s0_robFlushRedirect.bits.ftqOffset, s0_robFlushRedirect.valid) << instOffsetBits)
11124519898SXuan Hu  private val s3_redirectGen = redirectGen.io.stage2Redirect
11224519898SXuan Hu  private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen)
11324519898SXuan Hu  private val s2_s4_pendingRedirectValid = RegInit(false.B)
11424519898SXuan Hu  when (s1_s3_redirect.valid) {
11524519898SXuan Hu    s2_s4_pendingRedirectValid := true.B
1165f8b6c9eSsinceforYy  }.elsewhen (GatedValidRegNext(io.frontend.toFtq.redirect.valid)) {
11724519898SXuan Hu    s2_s4_pendingRedirectValid := false.B
11824519898SXuan Hu  }
11924519898SXuan Hu
12024519898SXuan Hu  // Redirect will be RegNext at ExuBlocks and IssueBlocks
12124519898SXuan Hu  val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect)
12224519898SXuan Hu  val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect)
12324519898SXuan Hu
12424519898SXuan Hu  private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => {
12524519898SXuan Hu    val valid = x.valid
12654c6d89dSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
12724519898SXuan Hu    val delayed = Wire(Valid(new ExuOutput(x.bits.params)))
1285f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
12924519898SXuan Hu    delayed.bits := RegEnable(x.bits, x.valid)
13096e858baSXuan Hu    delayed.bits.debugInfo.writebackTime := GTimer()
13124519898SXuan Hu    delayed
13283ba63b3SXuan Hu  }).toSeq
133bd5909d0Sxiaofeibao-xjtu  private val delayedWriteBack = Wire(chiselTypeOf(io.fromWB.wbData))
134bd5909d0Sxiaofeibao-xjtu  delayedWriteBack.zipWithIndex.map{ case (x,i) =>
135bd5909d0Sxiaofeibao-xjtu    x.valid := GatedValidRegNext(io.fromWB.wbData(i).valid)
136bd5909d0Sxiaofeibao-xjtu    x.bits := delayedNotFlushedWriteBack(i).bits
137bd5909d0Sxiaofeibao-xjtu  }
138571677c9Sxiaofeibao-xjtu  val delayedNotFlushedWriteBackNeedFlush = Wire(Vec(params.allExuParams.filter(_.needExceptionGen).length, Bool()))
139571677c9Sxiaofeibao-xjtu  delayedNotFlushedWriteBackNeedFlush := delayedNotFlushedWriteBack.filter(_.bits.params.needExceptionGen).map{ x =>
140571677c9Sxiaofeibao-xjtu    x.bits.exceptionVec.get.asUInt.orR || x.bits.flushPipe.getOrElse(false.B) || x.bits.replay.getOrElse(false.B) ||
1417e0f64b0SGuanghui Cheng      (if (x.bits.trigger.nonEmpty) TriggerAction.isDmode(x.bits.trigger.get) else false.B)
142571677c9Sxiaofeibao-xjtu  }
14324519898SXuan Hu
14485f51ecaSxiaofeibao-xjtu  val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu)
14547c01b71Sxiaofeibao-xjtu  val intScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[IntScheduler])
1465e7a1fcaSxiaofeibao  val fpScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[FpScheduler])
14747c01b71Sxiaofeibao-xjtu  val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler])
148618b89e6Slewislzh  val intCanCompress = intScheWbData.filter(_.bits.params.CanCompress)
149618b89e6Slewislzh  val i2vWbData = intScheWbData.filter(_.bits.params.writeVecRf)
150618b89e6Slewislzh  val f2vWbData = fpScheWbData.filter(_.bits.params.writeVecRf)
15147c01b71Sxiaofeibao-xjtu  val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu)
15285f51ecaSxiaofeibao-xjtu  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
15385f51ecaSxiaofeibao-xjtu    val valid = x.valid
15485f51ecaSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
15585f51ecaSxiaofeibao-xjtu    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
1565f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
157618b89e6Slewislzh    val isIntSche = intCanCompress.contains(x)
1585e7a1fcaSxiaofeibao    val isFpSche = fpScheWbData.contains(x)
15947c01b71Sxiaofeibao-xjtu    val isVfSche = vfScheWbData.contains(x)
16047c01b71Sxiaofeibao-xjtu    val isMemVload = memVloadWbData.contains(x)
161618b89e6Slewislzh    val isi2v = i2vWbData.contains(x)
162618b89e6Slewislzh    val isf2v = f2vWbData.contains(x)
163618b89e6Slewislzh    val canSameRobidxWbData = if(isVfSche) {
164618b89e6Slewislzh      i2vWbData ++ f2vWbData ++ vfScheWbData
165618b89e6Slewislzh    } else if(isi2v) {
166618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
167618b89e6Slewislzh    } else if (isf2v) {
168618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
169618b89e6Slewislzh    } else if (isIntSche) {
170618b89e6Slewislzh      intCanCompress ++ fpScheWbData
1715e7a1fcaSxiaofeibao    } else if (isFpSche) {
172618b89e6Slewislzh      intCanCompress ++ fpScheWbData
17347c01b71Sxiaofeibao-xjtu    }  else if (isMemVload) {
17447c01b71Sxiaofeibao-xjtu      memVloadWbData
17547c01b71Sxiaofeibao-xjtu    } else {
17647c01b71Sxiaofeibao-xjtu      Seq(x)
17747c01b71Sxiaofeibao-xjtu    }
17847c01b71Sxiaofeibao-xjtu    val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => {
17985f51ecaSxiaofeibao-xjtu      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
18085f51ecaSxiaofeibao-xjtu      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
18185f51ecaSxiaofeibao-xjtu    }).toSeq)
18241dbbdfdSsinceforYy    delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid)
18385f51ecaSxiaofeibao-xjtu    delayed
18485f51ecaSxiaofeibao-xjtu  }).toSeq
18585f51ecaSxiaofeibao-xjtu
18624519898SXuan Hu  private val exuPredecode = VecInit(
18754c6d89dSxiaofeibao-xjtu    io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
18824519898SXuan Hu  )
18924519898SXuan Hu
19054c6d89dSxiaofeibao-xjtu  private val exuRedirects: Seq[ValidIO[Redirect]] = io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => {
19151aa1b60Sxiaofeibao-xjtu    val hasCSR = x.bits.params.hasCSR
19224519898SXuan Hu    val out = Wire(Valid(new Redirect()))
193961164a6SZhaoyang You    out.valid := x.valid && x.bits.redirect.get.valid && (x.bits.redirect.get.bits.cfiUpdate.isMisPred || x.bits.redirect.get.bits.cfiUpdate.hasBackendFault) && !x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
19424519898SXuan Hu    out.bits := x.bits.redirect.get.bits
195a63155a6SXuan Hu    out.bits.debugIsCtrl := true.B
196a63155a6SXuan Hu    out.bits.debugIsMemVio := false.B
1977da4513bSxiaofeibao    // for fix timing, next cycle assgin
19851aa1b60Sxiaofeibao-xjtu    if (!hasCSR) {
1997da4513bSxiaofeibao      out.bits.cfiUpdate.backendIAF := false.B
2007da4513bSxiaofeibao      out.bits.cfiUpdate.backendIPF := false.B
2017da4513bSxiaofeibao      out.bits.cfiUpdate.backendIGPF := false.B
20251aa1b60Sxiaofeibao-xjtu    }
20324519898SXuan Hu    out
20483ba63b3SXuan Hu  }).toSeq
20554c6d89dSxiaofeibao-xjtu  private val oldestOneHot = Redirect.selectOldestRedirect(exuRedirects)
20651aa1b60Sxiaofeibao-xjtu  private val CSROH = VecInit(io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => x.bits.params.hasCSR.B))
20751aa1b60Sxiaofeibao-xjtu  private val oldestExuRedirectIsCSR = oldestOneHot === CSROH
20854c6d89dSxiaofeibao-xjtu  private val oldestExuRedirect = Mux1H(oldestOneHot, exuRedirects)
20954c6d89dSxiaofeibao-xjtu  private val oldestExuPredecode = Mux1H(oldestOneHot, exuPredecode)
21024519898SXuan Hu
21124519898SXuan Hu  private val memViolation = io.fromMem.violation
21224519898SXuan Hu  val loadReplay = Wire(ValidIO(new Redirect))
21354c6d89dSxiaofeibao-xjtu  loadReplay.valid := GatedValidRegNext(memViolation.valid)
21424519898SXuan Hu  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
215a63155a6SXuan Hu  loadReplay.bits.debugIsCtrl := false.B
216a63155a6SXuan Hu  loadReplay.bits.debugIsMemVio := true.B
21724519898SXuan Hu
21854c6d89dSxiaofeibao-xjtu  pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := memViolation.valid
21954c6d89dSxiaofeibao-xjtu  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := memViolation.bits.ftqIdx.value
22054c6d89dSxiaofeibao-xjtu  pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := memViolation.valid
22154c6d89dSxiaofeibao-xjtu  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := memViolation.bits.stFtqIdx.value
222a2fa0ad9Sxiaofeibao  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).startAddr + (RegEnable(memViolation.bits.stFtqOffset, memViolation.valid) << instOffsetBits)
22324519898SXuan Hu
224c37914a4Sxiaofeibao  for ((pcMemIdx, i) <- pcMemRdIndexes("bjuPc").zipWithIndex) {
225c37914a4Sxiaofeibao    val ren = io.toDataPath.pcToDataPathIO.fromDataPathValid(i)
226c37914a4Sxiaofeibao    val raddr = io.toDataPath.pcToDataPathIO.fromDataPathFtqPtr(i).value
227c37914a4Sxiaofeibao    val roffset = io.toDataPath.pcToDataPathIO.fromDataPathFtqOffset(i)
228c37914a4Sxiaofeibao    pcMem.io.ren.get(pcMemIdx) := ren
229c37914a4Sxiaofeibao    pcMem.io.raddr(pcMemIdx) := raddr
230a2fa0ad9Sxiaofeibao    io.toDataPath.pcToDataPathIO.toDataPathPC(i) := pcMem.io.rdata(pcMemIdx).startAddr
231c37914a4Sxiaofeibao  }
232c37914a4Sxiaofeibao
233f56a77d4Sxiaofeibao  val newestEn = RegNext(io.frontend.fromFtq.newest_entry_en)
234f56a77d4Sxiaofeibao  val newestTarget = RegEnable(io.frontend.fromFtq.newest_entry_target, io.frontend.fromFtq.newest_entry_en)
235f56a77d4Sxiaofeibao  val newestPtr = RegEnable(io.frontend.fromFtq.newest_entry_ptr, io.frontend.fromFtq.newest_entry_en)
236f56a77d4Sxiaofeibao  val newestTargetNext = RegEnable(newestTarget, newestEn)
237c37914a4Sxiaofeibao  for ((pcMemIdx, i) <- pcMemRdIndexes("bjuTarget").zipWithIndex) {
238c37914a4Sxiaofeibao    val ren = io.toDataPath.pcToDataPathIO.fromDataPathValid(i)
239f56a77d4Sxiaofeibao    val baseAddr = io.toDataPath.pcToDataPathIO.fromDataPathFtqPtr(i).value
240c37914a4Sxiaofeibao    val raddr = io.toDataPath.pcToDataPathIO.fromDataPathFtqPtr(i).value + 1.U
241c37914a4Sxiaofeibao    pcMem.io.ren.get(pcMemIdx) := ren
242c37914a4Sxiaofeibao    pcMem.io.raddr(pcMemIdx) := raddr
243f56a77d4Sxiaofeibao    val needNewest = RegNext(baseAddr === newestPtr.value)
244f56a77d4Sxiaofeibao    io.toDataPath.pcToDataPathIO.toDataPathTargetPC(i) := Mux(needNewest, newestTargetNext, pcMem.io.rdata(pcMemIdx).startAddr)
245c37914a4Sxiaofeibao  }
246c37914a4Sxiaofeibao
247c37914a4Sxiaofeibao  val baseIdx = params.BrhCnt
24824519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
2498241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
250c37914a4Sxiaofeibao    val ren = io.toDataPath.pcToDataPathIO.fromDataPathValid(baseIdx+i)
251c37914a4Sxiaofeibao    val raddr = io.toDataPath.pcToDataPathIO.fromDataPathFtqPtr(baseIdx+i).value
252c37914a4Sxiaofeibao    val roffset = io.toDataPath.pcToDataPathIO.fromDataPathFtqOffset(baseIdx+i)
253c37914a4Sxiaofeibao    pcMem.io.ren.get(pcMemIdx) := ren
254c37914a4Sxiaofeibao    pcMem.io.raddr(pcMemIdx) := raddr
255a2fa0ad9Sxiaofeibao    io.toDataPath.pcToDataPathIO.toDataPathPC(baseIdx+i) := pcMem.io.rdata(pcMemIdx).startAddr
25624519898SXuan Hu  }
25724519898SXuan Hu
258b133b458SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
2598241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
26054c6d89dSxiaofeibao-xjtu    pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).valid
261b133b458SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
262a2fa0ad9Sxiaofeibao    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).startAddr + (RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).valid) << instOffsetBits)
263b133b458SXuan Hu  }
264b133b458SXuan Hu
2654b0d80d8SXuan Hu  if (EnableStorePrefetchSMS) {
2664b0d80d8SXuan Hu    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
26754c6d89dSxiaofeibao-xjtu      pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).valid
2684b0d80d8SXuan Hu      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
269a2fa0ad9Sxiaofeibao      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).startAddr + (RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).valid) << instOffsetBits)
2704b0d80d8SXuan Hu    }
2714b0d80d8SXuan Hu  } else {
27283ba63b3SXuan Hu    io.memStPcRead.foreach(_.data := 0.U)
2734b0d80d8SXuan Hu  }
2744b0d80d8SXuan Hu
2754907ec88Schengguanghui  /**
2764907ec88Schengguanghui   * trace begin
2774907ec88Schengguanghui   */
2784907ec88Schengguanghui  val trace = Module(new Trace)
279c308d936Schengguanghui  trace.io.in.fromEncoder.stall  := io.traceCoreInterface.fromEncoder.stall
280c308d936Schengguanghui  trace.io.in.fromEncoder.enable := io.traceCoreInterface.fromEncoder.enable
281c308d936Schengguanghui  trace.io.in.fromRob            := rob.io.trace.traceCommitInfo
282c308d936Schengguanghui  rob.io.trace.blockCommit       := trace.io.out.blockRobCommit
283fd448a9dSchengguanghui  val tracePcStart = Wire(Vec(TraceGroupNum, UInt(IaddrWidth.W)))
2844907ec88Schengguanghui  for ((pcMemIdx, i) <- pcMemRdIndexes("trace").zipWithIndex) {
285c308d936Schengguanghui    val traceValid = trace.toPcMem.blocks(i).valid
2864907ec88Schengguanghui    pcMem.io.ren.get(pcMemIdx) := traceValid
287c308d936Schengguanghui    pcMem.io.raddr(pcMemIdx) := trace.toPcMem.blocks(i).bits.ftqIdx.get.value
288fd448a9dSchengguanghui    tracePcStart(i) := pcMem.io.rdata(pcMemIdx).startAddr
2894907ec88Schengguanghui  }
2904907ec88Schengguanghui
2918cbf000bSchengguanghui  // Trap/Xret only occur in block(0).
292c308d936Schengguanghui  val tracePriv = Mux(Itype.isTrapOrXret(trace.toEncoder.blocks(0).bits.tracePipe.itype),
293c308d936Schengguanghui    io.fromCSR.traceCSR.lastPriv,
294c308d936Schengguanghui    io.fromCSR.traceCSR.currentPriv
295c308d936Schengguanghui  )
2963ad9f3ddSchengguanghui  io.traceCoreInterface.toEncoder.trap.cause := io.fromCSR.traceCSR.cause.asUInt
2973ad9f3ddSchengguanghui  io.traceCoreInterface.toEncoder.trap.tval  := io.fromCSR.traceCSR.tval.asUInt
298c308d936Schengguanghui  io.traceCoreInterface.toEncoder.priv       := tracePriv
2993ad9f3ddSchengguanghui  (0 until TraceGroupNum).foreach(i => {
3003ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).valid := trace.io.out.toEncoder.blocks(i).valid
301fd448a9dSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.iaddr := tracePcStart(i)
302fd448a9dSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.ftqOffset.foreach(_ := trace.io.out.toEncoder.blocks(i).bits.ftqOffset.getOrElse(0.U))
3033ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.itype := trace.io.out.toEncoder.blocks(i).bits.tracePipe.itype
3043ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.iretire := trace.io.out.toEncoder.blocks(i).bits.tracePipe.iretire
3053ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.ilastsize := trace.io.out.toEncoder.blocks(i).bits.tracePipe.ilastsize
3063ad9f3ddSchengguanghui  })
3074907ec88Schengguanghui  /**
3084907ec88Schengguanghui   * trace end
3094907ec88Schengguanghui   */
3104907ec88Schengguanghui
3114907ec88Schengguanghui
31224519898SXuan Hu  redirectGen.io.hartId := io.fromTop.hartId
31354c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuRedirect.valid := GatedValidRegNext(oldestExuRedirect.valid)
31454c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuRedirect.bits := RegEnable(oldestExuRedirect.bits, oldestExuRedirect.valid)
31551aa1b60Sxiaofeibao-xjtu  redirectGen.io.oldestExuRedirectIsCSR := RegEnable(oldestExuRedirectIsCSR, oldestExuRedirect.valid)
3167da4513bSxiaofeibao  redirectGen.io.instrAddrTransType := RegNext(io.fromCSR.instrAddrTransType)
31754c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuOutPredecode.valid := GatedValidRegNext(oldestExuPredecode.valid)
31854c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuOutPredecode := RegEnable(oldestExuPredecode, oldestExuPredecode.valid)
31924519898SXuan Hu  redirectGen.io.loadReplay <> loadReplay
320a2fa0ad9Sxiaofeibao  val loadRedirectOffset = Mux(memViolation.bits.flushItself(), 0.U, Mux(memViolation.bits.isRVC, 2.U, 4.U))
321a2fa0ad9Sxiaofeibao  val loadRedirectPcFtqOffset = RegEnable((memViolation.bits.ftqOffset << instOffsetBits).asUInt +& loadRedirectOffset, memViolation.valid)
322a2fa0ad9Sxiaofeibao  val loadRedirectPcRead = pcMem.io.rdata(pcMemRdIndexes("redirect").head).startAddr + loadRedirectPcFtqOffset
323a2fa0ad9Sxiaofeibao
32454c6d89dSxiaofeibao-xjtu  redirectGen.io.loadReplay.bits.cfiUpdate.pc := loadRedirectPcRead
325a2fa0ad9Sxiaofeibao  val load_target = loadRedirectPcRead
32654c6d89dSxiaofeibao-xjtu  redirectGen.io.loadReplay.bits.cfiUpdate.target := load_target
32724519898SXuan Hu
32854c6d89dSxiaofeibao-xjtu  redirectGen.io.robFlush := s1_robFlushRedirect
32924519898SXuan Hu
330ff7f931dSXuan Hu  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
3315f8b6c9eSsinceforYy  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
33224519898SXuan Hu  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
33324519898SXuan Hu  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
33424519898SXuan Hu  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
33524519898SXuan Hu  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
33624519898SXuan Hu  for (i <- 0 until CommitWidth) {
33724519898SXuan Hu    // why flushOut: instructions with flushPipe are not commited to frontend
33824519898SXuan Hu    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
33924519898SXuan Hu    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
3405f8b6c9eSsinceforYy    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
34124519898SXuan Hu    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
34224519898SXuan Hu  }
343ff7f931dSXuan Hu  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
344ff7f931dSXuan Hu  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
345ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
346ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
3479342624fSGao-Zeyu
34854c6d89dSxiaofeibao-xjtu  //jmp/brh, sel oldest first, only use one read port
34954c6d89dSxiaofeibao-xjtu  io.frontend.toFtq.ftqIdxAhead(0).valid := RegNext(oldestExuRedirect.valid) && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
35054c6d89dSxiaofeibao-xjtu  io.frontend.toFtq.ftqIdxAhead(0).bits := RegEnable(oldestExuRedirect.bits.ftqIdx, oldestExuRedirect.valid)
3519342624fSGao-Zeyu  //loadreplay
352ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
3539342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
3549342624fSGao-Zeyu  //exception
355ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
3569342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
35705cc2a4eSXuan Hu
35824519898SXuan Hu  // Be careful here:
35924519898SXuan Hu  // T0: rob.io.flushOut, s0_robFlushRedirect
36024519898SXuan Hu  // T1: s1_robFlushRedirect, rob.io.exception.valid
36124519898SXuan Hu  // T2: csr.redirect.valid
36224519898SXuan Hu  // T3: csr.exception.valid
36324519898SXuan Hu  // T4: csr.trapTarget
36424519898SXuan Hu  // T5: ctrlBlock.trapTarget
36524519898SXuan Hu  // T6: io.frontend.toFtq.stage2Redirect.valid
36624519898SXuan Hu  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
36724519898SXuan Hu    s1_robFlushPc, // replay inst
368870f462dSXuan Hu    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
36924519898SXuan Hu  ), s1_robFlushRedirect.valid)
37024519898SXuan Hu  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
371dcdd1406SXuan Hu  private val s5_trapTargetFromCsr = io.robio.csr.trapTarget
37224519898SXuan Hu
373c1b28b66STang Haojin  val flushTarget = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.pc, s2_robFlushPc)
374c1b28b66STang Haojin  val s5_trapTargetIAF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIAF, false.B)
375c1b28b66STang Haojin  val s5_trapTargetIPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIPF, false.B)
376c1b28b66STang Haojin  val s5_trapTargetIGPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIGPF, false.B)
377ff7f931dSXuan Hu  when (s6_flushFromRobValid) {
37824519898SXuan Hu    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
37974f21f21SsinceforYy    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
380c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIAF := RegEnable(s5_trapTargetIAF, s5_flushFromRobValidAhead)
381c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIPF := RegEnable(s5_trapTargetIPF, s5_flushFromRobValidAhead)
382c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIGPF := RegEnable(s5_trapTargetIGPF, s5_flushFromRobValidAhead)
38324519898SXuan Hu  }
38424519898SXuan Hu
3856f483f86SXuan Hu  for (i <- 0 until DecodeWidth) {
3866f483f86SXuan Hu    gpaMem.io.fromIFU := io.frontend.fromIfu
3876f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
3886f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
3896f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
3906f483f86SXuan Hu  }
3916f483f86SXuan Hu
39224519898SXuan Hu  // vtype commit
39315ed99a7SXuan Hu  decode.io.fromCSR := io.fromCSR.toDecode
394d275ad0eSZiyue Zhang  decode.io.fromRob.isResumeVType := rob.io.toDecode.isResumeVType
395d275ad0eSZiyue Zhang  decode.io.fromRob.walkToArchVType := rob.io.toDecode.walkToArchVType
396d275ad0eSZiyue Zhang  decode.io.fromRob.commitVType := rob.io.toDecode.commitVType
397d275ad0eSZiyue Zhang  decode.io.fromRob.walkVType := rob.io.toDecode.walkVType
39824519898SXuan Hu
399e25c13faSXuan Hu  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
40024519898SXuan Hu
401d19fa3e9Sxiaofeibao-xjtu  // add decode Buf for in.ready better timing
402f7fe02a8Sjunxiong-ji  /**
403f7fe02a8Sjunxiong-ji   * Decode buffer: when decode.io.in cannot accept all insts, use this buffer to temporarily store insts that cannot
404f7fe02a8Sjunxiong-ji   * be sent to DecodeStage.
405f7fe02a8Sjunxiong-ji   *
406f7fe02a8Sjunxiong-ji   * Decode buffer is a "DecodeWidth"-element long register Vector of StaticInst (in decodeBufBits), with valid signals
407f7fe02a8Sjunxiong-ji   * (in decodeBufValid). At the same time, fetch insts input from frontend and their valid bits. All valid elements
408f7fe02a8Sjunxiong-ji   * in these two vector of insts are at the beginning, with all invalid vector elements followed.
409f7fe02a8Sjunxiong-ji   *
410f7fe02a8Sjunxiong-ji   * After dealing with redirection, try to use all insts in decode buffer to fulfill decoder.io.in. If decode buffer
411f7fe02a8Sjunxiong-ji   * has no valid insts, use insts from frontend to supply decoder.
412f7fe02a8Sjunxiong-ji   */
413f7fe02a8Sjunxiong-ji
414f7fe02a8Sjunxiong-ji  /** Insts to be decoded, Registers in vector of DecodeWidth */
415d19fa3e9Sxiaofeibao-xjtu  val decodeBufBits = Reg(Vec(DecodeWidth, new StaticInst))
416f7fe02a8Sjunxiong-ji
417f7fe02a8Sjunxiong-ji  /** Valid receiving signals of instructions to be decoded, Registers in vector of DecodeWidth */
418d19fa3e9Sxiaofeibao-xjtu  val decodeBufValid = RegInit(VecInit(Seq.fill(DecodeWidth)(false.B)))
419f7fe02a8Sjunxiong-ji
420f7fe02a8Sjunxiong-ji  /** Insts input from frontend, in vector of DecodeWidth */
421d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontend = io.frontend.cfVec
422f7fe02a8Sjunxiong-ji
423f7fe02a8Sjunxiong-ji  /** Insts in buffer that is not ready but valid in decodeBufValid */
424d19fa3e9Sxiaofeibao-xjtu  val decodeBufNotAccept = VecInit(decodeBufValid.zip(decode.io.in).map(x => x._1 && !x._2.ready))
425f7fe02a8Sjunxiong-ji
426f7fe02a8Sjunxiong-ji  /** Number of insts in decode buffer that is accepted. All accepted insts are before the first unaccepted one. */
427d19fa3e9Sxiaofeibao-xjtu  val decodeBufAcceptNum = PriorityMuxDefault(decodeBufNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
428f7fe02a8Sjunxiong-ji
429f7fe02a8Sjunxiong-ji  /** Input valid insts from frontend that is not ready to be accepted, or decoder prefer insts in decode buffer */
430d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontendNotAccept = VecInit(decodeFromFrontend.zip(decode.io.in).map(x => decodeBufValid(0) || x._1.valid && !x._2.ready))
431f7fe02a8Sjunxiong-ji
432f7fe02a8Sjunxiong-ji  /** Number of input insts that is accepted.
433f7fe02a8Sjunxiong-ji   * All accepted insts are before the first unaccepted one. */
434d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontendAcceptNum = PriorityMuxDefault(decodeFromFrontendNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
435f7fe02a8Sjunxiong-ji
436d19fa3e9Sxiaofeibao-xjtu  if (backendParams.debugEn) {
437d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeBufNotAccept)
438d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeBufAcceptNum)
439d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeFromFrontendNotAccept)
440d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeFromFrontendAcceptNum)
441d19fa3e9Sxiaofeibao-xjtu  }
442f7fe02a8Sjunxiong-ji
443f7fe02a8Sjunxiong-ji  /**
444f7fe02a8Sjunxiong-ji   * State machine of "decodeBufValid(i)":
445f7fe02a8Sjunxiong-ji   *   redirect || decodeBufValid(i) is the last accepted instr in decodeBuf:
446f7fe02a8Sjunxiong-ji   *     false
447f7fe02a8Sjunxiong-ji   *   decodeBufValid(i) is true, decodeBufNotAccept.drop(i) has some true signals
448f7fe02a8Sjunxiong-ji   *     (decodeBufAcceptNum > DecodeWidth-1-i) ? false
449f7fe02a8Sjunxiong-ji   *                                     if not : decodeBufValid(i+decodeBufAcceptNum)
450f7fe02a8Sjunxiong-ji   *     Pop "decodeBufAcceptNum" insts out of the decodeBufValid, and move others forward
451f7fe02a8Sjunxiong-ji   *   decodeBufValid(0) is false, decodeFromFrontendNotAccept.drop(i) has some true signals
452f7fe02a8Sjunxiong-ji   *     (decodeFromFrontendAcceptNum > DecodeWidth-1-i) ? false
453f7fe02a8Sjunxiong-ji   *                                              if not : decodeFromFrontend(i+decodeFromFrontendAcceptNum).valid
454f7fe02a8Sjunxiong-ji   *     Get first "decodeFromFrontendAcceptNum" insts from decodeFromFrontend, and move others to decodeBufValid
455f7fe02a8Sjunxiong-ji   *
456f7fe02a8Sjunxiong-ji   * State machine of "decodeBufBits(i)":
457f7fe02a8Sjunxiong-ji   *   decodeBufValid(i) is true, decodeBufNotAccept.drop(i) has some true signals
458f7fe02a8Sjunxiong-ji   *     decodeBufBits(i+decodeBufAcceptNum)
459f7fe02a8Sjunxiong-ji   *   decodeBufValid(0) is false, decodeFromFrontendNotAccept.drop(i) has some true signals
460f7fe02a8Sjunxiong-ji   *     decodeFromFrontend(i+decodeFromFrontendAcceptNum)
461f7fe02a8Sjunxiong-ji   */
462d19fa3e9Sxiaofeibao-xjtu  for (i <- 0 until DecodeWidth) {
463d19fa3e9Sxiaofeibao-xjtu    // decodeBufValid update
464d19fa3e9Sxiaofeibao-xjtu    when(decode.io.redirect || decodeBufValid(0) && decodeBufValid(i) && decode.io.in(i).ready && !VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
465d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := false.B
466d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
467d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := Mux(decodeBufAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeBufValid(i.U + decodeBufAcceptNum))
468d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
469d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := Mux(decodeFromFrontendAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).valid)
470d19fa3e9Sxiaofeibao-xjtu    }
471d19fa3e9Sxiaofeibao-xjtu    // decodeBufBits update
472d19fa3e9Sxiaofeibao-xjtu    when(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
473d19fa3e9Sxiaofeibao-xjtu      decodeBufBits(i) := decodeBufBits(i.U + decodeBufAcceptNum)
474d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
475d19fa3e9Sxiaofeibao-xjtu      decodeBufBits(i).connectCtrlFlow(decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).bits)
476d19fa3e9Sxiaofeibao-xjtu    }
477d19fa3e9Sxiaofeibao-xjtu  }
478f7fe02a8Sjunxiong-ji  /** Insts input from frontend, in vector of DecodeWidth */
479d19fa3e9Sxiaofeibao-xjtu  val decodeConnectFromFrontend = Wire(Vec(DecodeWidth, new StaticInst))
480d19fa3e9Sxiaofeibao-xjtu  decodeConnectFromFrontend.zip(decodeFromFrontend).map(x => x._1.connectCtrlFlow(x._2.bits))
481f7fe02a8Sjunxiong-ji
482f7fe02a8Sjunxiong-ji  /**
483f7fe02a8Sjunxiong-ji   * DecodeStage's input:
484f7fe02a8Sjunxiong-ji   *   decode.io.in(i).valid:
485f7fe02a8Sjunxiong-ji   *     decodeBufValid(0) is true : decodeBufValid(i)            | from decode buffer
486f7fe02a8Sjunxiong-ji   *                         false : decodeFromFrontend(i).valid  | from frontend
487f7fe02a8Sjunxiong-ji   *
488f7fe02a8Sjunxiong-ji   *   decodeFromFrontend(i).ready:
489f7fe02a8Sjunxiong-ji   *     decodeFromFrontend(0).valid && !decodeBufValid(0) && decodeFromFrontend(i).valid && !decode.io.redirect
490f7fe02a8Sjunxiong-ji   *     valid instr in input, no instr in decode buffer, decodeFromFrontend(i) is valid, no redirection
491f7fe02a8Sjunxiong-ji   *
492f7fe02a8Sjunxiong-ji   *   decode.io.in(i).bits:
493f7fe02a8Sjunxiong-ji   *     decodeBufValid(i) is true : decodeBufBits(i)             | from decode buffer
494f7fe02a8Sjunxiong-ji   *                         false : decodeConnectFromFrontend(i) | from frontend
495f7fe02a8Sjunxiong-ji   */
496d19fa3e9Sxiaofeibao-xjtu  decode.io.in.zipWithIndex.foreach { case (decodeIn, i) =>
497d19fa3e9Sxiaofeibao-xjtu    decodeIn.valid := Mux(decodeBufValid(0), decodeBufValid(i), decodeFromFrontend(i).valid)
498d19fa3e9Sxiaofeibao-xjtu    decodeFromFrontend(i).ready := decodeFromFrontend(0).valid && !decodeBufValid(0) && decodeFromFrontend(i).valid && !decode.io.redirect
499d19fa3e9Sxiaofeibao-xjtu    decodeIn.bits := Mux(decodeBufValid(i), decodeBufBits(i), decodeConnectFromFrontend(i))
50024519898SXuan Hu  }
501f7fe02a8Sjunxiong-ji  /** no valid instr in decode buffer && no valid instr from frontend --> can accept new instr from frontend */
5028506cfc0Sxiaofeibao  io.frontend.canAccept := !decodeBufValid(0) || !decodeFromFrontend(0).valid
50324519898SXuan Hu  decode.io.csrCtrl := RegNext(io.csrCtrl)
50424519898SXuan Hu  decode.io.intRat <> rat.io.intReadPorts
50524519898SXuan Hu  decode.io.fpRat <> rat.io.fpReadPorts
50624519898SXuan Hu  decode.io.vecRat <> rat.io.vecReadPorts
507368cbcecSxiaofeibao  decode.io.v0Rat <> rat.io.v0ReadPorts
508368cbcecSxiaofeibao  decode.io.vlRat <> rat.io.vlReadPorts
50924519898SXuan Hu  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
510870f462dSXuan Hu  decode.io.stallReason.in <> io.frontend.stallReason
51124519898SXuan Hu
512fa7f2c26STang Haojin  // snapshot check
513c4b56310SHaojin Tang  class CFIRobIdx extends Bundle {
514c4b56310SHaojin Tang    val robIdx = Vec(RenameWidth, new RobPtr)
515c4b56310SHaojin Tang    val isCFI = Vec(RenameWidth, Bool())
516c4b56310SHaojin Tang  }
517c4b56310SHaojin Tang  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
518c4b56310SHaojin Tang  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
519c4b56310SHaojin Tang  snpt.io.enq := genSnapshot
520c4b56310SHaojin Tang  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
521c4b56310SHaojin Tang  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
522fa7f2c26STang Haojin  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
523c4b56310SHaojin 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
524c4b56310SHaojin Tang  snpt.io.redirect := s1_s3_redirect.valid
525c4b56310SHaojin Tang  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
526c4b56310SHaojin Tang    val notCFIMask = snapshot.isCFI.map(~_)
52737d77575SzhanglyGit    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
52837d77575SzhanglyGit    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
52937d77575SzhanglyGit    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
530c4b56310SHaojin Tang  })
531a6742963SHaojin Tang  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
532c4b56310SHaojin Tang  snpt.io.flushVec := flushVecNext
533fa7f2c26STang Haojin
534573366c7Sxiaofeibao  val redirectRobidx = s1_s3_redirect.bits.robIdx
535573366c7Sxiaofeibao  val useSnpt = VecInit.tabulate(RenameSnapshotNum){ case idx =>
536573366c7Sxiaofeibao    val snptRobidx = snpt.io.snapshots(idx).robIdx.head
537573366c7Sxiaofeibao    // (redirectRobidx.value =/= snptRobidx.value) for only flag diffrence
538573366c7Sxiaofeibao    snpt.io.valids(idx) && ((redirectRobidx > snptRobidx) && (redirectRobidx.value =/= snptRobidx.value) ||
539573366c7Sxiaofeibao      !s1_s3_redirect.bits.flushItself() && redirectRobidx === snptRobidx)
540573366c7Sxiaofeibao  }.reduceTree(_ || _)
541c61abc0cSXuan Hu  val snptSelect = MuxCase(
542c61abc0cSXuan Hu    0.U(log2Ceil(RenameSnapshotNum).W),
5433aa6fb4dSxiaofeibao    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map{case idx =>
5443aa6fb4dSxiaofeibao      val thisSnapRobidx = snpt.io.snapshots(idx).robIdx.head
5453aa6fb4dSxiaofeibao      (snpt.io.valids(idx) && (redirectRobidx > thisSnapRobidx && (redirectRobidx.value =/= thisSnapRobidx.value) ||
5463aa6fb4dSxiaofeibao        !s1_s3_redirect.bits.flushItself() && redirectRobidx === thisSnapRobidx), idx)
5473aa6fb4dSxiaofeibao    }
548c61abc0cSXuan Hu  )
549fa7f2c26STang Haojin
550fa7f2c26STang Haojin  rob.io.snpt.snptEnq := DontCare
551fa7f2c26STang Haojin  rob.io.snpt.snptDeq := snpt.io.deq
552fa7f2c26STang Haojin  rob.io.snpt.useSnpt := useSnpt
553fa7f2c26STang Haojin  rob.io.snpt.snptSelect := snptSelect
554c4b56310SHaojin Tang  rob.io.snpt.flushVec := flushVecNext
555c4b56310SHaojin Tang  rat.io.snpt.snptEnq := genSnapshot
556fa7f2c26STang Haojin  rat.io.snpt.snptDeq := snpt.io.deq
557fa7f2c26STang Haojin  rat.io.snpt.useSnpt := useSnpt
558fa7f2c26STang Haojin  rat.io.snpt.snptSelect := snptSelect
559c4b56310SHaojin Tang  rat.io.snpt.flushVec := flushVec
560fa7f2c26STang Haojin
56162d7c919SGuanghui Cheng  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec.asUInt.orR || (!TriggerAction.isNone(x.bits.trigger)))
56224519898SXuan Hu  // fusion decoder
563*bbb50258STang Haojin  fusionDecoder.io.disableFusion := disableFusion
56424519898SXuan Hu  for (i <- 0 until DecodeWidth) {
565*bbb50258STang Haojin    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !decodeHasException(i)
56624519898SXuan Hu    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
56724519898SXuan Hu    if (i > 0) {
56824519898SXuan Hu      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
56924519898SXuan Hu    }
57024519898SXuan Hu  }
57124519898SXuan Hu
57224519898SXuan Hu  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
57324519898SXuan Hu  for (i <- 0 until RenameWidth) {
574b9a37d2fSXuan Hu    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
57524519898SXuan Hu      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
57624519898SXuan Hu
57724519898SXuan Hu    decodePipeRename(i).ready := rename.io.in(i).ready
57824519898SXuan Hu    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
57924519898SXuan Hu    rename.io.in(i).bits := decodePipeRename(i).bits
5800a7d1d5cSxiaofeibao    dispatch.io.renameIn(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i) && !decodePipeRename(i).bits.isMove
5810a7d1d5cSxiaofeibao    dispatch.io.renameIn(i).bits := decodePipeRename(i).bits
58224519898SXuan Hu  }
58324519898SXuan Hu
58424519898SXuan Hu  for (i <- 0 until RenameWidth - 1) {
58524519898SXuan Hu    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
58624519898SXuan Hu    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
58724519898SXuan Hu
58824519898SXuan Hu    // update the first RenameWidth - 1 instructions
58924519898SXuan Hu    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
59024519898SXuan Hu    // TODO: remove this dirty code for ftq update
59124519898SXuan Hu    val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
59224519898SXuan Hu    val ftqOffset0 = rename.io.in(i).bits.ftqOffset
59324519898SXuan Hu    val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
59424519898SXuan Hu    val ftqOffsetDiff = ftqOffset1 - ftqOffset0
59524519898SXuan Hu    val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
59624519898SXuan Hu    val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
59724519898SXuan Hu    val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
59824519898SXuan Hu    val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
5998b33cd30Sklin02    when (fusionDecoder.io.out(i).valid) {
6008b33cd30Sklin02      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
6018b33cd30Sklin02      fusionDecoder.io.out(i).bits.update(dispatch.io.renameIn(i).bits)
60224519898SXuan Hu      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
60324519898SXuan Hu    }
6048b33cd30Sklin02    XSError(fusionDecoder.io.out(i).valid && !cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
60524519898SXuan Hu  }
60624519898SXuan Hu
60724519898SXuan Hu  // memory dependency predict
60824519898SXuan Hu  // when decode, send fold pc to mdp
6099477429fSsinceforYy  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
61024519898SXuan Hu  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
61124519898SXuan Hu  for (i <- 0 until DecodeWidth) {
6129477429fSsinceforYy    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
61324519898SXuan Hu    mdpFlodPcVec(i) := Mux(
61424519898SXuan Hu      decode.io.out(i).fire,
61524519898SXuan Hu      decode.io.in(i).bits.foldpc,
61624519898SXuan Hu      rename.io.in(i).bits.foldpc
61724519898SXuan Hu    )
61824519898SXuan Hu  }
61924519898SXuan Hu
62024519898SXuan Hu  // currently, we only update mdp info when isReplay
62124519898SXuan Hu  memCtrl.io.redirect := s1_s3_redirect
62224519898SXuan Hu  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
62324519898SXuan Hu  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
62424519898SXuan Hu  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
6259477429fSsinceforYy  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
62624519898SXuan Hu  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
62724519898SXuan Hu  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
62824519898SXuan Hu
62924519898SXuan Hu  rat.io.redirect := s1_s3_redirect.valid
6306b102a39SHaojin Tang  rat.io.rabCommits := rob.io.rabCommits
631cda1c534Sxiaofeibao-xjtu  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
63224519898SXuan Hu  rat.io.intRenamePorts := rename.io.intRenamePorts
63324519898SXuan Hu  rat.io.fpRenamePorts := rename.io.fpRenamePorts
63424519898SXuan Hu  rat.io.vecRenamePorts := rename.io.vecRenamePorts
635368cbcecSxiaofeibao  rat.io.v0RenamePorts := rename.io.v0RenamePorts
636368cbcecSxiaofeibao  rat.io.vlRenamePorts := rename.io.vlRenamePorts
63724519898SXuan Hu
63824519898SXuan Hu  rename.io.redirect := s1_s3_redirect
6396b102a39SHaojin Tang  rename.io.rabCommits := rob.io.rabCommits
640a3fe955fSGuanghui Cheng  rename.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
64124519898SXuan Hu  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
64224519898SXuan Hu    RegEnable(waittable2rename, decodeOut.fire)
64324519898SXuan Hu  }
64424519898SXuan Hu  rename.io.ssit := memCtrl.io.ssit2Rename
6456dbc37d2Sxiaofeibao  // disble mdp
6466dbc37d2Sxiaofeibao  dispatch.io.lfst.resp := 0.U.asTypeOf(dispatch.io.lfst.resp)
6476dbc37d2Sxiaofeibao  rename.io.waittable := 0.U.asTypeOf(rename.io.waittable)
6486dbc37d2Sxiaofeibao  rename.io.ssit := 0.U.asTypeOf(rename.io.ssit)
64924519898SXuan Hu  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
65024519898SXuan Hu  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
65124519898SXuan Hu  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
652368cbcecSxiaofeibao  rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data)))
653368cbcecSxiaofeibao  rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data)))
654dcf3a679STang Haojin  rename.io.int_need_free := rat.io.int_need_free
655dcf3a679STang Haojin  rename.io.int_old_pdest := rat.io.int_old_pdest
656dcf3a679STang Haojin  rename.io.fp_old_pdest := rat.io.fp_old_pdest
6573cf50307SZiyue Zhang  rename.io.vec_old_pdest := rat.io.vec_old_pdest
658368cbcecSxiaofeibao  rename.io.v0_old_pdest := rat.io.v0_old_pdest
659368cbcecSxiaofeibao  rename.io.vl_old_pdest := rat.io.vl_old_pdest
660b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
661b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
662b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
663368cbcecSxiaofeibao  rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get)
664368cbcecSxiaofeibao  rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get)
665d2b20d1aSTang Haojin  rename.io.stallReason.in <> decode.io.stallReason.out
666870f462dSXuan Hu  rename.io.snpt.snptEnq := DontCare
667870f462dSXuan Hu  rename.io.snpt.snptDeq := snpt.io.deq
668870f462dSXuan Hu  rename.io.snpt.useSnpt := useSnpt
669870f462dSXuan Hu  rename.io.snpt.snptSelect := snptSelect
670bb7e6e3aSxiaofeibao-xjtu  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
671c4b56310SHaojin Tang  rename.io.snpt.flushVec := flushVecNext
672c4b56310SHaojin Tang  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
673c4b56310SHaojin Tang  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
674870f462dSXuan Hu
675870f462dSXuan Hu  val renameOut = Wire(chiselTypeOf(rename.io.out))
676870f462dSXuan Hu  renameOut <> rename.io.out
677ac78003fSzhanglyGit  // pass all snapshot in the first element for correctness of blockBackward
678ac78003fSzhanglyGit  renameOut.tail.foreach(_.bits.snapshot := false.B)
679ac78003fSzhanglyGit  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
680ac78003fSzhanglyGit    false.B,
681ac78003fSzhanglyGit    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
682ac78003fSzhanglyGit  )
683ac78003fSzhanglyGit
684ac78003fSzhanglyGit  // pipeline between rename and dispatch
685f5c17053Sxiaofeibao-xjtu  PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch")
686ff3fcdf1Sxiaofeibao-xjtu
68724519898SXuan Hu  dispatch.io.redirect := s1_s3_redirect
68835b3b30bSxiaofeibao  val enqRob = Wire(chiselTypeOf(rob.io.enq))
68935b3b30bSxiaofeibao  enqRob.canAccept := rob.io.enq.canAccept
69035b3b30bSxiaofeibao  enqRob.canAcceptForDispatch := rob.io.enq.canAcceptForDispatch
69135b3b30bSxiaofeibao  enqRob.isEmpty := rob.io.enq.isEmpty
69235b3b30bSxiaofeibao  enqRob.resp := rob.io.enq.resp
69335b3b30bSxiaofeibao  enqRob.needAlloc := RegNext(dispatch.io.enqRob.needAlloc)
69435b3b30bSxiaofeibao  enqRob.req.zip(dispatch.io.enqRob.req).map { case (sink, source) =>
69535b3b30bSxiaofeibao    sink.valid := RegNext(source.valid && !rob.io.redirect.valid)
69635b3b30bSxiaofeibao    sink.bits := RegEnable(source.bits, source.valid)
69735b3b30bSxiaofeibao  }
69835b3b30bSxiaofeibao  dispatch.io.enqRob.canAccept := enqRob.canAcceptForDispatch && !enqRob.req.map(x => x.valid && x.bits.blockBackward && enqRob.canAccept).reduce(_ || _)
69935b3b30bSxiaofeibao  dispatch.io.enqRob.canAcceptForDispatch := enqRob.canAcceptForDispatch
70035b3b30bSxiaofeibao  dispatch.io.enqRob.isEmpty := enqRob.isEmpty && !enqRob.req.map(_.valid).reduce(_ || _)
70135b3b30bSxiaofeibao  dispatch.io.enqRob.resp := enqRob.resp
70235b3b30bSxiaofeibao  rob.io.enq.needAlloc := enqRob.needAlloc
70335b3b30bSxiaofeibao  rob.io.enq.req := enqRob.req
704d2b20d1aSTang Haojin  dispatch.io.robHead := rob.io.debugRobHead
705d2b20d1aSTang Haojin  dispatch.io.stallReason <> rename.io.stallReason.out
706d2b20d1aSTang Haojin  dispatch.io.lqCanAccept := io.lqCanAccept
707d2b20d1aSTang Haojin  dispatch.io.sqCanAccept := io.sqCanAccept
7080a7d1d5cSxiaofeibao  dispatch.io.fromMem.lcommit := io.fromMemToDispatch.lcommit
7090a7d1d5cSxiaofeibao  dispatch.io.fromMem.scommit := io.fromMemToDispatch.scommit
7100a7d1d5cSxiaofeibao  dispatch.io.fromMem.lqDeqPtr := io.fromMemToDispatch.lqDeqPtr
7110a7d1d5cSxiaofeibao  dispatch.io.fromMem.sqDeqPtr := io.fromMemToDispatch.sqDeqPtr
7120a7d1d5cSxiaofeibao  dispatch.io.fromMem.lqCancelCnt := io.fromMemToDispatch.lqCancelCnt
7130a7d1d5cSxiaofeibao  dispatch.io.fromMem.sqCancelCnt := io.fromMemToDispatch.sqCancelCnt
7140a7d1d5cSxiaofeibao  io.toMem.lsqEnqIO <> dispatch.io.toMem.lsqEnqIO
7150a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpInt := io.toDispatch.wakeUpInt
7160a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpFp  := io.toDispatch.wakeUpFp
7170a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpVec := io.toDispatch.wakeUpVec
7180a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpMem := io.toDispatch.wakeUpMem
7190a7d1d5cSxiaofeibao  dispatch.io.IQValidNumVec := io.toDispatch.IQValidNumVec
7200a7d1d5cSxiaofeibao  dispatch.io.ldCancel := io.toDispatch.ldCancel
7210a7d1d5cSxiaofeibao  dispatch.io.og0Cancel := io.toDispatch.og0Cancel
7220a7d1d5cSxiaofeibao  dispatch.io.wbPregsInt := io.toDispatch.wbPregsInt
7230a7d1d5cSxiaofeibao  dispatch.io.wbPregsFp := io.toDispatch.wbPregsFp
7240a7d1d5cSxiaofeibao  dispatch.io.wbPregsVec := io.toDispatch.wbPregsVec
7250a7d1d5cSxiaofeibao  dispatch.io.wbPregsV0 := io.toDispatch.wbPregsV0
7260a7d1d5cSxiaofeibao  dispatch.io.wbPregsVl := io.toDispatch.wbPregsVl
7277edcfc93SZiyue Zhang  dispatch.io.vlWriteBackInfo := io.toDispatch.vlWriteBackInfo
728d2b20d1aSTang Haojin  dispatch.io.robHeadNotReady := rob.io.headNotReady
729d2b20d1aSTang Haojin  dispatch.io.robFull := rob.io.robFull
7305f8b6c9eSsinceforYy  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
73124519898SXuan Hu
7320a7d1d5cSxiaofeibao  val toIssueBlockUops = Seq(io.toIssueBlock.intUops, io.toIssueBlock.fpUops, io.toIssueBlock.vfUops, io.toIssueBlock.memUops).flatten
7330a7d1d5cSxiaofeibao  toIssueBlockUops.zip(dispatch.io.toIssueQueues).map(x => x._1 <> x._2)
73424519898SXuan Hu  io.toIssueBlock.flush   <> s2_s4_redirect
73524519898SXuan Hu
7365f8b6c9eSsinceforYy  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
737f533cba7SHuSipeng  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
7383827c997SsinceforYy  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
73924519898SXuan Hu
74024519898SXuan Hu  io.toDataPath.flush := s2_s4_redirect
74124519898SXuan Hu  io.toExuBlock.flush := s2_s4_redirect
74224519898SXuan Hu
74324519898SXuan Hu
74424519898SXuan Hu  rob.io.hartId := io.fromTop.hartId
74524519898SXuan Hu  rob.io.redirect := s1_s3_redirect
74624519898SXuan Hu  rob.io.writeback := delayedNotFlushedWriteBack
747bd5909d0Sxiaofeibao-xjtu  rob.io.exuWriteback := delayedWriteBack
74885f51ecaSxiaofeibao-xjtu  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
749571677c9Sxiaofeibao-xjtu  rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush
7506f483f86SXuan Hu  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
751b9a37d2fSXuan Hu  rob.io.fromVecExcpMod.busy := io.fromVecExcpMod.busy
75224519898SXuan Hu
75324519898SXuan Hu  io.redirect := s1_s3_redirect
75424519898SXuan Hu
75524519898SXuan Hu  // rob to int block
75624519898SXuan Hu  io.robio.csr <> rob.io.csr
75724519898SXuan Hu  // When wfi is disabled, it will not block ROB commit.
75824519898SXuan Hu  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
75924519898SXuan Hu  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
76024519898SXuan Hu
76124519898SXuan Hu  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
76224519898SXuan Hu
76324519898SXuan Hu  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
76424519898SXuan Hu  io.robio.exception := rob.io.exception
76524519898SXuan Hu  io.robio.exception.bits.pc := s1_robFlushPc
76624519898SXuan Hu
76724519898SXuan Hu  // rob to mem block
76824519898SXuan Hu  io.robio.lsq <> rob.io.lsq
76924519898SXuan Hu
77063d67ef3STang Haojin  io.diff_int_rat.foreach(_ := rat.io.diff_int_rat.get)
77163d67ef3STang Haojin  io.diff_fp_rat .foreach(_ := rat.io.diff_fp_rat.get)
77263d67ef3STang Haojin  io.diff_vec_rat.foreach(_ := rat.io.diff_vec_rat.get)
77363d67ef3STang Haojin  io.diff_v0_rat .foreach(_ := rat.io.diff_v0_rat.get)
77463d67ef3STang Haojin  io.diff_vl_rat .foreach(_ := rat.io.diff_vl_rat.get)
77524519898SXuan Hu
77617b21f45SHaojin Tang  rob.io.debug_ls := io.robio.debug_ls
77717b21f45SHaojin Tang  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
77817b21f45SHaojin Tang  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
779a751b11aSchengguanghui  rob.io.csr.criticalErrorState := io.robio.csr.criticalErrorState
7806ce10964SXuan Hu  rob.io.debugEnqLsq := io.debugEnqLsq
7816ce10964SXuan Hu
78217b21f45SHaojin Tang  io.robio.robDeqPtr := rob.io.robDeqPtr
7838744445eSMaxpicca-Li
7841bf9a598SAnzo  io.robio.storeDebugInfo <> rob.io.storeDebugInfo
7851bf9a598SAnzo
7867e4f0b19SZiyue-Zhang  // rob to backend
7877e4f0b19SZiyue-Zhang  io.robio.commitVType := rob.io.toDecode.commitVType
7887e4f0b19SZiyue-Zhang  // exu block to decode
789d8a50338SZiyue Zhang  decode.io.vsetvlVType := io.toDecode.vsetvlVType
7905110577fSZiyue Zhang  // backend to decode
7915110577fSZiyue Zhang  decode.io.vstart := io.toDecode.vstart
7925110577fSZiyue Zhang  // backend to rob
7935110577fSZiyue Zhang  rob.io.vstartIsZero := io.toDecode.vstart === 0.U
7947e4f0b19SZiyue-Zhang
79592c61038SXuan Hu  io.toCSR.trapInstInfo := decode.io.toCSR.trapInstInfo
79692c61038SXuan Hu
797e43bb916SXuan Hu  io.toVecExcpMod.logicPhyRegMap := rob.io.toVecExcpMod.logicPhyRegMap
798e43bb916SXuan Hu  io.toVecExcpMod.excpInfo       := rob.io.toVecExcpMod.excpInfo
799e43bb916SXuan Hu  // T  : rat receive rabCommit
800e43bb916SXuan Hu  // T+1: rat return oldPdest
801e43bb916SXuan Hu  io.toVecExcpMod.ratOldPest match {
802e43bb916SXuan Hu    case fromRat =>
803e43bb916SXuan Hu      (0 until RabCommitWidth).foreach { idx =>
804ea7e6d59Sxiaofeibao        val v0Valid = RegNext(
805e43bb916SXuan Hu          rat.io.rabCommits.isCommit &&
806e43bb916SXuan Hu          rat.io.rabCommits.isWalk &&
807e43bb916SXuan Hu          rat.io.rabCommits.commitValid(idx) &&
808e43bb916SXuan Hu          rat.io.rabCommits.info(idx).v0Wen
809e43bb916SXuan Hu        )
810ea7e6d59Sxiaofeibao        fromRat.v0OldVdPdest(idx).valid := RegNext(v0Valid)
811ea7e6d59Sxiaofeibao        fromRat.v0OldVdPdest(idx).bits := RegEnable(rat.io.v0_old_pdest(idx), v0Valid)
812ea7e6d59Sxiaofeibao        val vecValid = RegNext(
813e43bb916SXuan Hu          rat.io.rabCommits.isCommit &&
814e43bb916SXuan Hu          rat.io.rabCommits.isWalk &&
815e43bb916SXuan Hu          rat.io.rabCommits.commitValid(idx) &&
816e43bb916SXuan Hu          rat.io.rabCommits.info(idx).vecWen
817e43bb916SXuan Hu        )
818ea7e6d59Sxiaofeibao        fromRat.vecOldVdPdest(idx).valid := RegNext(vecValid)
819ea7e6d59Sxiaofeibao        fromRat.vecOldVdPdest(idx).bits := RegEnable(rat.io.vec_old_pdest(idx), vecValid)
820e43bb916SXuan Hu      }
821e43bb916SXuan Hu  }
822e43bb916SXuan Hu
82360ebee38STang Haojin  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
82460ebee38STang Haojin  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
82560ebee38STang Haojin  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
8267cf78eb2Shappy-lx  io.debugRolling := rob.io.debugRolling
82760ebee38STang Haojin
8285f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
8290a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.intdqFull := false.B
8300a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.fpdqFull := false.B
8310a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.lsdqFull := false.B
83224519898SXuan Hu
8330a7d1d5cSxiaofeibao  val perfEvents = Seq(decode, rename, dispatch, rob).flatMap(_.getPerfEvents)
83424519898SXuan Hu  generatePerfEvent()
83585a8d7caSZehao Liu
83685a8d7caSZehao Liu  val criticalErrors = rob.getCriticalErrors
83785a8d7caSZehao Liu  generateCriticalErrors()
83824519898SXuan Hu}
83924519898SXuan Hu
84024519898SXuan Huclass CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
84124519898SXuan Hu  val fromTop = new Bundle {
84224519898SXuan Hu    val hartId = Input(UInt(8.W))
84324519898SXuan Hu  }
84424519898SXuan Hu  val toTop = new Bundle {
84524519898SXuan Hu    val cpuHalt = Output(Bool())
84624519898SXuan Hu  }
84724519898SXuan Hu  val frontend = Flipped(new FrontendToCtrlIO())
84815ed99a7SXuan Hu  val fromCSR = new Bundle{
84915ed99a7SXuan Hu    val toDecode = Input(new CSRToDecode)
850c308d936Schengguanghui    val traceCSR = Input(new TraceCSR)
8517da4513bSxiaofeibao    val instrAddrTransType = Input(new AddrTransType)
85215ed99a7SXuan Hu  }
85324519898SXuan Hu  val toIssueBlock = new Bundle {
85424519898SXuan Hu    val flush = ValidIO(new Redirect)
8550a7d1d5cSxiaofeibao    val intUopsNum = backendParams.intSchdParams.get.issueBlockParams.map(_.numEnq).sum
8560a7d1d5cSxiaofeibao    val fpUopsNum = backendParams.fpSchdParams.get.issueBlockParams.map(_.numEnq).sum
8570a7d1d5cSxiaofeibao    val vfUopsNum = backendParams.vfSchdParams.get.issueBlockParams.map(_.numEnq).sum
8580a7d1d5cSxiaofeibao    val memUopsNum = backendParams.memSchdParams.get.issueBlockParams.filter(x => x.StdCnt == 0).map(_.numEnq).sum
8590a7d1d5cSxiaofeibao    val intUops = Vec(intUopsNum, DecoupledIO(new DynInst))
8600a7d1d5cSxiaofeibao    val fpUops = Vec(fpUopsNum, DecoupledIO(new DynInst))
8610a7d1d5cSxiaofeibao    val vfUops = Vec(vfUopsNum, DecoupledIO(new DynInst))
8620a7d1d5cSxiaofeibao    val memUops = Vec(memUopsNum, DecoupledIO(new DynInst))
8630a7d1d5cSxiaofeibao  }
8640a7d1d5cSxiaofeibao  val fromMemToDispatch = new Bundle {
8650a7d1d5cSxiaofeibao    val lcommit = Input(UInt(log2Up(CommitWidth + 1).W))
8660a7d1d5cSxiaofeibao    val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) // connected to `memBlock.io.sqDeq` instead of ROB
8670a7d1d5cSxiaofeibao    val lqDeqPtr = Input(new LqPtr)
8680a7d1d5cSxiaofeibao    val sqDeqPtr = Input(new SqPtr)
8690a7d1d5cSxiaofeibao    // from lsq
8700a7d1d5cSxiaofeibao    val lqCancelCnt = Input(UInt(log2Up(VirtualLoadQueueSize + 1).W))
8710a7d1d5cSxiaofeibao    val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W))
8720a7d1d5cSxiaofeibao  }
8730a7d1d5cSxiaofeibao  //toMem
8740a7d1d5cSxiaofeibao  val toMem = new Bundle {
8750a7d1d5cSxiaofeibao    val lsqEnqIO = Flipped(new LsqEnqIO)
8760a7d1d5cSxiaofeibao  }
8770a7d1d5cSxiaofeibao  val toDispatch = new Bundle {
8780a7d1d5cSxiaofeibao    val wakeUpInt = Flipped(backendParams.intSchdParams.get.genIQWakeUpOutValidBundle)
8790a7d1d5cSxiaofeibao    val wakeUpFp  = Flipped(backendParams.fpSchdParams.get.genIQWakeUpOutValidBundle)
8800a7d1d5cSxiaofeibao    val wakeUpVec = Flipped(backendParams.vfSchdParams.get.genIQWakeUpOutValidBundle)
8810a7d1d5cSxiaofeibao    val wakeUpMem = Flipped(backendParams.memSchdParams.get.genIQWakeUpOutValidBundle)
8820a7d1d5cSxiaofeibao    val allIssueParams = backendParams.allIssueParams.filter(_.StdCnt == 0)
8830a7d1d5cSxiaofeibao    val allExuParams = allIssueParams.map(_.exuBlockParams).flatten
8840a7d1d5cSxiaofeibao    val exuNum = allExuParams.size
8850a7d1d5cSxiaofeibao    val maxIQSize = allIssueParams.map(_.numEntries).max
8860a7d1d5cSxiaofeibao    val IQValidNumVec = Vec(exuNum, Input(UInt(maxIQSize.U.getWidth.W)))
8870a7d1d5cSxiaofeibao    val og0Cancel = Input(ExuVec())
8880a7d1d5cSxiaofeibao    val ldCancel = Vec(backendParams.LdExuCnt, Flipped(new LoadCancelIO))
8890a7d1d5cSxiaofeibao    val wbPregsInt = Vec(backendParams.numPregWb(IntData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
8900a7d1d5cSxiaofeibao    val wbPregsFp = Vec(backendParams.numPregWb(FpData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
8910a7d1d5cSxiaofeibao    val wbPregsVec = Vec(backendParams.numPregWb(VecData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
8920a7d1d5cSxiaofeibao    val wbPregsV0 = Vec(backendParams.numPregWb(V0Data()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
8930a7d1d5cSxiaofeibao    val wbPregsVl = Vec(backendParams.numPregWb(VlData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
8947edcfc93SZiyue Zhang    val vlWriteBackInfo = new Bundle {
8957edcfc93SZiyue Zhang      val vlFromIntIsZero  = Input(Bool())
8967edcfc93SZiyue Zhang      val vlFromIntIsVlmax = Input(Bool())
8977edcfc93SZiyue Zhang      val vlFromVfIsZero   = Input(Bool())
8987edcfc93SZiyue Zhang      val vlFromVfIsVlmax  = Input(Bool())
8997edcfc93SZiyue Zhang    }
90024519898SXuan Hu  }
90124519898SXuan Hu  val toDataPath = new Bundle {
90224519898SXuan Hu    val flush = ValidIO(new Redirect)
903c37914a4Sxiaofeibao    val pcToDataPathIO = new PcToDataPathIO(params)
90424519898SXuan Hu  }
90524519898SXuan Hu  val toExuBlock = new Bundle {
90624519898SXuan Hu    val flush = ValidIO(new Redirect)
90724519898SXuan Hu  }
90892c61038SXuan Hu  val toCSR = new Bundle {
90992c61038SXuan Hu    val trapInstInfo = Output(ValidIO(new TrapInstInfo))
91092c61038SXuan Hu  }
91124519898SXuan Hu  val fromWB = new Bundle {
91224519898SXuan Hu    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
91324519898SXuan Hu  }
91424519898SXuan Hu  val redirect = ValidIO(new Redirect)
91524519898SXuan Hu  val fromMem = new Bundle {
916272ec6b1SHaojin Tang    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
91724519898SXuan Hu    val violation = Flipped(ValidIO(new Redirect))
91824519898SXuan Hu  }
91983ba63b3SXuan Hu  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
920b133b458SXuan Hu  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
9214b0d80d8SXuan Hu
92224519898SXuan Hu  val csrCtrl = Input(new CustomCSRCtrlIO)
92324519898SXuan Hu  val robio = new Bundle {
92424519898SXuan Hu    val csr = new RobCSRIO
92524519898SXuan Hu    val exception = ValidIO(new ExceptionInfo)
92624519898SXuan Hu    val lsq = new RobLsqIO
9276810d1e8Ssfencevma    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
9282326221cSXuan Hu    val debug_ls = Input(new DebugLSIO())
92917b21f45SHaojin Tang    val robHeadLsIssue = Input(Bool())
93017b21f45SHaojin Tang    val robDeqPtr = Output(new RobPtr)
9317e4f0b19SZiyue-Zhang    val commitVType = new Bundle {
9327e4f0b19SZiyue-Zhang      val vtype = Output(ValidIO(VType()))
9337e4f0b19SZiyue-Zhang      val hasVsetvl = Output(Bool())
9347e4f0b19SZiyue-Zhang    }
9351bf9a598SAnzo
9361bf9a598SAnzo    // store event difftest information
9371bf9a598SAnzo    val storeDebugInfo = Vec(EnsbufferWidth, new Bundle {
9381bf9a598SAnzo      val robidx = Input(new RobPtr)
9391bf9a598SAnzo      val pc     = Output(UInt(VAddrBits.W))
9401bf9a598SAnzo    })
94124519898SXuan Hu  }
94224519898SXuan Hu
943d8a50338SZiyue Zhang  val toDecode = new Bundle {
944d8a50338SZiyue Zhang    val vsetvlVType = Input(VType())
9455110577fSZiyue Zhang    val vstart = Input(Vl())
946d8a50338SZiyue Zhang  }
947d8a50338SZiyue Zhang
948e43bb916SXuan Hu  val fromVecExcpMod = Input(new Bundle {
949e43bb916SXuan Hu    val busy = Bool()
950e43bb916SXuan Hu  })
951e43bb916SXuan Hu
952e43bb916SXuan Hu  val toVecExcpMod = Output(new Bundle {
953e43bb916SXuan Hu    val logicPhyRegMap = Vec(RabCommitWidth, ValidIO(new RegWriteFromRab))
954e43bb916SXuan Hu    val excpInfo = ValidIO(new VecExcpInfo)
955e43bb916SXuan Hu    val ratOldPest = new RatToVecExcpMod
956e43bb916SXuan Hu  })
957e43bb916SXuan Hu
958fd448a9dSchengguanghui  val traceCoreInterface = new TraceCoreInterface(hasOffset = true)
9594907ec88Schengguanghui
96024519898SXuan Hu  val perfInfo = Output(new Bundle{
96124519898SXuan Hu    val ctrlInfo = new Bundle {
96224519898SXuan Hu      val robFull   = Bool()
96324519898SXuan Hu      val intdqFull = Bool()
96424519898SXuan Hu      val fpdqFull  = Bool()
96524519898SXuan Hu      val lsdqFull  = Bool()
96624519898SXuan Hu    }
96724519898SXuan Hu  })
96863d67ef3STang Haojin  val diff_int_rat = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
96963d67ef3STang Haojin  val diff_fp_rat  = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
97063d67ef3STang Haojin  val diff_vec_rat = if (params.basicDebugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None
97163d67ef3STang Haojin  val diff_v0_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
97263d67ef3STang Haojin  val diff_vl_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
97324519898SXuan Hu
974c61abc0cSXuan Hu  val sqCanAccept = Input(Bool())
975c61abc0cSXuan Hu  val lqCanAccept = Input(Bool())
9764b0d80d8SXuan Hu
9774b0d80d8SXuan Hu  val debugTopDown = new Bundle {
9784b0d80d8SXuan Hu    val fromRob = new RobCoreTopDownIO
9794b0d80d8SXuan Hu    val fromCore = new CoreDispatchTopDownIO
9804b0d80d8SXuan Hu  }
9814b0d80d8SXuan Hu  val debugRolling = new RobDebugRollingIO
9826ce10964SXuan Hu  val debugEnqLsq = Input(new LsqEnqIO)
98324519898SXuan Hu}
98424519898SXuan Hu
98524519898SXuan Huclass NamedIndexes(namedCnt: Seq[(String, Int)]) {
98624519898SXuan Hu  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
98724519898SXuan Hu
98824519898SXuan Hu  val maxIdx = namedCnt.map(_._2).sum
98924519898SXuan Hu  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
99024519898SXuan Hu    val begin = namedCnt.slice(0, i).map(_._2).sum
99124519898SXuan Hu    val end = begin + namedCnt(i)._2
99224519898SXuan Hu    (namedCnt(i)._1, (begin, end))
99324519898SXuan Hu  }.toMap
99424519898SXuan Hu
99524519898SXuan Hu  def apply(name: String): Seq[Int] = {
99624519898SXuan Hu    require(nameRangeMap.contains(name))
99724519898SXuan Hu    nameRangeMap(name)._1 until nameRangeMap(name)._2
99824519898SXuan Hu  }
99924519898SXuan Hu}
1000