xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 38d0d7c5a34a23dfdb58a3cb2737c3cfddb3ec9d)
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  trace.io.in.fromEncoder.stall  := io.traceCoreInterface.fromEncoder.stall
249  trace.io.in.fromEncoder.enable := io.traceCoreInterface.fromEncoder.enable
250  trace.io.in.fromRob            := rob.io.trace.traceCommitInfo
251  rob.io.trace.blockCommit       := trace.io.out.blockRobCommit
252
253  for ((pcMemIdx, i) <- pcMemRdIndexes("trace").zipWithIndex) {
254    val traceValid = trace.toPcMem.blocks(i).valid
255    pcMem.io.ren.get(pcMemIdx) := traceValid
256    pcMem.io.raddr(pcMemIdx) := trace.toPcMem.blocks(i).bits.ftqIdx.get.value
257    trace.io.in.fromPcMem(i) := pcMem.io.rdata(pcMemIdx).getPc(RegEnable(trace.toPcMem.blocks(i).bits.ftqOffset.get, traceValid))
258  }
259
260  // Trap/Xret only occur in block(0).
261  val tracePriv = Mux(Itype.isTrapOrXret(trace.toEncoder.blocks(0).bits.tracePipe.itype),
262    io.fromCSR.traceCSR.lastPriv,
263    io.fromCSR.traceCSR.currentPriv
264  )
265  io.traceCoreInterface.toEncoder.trap.cause := io.fromCSR.traceCSR.cause.asUInt
266  io.traceCoreInterface.toEncoder.trap.tval  := io.fromCSR.traceCSR.tval.asUInt
267  io.traceCoreInterface.toEncoder.priv       := tracePriv
268  (0 until TraceGroupNum).foreach(i => {
269    io.traceCoreInterface.toEncoder.groups(i).valid := trace.io.out.toEncoder.blocks(i).valid
270    io.traceCoreInterface.toEncoder.groups(i).bits.iaddr := trace.io.out.toEncoder.blocks(i).bits.iaddr.getOrElse(0.U)
271    io.traceCoreInterface.toEncoder.groups(i).bits.itype := trace.io.out.toEncoder.blocks(i).bits.tracePipe.itype
272    io.traceCoreInterface.toEncoder.groups(i).bits.iretire := trace.io.out.toEncoder.blocks(i).bits.tracePipe.iretire
273    io.traceCoreInterface.toEncoder.groups(i).bits.ilastsize := trace.io.out.toEncoder.blocks(i).bits.tracePipe.ilastsize
274  })
275  /**
276   * trace end
277   */
278
279
280  redirectGen.io.hartId := io.fromTop.hartId
281  redirectGen.io.oldestExuRedirect.valid := GatedValidRegNext(oldestExuRedirect.valid)
282  redirectGen.io.oldestExuRedirect.bits := RegEnable(oldestExuRedirect.bits, oldestExuRedirect.valid)
283  redirectGen.io.oldestExuOutPredecode.valid := GatedValidRegNext(oldestExuPredecode.valid)
284  redirectGen.io.oldestExuOutPredecode := RegEnable(oldestExuPredecode, oldestExuPredecode.valid)
285  redirectGen.io.loadReplay <> loadReplay
286  val loadRedirectPcRead = pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegEnable(memViolation.bits.ftqOffset, memViolation.valid))
287  redirectGen.io.loadReplay.bits.cfiUpdate.pc := loadRedirectPcRead
288  val load_pc_offset = Mux(loadReplay.bits.flushItself(), 0.U, Mux(loadReplay.bits.isRVC, 2.U, 4.U))
289  val load_target = loadRedirectPcRead + load_pc_offset
290  redirectGen.io.loadReplay.bits.cfiUpdate.target := load_target
291
292  redirectGen.io.robFlush := s1_robFlushRedirect
293
294  val s5_flushFromRobValidAhead = DelayN(s1_robFlushRedirect.valid, 4)
295  val s6_flushFromRobValid = GatedValidRegNext(s5_flushFromRobValidAhead)
296  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
297  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
298  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
299  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
300  for (i <- 0 until CommitWidth) {
301    // why flushOut: instructions with flushPipe are not commited to frontend
302    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
303    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && !s0_robFlushRedirect.valid
304    io.frontend.toFtq.rob_commits(i).valid := GatedValidRegNext(s1_isCommit)
305    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
306  }
307  io.frontend.toFtq.redirect.valid := s6_flushFromRobValid || s3_redirectGen.valid
308  io.frontend.toFtq.redirect.bits := Mux(s6_flushFromRobValid, frontendFlushBits, s3_redirectGen.bits)
309  io.frontend.toFtq.ftqIdxSelOH.valid := s6_flushFromRobValid || redirectGen.io.stage2Redirect.valid
310  io.frontend.toFtq.ftqIdxSelOH.bits := Cat(s6_flushFromRobValid, redirectGen.io.stage2oldestOH & Fill(NumRedirect + 1, !s6_flushFromRobValid))
311
312  //jmp/brh, sel oldest first, only use one read port
313  io.frontend.toFtq.ftqIdxAhead(0).valid := RegNext(oldestExuRedirect.valid) && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
314  io.frontend.toFtq.ftqIdxAhead(0).bits := RegEnable(oldestExuRedirect.bits.ftqIdx, oldestExuRedirect.valid)
315  //loadreplay
316  io.frontend.toFtq.ftqIdxAhead(NumRedirect).valid := loadReplay.valid && !s1_robFlushRedirect.valid && !s5_flushFromRobValidAhead
317  io.frontend.toFtq.ftqIdxAhead(NumRedirect).bits := loadReplay.bits.ftqIdx
318  //exception
319  io.frontend.toFtq.ftqIdxAhead.last.valid := s5_flushFromRobValidAhead
320  io.frontend.toFtq.ftqIdxAhead.last.bits := frontendFlushBits.ftqIdx
321
322  // Be careful here:
323  // T0: rob.io.flushOut, s0_robFlushRedirect
324  // T1: s1_robFlushRedirect, rob.io.exception.valid
325  // T2: csr.redirect.valid
326  // T3: csr.exception.valid
327  // T4: csr.trapTarget
328  // T5: ctrlBlock.trapTarget
329  // T6: io.frontend.toFtq.stage2Redirect.valid
330  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
331    s1_robFlushPc, // replay inst
332    s1_robFlushPc + Mux(s1_robFlushRedirect.bits.isRVC, 2.U, 4.U) // flush pipe
333  ), s1_robFlushRedirect.valid)
334  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
335  private val s5_trapTargetFromCsr = io.robio.csr.trapTarget
336
337  val flushTarget = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.pc, s2_robFlushPc)
338  val s5_trapTargetIAF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIAF, false.B)
339  val s5_trapTargetIPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIPF, false.B)
340  val s5_trapTargetIGPF = Mux(s5_csrIsTrap, s5_trapTargetFromCsr.raiseIGPF, false.B)
341  when (s6_flushFromRobValid) {
342    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
343    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegEnable(flushTarget, s5_flushFromRobValidAhead)
344    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIAF := RegEnable(s5_trapTargetIAF, s5_flushFromRobValidAhead)
345    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIPF := RegEnable(s5_trapTargetIPF, s5_flushFromRobValidAhead)
346    io.frontend.toFtq.redirect.bits.cfiUpdate.backendIGPF := RegEnable(s5_trapTargetIGPF, s5_flushFromRobValidAhead)
347  }
348
349  for (i <- 0 until DecodeWidth) {
350    gpaMem.io.fromIFU := io.frontend.fromIfu
351    gpaMem.io.exceptionReadAddr.valid := rob.io.readGPAMemAddr.valid
352    gpaMem.io.exceptionReadAddr.bits.ftqPtr := rob.io.readGPAMemAddr.bits.ftqPtr
353    gpaMem.io.exceptionReadAddr.bits.ftqOffset := rob.io.readGPAMemAddr.bits.ftqOffset
354  }
355
356  // vtype commit
357  decode.io.fromCSR := io.fromCSR.toDecode
358  decode.io.fromRob.isResumeVType := rob.io.toDecode.isResumeVType
359  decode.io.fromRob.walkToArchVType := rob.io.toDecode.walkToArchVType
360  decode.io.fromRob.commitVType := rob.io.toDecode.commitVType
361  decode.io.fromRob.walkVType := rob.io.toDecode.walkVType
362
363  decode.io.redirect := s1_s3_redirect.valid || s2_s4_pendingRedirectValid
364
365  // add decode Buf for in.ready better timing
366  val decodeBufBits = Reg(Vec(DecodeWidth, new StaticInst))
367  val decodeBufValid = RegInit(VecInit(Seq.fill(DecodeWidth)(false.B)))
368  val decodeFromFrontend = io.frontend.cfVec
369  val decodeBufNotAccept = VecInit(decodeBufValid.zip(decode.io.in).map(x => x._1 && !x._2.ready))
370  val decodeBufAcceptNum = PriorityMuxDefault(decodeBufNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
371  val decodeFromFrontendNotAccept = VecInit(decodeFromFrontend.zip(decode.io.in).map(x => decodeBufValid(0) || x._1.valid && !x._2.ready))
372  val decodeFromFrontendAcceptNum = PriorityMuxDefault(decodeFromFrontendNotAccept.zip(Seq.tabulate(DecodeWidth)(i => i.U)), DecodeWidth.U)
373  if (backendParams.debugEn) {
374    dontTouch(decodeBufNotAccept)
375    dontTouch(decodeBufAcceptNum)
376    dontTouch(decodeFromFrontendNotAccept)
377    dontTouch(decodeFromFrontendAcceptNum)
378  }
379  val a = decodeBufNotAccept.drop(2)
380  for (i <- 0 until DecodeWidth) {
381    // decodeBufValid update
382    when(decode.io.redirect || decodeBufValid(0) && decodeBufValid(i) && decode.io.in(i).ready && !VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
383      decodeBufValid(i) := false.B
384    }.elsewhen(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
385      decodeBufValid(i) := Mux(decodeBufAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeBufValid(i.U + decodeBufAcceptNum))
386    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
387      decodeBufValid(i) := Mux(decodeFromFrontendAcceptNum > DecodeWidth.U - 1.U - i.U, false.B, decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).valid)
388    }
389    // decodeBufBits update
390    when(decodeBufValid(i) && VecInit(decodeBufNotAccept.drop(i)).asUInt.orR) {
391      decodeBufBits(i) := decodeBufBits(i.U + decodeBufAcceptNum)
392    }.elsewhen(!decodeBufValid(0) && VecInit(decodeFromFrontendNotAccept.drop(i)).asUInt.orR) {
393      decodeBufBits(i).connectCtrlFlow(decodeFromFrontend(i.U + decodeFromFrontendAcceptNum).bits)
394    }
395  }
396  val decodeConnectFromFrontend = Wire(Vec(DecodeWidth, new StaticInst))
397  decodeConnectFromFrontend.zip(decodeFromFrontend).map(x => x._1.connectCtrlFlow(x._2.bits))
398  decode.io.in.zipWithIndex.foreach { case (decodeIn, i) =>
399    decodeIn.valid := Mux(decodeBufValid(0), decodeBufValid(i), decodeFromFrontend(i).valid)
400    decodeFromFrontend(i).ready := decodeFromFrontend(0).valid && !decodeBufValid(0) && decodeFromFrontend(i).valid && !decode.io.redirect
401    decodeIn.bits := Mux(decodeBufValid(i), decodeBufBits(i), decodeConnectFromFrontend(i))
402  }
403  io.frontend.canAccept := !decodeBufValid(0) || !decodeFromFrontend(0).valid
404  decode.io.csrCtrl := RegNext(io.csrCtrl)
405  decode.io.intRat <> rat.io.intReadPorts
406  decode.io.fpRat <> rat.io.fpReadPorts
407  decode.io.vecRat <> rat.io.vecReadPorts
408  decode.io.v0Rat <> rat.io.v0ReadPorts
409  decode.io.vlRat <> rat.io.vlReadPorts
410  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
411  decode.io.stallReason.in <> io.frontend.stallReason
412
413  // snapshot check
414  class CFIRobIdx extends Bundle {
415    val robIdx = Vec(RenameWidth, new RobPtr)
416    val isCFI = Vec(RenameWidth, Bool())
417  }
418  val genSnapshot = Cat(rename.io.out.map(out => out.fire && out.bits.snapshot)).orR
419  val snpt = Module(new SnapshotGenerator(0.U.asTypeOf(new CFIRobIdx)))
420  snpt.io.enq := genSnapshot
421  snpt.io.enqData.robIdx := rename.io.out.map(_.bits.robIdx)
422  snpt.io.enqData.isCFI := rename.io.out.map(_.bits.snapshot)
423  snpt.io.deq := snpt.io.valids(snpt.io.deqPtr.value) && rob.io.commits.isCommit &&
424    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
425  snpt.io.redirect := s1_s3_redirect.valid
426  val flushVec = VecInit(snpt.io.snapshots.map { snapshot =>
427    val notCFIMask = snapshot.isCFI.map(~_)
428    val shouldFlush = snapshot.robIdx.map(robIdx => robIdx >= s1_s3_redirect.bits.robIdx || robIdx.value === s1_s3_redirect.bits.robIdx.value)
429    val shouldFlushMask = (1 to RenameWidth).map(shouldFlush take _ reduce (_ || _))
430    s1_s3_redirect.valid && Cat(shouldFlushMask.zip(notCFIMask).map(x => x._1 | x._2)).andR
431  })
432  val flushVecNext = flushVec zip snpt.io.valids map (x => GatedValidRegNext(x._1 && x._2, false.B))
433  snpt.io.flushVec := flushVecNext
434
435  val useSnpt = VecInit.tabulate(RenameSnapshotNum)(idx =>
436    snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
437      !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head)
438  ).reduceTree(_ || _)
439  val snptSelect = MuxCase(
440    0.U(log2Ceil(RenameSnapshotNum).W),
441    (1 to RenameSnapshotNum).map(i => (snpt.io.enqPtr - i.U).value).map(idx =>
442      (snpt.io.valids(idx) && (s1_s3_redirect.bits.robIdx > snpt.io.snapshots(idx).robIdx.head ||
443        !s1_s3_redirect.bits.flushItself() && s1_s3_redirect.bits.robIdx === snpt.io.snapshots(idx).robIdx.head), idx)
444    )
445  )
446
447  rob.io.snpt.snptEnq := DontCare
448  rob.io.snpt.snptDeq := snpt.io.deq
449  rob.io.snpt.useSnpt := useSnpt
450  rob.io.snpt.snptSelect := snptSelect
451  rob.io.snpt.flushVec := flushVecNext
452  rat.io.snpt.snptEnq := genSnapshot
453  rat.io.snpt.snptDeq := snpt.io.deq
454  rat.io.snpt.useSnpt := useSnpt
455  rat.io.snpt.snptSelect := snptSelect
456  rat.io.snpt.flushVec := flushVec
457
458  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
459  // fusion decoder
460  for (i <- 0 until DecodeWidth) {
461    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
462    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
463    if (i > 0) {
464      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
465    }
466  }
467
468  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
469  for (i <- 0 until RenameWidth) {
470    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
471      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
472
473    decodePipeRename(i).ready := rename.io.in(i).ready
474    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
475    rename.io.in(i).bits := decodePipeRename(i).bits
476  }
477
478  for (i <- 0 until RenameWidth - 1) {
479    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
480    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
481
482    // update the first RenameWidth - 1 instructions
483    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
484    when (fusionDecoder.io.out(i).valid) {
485      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
486      // TODO: remove this dirty code for ftq update
487      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
488      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
489      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
490      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
491      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
492      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
493      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
494      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
495      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
496      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
497    }
498
499  }
500
501  // memory dependency predict
502  // when decode, send fold pc to mdp
503  private val mdpFlodPcVecVld = Wire(Vec(DecodeWidth, Bool()))
504  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
505  for (i <- 0 until DecodeWidth) {
506    mdpFlodPcVecVld(i) := decode.io.out(i).fire || GatedValidRegNext(decode.io.out(i).fire)
507    mdpFlodPcVec(i) := Mux(
508      decode.io.out(i).fire,
509      decode.io.in(i).bits.foldpc,
510      rename.io.in(i).bits.foldpc
511    )
512  }
513
514  // currently, we only update mdp info when isReplay
515  memCtrl.io.redirect := s1_s3_redirect
516  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
517  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
518  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
519  memCtrl.io.mdpFoldPcVecVld := mdpFlodPcVecVld
520  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
521  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
522
523  rat.io.redirect := s1_s3_redirect.valid
524  rat.io.rabCommits := rob.io.rabCommits
525  rat.io.diffCommits.foreach(_ := rob.io.diffCommits.get)
526  rat.io.intRenamePorts := rename.io.intRenamePorts
527  rat.io.fpRenamePorts := rename.io.fpRenamePorts
528  rat.io.vecRenamePorts := rename.io.vecRenamePorts
529  rat.io.v0RenamePorts := rename.io.v0RenamePorts
530  rat.io.vlRenamePorts := rename.io.vlRenamePorts
531
532  rename.io.redirect := s1_s3_redirect
533  rename.io.rabCommits := rob.io.rabCommits
534  rename.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
535  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
536    RegEnable(waittable2rename, decodeOut.fire)
537  }
538  rename.io.ssit := memCtrl.io.ssit2Rename
539  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
540  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
541  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
542  rename.io.v0ReadPorts := VecInit(rat.io.v0ReadPorts.map(x => VecInit(x.data)))
543  rename.io.vlReadPorts := VecInit(rat.io.vlReadPorts.map(x => VecInit(x.data)))
544  rename.io.int_need_free := rat.io.int_need_free
545  rename.io.int_old_pdest := rat.io.int_old_pdest
546  rename.io.fp_old_pdest := rat.io.fp_old_pdest
547  rename.io.vec_old_pdest := rat.io.vec_old_pdest
548  rename.io.v0_old_pdest := rat.io.v0_old_pdest
549  rename.io.vl_old_pdest := rat.io.vl_old_pdest
550  rename.io.debug_int_rat.foreach(_ := rat.io.debug_int_rat.get)
551  rename.io.debug_fp_rat.foreach(_ := rat.io.debug_fp_rat.get)
552  rename.io.debug_vec_rat.foreach(_ := rat.io.debug_vec_rat.get)
553  rename.io.debug_v0_rat.foreach(_ := rat.io.debug_v0_rat.get)
554  rename.io.debug_vl_rat.foreach(_ := rat.io.debug_vl_rat.get)
555  rename.io.stallReason.in <> decode.io.stallReason.out
556  rename.io.snpt.snptEnq := DontCare
557  rename.io.snpt.snptDeq := snpt.io.deq
558  rename.io.snpt.useSnpt := useSnpt
559  rename.io.snpt.snptSelect := snptSelect
560  rename.io.snptIsFull := snpt.io.valids.asUInt.andR
561  rename.io.snpt.flushVec := flushVecNext
562  rename.io.snptLastEnq.valid := !isEmpty(snpt.io.enqPtr, snpt.io.deqPtr)
563  rename.io.snptLastEnq.bits := snpt.io.snapshots((snpt.io.enqPtr - 1.U).value).robIdx.head
564
565  val renameOut = Wire(chiselTypeOf(rename.io.out))
566  renameOut <> rename.io.out
567  // pass all snapshot in the first element for correctness of blockBackward
568  renameOut.tail.foreach(_.bits.snapshot := false.B)
569  renameOut.head.bits.snapshot := Mux(isFull(snpt.io.enqPtr, snpt.io.deqPtr),
570    false.B,
571    Cat(rename.io.out.map(out => out.valid && out.bits.snapshot)).orR
572  )
573
574  // pipeline between rename and dispatch
575  PipeGroupConnect(renameOut, dispatch.io.fromRename, s1_s3_redirect.valid, dispatch.io.toRenameAllFire, "renamePipeDispatch")
576  dispatch.io.intIQValidNumVec := io.intIQValidNumVec
577  dispatch.io.fpIQValidNumVec := io.fpIQValidNumVec
578  dispatch.io.fromIntDQ.intDQ0ValidDeq0Num := intDq0.io.validDeq0Num
579  dispatch.io.fromIntDQ.intDQ0ValidDeq1Num := intDq0.io.validDeq1Num
580  dispatch.io.fromIntDQ.intDQ1ValidDeq0Num := intDq1.io.validDeq0Num
581  dispatch.io.fromIntDQ.intDQ1ValidDeq1Num := intDq1.io.validDeq1Num
582
583  dispatch.io.hartId := io.fromTop.hartId
584  dispatch.io.redirect := s1_s3_redirect
585  dispatch.io.enqRob <> rob.io.enq
586  dispatch.io.robHead := rob.io.debugRobHead
587  dispatch.io.stallReason <> rename.io.stallReason.out
588  dispatch.io.lqCanAccept := io.lqCanAccept
589  dispatch.io.sqCanAccept := io.sqCanAccept
590  dispatch.io.robHeadNotReady := rob.io.headNotReady
591  dispatch.io.robFull := rob.io.robFull
592  dispatch.io.singleStep := GatedValidRegNext(io.csrCtrl.singlestep)
593
594  intDq0.io.enq <> dispatch.io.toIntDq0
595  intDq0.io.redirect <> s2_s4_redirect
596  intDq1.io.enq <> dispatch.io.toIntDq1
597  intDq1.io.redirect <> s2_s4_redirect
598
599  fpDq.io.enq <> dispatch.io.toFpDq
600  fpDq.io.redirect <> s2_s4_redirect
601
602  vecDq.io.enq <> dispatch.io.toVecDq
603  vecDq.io.redirect <> s2_s4_redirect
604
605  lsDq.io.enq <> dispatch.io.toLsDq
606  lsDq.io.redirect <> s2_s4_redirect
607
608  io.toIssueBlock.intUops <> (intDq0.io.deq :++ intDq1.io.deq)
609  io.toIssueBlock.fpUops <> fpDq.io.deq
610  io.toIssueBlock.vfUops  <> vecDq.io.deq
611  io.toIssueBlock.memUops <> lsDq.io.deq
612  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
613  io.toIssueBlock.flush   <> s2_s4_redirect
614
615  pcMem.io.wen.head   := GatedValidRegNext(io.frontend.fromFtq.pc_mem_wen)
616  pcMem.io.waddr.head := RegEnable(io.frontend.fromFtq.pc_mem_waddr, io.frontend.fromFtq.pc_mem_wen)
617  pcMem.io.wdata.head := RegEnable(io.frontend.fromFtq.pc_mem_wdata, io.frontend.fromFtq.pc_mem_wen)
618
619  io.toDataPath.flush := s2_s4_redirect
620  io.toExuBlock.flush := s2_s4_redirect
621
622
623  rob.io.hartId := io.fromTop.hartId
624  rob.io.redirect := s1_s3_redirect
625  rob.io.writeback := delayedNotFlushedWriteBack
626  rob.io.exuWriteback := delayedWriteBack
627  rob.io.writebackNums := VecInit(delayedNotFlushedWriteBackNums)
628  rob.io.writebackNeedFlush := delayedNotFlushedWriteBackNeedFlush
629  rob.io.readGPAMemData := gpaMem.io.exceptionReadData
630  rob.io.fromVecExcpMod.busy := io.fromVecExcpMod.busy
631
632  io.redirect := s1_s3_redirect
633
634  // rob to int block
635  io.robio.csr <> rob.io.csr
636  // When wfi is disabled, it will not block ROB commit.
637  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
638  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
639
640  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
641
642  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
643  io.robio.exception := rob.io.exception
644  io.robio.exception.bits.pc := s1_robFlushPc
645
646  // rob to mem block
647  io.robio.lsq <> rob.io.lsq
648
649  io.diff_int_rat.foreach(_ := rat.io.diff_int_rat.get)
650  io.diff_fp_rat .foreach(_ := rat.io.diff_fp_rat.get)
651  io.diff_vec_rat.foreach(_ := rat.io.diff_vec_rat.get)
652  io.diff_v0_rat .foreach(_ := rat.io.diff_v0_rat.get)
653  io.diff_vl_rat .foreach(_ := rat.io.diff_vl_rat.get)
654
655  rob.io.debug_ls := io.robio.debug_ls
656  rob.io.debugHeadLsIssue := io.robio.robHeadLsIssue
657  rob.io.lsTopdownInfo := io.robio.lsTopdownInfo
658  rob.io.csr.criticalErrorState := io.robio.csr.criticalErrorState
659  rob.io.debugEnqLsq := io.debugEnqLsq
660
661  io.robio.robDeqPtr := rob.io.robDeqPtr
662
663  io.robio.storeDebugInfo <> rob.io.storeDebugInfo
664
665  // rob to backend
666  io.robio.commitVType := rob.io.toDecode.commitVType
667  // exu block to decode
668  decode.io.vsetvlVType := io.toDecode.vsetvlVType
669  // backend to decode
670  decode.io.vstart := io.toDecode.vstart
671  // backend to rob
672  rob.io.vstartIsZero := io.toDecode.vstart === 0.U
673
674  io.toCSR.trapInstInfo := decode.io.toCSR.trapInstInfo
675
676  io.toVecExcpMod.logicPhyRegMap := rob.io.toVecExcpMod.logicPhyRegMap
677  io.toVecExcpMod.excpInfo       := rob.io.toVecExcpMod.excpInfo
678  // T  : rat receive rabCommit
679  // T+1: rat return oldPdest
680  io.toVecExcpMod.ratOldPest match {
681    case fromRat =>
682      (0 until RabCommitWidth).foreach { idx =>
683        fromRat.v0OldVdPdest(idx).valid := RegNext(
684          rat.io.rabCommits.isCommit &&
685          rat.io.rabCommits.isWalk &&
686          rat.io.rabCommits.commitValid(idx) &&
687          rat.io.rabCommits.info(idx).v0Wen
688        )
689        fromRat.v0OldVdPdest(idx).bits := rat.io.v0_old_pdest(idx)
690        fromRat.vecOldVdPdest(idx).valid := RegNext(
691          rat.io.rabCommits.isCommit &&
692          rat.io.rabCommits.isWalk &&
693          rat.io.rabCommits.commitValid(idx) &&
694          rat.io.rabCommits.info(idx).vecWen
695        )
696        fromRat.vecOldVdPdest(idx).bits := rat.io.vec_old_pdest(idx)
697      }
698  }
699
700  io.debugTopDown.fromRob := rob.io.debugTopDown.toCore
701  dispatch.io.debugTopDown.fromRob := rob.io.debugTopDown.toDispatch
702  dispatch.io.debugTopDown.fromCore := io.debugTopDown.fromCore
703  io.debugRolling := rob.io.debugRolling
704
705  io.perfInfo.ctrlInfo.robFull := GatedValidRegNext(rob.io.robFull)
706  io.perfInfo.ctrlInfo.intdqFull := GatedValidRegNext(intDq0.io.dqFull || intDq1.io.dqFull)
707  io.perfInfo.ctrlInfo.fpdqFull := GatedValidRegNext(vecDq.io.dqFull)
708  io.perfInfo.ctrlInfo.lsdqFull := GatedValidRegNext(lsDq.io.dqFull)
709
710  val perfEvents = Seq(decode, rename, dispatch, intDq0, intDq1, vecDq, lsDq, rob).flatMap(_.getPerfEvents)
711  generatePerfEvent()
712
713  val criticalErrors = rob.getCriticalErrors
714  generateCriticalErrors()
715}
716
717class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
718  val fromTop = new Bundle {
719    val hartId = Input(UInt(8.W))
720  }
721  val toTop = new Bundle {
722    val cpuHalt = Output(Bool())
723  }
724  val frontend = Flipped(new FrontendToCtrlIO())
725  val fromCSR = new Bundle{
726    val toDecode = Input(new CSRToDecode)
727    val traceCSR = Input(new TraceCSR)
728  }
729  val toIssueBlock = new Bundle {
730    val flush = ValidIO(new Redirect)
731    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
732    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
733    val vfUops = Vec(dpParams.VecDqDeqWidth, DecoupledIO(new DynInst))
734    val fpUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
735    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
736  }
737  val toDataPath = new Bundle {
738    val flush = ValidIO(new Redirect)
739  }
740  val toExuBlock = new Bundle {
741    val flush = ValidIO(new Redirect)
742  }
743  val toCSR = new Bundle {
744    val trapInstInfo = Output(ValidIO(new TrapInstInfo))
745  }
746  val intIQValidNumVec = Input(MixedVec(params.genIntIQValidNumBundle))
747  val fpIQValidNumVec = Input(MixedVec(params.genFpIQValidNumBundle))
748  val fromWB = new Bundle {
749    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
750  }
751  val redirect = ValidIO(new Redirect)
752  val fromMem = new Bundle {
753    val stIn = Vec(params.StaExuCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
754    val violation = Flipped(ValidIO(new Redirect))
755  }
756  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
757  val memStPcRead = Vec(params.StaCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
758  val memHyPcRead = Vec(params.HyuCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
759
760  val csrCtrl = Input(new CustomCSRCtrlIO)
761  val robio = new Bundle {
762    val csr = new RobCSRIO
763    val exception = ValidIO(new ExceptionInfo)
764    val lsq = new RobLsqIO
765    val lsTopdownInfo = Vec(params.LduCnt + params.HyuCnt, Input(new LsTopdownInfo))
766    val debug_ls = Input(new DebugLSIO())
767    val robHeadLsIssue = Input(Bool())
768    val robDeqPtr = Output(new RobPtr)
769    val commitVType = new Bundle {
770      val vtype = Output(ValidIO(VType()))
771      val hasVsetvl = Output(Bool())
772    }
773
774    // store event difftest information
775    val storeDebugInfo = Vec(EnsbufferWidth, new Bundle {
776      val robidx = Input(new RobPtr)
777      val pc     = Output(UInt(VAddrBits.W))
778    })
779  }
780
781  val toDecode = new Bundle {
782    val vsetvlVType = Input(VType())
783    val vstart = Input(Vl())
784  }
785
786  val fromVecExcpMod = Input(new Bundle {
787    val busy = Bool()
788  })
789
790  val toVecExcpMod = Output(new Bundle {
791    val logicPhyRegMap = Vec(RabCommitWidth, ValidIO(new RegWriteFromRab))
792    val excpInfo = ValidIO(new VecExcpInfo)
793    val ratOldPest = new RatToVecExcpMod
794  })
795
796  val traceCoreInterface = new TraceCoreInterface
797
798  val perfInfo = Output(new Bundle{
799    val ctrlInfo = new Bundle {
800      val robFull   = Bool()
801      val intdqFull = Bool()
802      val fpdqFull  = Bool()
803      val lsdqFull  = Bool()
804    }
805  })
806  val diff_int_rat = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
807  val diff_fp_rat  = if (params.basicDebugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None
808  val diff_vec_rat = if (params.basicDebugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None
809  val diff_v0_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
810  val diff_vl_rat  = if (params.basicDebugEn) Some(Vec(1, Output(UInt(PhyRegIdxWidth.W)))) else None
811
812  val sqCanAccept = Input(Bool())
813  val lqCanAccept = Input(Bool())
814
815  val debugTopDown = new Bundle {
816    val fromRob = new RobCoreTopDownIO
817    val fromCore = new CoreDispatchTopDownIO
818  }
819  val debugRolling = new RobDebugRollingIO
820  val debugEnqLsq = Input(new LsqEnqIO)
821}
822
823class NamedIndexes(namedCnt: Seq[(String, Int)]) {
824  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
825
826  val maxIdx = namedCnt.map(_._2).sum
827  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
828    val begin = namedCnt.slice(0, i).map(_._2).sum
829    val end = begin + namedCnt(i)._2
830    (namedCnt(i)._1, (begin, end))
831  }.toMap
832
833  def apply(name: String): Seq[Int] = {
834    require(nameRangeMap.contains(name))
835    nameRangeMap(name)._1 until nameRangeMap(name)._2
836  }
837}
838