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