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