xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 1bc48dd1fa0af361fd194c65bad3b86349ec2903)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.backend
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
23import utility._
24import utils._
25import xiangshan.ExceptionNO._
26import xiangshan._
27import xiangshan.backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput, StaticInst, TrapInstInfo}
28import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfoBundle, LsTopdownInfo, MemCtrl, RedirectGenerator}
29import xiangshan.backend.datapath.DataConfig.VAddrData
30import xiangshan.backend.decode.{DecodeStage, FusionDecoder}
31import xiangshan.backend.dispatch.{CoreDispatchTopDownIO, Dispatch, DispatchQueue}
32import xiangshan.backend.fu.PFEvent
33import xiangshan.backend.fu.vector.Bundles.{VType, Vl}
34import xiangshan.backend.fu.wrapper.CSRToDecode
35import xiangshan.backend.rename.{Rename, RenameTableWrapper, SnapshotGenerator}
36import xiangshan.backend.rob.{Rob, RobCSRIO, RobCoreTopDownIO, RobDebugRollingIO, RobLsqIO, RobPtr}
37import xiangshan.frontend.{FtqPtr, FtqRead, Ftq_RF_Components}
38import xiangshan.mem.{LqPtr, LsqEnqIO}
39import xiangshan.backend.issue.{FpScheduler, IntScheduler, MemScheduler, VfScheduler}
40
41class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
42  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
43  val redirect = Valid(new Redirect)
44  val ftqIdxAhead = Vec(BackendRedirectNum, Valid(new FtqPtr))
45  val ftqIdxSelOH = Valid(UInt((BackendRedirectNum).W))
46}
47
48class CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
49  override def shouldBeInlined: Boolean = false
50
51  val rob = LazyModule(new Rob(params))
52
53  lazy val module = new CtrlBlockImp(this)(p, params)
54
55  val gpaMem = LazyModule(new GPAMem())
56}
57
58class CtrlBlockImp(
59  override val wrapper: CtrlBlock
60)(implicit
61  p: Parameters,
62  params: BackendParams
63) extends LazyModuleImp(wrapper)
64  with HasXSParameter
65  with HasCircularQueuePtrHelper
66  with HasPerfEvents
67{
68  val pcMemRdIndexes = new NamedIndexes(Seq(
69    "redirect"  -> 1,
70    "memPred"   -> 1,
71    "robFlush"  -> 1,
72    "load"      -> params.LduCnt,
73    "hybrid"    -> params.HyuCnt,
74    "store"     -> (if(EnableStorePrefetchSMS) params.StaCnt else 0)
75  ))
76
77  private val numPcMemReadForExu = params.numPcReadPort
78  private val numPcMemRead = pcMemRdIndexes.maxIdx
79
80  // now pcMem read for exu is moved to PcTargetMem (OG0)
81  println(s"pcMem read num: $numPcMemRead")
82  println(s"pcMem read num for exu: $numPcMemReadForExu")
83
84  val io = IO(new CtrlBlockIO())
85
86  val gpaMem = wrapper.gpaMem.module
87  val decode = Module(new DecodeStage)
88  val fusionDecoder = Module(new FusionDecoder)
89  val rat = Module(new RenameTableWrapper)
90  val rename = Module(new Rename)
91  val dispatch = Module(new Dispatch)
92  val intDq0 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 0))
93  val intDq1 = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth/2, dqIndex = 1))
94  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth))
95  val vecDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.VecDqDeqWidth))
96  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
97  val redirectGen = Module(new RedirectGenerator)
98  private def hasRen: Boolean = true
99  private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC", hasRen = hasRen))
100  private val rob = wrapper.rob.module
101  private val memCtrl = Module(new MemCtrl(params))
102
103  private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
104
105  private val s0_robFlushRedirect = rob.io.flushOut
106  private val s1_robFlushRedirect = Wire(Valid(new Redirect))
107  s1_robFlushRedirect.valid := GatedValidRegNext(s0_robFlushRedirect.valid, false.B)
108  s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid)
109
110  pcMem.io.ren.get(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.valid
111  pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value
112  private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).getPc(RegEnable(s0_robFlushRedirect.bits.ftqOffset, s0_robFlushRedirect.valid))
113  private val s3_redirectGen = redirectGen.io.stage2Redirect
114  private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen)
115  private val s2_s4_pendingRedirectValid = RegInit(false.B)
116  when (s1_s3_redirect.valid) {
117    s2_s4_pendingRedirectValid := true.B
118  }.elsewhen (GatedValidRegNext(io.frontend.toFtq.redirect.valid)) {
119    s2_s4_pendingRedirectValid := false.B
120  }
121
122  // Redirect will be RegNext at ExuBlocks and IssueBlocks
123  val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect)
124  val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect)
125
126  private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => {
127    val valid = x.valid
128    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
129    val delayed = Wire(Valid(new ExuOutput(x.bits.params)))
130    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
131    delayed.bits := RegEnable(x.bits, x.valid)
132    delayed.bits.debugInfo.writebackTime := GTimer()
133    delayed
134  }).toSeq
135  private val delayedWriteBack = Wire(chiselTypeOf(io.fromWB.wbData))
136  delayedWriteBack.zipWithIndex.map{ case (x,i) =>
137    x.valid := GatedValidRegNext(io.fromWB.wbData(i).valid)
138    x.bits := delayedNotFlushedWriteBack(i).bits
139  }
140  val delayedNotFlushedWriteBackNeedFlush = Wire(Vec(params.allExuParams.filter(_.needExceptionGen).length, Bool()))
141  delayedNotFlushedWriteBackNeedFlush := delayedNotFlushedWriteBack.filter(_.bits.params.needExceptionGen).map{ x =>
142    x.bits.exceptionVec.get.asUInt.orR || x.bits.flushPipe.getOrElse(false.B) || x.bits.replay.getOrElse(false.B) ||
143      (if (x.bits.trigger.nonEmpty) TriggerAction.isDmode(x.bits.trigger.get) else false.B)
144  }
145
146  val wbDataNoStd = io.fromWB.wbData.filter(!_.bits.params.hasStdFu)
147  val intScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[IntScheduler])
148  val fpScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[FpScheduler])
149  val vfScheWbData = io.fromWB.wbData.filter(_.bits.params.schdType.isInstanceOf[VfScheduler])
150  val intCanCompress = intScheWbData.filter(_.bits.params.CanCompress)
151  val i2vWbData = intScheWbData.filter(_.bits.params.writeVecRf)
152  val f2vWbData = fpScheWbData.filter(_.bits.params.writeVecRf)
153  val memVloadWbData = io.fromWB.wbData.filter(x => x.bits.params.schdType.isInstanceOf[MemScheduler] && x.bits.params.hasVLoadFu)
154  private val delayedNotFlushedWriteBackNums = wbDataNoStd.map(x => {
155    val valid = x.valid
156    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
157    val delayed = Wire(Valid(UInt(io.fromWB.wbData.size.U.getWidth.W)))
158    delayed.valid := GatedValidRegNext(valid && !killedByOlder)
159    val isIntSche = intCanCompress.contains(x)
160    val isFpSche = fpScheWbData.contains(x)
161    val isVfSche = vfScheWbData.contains(x)
162    val isMemVload = memVloadWbData.contains(x)
163    val isi2v = i2vWbData.contains(x)
164    val isf2v = f2vWbData.contains(x)
165    val canSameRobidxWbData = if(isVfSche) {
166      i2vWbData ++ f2vWbData ++ vfScheWbData
167    } else if(isi2v) {
168      intCanCompress ++ fpScheWbData ++ vfScheWbData
169    } else if (isf2v) {
170      intCanCompress ++ fpScheWbData ++ vfScheWbData
171    } else if (isIntSche) {
172      intCanCompress ++ fpScheWbData
173    } else if (isFpSche) {
174      intCanCompress ++ fpScheWbData
175    }  else if (isMemVload) {
176      memVloadWbData
177    } else {
178      Seq(x)
179    }
180    val sameRobidxBools = VecInit(canSameRobidxWbData.map( wb => {
181      val killedByOlderThat = wb.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
182      (wb.bits.robIdx === x.bits.robIdx) && wb.valid && x.valid && !killedByOlderThat && !killedByOlder
183    }).toSeq)
184    delayed.bits := RegEnable(PopCount(sameRobidxBools), x.valid)
185    delayed
186  }).toSeq
187
188  private val exuPredecode = VecInit(
189    io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get).toSeq
190  )
191
192  private val exuRedirects: Seq[ValidIO[Redirect]] = io.fromWB.wbData.filter(_.bits.redirect.nonEmpty).map(x => {
193    val out = Wire(Valid(new Redirect()))
194    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))
195    out.bits := x.bits.redirect.get.bits
196    out.bits.debugIsCtrl := true.B
197    out.bits.debugIsMemVio := false.B
198    out
199  }).toSeq
200  private val oldestOneHot = Redirect.selectOldestRedirect(exuRedirects)
201  private val oldestExuRedirect = Mux1H(oldestOneHot, exuRedirects)
202  private val oldestExuPredecode = Mux1H(oldestOneHot, exuPredecode)
203
204  private val memViolation = io.fromMem.violation
205  val loadReplay = Wire(ValidIO(new Redirect))
206  loadReplay.valid := GatedValidRegNext(memViolation.valid)
207  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
208  loadReplay.bits.debugIsCtrl := false.B
209  loadReplay.bits.debugIsMemVio := true.B
210
211  pcMem.io.ren.get(pcMemRdIndexes("redirect").head) := memViolation.valid
212  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := memViolation.bits.ftqIdx.value
213  pcMem.io.ren.get(pcMemRdIndexes("memPred").head) := memViolation.valid
214  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := memViolation.bits.stFtqIdx.value
215  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegEnable(memViolation.bits.stFtqOffset, memViolation.valid))
216
217  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
218    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
219    pcMem.io.ren.get(pcMemIdx) := io.memLdPcRead(i).valid
220    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
221    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memLdPcRead(i).offset, io.memLdPcRead(i).valid))
222  }
223
224  for ((pcMemIdx, i) <- pcMemRdIndexes("hybrid").zipWithIndex) {
225    // load read pcMem (s0) -> get rdata (s1) -> reg next in Memblock (s2) -> reg next in Memblock (s3) -> consumed by pf (s3)
226    pcMem.io.ren.get(pcMemIdx) := io.memHyPcRead(i).valid
227    pcMem.io.raddr(pcMemIdx) := io.memHyPcRead(i).ptr.value
228    io.memHyPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memHyPcRead(i).offset, io.memHyPcRead(i).valid))
229  }
230
231  if (EnableStorePrefetchSMS) {
232    for ((pcMemIdx, i) <- pcMemRdIndexes("store").zipWithIndex) {
233      pcMem.io.ren.get(pcMemIdx) := io.memStPcRead(i).valid
234      pcMem.io.raddr(pcMemIdx) := io.memStPcRead(i).ptr.value
235      io.memStPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(io.memStPcRead(i).offset, io.memStPcRead(i).valid))
236    }
237  } else {
238    io.memStPcRead.foreach(_.data := 0.U)
239  }
240
241  redirectGen.io.hartId := io.fromTop.hartId
242  redirectGen.io.oldestExuRedirect.valid := GatedValidRegNext(oldestExuRedirect.valid)
243  redirectGen.io.oldestExuRedirect.bits := RegEnable(oldestExuRedirect.bits, oldestExuRedirect.valid)
244  redirectGen.io.oldestExuOutPredecode.valid := GatedValidRegNext(oldestExuPredecode.valid)
245  redirectGen.io.oldestExuOutPredecode := RegEnable(oldestExuPredecode, oldestExuPredecode.valid)
246  redirectGen.io.loadReplay <> loadReplay
247  val loadRedirectPcRead = pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(memViolation.bits.ftqOffset, memViolation.valid))
248  redirectGen.io.loadReplay.bits.cfiUpdate.pc := loadRedirectPcRead
249  val load_pc_offset = Mux(loadReplay.bits.flushItself(), 0.U, Mux(loadReplay.bits.isRVC, 2.U, 4.U))
250  val load_target = loadRedirectPcRead + load_pc_offset
251  redirectGen.io.loadReplay.bits.cfiUpdate.target := load_target
252
253  redirectGen.io.robFlush := s1_robFlushRedirect
254
255  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
256  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
257  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
258  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
259  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
260  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
261  for (i <- 0 until CommitWidth) {
262    // why flushOut: instructions with flushPipe are not commited to frontend
263    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
264    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
265    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
266    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
267  }
268  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
269  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
270  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
271  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
272
273  //jmp/brh, sel oldest first, only use one read port
274  io.frontend.toFtq.ftqIdxAhead(0).valid := RegNext(oldestExuRedirect.valid) && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
275  io.frontend.toFtq.ftqIdxAhead(0).bits := RegEnable(oldestExuRedirect.bits.ftqIdx, oldestExuRedirect.valid)
276  //loadreplay
277  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
278  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
279  //exception
280  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
281  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
282
283  // Be careful here:
284  // T0: rob.io.flushOut, s0_robFlushRedirect
285  // T1: s1_robFlushRedirect, rob.io.exception.valid
286  // T2: csr.redirect.valid
287  // T3: csr.exception.valid
288  // T4: csr.trapTarget
289  // T5: ctrlBlock.trapTarget
290  // T6: io.frontend.toFtq.stage2Redirect.valid
291  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
292    s1_robFlushPc, // replay inst
293    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
294  ), s1_robFlushRedirect.valid)
295  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
296  private val s5_trapTargetFromCsr = io.robio.csr.trapTarget
297
298  val flushTarget = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.pc, s2_robFlushPc)
299  val s5_trapTargetIAF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIAF, false.B)
300  val s5_trapTargetIPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIPF, false.B)
301  val s5_trapTargetIGPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIGPF, false.B)
302  when (s6_flushFromRobValid) {
303    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
304    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
305    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIAF := RegEnable(s5_trapTargetIAF, s5_flushFromRobValidAhead)
306    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIPF := RegEnable(s5_trapTargetIPF, s5_flushFromRobValidAhead)
307    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIGPF := RegEnable(s5_trapTargetIGPF, s5_flushFromRobValidAhead)
308  }
309
310  for (i <- 0 until DecodeWidth) {
311    gpaMem.io.fromIFU := io.frontend.fromIfu
312    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
313    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
314    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
315  }
316
317  // vtype commit
318  decode.io.fromCSR := io.fromCSR.toDecode
319  decode.io.fromRob.isResumeVType := rob.io.toDecode.isResumeVType
320  decode.io.fromRob.walkToArchVType := rob.io.toDecode.walkToArchVType
321  decode.io.fromRob.commitVType := rob.io.toDecode.commitVType
322  decode.io.fromRob.walkVType := rob.io.toDecode.walkVType
323
324  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
325
326  // add decode Buf for in.ready better timing
327  val decodeBufBits = Reg(Vec(DecodeWidth, new StaticInst))
328  val decodeBufValid = RegInit(VecInit(Seq.fill(DecodeWidth)(false.B)))
329  val decodeFromFrontend = io.frontend.cfVec
330  val decodeBufNotAccept = VecInit(decodeBufValid.zip(decode.io.in).map(x => x._1 && !x._2.ready))
331  val decodeBufAcceptNum = PriorityMuxDefault(decodeBufNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
332  val decodeFromFrontendNotAccept = VecInit(decodeFromFrontend.zip(decode.io.in).map(x => decodeBufValid(0) || x._1.valid && !x._2.ready))
333  val decodeFromFrontendAcceptNum = PriorityMuxDefault(decodeFromFrontendNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
334  if (backendParams.debugEn) {
335    dontTouch(decodeBufNotAccept)
336    dontTouch(decodeBufAcceptNum)
337    dontTouch(decodeFromFrontendNotAccept)
338    dontTouch(decodeFromFrontendAcceptNum)
339  }
340  val a = decodeBufNotAccept.drop(2)
341  for (i <- 0 until DecodeWidth) {
342    // decodeBufValid update
343    when(decode.io.redirect || decodeBufValid(0) && decodeBufValid(i) && decode.io.in(i).ready && !VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
344      decodeBufValid(i) := false.B
345    }.elsewhen(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
346      decodeBufValid(i) := Mux(decodeBufAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeBufValid(i.U + decodeBufAcceptNum))
347    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
348      decodeBufValid(i) := Mux(decodeFromFrontendAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).valid)
349    }
350    // decodeBufBits update
351    when(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
352      decodeBufBits(i) := decodeBufBits(i.U + decodeBufAcceptNum)
353    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
354      decodeBufBits(i).connectCtrlFlow(decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).bits)
355    }
356  }
357  val decodeConnectFromFrontend = Wire(Vec(DecodeWidth, new StaticInst))
358  decodeConnectFromFrontend.zip(decodeFromFrontend).map(x => x._1.connectCtrlFlow(x._2.bits))
359  decode.io.in.zipWithIndex.foreach { case (decodeIn, i) =>
360    decodeIn.valid := Mux(decodeBufValid(0), decodeBufValid(i), decodeFromFrontend(i).valid)
361    decodeFromFrontend(i).ready := decodeFromFrontend(0).valid && !decodeBufValid(0) && decodeFromFrontend(i).valid && !decode.io.redirect
362    decodeIn.bits := Mux(decodeBufValid(i), decodeBufBits(i), decodeConnectFromFrontend(i))
363  }
364  io.frontend.canAccept := !decodeBufValid(0) || !decodeFromFrontend(0).valid
365  decode.io.csrCtrl := RegNext(io.csrCtrl)
366  decode.io.intRat <> rat.io.intReadPorts
367  decode.io.fpRat <> rat.io.fpReadPorts
368  decode.io.vecRat <> rat.io.vecReadPorts
369  decode.io.v0Rat <> rat.io.v0ReadPorts
370  decode.io.vlRat <> rat.io.vlReadPorts
371  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
372  decode.io.stallReason.in <> io.frontend.stallReason
373
374  // snapshot check
375  class CFIRobIdx extends Bundle {
376    val robIdx = Vec(RenameWidth, new RobPtr)
377    val isCFI = Vec(RenameWidth, Bool())
378  }
379  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
380  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
381  snpt.io.enq := genSnapshot
382  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
383  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
384  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
385    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
386  snpt.io.redirect := s1_s3_redirect.valid
387  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
388    val notCFIMask = snapshot.isCFI.map(~_)
389    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
390    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
391    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
392  })
393  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
394  snpt.io.flushVec := flushVecNext
395
396  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
397    snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
398      !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head)
399  ).reduceTree(_ || _)
400  val snptSelect = MuxCase(
401    0.U(log2Ceil(RenameSnapshotNum).W),
402    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
403      (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
404        !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx)
405    )
406  )
407
408  rob.io.snpt.snptEnq := DontCare
409  rob.io.snpt.snptDeq := snpt.io.deq
410  rob.io.snpt.useSnpt := useSnpt
411  rob.io.snpt.snptSelect := snptSelect
412  rob.io.snpt.flushVec := flushVecNext
413  rat.io.snpt.snptEnq := genSnapshot
414  rat.io.snpt.snptDeq := snpt.io.deq
415  rat.io.snpt.useSnpt := useSnpt
416  rat.io.snpt.snptSelect := snptSelect
417  rat.io.snpt.flushVec := flushVec
418
419  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
420  // fusion decoder
421  for (i <- 0 until DecodeWidth) {
422    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
423    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
424    if (i > 0) {
425      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
426    }
427  }
428
429  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
430  for (i <- 0 until RenameWidth) {
431    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
432      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
433
434    decodePipeRename(i).ready := rename.io.in(i).ready
435    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
436    rename.io.in(i).bits := decodePipeRename(i).bits
437  }
438
439  for (i <- 0 until RenameWidth - 1) {
440    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
441    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
442
443    // update the first RenameWidth - 1 instructions
444    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
445    when (fusionDecoder.io.out(i).valid) {
446      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
447      // TODO: remove this dirty code for ftq update
448      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
449      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
450      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
451      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
452      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
453      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
454      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
455      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
456      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
457      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
458    }
459
460  }
461
462  // memory dependency predict
463  // when decode, send fold pc to mdp
464  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
465  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
466  for (i <- 0 until DecodeWidth) {
467    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
468    mdpFlodPcVec(i) := Mux(
469      decode.io.out(i).fire,
470      decode.io.in(i).bits.foldpc,
471      rename.io.in(i).bits.foldpc
472    )
473  }
474
475  // currently, we only update mdp info when isReplay
476  memCtrl.io.redirect := s1_s3_redirect
477  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
478  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
479  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
480  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
481  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
482  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
483
484  rat.io.redirect := s1_s3_redirect.valid
485  rat.io.rabCommits := rob.io.rabCommits
486  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
487  rat.io.intRenamePorts := rename.io.intRenamePorts
488  rat.io.fpRenamePorts := rename.io.fpRenamePorts
489  rat.io.vecRenamePorts := rename.io.vecRenamePorts
490  rat.io.v0RenamePorts := rename.io.v0RenamePorts
491  rat.io.vlRenamePorts := rename.io.vlRenamePorts
492
493  rename.io.redirect := s1_s3_redirect
494  rename.io.rabCommits := rob.io.rabCommits
495  rename.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
496  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
497    RegEnable(waittable2rename, decodeOut.fire)
498  }
499  rename.io.ssit := memCtrl.io.ssit2Rename
500  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
501  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
502  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
503  rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data)))
504  rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data)))
505  rename.io.int_need_free := rat.io.int_need_free
506  rename.io.int_old_pdest := rat.io.int_old_pdest
507  rename.io.fp_old_pdest := rat.io.fp_old_pdest
508  rename.io.vec_old_pdest := rat.io.vec_old_pdest
509  rename.io.v0_old_pdest := rat.io.v0_old_pdest
510  rename.io.vl_old_pdest := rat.io.vl_old_pdest
511  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
512  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
513  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
514  rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get)
515  rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get)
516  rename.io.stallReason.in <> decode.io.stallReason.out
517  rename.io.snpt.snptEnq := DontCare
518  rename.io.snpt.snptDeq := snpt.io.deq
519  rename.io.snpt.useSnpt := useSnpt
520  rename.io.snpt.snptSelect := snptSelect
521  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
522  rename.io.snpt.flushVec := flushVecNext
523  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
524  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
525
526  val renameOut = Wire(chiselTypeOf(rename.io.out))
527  renameOut <> rename.io.out
528  // pass all snapshot in the first element for correctness of blockBackward
529  renameOut.tail.foreach(_.bits.snapshot := false.B)
530  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
531    false.B,
532    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
533  )
534
535  // pipeline between rename and dispatch
536  PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch")
537  dispatch.io.intIQValidNumVec := io.intIQValidNumVec
538  dispatch.io.fpIQValidNumVec := io.fpIQValidNumVec
539  dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num
540  dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num
541  dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num
542  dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num
543
544  dispatch.io.hartId := io.fromTop.hartId
545  dispatch.io.redirect := s1_s3_redirect
546  dispatch.io.enqRob <> rob.io.enq
547  dispatch.io.robHead := rob.io.debugRobHead
548  dispatch.io.stallReason <> rename.io.stallReason.out
549  dispatch.io.lqCanAccept := io.lqCanAccept
550  dispatch.io.sqCanAccept := io.sqCanAccept
551  dispatch.io.robHeadNotReady := rob.io.headNotReady
552  dispatch.io.robFull := rob.io.robFull
553  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
554
555  intDq0.io.enq <> dispatch.io.toIntDq0
556  intDq0.io.redirect <> s2_s4_redirect
557  intDq1.io.enq <> dispatch.io.toIntDq1
558  intDq1.io.redirect <> s2_s4_redirect
559
560  fpDq.io.enq <> dispatch.io.toFpDq
561  fpDq.io.redirect <> s2_s4_redirect
562
563  vecDq.io.enq <> dispatch.io.toVecDq
564  vecDq.io.redirect <> s2_s4_redirect
565
566  lsDq.io.enq <> dispatch.io.toLsDq
567  lsDq.io.redirect <> s2_s4_redirect
568
569  io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq)
570  io.toIssueBlock.fpUops <> fpDq.io.deq
571  io.toIssueBlock.vfUops  <> vecDq.io.deq
572  io.toIssueBlock.memUops <> lsDq.io.deq
573  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
574  io.toIssueBlock.flush   <> s2_s4_redirect
575
576  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
577  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
578  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
579
580  io.toDataPath.flush := s2_s4_redirect
581  io.toExuBlock.flush := s2_s4_redirect
582
583
584  rob.io.hartId := io.fromTop.hartId
585  rob.io.redirect := s1_s3_redirect
586  rob.io.writeback := delayedNotFlushedWriteBack
587  rob.io.exuWriteback := delayedWriteBack
588  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
589  rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush
590  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
591  rob.io.fromVecExcpMod.busy := io.fromVecExcpMod.busy
592
593  io.redirect := s1_s3_redirect
594
595  // rob to int block
596  io.robio.csr <> rob.io.csr
597  // When wfi is disabled, it will not block ROB commit.
598  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
599  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
600
601  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
602
603  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
604  io.robio.exception := rob.io.exception
605  io.robio.exception.bits.pc := s1_robFlushPc
606
607  // rob to mem block
608  io.robio.lsq <> rob.io.lsq
609
610  io.diff_int_rat.foreach(_ := rat.io.diff_int_rat.get)
611  io.diff_fp_rat .foreach(_ := rat.io.diff_fp_rat.get)
612  io.diff_vec_rat.foreach(_ := rat.io.diff_vec_rat.get)
613  io.diff_v0_rat .foreach(_ := rat.io.diff_v0_rat.get)
614  io.diff_vl_rat .foreach(_ := rat.io.diff_vl_rat.get)
615
616  rob.io.debug_ls := io.robio.debug_ls
617  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
618  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
619  rob.io.debugEnqLsq := io.debugEnqLsq
620
621  io.robio.robDeqPtr := rob.io.robDeqPtr
622
623  // rob to backend
624  io.robio.commitVType := rob.io.toDecode.commitVType
625  // exu block to decode
626  decode.io.vsetvlVType := io.toDecode.vsetvlVType
627  // backend to decode
628  decode.io.vstart := io.toDecode.vstart
629  // backend to rob
630  rob.io.vstartIsZero := io.toDecode.vstart === 0.U
631
632  io.toCSR.trapInstInfo := decode.io.toCSR.trapInstInfo
633
634  io.toVecExcpMod.logicPhyRegMap := rob.io.toVecExcpMod.logicPhyRegMap
635  io.toVecExcpMod.excpInfo       := rob.io.toVecExcpMod.excpInfo
636  // T  : rat receive rabCommit
637  // T+1: rat return oldPdest
638  io.toVecExcpMod.ratOldPest match {
639    case fromRat =>
640      (0 until RabCommitWidth).foreach { idx =>
641        fromRat.v0OldVdPdest(idx).valid := RegNext(
642          rat.io.rabCommits.isCommit &&
643          rat.io.rabCommits.isWalk &&
644          rat.io.rabCommits.commitValid(idx) &&
645          rat.io.rabCommits.info(idx).v0Wen
646        )
647        fromRat.v0OldVdPdest(idx).bits := rat.io.v0_old_pdest(idx)
648        fromRat.vecOldVdPdest(idx).valid := RegNext(
649          rat.io.rabCommits.isCommit &&
650          rat.io.rabCommits.isWalk &&
651          rat.io.rabCommits.commitValid(idx) &&
652          rat.io.rabCommits.info(idx).vecWen
653        )
654        fromRat.vecOldVdPdest(idx).bits := rat.io.vec_old_pdest(idx)
655      }
656  }
657
658  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
659  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
660  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
661  io.debugRolling := rob.io.debugRolling
662
663  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
664  io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull)
665  io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull)
666  io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull)
667
668  val perfEvents = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents)
669  generatePerfEvent()
670}
671
672class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
673  val fromTop = new Bundle {
674    val hartId = Input(UInt(8.W))
675  }
676  val toTop = new Bundle {
677    val cpuHalt = Output(Bool())
678  }
679  val frontend = Flipped(new FrontendToCtrlIO())
680  val fromCSR = new Bundle{
681    val toDecode = Input(new CSRToDecode)
682  }
683  val toIssueBlock = new Bundle {
684    val flush = ValidIO(new Redirect)
685    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
686    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
687    val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst))
688    val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
689    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
690  }
691  val toDataPath = new Bundle {
692    val flush = ValidIO(new Redirect)
693  }
694  val toExuBlock = new Bundle {
695    val flush = ValidIO(new Redirect)
696  }
697  val toCSR = new Bundle {
698    val trapInstInfo = Output(ValidIO(new TrapInstInfo))
699  }
700  val intIQValidNumVec = Input(MixedVec(params.genIntIQValidNumBundle))
701  val fpIQValidNumVec = Input(MixedVec(params.genFpIQValidNumBundle))
702  val fromWB = new Bundle {
703    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
704  }
705  val redirect = ValidIO(new Redirect)
706  val fromMem = new Bundle {
707    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
708    val violation = Flipped(ValidIO(new Redirect))
709  }
710  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
711  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
712  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
713
714  val csrCtrl = Input(new CustomCSRCtrlIO)
715  val robio = new Bundle {
716    val csr = new RobCSRIO
717    val exception = ValidIO(new ExceptionInfo)
718    val lsq = new RobLsqIO
719    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
720    val debug_ls = Input(new DebugLSIO())
721    val robHeadLsIssue = Input(Bool())
722    val robDeqPtr = Output(new RobPtr)
723    val commitVType = new Bundle {
724      val vtype = Output(ValidIO(VType()))
725      val hasVsetvl = Output(Bool())
726    }
727  }
728
729  val toDecode = new Bundle {
730    val vsetvlVType = Input(VType())
731    val vstart = Input(Vl())
732  }
733
734  val fromVecExcpMod = Input(new Bundle {
735    val busy = Bool()
736  })
737
738  val toVecExcpMod = Output(new Bundle {
739    val logicPhyRegMap = Vec(RabCommitWidth, ValidIO(new RegWriteFromRab))
740    val excpInfo = ValidIO(new VecExcpInfo)
741    val ratOldPest = new RatToVecExcpMod
742  })
743
744  val perfInfo = Output(new Bundle{
745    val ctrlInfo = new Bundle {
746      val robFull   = Bool()
747      val intdqFull = Bool()
748      val fpdqFull  = Bool()
749      val lsdqFull  = Bool()
750    }
751  })
752  val diff_int_rat = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
753  val diff_fp_rat  = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
754  val diff_vec_rat = if (params.basicDebugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None
755  val diff_v0_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
756  val diff_vl_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
757
758  val sqCanAccept = Input(Bool())
759  val lqCanAccept = Input(Bool())
760
761  val debugTopDown = new Bundle {
762    val fromRob = new RobCoreTopDownIO
763    val fromCore = new CoreDispatchTopDownIO
764  }
765  val debugRolling = new RobDebugRollingIO
766  val debugEnqLsq = Input(new LsqEnqIO)
767}
768
769class NamedIndexes(namedCnt: Seq[(String, Int)]) {
770  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
771
772  val maxIdx = namedCnt.map(_._2).sum
773  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
774    val begin = namedCnt.slice(0, i).map(_._2).sum
775    val end = begin + namedCnt(i)._2
776    (namedCnt(i)._1, (begin, end))
777  }.toMap
778
779  def apply(name: String): Seq[Int] = {
780    require(nameRangeMap.contains(name))
781    nameRangeMap(name)._1 until nameRangeMap(name)._2
782  }
783}
784