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