xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 0a7d1d5cc74078a0d2fe9270a78ac80db6cb1ad0)
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._
27*0a7d1d5cSxiaofeibaoimport xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput, ExuVec, StaticInst, TrapInstInfo}
282326221cSXuan Huimport xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator}
29*0a7d1d5cSxiaofeibaoimport xiangshan.backend.datapath.DataConfig.{FpData, IntData, V0Data, VAddrData, VecData, VlData}
3024519898SXuan Huimport xiangshan.backend.decode.{DecodeStage, FusionDecoder}
3183ba63b3SXuan Huimport xiangshan.backend.dispatch.{CoreDispatchTopDownIO, Dispatch, DispatchQueue}
32*0a7d1d5cSxiaofeibaoimport xiangshan.backend.dispatch.NewDispatch
3324519898SXuan Huimport xiangshan.backend.fu.PFEvent
345110577fSZiyue Zhangimport xiangshan.backend.fu.vector.Bundles.{VType, Vl}
3515ed99a7SXuan Huimport xiangshan.backend.fu.wrapper.CSRToDecode
36870f462dSXuan Huimport xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator}
3783ba63b3SXuan Huimport xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr}
386ce10964SXuan Huimport xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
39*0a7d1d5cSxiaofeibaoimport xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr}
4015ed99a7SXuan Huimport xiangshan.backend.issue.{FpScheduler, IntScheduler, MemScheduler, VfScheduler}
414907ec88Schengguanghuiimport xiangshan.backend.trace._
4224519898SXuan Hu
4324519898SXuan Huclass CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
4424519898SXuan Hu  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
4524519898SXuan Hu  val redirect = Valid(new Redirect)
469342624fSGao-Zeyu  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
479342624fSGao-Zeyu  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
4824519898SXuan Hu}
4924519898SXuan Hu
5024519898SXuan Huclass CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
511ca4a39dSXuan Hu  override def shouldBeInlined: Boolean = false
521ca4a39dSXuan Hu
5324519898SXuan Hu  val rob = LazyModule(new Rob(params))
5424519898SXuan Hu
5524519898SXuan Hu  lazy val module = new CtrlBlockImp(this)(p, params)
5624519898SXuan Hu
576f483f86SXuan Hu  val gpaMem = LazyModule(new GPAMem())
5824519898SXuan Hu}
5924519898SXuan Hu
6024519898SXuan Huclass CtrlBlockImp(
6124519898SXuan Hu  override val wrapper: CtrlBlock
6224519898SXuan Hu)(implicit
6324519898SXuan Hu  p: Parameters,
6424519898SXuan Hu  params: BackendParams
6524519898SXuan Hu) extends LazyModuleImp(wrapper)
6624519898SXuan Hu  with HasXSParameter
6724519898SXuan Hu  with HasCircularQueuePtrHelper
6824519898SXuan Hu  with HasPerfEvents
6985a8d7caSZehao Liu  with HasCriticalErrors
7024519898SXuan Hu{
7124519898SXuan Hu  val pcMemRdIndexes = new NamedIndexes(Seq(
7224519898SXuan Hu    "redirect"  -> 1,
7324519898SXuan Hu    "memPred"   -> 1,
7424519898SXuan Hu    "robFlush"  -> 1,
7524519898SXuan Hu    "load"      -> params.LduCnt,
76b133b458SXuan Hu    "hybrid"    -> params.HyuCnt,
774907ec88Schengguanghui    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0),
784907ec88Schengguanghui    "trace"     -> TraceGroupNum
7924519898SXuan Hu  ))
8024519898SXuan Hu
8124519898SXuan Hu  private val numPcMemReadForExu = params.numPcReadPort
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  println(s"pcMem read num for exu: $numPcMemReadForExu")
8724519898SXuan Hu
8824519898SXuan Hu  val io = IO(new CtrlBlockIO())
8924519898SXuan Hu
90*0a7d1d5cSxiaofeibao  val dispatch = Module(new NewDispatch)
916f483f86SXuan Hu  val gpaMem = wrapper.gpaMem.module
9224519898SXuan Hu  val decode = Module(new DecodeStage)
9324519898SXuan Hu  val fusionDecoder = Module(new FusionDecoder)
9424519898SXuan Hu  val rat = Module(new RenameTableWrapper)
9524519898SXuan Hu  val rename = Module(new Rename)
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
12754c6d89dSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_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
134bd5909d0Sxiaofeibao-xjtu  private val delayedWriteBack = Wire(chiselTypeOf(io.fromWB.wbData))
135bd5909d0Sxiaofeibao-xjtu  delayedWriteBack.zipWithIndex.map{ case (x,i) =>
136bd5909d0Sxiaofeibao-xjtu    x.valid := GatedValidRegNext(io.fromWB.wbData(i).valid)
137bd5909d0Sxiaofeibao-xjtu    x.bits := delayedNotFlushedWriteBack(i).bits
138bd5909d0Sxiaofeibao-xjtu  }
139571677c9Sxiaofeibao-xjtu  val delayedNotFlushedWriteBackNeedFlush = Wire(Vec(params.allExuParams.filter(_.needExceptionGen).length, Bool()))
140571677c9Sxiaofeibao-xjtu  delayedNotFlushedWriteBackNeedFlush := delayedNotFlushedWriteBack.filter(_.bits.params.needExceptionGen).map{ x =>
141571677c9Sxiaofeibao-xjtu    x.bits.exceptionVec.get.asUInt.orR || x.bits.flushPipe.getOrElse(false.B) || x.bits.replay.getOrElse(false.B) ||
1427e0f64b0SGuanghui Cheng      (if (x.bits.trigger.nonEmpty) TriggerAction.isDmode(x.bits.trigger.get) else false.B)
143571677c9Sxiaofeibao-xjtu  }
14424519898SXuan Hu
14585f51ecaSxiaofeibao-xjtu  val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu)
14647c01b71Sxiaofeibao-xjtu  val intScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[IntScheduler])
1475e7a1fcaSxiaofeibao  val fpScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[FpScheduler])
14847c01b71Sxiaofeibao-xjtu  val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler])
149618b89e6Slewislzh  val intCanCompress = intScheWbData.filter(_.bits.params.CanCompress)
150618b89e6Slewislzh  val i2vWbData = intScheWbData.filter(_.bits.params.writeVecRf)
151618b89e6Slewislzh  val f2vWbData = fpScheWbData.filter(_.bits.params.writeVecRf)
15247c01b71Sxiaofeibao-xjtu  val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu)
15385f51ecaSxiaofeibao-xjtu  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
15485f51ecaSxiaofeibao-xjtu    val valid = x.valid
15585f51ecaSxiaofeibao-xjtu    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
15685f51ecaSxiaofeibao-xjtu    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
1575f8b6c9eSsinceforYy    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
158618b89e6Slewislzh    val isIntSche = intCanCompress.contains(x)
1595e7a1fcaSxiaofeibao    val isFpSche = fpScheWbData.contains(x)
16047c01b71Sxiaofeibao-xjtu    val isVfSche = vfScheWbData.contains(x)
16147c01b71Sxiaofeibao-xjtu    val isMemVload = memVloadWbData.contains(x)
162618b89e6Slewislzh    val isi2v = i2vWbData.contains(x)
163618b89e6Slewislzh    val isf2v = f2vWbData.contains(x)
164618b89e6Slewislzh    val canSameRobidxWbData = if(isVfSche) {
165618b89e6Slewislzh      i2vWbData ++ f2vWbData ++ vfScheWbData
166618b89e6Slewislzh    } else if(isi2v) {
167618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
168618b89e6Slewislzh    } else if (isf2v) {
169618b89e6Slewislzh      intCanCompress ++ fpScheWbData ++ vfScheWbData
170618b89e6Slewislzh    } else if (isIntSche) {
171618b89e6Slewislzh      intCanCompress ++ fpScheWbData
1725e7a1fcaSxiaofeibao    } else if (isFpSche) {
173618b89e6Slewislzh      intCanCompress ++ fpScheWbData
17447c01b71Sxiaofeibao-xjtu    }  else if (isMemVload) {
17547c01b71Sxiaofeibao-xjtu      memVloadWbData
17647c01b71Sxiaofeibao-xjtu    } else {
17747c01b71Sxiaofeibao-xjtu      Seq(x)
17847c01b71Sxiaofeibao-xjtu    }
17947c01b71Sxiaofeibao-xjtu    val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => {
18085f51ecaSxiaofeibao-xjtu      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
18185f51ecaSxiaofeibao-xjtu      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
18285f51ecaSxiaofeibao-xjtu    }).toSeq)
18341dbbdfdSsinceforYy    delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid)
18485f51ecaSxiaofeibao-xjtu    delayed
18585f51ecaSxiaofeibao-xjtu  }).toSeq
18685f51ecaSxiaofeibao-xjtu
18724519898SXuan Hu  private val exuPredecode = VecInit(
18854c6d89dSxiaofeibao-xjtu    io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
18924519898SXuan Hu  )
19024519898SXuan Hu
19154c6d89dSxiaofeibao-xjtu  private val exuRedirects: Seq[ValidIO[Redirect]] = io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => {
19224519898SXuan Hu    val out = Wire(Valid(new Redirect()))
19354c6d89dSxiaofeibao-xjtu    out.valid := x.valid && x.bits.redirect.get.valid && x.bits.redirect.get.bits.cfiUpdate.isMisPred && !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
19724519898SXuan Hu    out
19883ba63b3SXuan Hu  }).toSeq
19954c6d89dSxiaofeibao-xjtu  private val oldestOneHot = Redirect.selectOldestRedirect(exuRedirects)
20054c6d89dSxiaofeibao-xjtu  private val oldestExuRedirect = Mux1H(oldestOneHot, exuRedirects)
20154c6d89dSxiaofeibao-xjtu  private val oldestExuPredecode = Mux1H(oldestOneHot, exuPredecode)
20224519898SXuan Hu
20324519898SXuan Hu  private val memViolation = io.fromMem.violation
20424519898SXuan Hu  val loadReplay = Wire(ValidIO(new Redirect))
20554c6d89dSxiaofeibao-xjtu  loadReplay.valid := GatedValidRegNext(memViolation.valid)
20624519898SXuan Hu  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
207a63155a6SXuan Hu  loadReplay.bits.debugIsCtrl := false.B
208a63155a6SXuan Hu  loadReplay.bits.debugIsMemVio := true.B
20924519898SXuan Hu
21054c6d89dSxiaofeibao-xjtu  pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := memViolation.valid
21154c6d89dSxiaofeibao-xjtu  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := memViolation.bits.ftqIdx.value
21254c6d89dSxiaofeibao-xjtu  pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := memViolation.valid
21354c6d89dSxiaofeibao-xjtu  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := memViolation.bits.stFtqIdx.value
21454c6d89dSxiaofeibao-xjtu  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegEnable(memViolation.bits.stFtqOffset, memViolation.valid))
21524519898SXuan Hu
21624519898SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
2178241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
21854c6d89dSxiaofeibao-xjtu    pcMem.io.ren.get(pcMemIdx) := io.memLdPcRead(i).valid
21924519898SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
22054c6d89dSxiaofeibao-xjtu    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memLdPcRead(i).offset, io.memLdPcRead(i).valid))
22124519898SXuan Hu  }
22224519898SXuan Hu
223b133b458SXuan Hu  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
2248241cb85SXuan Hu    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
22554c6d89dSxiaofeibao-xjtu    pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).valid
226b133b458SXuan Hu    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
22754c6d89dSxiaofeibao-xjtu    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).valid))
228b133b458SXuan Hu  }
229b133b458SXuan Hu
2304b0d80d8SXuan Hu  if (EnableStorePrefetchSMS) {
2314b0d80d8SXuan Hu    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
23254c6d89dSxiaofeibao-xjtu      pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).valid
2334b0d80d8SXuan Hu      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
23454c6d89dSxiaofeibao-xjtu      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).valid))
2354b0d80d8SXuan Hu    }
2364b0d80d8SXuan Hu  } else {
23783ba63b3SXuan Hu    io.memStPcRead.foreach(_.data := 0.U)
2384b0d80d8SXuan Hu  }
2394b0d80d8SXuan Hu
2404907ec88Schengguanghui  /**
2414907ec88Schengguanghui   * trace begin
2424907ec88Schengguanghui   */
2434907ec88Schengguanghui  val trace = Module(new Trace)
244c308d936Schengguanghui  trace.io.in.fromEncoder.stall  := io.traceCoreInterface.fromEncoder.stall
245c308d936Schengguanghui  trace.io.in.fromEncoder.enable := io.traceCoreInterface.fromEncoder.enable
246c308d936Schengguanghui  trace.io.in.fromRob            := rob.io.trace.traceCommitInfo
247c308d936Schengguanghui  rob.io.trace.blockCommit       := trace.io.out.blockRobCommit
2484907ec88Schengguanghui
2494907ec88Schengguanghui  for ((pcMemIdx, i) <- pcMemRdIndexes("trace").zipWithIndex) {
250c308d936Schengguanghui    val traceValid = trace.toPcMem.blocks(i).valid
2514907ec88Schengguanghui    pcMem.io.ren.get(pcMemIdx) := traceValid
252c308d936Schengguanghui    pcMem.io.raddr(pcMemIdx) := trace.toPcMem.blocks(i).bits.ftqIdx.get.value
253c308d936Schengguanghui    trace.io.in.fromPcMem(i) := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(trace.toPcMem.blocks(i).bits.ftqOffset.get, traceValid))
2544907ec88Schengguanghui  }
2554907ec88Schengguanghui
2568cbf000bSchengguanghui  // Trap/Xret only occur in block(0).
257c308d936Schengguanghui  val tracePriv = Mux(Itype.isTrapOrXret(trace.toEncoder.blocks(0).bits.tracePipe.itype),
258c308d936Schengguanghui    io.fromCSR.traceCSR.lastPriv,
259c308d936Schengguanghui    io.fromCSR.traceCSR.currentPriv
260c308d936Schengguanghui  )
2613ad9f3ddSchengguanghui  io.traceCoreInterface.toEncoder.trap.cause := io.fromCSR.traceCSR.cause.asUInt
2623ad9f3ddSchengguanghui  io.traceCoreInterface.toEncoder.trap.tval  := io.fromCSR.traceCSR.tval.asUInt
263c308d936Schengguanghui  io.traceCoreInterface.toEncoder.priv       := tracePriv
2643ad9f3ddSchengguanghui  (0 until TraceGroupNum).foreach(i => {
2653ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).valid := trace.io.out.toEncoder.blocks(i).valid
2663ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.iaddr := trace.io.out.toEncoder.blocks(i).bits.iaddr.getOrElse(0.U)
2673ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.itype := trace.io.out.toEncoder.blocks(i).bits.tracePipe.itype
2683ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.iretire := trace.io.out.toEncoder.blocks(i).bits.tracePipe.iretire
2693ad9f3ddSchengguanghui    io.traceCoreInterface.toEncoder.groups(i).bits.ilastsize := trace.io.out.toEncoder.blocks(i).bits.tracePipe.ilastsize
2703ad9f3ddSchengguanghui  })
2714907ec88Schengguanghui  /**
2724907ec88Schengguanghui   * trace end
2734907ec88Schengguanghui   */
2744907ec88Schengguanghui
2754907ec88Schengguanghui
27624519898SXuan Hu  redirectGen.io.hartId := io.fromTop.hartId
27754c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuRedirect.valid := GatedValidRegNext(oldestExuRedirect.valid)
27854c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuRedirect.bits := RegEnable(oldestExuRedirect.bits, oldestExuRedirect.valid)
27954c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuOutPredecode.valid := GatedValidRegNext(oldestExuPredecode.valid)
28054c6d89dSxiaofeibao-xjtu  redirectGen.io.oldestExuOutPredecode := RegEnable(oldestExuPredecode, oldestExuPredecode.valid)
28124519898SXuan Hu  redirectGen.io.loadReplay <> loadReplay
28254c6d89dSxiaofeibao-xjtu  val loadRedirectPcRead = pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(memViolation.bits.ftqOffset, memViolation.valid))
28354c6d89dSxiaofeibao-xjtu  redirectGen.io.loadReplay.bits.cfiUpdate.pc := loadRedirectPcRead
28454c6d89dSxiaofeibao-xjtu  val load_pc_offset = Mux(loadReplay.bits.flushItself(), 0.U, Mux(loadReplay.bits.isRVC, 2.U, 4.U))
28554c6d89dSxiaofeibao-xjtu  val load_target = loadRedirectPcRead + load_pc_offset
28654c6d89dSxiaofeibao-xjtu  redirectGen.io.loadReplay.bits.cfiUpdate.target := load_target
28724519898SXuan Hu
28854c6d89dSxiaofeibao-xjtu  redirectGen.io.robFlush := s1_robFlushRedirect
28924519898SXuan Hu
290ff7f931dSXuan Hu  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
2915f8b6c9eSsinceforYy  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
29224519898SXuan Hu  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
29324519898SXuan Hu  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
29424519898SXuan Hu  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
29524519898SXuan Hu  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
29624519898SXuan Hu  for (i <- 0 until CommitWidth) {
29724519898SXuan Hu    // why flushOut: instructions with flushPipe are not commited to frontend
29824519898SXuan Hu    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
29924519898SXuan Hu    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
3005f8b6c9eSsinceforYy    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
30124519898SXuan Hu    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
30224519898SXuan Hu  }
303ff7f931dSXuan Hu  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
304ff7f931dSXuan Hu  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
305ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
306ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
3079342624fSGao-Zeyu
30854c6d89dSxiaofeibao-xjtu  //jmp/brh, sel oldest first, only use one read port
30954c6d89dSxiaofeibao-xjtu  io.frontend.toFtq.ftqIdxAhead(0).valid := RegNext(oldestExuRedirect.valid) && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
31054c6d89dSxiaofeibao-xjtu  io.frontend.toFtq.ftqIdxAhead(0).bits := RegEnable(oldestExuRedirect.bits.ftqIdx, oldestExuRedirect.valid)
3119342624fSGao-Zeyu  //loadreplay
312ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
3139342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
3149342624fSGao-Zeyu  //exception
315ff7f931dSXuan Hu  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
3169342624fSGao-Zeyu  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
31705cc2a4eSXuan Hu
31824519898SXuan Hu  // Be careful here:
31924519898SXuan Hu  // T0: rob.io.flushOut, s0_robFlushRedirect
32024519898SXuan Hu  // T1: s1_robFlushRedirect, rob.io.exception.valid
32124519898SXuan Hu  // T2: csr.redirect.valid
32224519898SXuan Hu  // T3: csr.exception.valid
32324519898SXuan Hu  // T4: csr.trapTarget
32424519898SXuan Hu  // T5: ctrlBlock.trapTarget
32524519898SXuan Hu  // T6: io.frontend.toFtq.stage2Redirect.valid
32624519898SXuan Hu  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
32724519898SXuan Hu    s1_robFlushPc, // replay inst
328870f462dSXuan Hu    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
32924519898SXuan Hu  ), s1_robFlushRedirect.valid)
33024519898SXuan Hu  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
331dcdd1406SXuan Hu  private val s5_trapTargetFromCsr = io.robio.csr.trapTarget
33224519898SXuan Hu
333c1b28b66STang Haojin  val flushTarget = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.pc, s2_robFlushPc)
334c1b28b66STang Haojin  val s5_trapTargetIAF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIAF, false.B)
335c1b28b66STang Haojin  val s5_trapTargetIPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIPF, false.B)
336c1b28b66STang Haojin  val s5_trapTargetIGPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIGPF, false.B)
337ff7f931dSXuan Hu  when (s6_flushFromRobValid) {
33824519898SXuan Hu    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
33974f21f21SsinceforYy    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
340c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIAF := RegEnable(s5_trapTargetIAF, s5_flushFromRobValidAhead)
341c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIPF := RegEnable(s5_trapTargetIPF, s5_flushFromRobValidAhead)
342c1b28b66STang Haojin    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIGPF := RegEnable(s5_trapTargetIGPF, s5_flushFromRobValidAhead)
34324519898SXuan Hu  }
34424519898SXuan Hu
3456f483f86SXuan Hu  for (i <- 0 until DecodeWidth) {
3466f483f86SXuan Hu    gpaMem.io.fromIFU := io.frontend.fromIfu
3476f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
3486f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
3496f483f86SXuan Hu    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
3506f483f86SXuan Hu  }
3516f483f86SXuan Hu
35224519898SXuan Hu  // vtype commit
35315ed99a7SXuan Hu  decode.io.fromCSR := io.fromCSR.toDecode
354d275ad0eSZiyue Zhang  decode.io.fromRob.isResumeVType := rob.io.toDecode.isResumeVType
355d275ad0eSZiyue Zhang  decode.io.fromRob.walkToArchVType := rob.io.toDecode.walkToArchVType
356d275ad0eSZiyue Zhang  decode.io.fromRob.commitVType := rob.io.toDecode.commitVType
357d275ad0eSZiyue Zhang  decode.io.fromRob.walkVType := rob.io.toDecode.walkVType
35824519898SXuan Hu
359e25c13faSXuan Hu  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
36024519898SXuan Hu
361d19fa3e9Sxiaofeibao-xjtu  // add decode Buf for in.ready better timing
362d19fa3e9Sxiaofeibao-xjtu  val decodeBufBits = Reg(Vec(DecodeWidth, new StaticInst))
363d19fa3e9Sxiaofeibao-xjtu  val decodeBufValid = RegInit(VecInit(Seq.fill(DecodeWidth)(false.B)))
364d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontend = io.frontend.cfVec
365d19fa3e9Sxiaofeibao-xjtu  val decodeBufNotAccept = VecInit(decodeBufValid.zip(decode.io.in).map(x => x._1 && !x._2.ready))
366d19fa3e9Sxiaofeibao-xjtu  val decodeBufAcceptNum = PriorityMuxDefault(decodeBufNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
367d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontendNotAccept = VecInit(decodeFromFrontend.zip(decode.io.in).map(x => decodeBufValid(0) || x._1.valid && !x._2.ready))
368d19fa3e9Sxiaofeibao-xjtu  val decodeFromFrontendAcceptNum = PriorityMuxDefault(decodeFromFrontendNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
369d19fa3e9Sxiaofeibao-xjtu  if (backendParams.debugEn) {
370d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeBufNotAccept)
371d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeBufAcceptNum)
372d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeFromFrontendNotAccept)
373d19fa3e9Sxiaofeibao-xjtu    dontTouch(decodeFromFrontendAcceptNum)
374d19fa3e9Sxiaofeibao-xjtu  }
375d19fa3e9Sxiaofeibao-xjtu  val a = decodeBufNotAccept.drop(2)
376d19fa3e9Sxiaofeibao-xjtu  for (i <- 0 until DecodeWidth) {
377d19fa3e9Sxiaofeibao-xjtu    // decodeBufValid update
378d19fa3e9Sxiaofeibao-xjtu    when(decode.io.redirect || decodeBufValid(0) && decodeBufValid(i) && decode.io.in(i).ready && !VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
379d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := false.B
380d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
381d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := Mux(decodeBufAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeBufValid(i.U + decodeBufAcceptNum))
382d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
383d19fa3e9Sxiaofeibao-xjtu      decodeBufValid(i) := Mux(decodeFromFrontendAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).valid)
384d19fa3e9Sxiaofeibao-xjtu    }
385d19fa3e9Sxiaofeibao-xjtu    // decodeBufBits update
386d19fa3e9Sxiaofeibao-xjtu    when(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
387d19fa3e9Sxiaofeibao-xjtu      decodeBufBits(i) := decodeBufBits(i.U + decodeBufAcceptNum)
388d19fa3e9Sxiaofeibao-xjtu    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
389d19fa3e9Sxiaofeibao-xjtu      decodeBufBits(i).connectCtrlFlow(decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).bits)
390d19fa3e9Sxiaofeibao-xjtu    }
391d19fa3e9Sxiaofeibao-xjtu  }
392d19fa3e9Sxiaofeibao-xjtu  val decodeConnectFromFrontend = Wire(Vec(DecodeWidth, new StaticInst))
393d19fa3e9Sxiaofeibao-xjtu  decodeConnectFromFrontend.zip(decodeFromFrontend).map(x => x._1.connectCtrlFlow(x._2.bits))
394d19fa3e9Sxiaofeibao-xjtu  decode.io.in.zipWithIndex.foreach { case (decodeIn, i) =>
395d19fa3e9Sxiaofeibao-xjtu    decodeIn.valid := Mux(decodeBufValid(0), decodeBufValid(i), decodeFromFrontend(i).valid)
396d19fa3e9Sxiaofeibao-xjtu    decodeFromFrontend(i).ready := decodeFromFrontend(0).valid && !decodeBufValid(0) && decodeFromFrontend(i).valid && !decode.io.redirect
397d19fa3e9Sxiaofeibao-xjtu    decodeIn.bits := Mux(decodeBufValid(i), decodeBufBits(i), decodeConnectFromFrontend(i))
39824519898SXuan Hu  }
3998506cfc0Sxiaofeibao  io.frontend.canAccept := !decodeBufValid(0) || !decodeFromFrontend(0).valid
40024519898SXuan Hu  decode.io.csrCtrl := RegNext(io.csrCtrl)
40124519898SXuan Hu  decode.io.intRat <> rat.io.intReadPorts
40224519898SXuan Hu  decode.io.fpRat <> rat.io.fpReadPorts
40324519898SXuan Hu  decode.io.vecRat <> rat.io.vecReadPorts
404368cbcecSxiaofeibao  decode.io.v0Rat <> rat.io.v0ReadPorts
405368cbcecSxiaofeibao  decode.io.vlRat <> rat.io.vlReadPorts
40624519898SXuan Hu  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
407870f462dSXuan Hu  decode.io.stallReason.in <> io.frontend.stallReason
40824519898SXuan Hu
409fa7f2c26STang Haojin  // snapshot check
410c4b56310SHaojin Tang  class CFIRobIdx extends Bundle {
411c4b56310SHaojin Tang    val robIdx = Vec(RenameWidth, new RobPtr)
412c4b56310SHaojin Tang    val isCFI = Vec(RenameWidth, Bool())
413c4b56310SHaojin Tang  }
414c4b56310SHaojin Tang  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
415c4b56310SHaojin Tang  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
416c4b56310SHaojin Tang  snpt.io.enq := genSnapshot
417c4b56310SHaojin Tang  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
418c4b56310SHaojin Tang  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
419fa7f2c26STang Haojin  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
420c4b56310SHaojin 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
421c4b56310SHaojin Tang  snpt.io.redirect := s1_s3_redirect.valid
422c4b56310SHaojin Tang  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
423c4b56310SHaojin Tang    val notCFIMask = snapshot.isCFI.map(~_)
42437d77575SzhanglyGit    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
42537d77575SzhanglyGit    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
42637d77575SzhanglyGit    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
427c4b56310SHaojin Tang  })
428a6742963SHaojin Tang  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
429c4b56310SHaojin Tang  snpt.io.flushVec := flushVecNext
430fa7f2c26STang Haojin
431fa7f2c26STang Haojin  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
432780712aaSxiaofeibao-xjtu    snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
433780712aaSxiaofeibao-xjtu      !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head)
434c61abc0cSXuan Hu  ).reduceTree(_ || _)
435c61abc0cSXuan Hu  val snptSelect = MuxCase(
436c61abc0cSXuan Hu    0.U(log2Ceil(RenameSnapshotNum).W),
437fa7f2c26STang Haojin    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
438780712aaSxiaofeibao-xjtu      (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
439780712aaSxiaofeibao-xjtu        !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx)
440c61abc0cSXuan Hu    )
441c61abc0cSXuan Hu  )
442fa7f2c26STang Haojin
443fa7f2c26STang Haojin  rob.io.snpt.snptEnq := DontCare
444fa7f2c26STang Haojin  rob.io.snpt.snptDeq := snpt.io.deq
445fa7f2c26STang Haojin  rob.io.snpt.useSnpt := useSnpt
446fa7f2c26STang Haojin  rob.io.snpt.snptSelect := snptSelect
447c4b56310SHaojin Tang  rob.io.snpt.flushVec := flushVecNext
448c4b56310SHaojin Tang  rat.io.snpt.snptEnq := genSnapshot
449fa7f2c26STang Haojin  rat.io.snpt.snptDeq := snpt.io.deq
450fa7f2c26STang Haojin  rat.io.snpt.useSnpt := useSnpt
451fa7f2c26STang Haojin  rat.io.snpt.snptSelect := snptSelect
452c4b56310SHaojin Tang  rat.io.snpt.flushVec := flushVec
453fa7f2c26STang Haojin
45424519898SXuan Hu  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
45524519898SXuan Hu  // fusion decoder
45624519898SXuan Hu  for (i <- 0 until DecodeWidth) {
45724519898SXuan Hu    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
45824519898SXuan Hu    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
45924519898SXuan Hu    if (i > 0) {
46024519898SXuan Hu      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
46124519898SXuan Hu    }
46224519898SXuan Hu  }
46324519898SXuan Hu
46424519898SXuan Hu  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
46524519898SXuan Hu  for (i <- 0 until RenameWidth) {
466b9a37d2fSXuan Hu    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
46724519898SXuan Hu      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
46824519898SXuan Hu
46924519898SXuan Hu    decodePipeRename(i).ready := rename.io.in(i).ready
47024519898SXuan Hu    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
47124519898SXuan Hu    rename.io.in(i).bits := decodePipeRename(i).bits
472*0a7d1d5cSxiaofeibao    dispatch.io.renameIn(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i) && !decodePipeRename(i).bits.isMove
473*0a7d1d5cSxiaofeibao    dispatch.io.renameIn(i).bits := decodePipeRename(i).bits
47424519898SXuan Hu  }
47524519898SXuan Hu
47624519898SXuan Hu  for (i <- 0 until RenameWidth - 1) {
47724519898SXuan Hu    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
47824519898SXuan Hu    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
47924519898SXuan Hu
48024519898SXuan Hu    // update the first RenameWidth - 1 instructions
48124519898SXuan Hu    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
48224519898SXuan Hu    when (fusionDecoder.io.out(i).valid) {
48324519898SXuan Hu      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
484*0a7d1d5cSxiaofeibao      fusionDecoder.io.out(i).bits.update(dispatch.io.renameIn(i).bits)
48524519898SXuan Hu      // TODO: remove this dirty code for ftq update
48624519898SXuan Hu      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
48724519898SXuan Hu      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
48824519898SXuan Hu      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
48924519898SXuan Hu      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
49024519898SXuan Hu      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
49124519898SXuan Hu      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
49224519898SXuan Hu      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
49324519898SXuan Hu      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
49424519898SXuan Hu      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
49524519898SXuan Hu      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
49624519898SXuan Hu    }
49724519898SXuan Hu
49824519898SXuan Hu  }
49924519898SXuan Hu
50024519898SXuan Hu  // memory dependency predict
50124519898SXuan Hu  // when decode, send fold pc to mdp
5029477429fSsinceforYy  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
50324519898SXuan Hu  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
50424519898SXuan Hu  for (i <- 0 until DecodeWidth) {
5059477429fSsinceforYy    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
50624519898SXuan Hu    mdpFlodPcVec(i) := Mux(
50724519898SXuan Hu      decode.io.out(i).fire,
50824519898SXuan Hu      decode.io.in(i).bits.foldpc,
50924519898SXuan Hu      rename.io.in(i).bits.foldpc
51024519898SXuan Hu    )
51124519898SXuan Hu  }
51224519898SXuan Hu
51324519898SXuan Hu  // currently, we only update mdp info when isReplay
51424519898SXuan Hu  memCtrl.io.redirect := s1_s3_redirect
51524519898SXuan Hu  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
51624519898SXuan Hu  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
51724519898SXuan Hu  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
5189477429fSsinceforYy  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
51924519898SXuan Hu  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
52024519898SXuan Hu  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
52124519898SXuan Hu
52224519898SXuan Hu  rat.io.redirect := s1_s3_redirect.valid
5236b102a39SHaojin Tang  rat.io.rabCommits := rob.io.rabCommits
524cda1c534Sxiaofeibao-xjtu  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
52524519898SXuan Hu  rat.io.intRenamePorts := rename.io.intRenamePorts
52624519898SXuan Hu  rat.io.fpRenamePorts := rename.io.fpRenamePorts
52724519898SXuan Hu  rat.io.vecRenamePorts := rename.io.vecRenamePorts
528368cbcecSxiaofeibao  rat.io.v0RenamePorts := rename.io.v0RenamePorts
529368cbcecSxiaofeibao  rat.io.vlRenamePorts := rename.io.vlRenamePorts
53024519898SXuan Hu
53124519898SXuan Hu  rename.io.redirect := s1_s3_redirect
5326b102a39SHaojin Tang  rename.io.rabCommits := rob.io.rabCommits
533a3fe955fSGuanghui Cheng  rename.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
53424519898SXuan Hu  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
53524519898SXuan Hu    RegEnable(waittable2rename, decodeOut.fire)
53624519898SXuan Hu  }
53724519898SXuan Hu  rename.io.ssit := memCtrl.io.ssit2Rename
53824519898SXuan Hu  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
53924519898SXuan Hu  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
54024519898SXuan Hu  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
541368cbcecSxiaofeibao  rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data)))
542368cbcecSxiaofeibao  rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data)))
543dcf3a679STang Haojin  rename.io.int_need_free := rat.io.int_need_free
544dcf3a679STang Haojin  rename.io.int_old_pdest := rat.io.int_old_pdest
545dcf3a679STang Haojin  rename.io.fp_old_pdest := rat.io.fp_old_pdest
5463cf50307SZiyue Zhang  rename.io.vec_old_pdest := rat.io.vec_old_pdest
547368cbcecSxiaofeibao  rename.io.v0_old_pdest := rat.io.v0_old_pdest
548368cbcecSxiaofeibao  rename.io.vl_old_pdest := rat.io.vl_old_pdest
549b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
550b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
551b7d9e8d5Sxiaofeibao-xjtu  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
552368cbcecSxiaofeibao  rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get)
553368cbcecSxiaofeibao  rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get)
554d2b20d1aSTang Haojin  rename.io.stallReason.in <> decode.io.stallReason.out
555870f462dSXuan Hu  rename.io.snpt.snptEnq := DontCare
556870f462dSXuan Hu  rename.io.snpt.snptDeq := snpt.io.deq
557870f462dSXuan Hu  rename.io.snpt.useSnpt := useSnpt
558870f462dSXuan Hu  rename.io.snpt.snptSelect := snptSelect
559bb7e6e3aSxiaofeibao-xjtu  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
560c4b56310SHaojin Tang  rename.io.snpt.flushVec := flushVecNext
561c4b56310SHaojin Tang  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
562c4b56310SHaojin Tang  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
563870f462dSXuan Hu
564870f462dSXuan Hu  val renameOut = Wire(chiselTypeOf(rename.io.out))
565870f462dSXuan Hu  renameOut <> rename.io.out
566ac78003fSzhanglyGit  // pass all snapshot in the first element for correctness of blockBackward
567ac78003fSzhanglyGit  renameOut.tail.foreach(_.bits.snapshot := false.B)
568ac78003fSzhanglyGit  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
569ac78003fSzhanglyGit    false.B,
570ac78003fSzhanglyGit    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
571ac78003fSzhanglyGit  )
572ac78003fSzhanglyGit
573ac78003fSzhanglyGit  // pipeline between rename and dispatch
574f5c17053Sxiaofeibao-xjtu  PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch")
575ff3fcdf1Sxiaofeibao-xjtu
57624519898SXuan Hu  dispatch.io.redirect := s1_s3_redirect
57724519898SXuan Hu  dispatch.io.enqRob <> rob.io.enq
578d2b20d1aSTang Haojin  dispatch.io.robHead := rob.io.debugRobHead
579d2b20d1aSTang Haojin  dispatch.io.stallReason <> rename.io.stallReason.out
580d2b20d1aSTang Haojin  dispatch.io.lqCanAccept := io.lqCanAccept
581d2b20d1aSTang Haojin  dispatch.io.sqCanAccept := io.sqCanAccept
582*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.lcommit := io.fromMemToDispatch.lcommit
583*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.scommit := io.fromMemToDispatch.scommit
584*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.lqDeqPtr := io.fromMemToDispatch.lqDeqPtr
585*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.sqDeqPtr := io.fromMemToDispatch.sqDeqPtr
586*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.lqCancelCnt := io.fromMemToDispatch.lqCancelCnt
587*0a7d1d5cSxiaofeibao  dispatch.io.fromMem.sqCancelCnt := io.fromMemToDispatch.sqCancelCnt
588*0a7d1d5cSxiaofeibao  io.toMem.lsqEnqIO <> dispatch.io.toMem.lsqEnqIO
589*0a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpInt := io.toDispatch.wakeUpInt
590*0a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpFp  := io.toDispatch.wakeUpFp
591*0a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpVec := io.toDispatch.wakeUpVec
592*0a7d1d5cSxiaofeibao  dispatch.io.wakeUpAll.wakeUpMem := io.toDispatch.wakeUpMem
593*0a7d1d5cSxiaofeibao  dispatch.io.IQValidNumVec := io.toDispatch.IQValidNumVec
594*0a7d1d5cSxiaofeibao  dispatch.io.ldCancel := io.toDispatch.ldCancel
595*0a7d1d5cSxiaofeibao  dispatch.io.og0Cancel := io.toDispatch.og0Cancel
596*0a7d1d5cSxiaofeibao  dispatch.io.wbPregsInt := io.toDispatch.wbPregsInt
597*0a7d1d5cSxiaofeibao  dispatch.io.wbPregsFp := io.toDispatch.wbPregsFp
598*0a7d1d5cSxiaofeibao  dispatch.io.wbPregsVec := io.toDispatch.wbPregsVec
599*0a7d1d5cSxiaofeibao  dispatch.io.wbPregsV0 := io.toDispatch.wbPregsV0
600*0a7d1d5cSxiaofeibao  dispatch.io.wbPregsVl := io.toDispatch.wbPregsVl
601d2b20d1aSTang Haojin  dispatch.io.robHeadNotReady := rob.io.headNotReady
602d2b20d1aSTang Haojin  dispatch.io.robFull := rob.io.robFull
6035f8b6c9eSsinceforYy  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
60424519898SXuan Hu
605*0a7d1d5cSxiaofeibao  val toIssueBlockUops = Seq(io.toIssueBlock.intUops, io.toIssueBlock.fpUops, io.toIssueBlock.vfUops, io.toIssueBlock.memUops).flatten
606*0a7d1d5cSxiaofeibao  toIssueBlockUops.zip(dispatch.io.toIssueQueues).map(x => x._1 <> x._2)
60724519898SXuan Hu  io.toIssueBlock.flush   <> s2_s4_redirect
60824519898SXuan Hu
6095f8b6c9eSsinceforYy  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
610f533cba7SHuSipeng  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
6113827c997SsinceforYy  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
61224519898SXuan Hu
61324519898SXuan Hu  io.toDataPath.flush := s2_s4_redirect
61424519898SXuan Hu  io.toExuBlock.flush := s2_s4_redirect
61524519898SXuan Hu
61624519898SXuan Hu
61724519898SXuan Hu  rob.io.hartId := io.fromTop.hartId
61824519898SXuan Hu  rob.io.redirect := s1_s3_redirect
61924519898SXuan Hu  rob.io.writeback := delayedNotFlushedWriteBack
620bd5909d0Sxiaofeibao-xjtu  rob.io.exuWriteback := delayedWriteBack
62185f51ecaSxiaofeibao-xjtu  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
622571677c9Sxiaofeibao-xjtu  rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush
6236f483f86SXuan Hu  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
624b9a37d2fSXuan Hu  rob.io.fromVecExcpMod.busy := io.fromVecExcpMod.busy
62524519898SXuan Hu
62624519898SXuan Hu  io.redirect := s1_s3_redirect
62724519898SXuan Hu
62824519898SXuan Hu  // rob to int block
62924519898SXuan Hu  io.robio.csr <> rob.io.csr
63024519898SXuan Hu  // When wfi is disabled, it will not block ROB commit.
63124519898SXuan Hu  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
63224519898SXuan Hu  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
63324519898SXuan Hu
63424519898SXuan Hu  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
63524519898SXuan Hu
63624519898SXuan Hu  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
63724519898SXuan Hu  io.robio.exception := rob.io.exception
63824519898SXuan Hu  io.robio.exception.bits.pc := s1_robFlushPc
63924519898SXuan Hu
64024519898SXuan Hu  // rob to mem block
64124519898SXuan Hu  io.robio.lsq <> rob.io.lsq
64224519898SXuan Hu
64363d67ef3STang Haojin  io.diff_int_rat.foreach(_ := rat.io.diff_int_rat.get)
64463d67ef3STang Haojin  io.diff_fp_rat .foreach(_ := rat.io.diff_fp_rat.get)
64563d67ef3STang Haojin  io.diff_vec_rat.foreach(_ := rat.io.diff_vec_rat.get)
64663d67ef3STang Haojin  io.diff_v0_rat .foreach(_ := rat.io.diff_v0_rat.get)
64763d67ef3STang Haojin  io.diff_vl_rat .foreach(_ := rat.io.diff_vl_rat.get)
64824519898SXuan Hu
64917b21f45SHaojin Tang  rob.io.debug_ls := io.robio.debug_ls
65017b21f45SHaojin Tang  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
65117b21f45SHaojin Tang  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
652a751b11aSchengguanghui  rob.io.csr.criticalErrorState := io.robio.csr.criticalErrorState
6536ce10964SXuan Hu  rob.io.debugEnqLsq := io.debugEnqLsq
6546ce10964SXuan Hu
65517b21f45SHaojin Tang  io.robio.robDeqPtr := rob.io.robDeqPtr
6568744445eSMaxpicca-Li
6571bf9a598SAnzo  io.robio.storeDebugInfo <> rob.io.storeDebugInfo
6581bf9a598SAnzo
6597e4f0b19SZiyue-Zhang  // rob to backend
6607e4f0b19SZiyue-Zhang  io.robio.commitVType := rob.io.toDecode.commitVType
6617e4f0b19SZiyue-Zhang  // exu block to decode
662d8a50338SZiyue Zhang  decode.io.vsetvlVType := io.toDecode.vsetvlVType
6635110577fSZiyue Zhang  // backend to decode
6645110577fSZiyue Zhang  decode.io.vstart := io.toDecode.vstart
6655110577fSZiyue Zhang  // backend to rob
6665110577fSZiyue Zhang  rob.io.vstartIsZero := io.toDecode.vstart === 0.U
6677e4f0b19SZiyue-Zhang
66892c61038SXuan Hu  io.toCSR.trapInstInfo := decode.io.toCSR.trapInstInfo
66992c61038SXuan Hu
670e43bb916SXuan Hu  io.toVecExcpMod.logicPhyRegMap := rob.io.toVecExcpMod.logicPhyRegMap
671e43bb916SXuan Hu  io.toVecExcpMod.excpInfo       := rob.io.toVecExcpMod.excpInfo
672e43bb916SXuan Hu  // T  : rat receive rabCommit
673e43bb916SXuan Hu  // T+1: rat return oldPdest
674e43bb916SXuan Hu  io.toVecExcpMod.ratOldPest match {
675e43bb916SXuan Hu    case fromRat =>
676e43bb916SXuan Hu      (0 until RabCommitWidth).foreach { idx =>
677e43bb916SXuan Hu        fromRat.v0OldVdPdest(idx).valid := RegNext(
678e43bb916SXuan Hu          rat.io.rabCommits.isCommit &&
679e43bb916SXuan Hu          rat.io.rabCommits.isWalk &&
680e43bb916SXuan Hu          rat.io.rabCommits.commitValid(idx) &&
681e43bb916SXuan Hu          rat.io.rabCommits.info(idx).v0Wen
682e43bb916SXuan Hu        )
683e43bb916SXuan Hu        fromRat.v0OldVdPdest(idx).bits := rat.io.v0_old_pdest(idx)
684e43bb916SXuan Hu        fromRat.vecOldVdPdest(idx).valid := RegNext(
685e43bb916SXuan Hu          rat.io.rabCommits.isCommit &&
686e43bb916SXuan Hu          rat.io.rabCommits.isWalk &&
687e43bb916SXuan Hu          rat.io.rabCommits.commitValid(idx) &&
688e43bb916SXuan Hu          rat.io.rabCommits.info(idx).vecWen
689e43bb916SXuan Hu        )
690e43bb916SXuan Hu        fromRat.vecOldVdPdest(idx).bits := rat.io.vec_old_pdest(idx)
691e43bb916SXuan Hu      }
692e43bb916SXuan Hu  }
693e43bb916SXuan Hu
69460ebee38STang Haojin  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
69560ebee38STang Haojin  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
69660ebee38STang Haojin  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
6977cf78eb2Shappy-lx  io.debugRolling := rob.io.debugRolling
69860ebee38STang Haojin
6995f8b6c9eSsinceforYy  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
700*0a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.intdqFull := false.B
701*0a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.fpdqFull := false.B
702*0a7d1d5cSxiaofeibao  io.perfInfo.ctrlInfo.lsdqFull := false.B
70324519898SXuan Hu
704*0a7d1d5cSxiaofeibao  val perfEvents = Seq(decode, rename, dispatch, rob).flatMap(_.getPerfEvents)
70524519898SXuan Hu  generatePerfEvent()
70685a8d7caSZehao Liu
70785a8d7caSZehao Liu  val criticalErrors = rob.getCriticalErrors
70885a8d7caSZehao Liu  generateCriticalErrors()
70924519898SXuan Hu}
71024519898SXuan Hu
71124519898SXuan Huclass CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
71224519898SXuan Hu  val fromTop = new Bundle {
71324519898SXuan Hu    val hartId = Input(UInt(8.W))
71424519898SXuan Hu  }
71524519898SXuan Hu  val toTop = new Bundle {
71624519898SXuan Hu    val cpuHalt = Output(Bool())
71724519898SXuan Hu  }
71824519898SXuan Hu  val frontend = Flipped(new FrontendToCtrlIO())
71915ed99a7SXuan Hu  val fromCSR = new Bundle{
72015ed99a7SXuan Hu    val toDecode = Input(new CSRToDecode)
721c308d936Schengguanghui    val traceCSR = Input(new TraceCSR)
72215ed99a7SXuan Hu  }
72324519898SXuan Hu  val toIssueBlock = new Bundle {
72424519898SXuan Hu    val flush = ValidIO(new Redirect)
725*0a7d1d5cSxiaofeibao    val intUopsNum = backendParams.intSchdParams.get.issueBlockParams.map(_.numEnq).sum
726*0a7d1d5cSxiaofeibao    val fpUopsNum = backendParams.fpSchdParams.get.issueBlockParams.map(_.numEnq).sum
727*0a7d1d5cSxiaofeibao    val vfUopsNum = backendParams.vfSchdParams.get.issueBlockParams.map(_.numEnq).sum
728*0a7d1d5cSxiaofeibao    val memUopsNum = backendParams.memSchdParams.get.issueBlockParams.filter(x => x.StdCnt == 0).map(_.numEnq).sum
729*0a7d1d5cSxiaofeibao    val intUops = Vec(intUopsNum, DecoupledIO(new DynInst))
730*0a7d1d5cSxiaofeibao    val fpUops = Vec(fpUopsNum, DecoupledIO(new DynInst))
731*0a7d1d5cSxiaofeibao    val vfUops = Vec(vfUopsNum, DecoupledIO(new DynInst))
732*0a7d1d5cSxiaofeibao    val memUops = Vec(memUopsNum, DecoupledIO(new DynInst))
733*0a7d1d5cSxiaofeibao  }
734*0a7d1d5cSxiaofeibao  val fromMemToDispatch = new Bundle {
735*0a7d1d5cSxiaofeibao    val lcommit = Input(UInt(log2Up(CommitWidth + 1).W))
736*0a7d1d5cSxiaofeibao    val scommit = Input(UInt(log2Ceil(EnsbufferWidth + 1).W)) // connected to `memBlock.io.sqDeq` instead of ROB
737*0a7d1d5cSxiaofeibao    val lqDeqPtr = Input(new LqPtr)
738*0a7d1d5cSxiaofeibao    val sqDeqPtr = Input(new SqPtr)
739*0a7d1d5cSxiaofeibao    // from lsq
740*0a7d1d5cSxiaofeibao    val lqCancelCnt = Input(UInt(log2Up(VirtualLoadQueueSize + 1).W))
741*0a7d1d5cSxiaofeibao    val sqCancelCnt = Input(UInt(log2Up(StoreQueueSize + 1).W))
742*0a7d1d5cSxiaofeibao  }
743*0a7d1d5cSxiaofeibao  //toMem
744*0a7d1d5cSxiaofeibao  val toMem = new Bundle {
745*0a7d1d5cSxiaofeibao    val lsqEnqIO = Flipped(new LsqEnqIO)
746*0a7d1d5cSxiaofeibao  }
747*0a7d1d5cSxiaofeibao  val toDispatch = new Bundle {
748*0a7d1d5cSxiaofeibao    val wakeUpInt = Flipped(backendParams.intSchdParams.get.genIQWakeUpOutValidBundle)
749*0a7d1d5cSxiaofeibao    val wakeUpFp  = Flipped(backendParams.fpSchdParams.get.genIQWakeUpOutValidBundle)
750*0a7d1d5cSxiaofeibao    val wakeUpVec = Flipped(backendParams.vfSchdParams.get.genIQWakeUpOutValidBundle)
751*0a7d1d5cSxiaofeibao    val wakeUpMem = Flipped(backendParams.memSchdParams.get.genIQWakeUpOutValidBundle)
752*0a7d1d5cSxiaofeibao    val allIssueParams = backendParams.allIssueParams.filter(_.StdCnt == 0)
753*0a7d1d5cSxiaofeibao    val allExuParams = allIssueParams.map(_.exuBlockParams).flatten
754*0a7d1d5cSxiaofeibao    val exuNum = allExuParams.size
755*0a7d1d5cSxiaofeibao    val maxIQSize = allIssueParams.map(_.numEntries).max
756*0a7d1d5cSxiaofeibao    val IQValidNumVec = Vec(exuNum, Input(UInt(maxIQSize.U.getWidth.W)))
757*0a7d1d5cSxiaofeibao    val og0Cancel = Input(ExuVec())
758*0a7d1d5cSxiaofeibao    val ldCancel = Vec(backendParams.LdExuCnt, Flipped(new LoadCancelIO))
759*0a7d1d5cSxiaofeibao    val wbPregsInt = Vec(backendParams.numPregWb(IntData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
760*0a7d1d5cSxiaofeibao    val wbPregsFp = Vec(backendParams.numPregWb(FpData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
761*0a7d1d5cSxiaofeibao    val wbPregsVec = Vec(backendParams.numPregWb(VecData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
762*0a7d1d5cSxiaofeibao    val wbPregsV0 = Vec(backendParams.numPregWb(V0Data()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
763*0a7d1d5cSxiaofeibao    val wbPregsVl = Vec(backendParams.numPregWb(VlData()), Flipped(ValidIO(UInt(PhyRegIdxWidth.W))))
76424519898SXuan Hu  }
76524519898SXuan Hu  val toDataPath = new Bundle {
76624519898SXuan Hu    val flush = ValidIO(new Redirect)
76724519898SXuan Hu  }
76824519898SXuan Hu  val toExuBlock = new Bundle {
76924519898SXuan Hu    val flush = ValidIO(new Redirect)
77024519898SXuan Hu  }
77192c61038SXuan Hu  val toCSR = new Bundle {
77292c61038SXuan Hu    val trapInstInfo = Output(ValidIO(new TrapInstInfo))
77392c61038SXuan Hu  }
77424519898SXuan Hu  val fromWB = new Bundle {
77524519898SXuan Hu    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
77624519898SXuan Hu  }
77724519898SXuan Hu  val redirect = ValidIO(new Redirect)
77824519898SXuan Hu  val fromMem = new Bundle {
779272ec6b1SHaojin Tang    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
78024519898SXuan Hu    val violation = Flipped(ValidIO(new Redirect))
78124519898SXuan Hu  }
78224519898SXuan Hu  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
78383ba63b3SXuan Hu  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
784b133b458SXuan Hu  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
7854b0d80d8SXuan Hu
78624519898SXuan Hu  val csrCtrl = Input(new CustomCSRCtrlIO)
78724519898SXuan Hu  val robio = new Bundle {
78824519898SXuan Hu    val csr = new RobCSRIO
78924519898SXuan Hu    val exception = ValidIO(new ExceptionInfo)
79024519898SXuan Hu    val lsq = new RobLsqIO
7916810d1e8Ssfencevma    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
7922326221cSXuan Hu    val debug_ls = Input(new DebugLSIO())
79317b21f45SHaojin Tang    val robHeadLsIssue = Input(Bool())
79417b21f45SHaojin Tang    val robDeqPtr = Output(new RobPtr)
7957e4f0b19SZiyue-Zhang    val commitVType = new Bundle {
7967e4f0b19SZiyue-Zhang      val vtype = Output(ValidIO(VType()))
7977e4f0b19SZiyue-Zhang      val hasVsetvl = Output(Bool())
7987e4f0b19SZiyue-Zhang    }
7991bf9a598SAnzo
8001bf9a598SAnzo    // store event difftest information
8011bf9a598SAnzo    val storeDebugInfo = Vec(EnsbufferWidth, new Bundle {
8021bf9a598SAnzo      val robidx = Input(new RobPtr)
8031bf9a598SAnzo      val pc     = Output(UInt(VAddrBits.W))
8041bf9a598SAnzo    })
80524519898SXuan Hu  }
80624519898SXuan Hu
807d8a50338SZiyue Zhang  val toDecode = new Bundle {
808d8a50338SZiyue Zhang    val vsetvlVType = Input(VType())
8095110577fSZiyue Zhang    val vstart = Input(Vl())
810d8a50338SZiyue Zhang  }
811d8a50338SZiyue Zhang
812e43bb916SXuan Hu  val fromVecExcpMod = Input(new Bundle {
813e43bb916SXuan Hu    val busy = Bool()
814e43bb916SXuan Hu  })
815e43bb916SXuan Hu
816e43bb916SXuan Hu  val toVecExcpMod = Output(new Bundle {
817e43bb916SXuan Hu    val logicPhyRegMap = Vec(RabCommitWidth, ValidIO(new RegWriteFromRab))
818e43bb916SXuan Hu    val excpInfo = ValidIO(new VecExcpInfo)
819e43bb916SXuan Hu    val ratOldPest = new RatToVecExcpMod
820e43bb916SXuan Hu  })
821e43bb916SXuan Hu
8224907ec88Schengguanghui  val traceCoreInterface = new TraceCoreInterface
8234907ec88Schengguanghui
82424519898SXuan Hu  val perfInfo = Output(new Bundle{
82524519898SXuan Hu    val ctrlInfo = new Bundle {
82624519898SXuan Hu      val robFull   = Bool()
82724519898SXuan Hu      val intdqFull = Bool()
82824519898SXuan Hu      val fpdqFull  = Bool()
82924519898SXuan Hu      val lsdqFull  = Bool()
83024519898SXuan Hu    }
83124519898SXuan Hu  })
83263d67ef3STang Haojin  val diff_int_rat = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
83363d67ef3STang Haojin  val diff_fp_rat  = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
83463d67ef3STang Haojin  val diff_vec_rat = if (params.basicDebugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None
83563d67ef3STang Haojin  val diff_v0_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
83663d67ef3STang Haojin  val diff_vl_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
83724519898SXuan Hu
838c61abc0cSXuan Hu  val sqCanAccept = Input(Bool())
839c61abc0cSXuan Hu  val lqCanAccept = Input(Bool())
8404b0d80d8SXuan Hu
8414b0d80d8SXuan Hu  val debugTopDown = new Bundle {
8424b0d80d8SXuan Hu    val fromRob = new RobCoreTopDownIO
8434b0d80d8SXuan Hu    val fromCore = new CoreDispatchTopDownIO
8444b0d80d8SXuan Hu  }
8454b0d80d8SXuan Hu  val debugRolling = new RobDebugRollingIO
8466ce10964SXuan Hu  val debugEnqLsq = Input(new LsqEnqIO)
84724519898SXuan Hu}
84824519898SXuan Hu
84924519898SXuan Huclass NamedIndexes(namedCnt: Seq[(String, Int)]) {
85024519898SXuan Hu  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
85124519898SXuan Hu
85224519898SXuan Hu  val maxIdx = namedCnt.map(_._2).sum
85324519898SXuan Hu  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
85424519898SXuan Hu    val begin = namedCnt.slice(0, i).map(_._2).sum
85524519898SXuan Hu    val end = begin + namedCnt(i)._2
85624519898SXuan Hu    (namedCnt(i)._1, (begin, end))
85724519898SXuan Hu  }.toMap
85824519898SXuan Hu
85924519898SXuan Hu  def apply(name: String): Seq[Int] = {
86024519898SXuan Hu    require(nameRangeMap.contains(name))
86124519898SXuan Hu    nameRangeMap(name)._1 until nameRangeMap(name)._2
86224519898SXuan Hu  }
86324519898SXuan Hu}
864