xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 141a6449de9d8f61eb4f2f5e670af29782902672)
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 chipsalliance.rocketchip.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.ctrlblock.{MemCtrl, RedirectGenerator}
28import xiangshan.backend.decode.{DecodeStage, FusionDecoder}
29import xiangshan.backend.dispatch.{Dispatch, DispatchQueue}
30import xiangshan.backend.fu.PFEvent
31import xiangshan.backend.rename.{Rename, RenameTableWrapper}
32import xiangshan.backend.rob.{Rob, RobCSRIO, RobLsqIO}
33import xiangshan.frontend.{FtqRead, Ftq_RF_Components}
34import xiangshan.v2backend.Bundles.{DecodedInst, DynInst, ExceptionInfo, ExuOutput}
35import xiangshan.v2backend.{BackendParams, VAddrData}
36
37class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
38  def numRedirect = backendParams.numRedirect
39  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
40  val redirect = Valid(new Redirect)
41}
42
43class CtrlBlock(params: BackendParams)(implicit p: Parameters) extends LazyModule {
44  val rob = LazyModule(new Rob(params))
45
46//  override def addWritebackSink(source: Seq[HasWritebackSource], index: Option[Seq[Int]]): HasWritebackSink = {
47//    rob.addWritebackSink(Seq(this), Some(Seq(writebackSinks.length)))
48//    super.addWritebackSink(source, index)
49//  }
50
51  lazy val module = new CtrlBlockImp(this)(p, params)
52
53}
54
55class CtrlBlockImp(
56  override val wrapper: CtrlBlock
57)(implicit
58  p: Parameters,
59  params: BackendParams
60) extends LazyModuleImp(wrapper)
61  with HasXSParameter
62  with HasCircularQueuePtrHelper
63  with HasPerfEvents
64{
65  val pcMemRdIndexes = new NamedIndexes(Seq(
66    "exu"       -> params.numPcReadPort,
67    "redirect"  -> 1,
68    "memPred"   -> 1,
69    "robFlush"  -> 1,
70    "load"      -> params.LduCnt,
71  ))
72
73  private val numPcMemReadForExu = params.numPcReadPort
74  private val numPcMemRead = pcMemRdIndexes.maxIdx
75
76  private val numTargetMemRead = numPcMemReadForExu
77
78  println(s"pcMem read num: $numPcMemRead")
79  println(s"pcMem read num for exu: $numPcMemReadForExu")
80  println(s"targetMem read num: $numTargetMemRead")
81
82  val io = IO(new CtrlBlockIO())
83
84  val decode = Module(new DecodeStage)
85  val fusionDecoder = Module(new FusionDecoder)
86  val rat = Module(new RenameTableWrapper)
87  val rename = Module(new Rename)
88  val dispatch = Module(new Dispatch)
89  val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth))
90  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth))
91  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
92  val redirectGen = Module(new RedirectGenerator)
93  private val pcMem = Module(new SyncDataModuleTemplate(new Ftq_RF_Components, FtqSize, numPcMemRead, 1, "BackendPC"))
94  private val targetMem = Module(new SyncDataModuleTemplate(UInt(VAddrData().dataWidth.W), FtqSize, numTargetMemRead, 1))
95  private val rob = wrapper.rob.module
96  private val memCtrl = Module(new MemCtrl(params))
97
98  private val disableFusion = decode.io.csrCtrl.singlestep || !decode.io.csrCtrl.fusion_enable
99
100  private val s0_robFlushRedirect = rob.io.flushOut
101  private val s1_robFlushRedirect = Wire(Valid(new Redirect))
102  s1_robFlushRedirect.valid := RegNext(s0_robFlushRedirect.valid)
103  s1_robFlushRedirect.bits := RegEnable(s0_robFlushRedirect.bits, s0_robFlushRedirect.valid)
104
105  pcMem.io.raddr(pcMemRdIndexes("robFlush").head) := s0_robFlushRedirect.bits.ftqIdx.value
106  private val s1_robFlushPc = pcMem.io.rdata(pcMemRdIndexes("robFlush").head).getPc(RegNext(s0_robFlushRedirect.bits.ftqOffset))
107  private val s3_redirectGen = redirectGen.io.stage2Redirect
108  private val s1_s3_redirect = Mux(s1_robFlushRedirect.valid, s1_robFlushRedirect, s3_redirectGen)
109  private val s2_s4_pendingRedirectValid = RegInit(false.B)
110  when (s1_s3_redirect.valid) {
111    s2_s4_pendingRedirectValid := true.B
112  }.elsewhen (RegNext(io.frontend.toFtq.redirect.valid)) {
113    s2_s4_pendingRedirectValid := false.B
114  }
115
116  // Redirect will be RegNext at ExuBlocks and IssueBlocks
117  val s2_s4_redirect = RegNextWithEnable(s1_s3_redirect)
118  val s3_s5_redirect = RegNextWithEnable(s2_s4_redirect)
119
120  private val delayedNotFlushedWriteBack = io.fromWB.wbData.map(x => {
121    val valid = x.valid
122    val killedByOlder = x.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect, s3_s5_redirect))
123    val delayed = Wire(Valid(new ExuOutput(x.bits.params)))
124    delayed.valid := RegNext(valid && !killedByOlder)
125    delayed.bits := RegEnable(x.bits, x.valid)
126    delayed
127  })
128
129  private val exuPredecode = VecInit(
130    delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => x.bits.predecodeInfo.get)
131  )
132
133  private val exuRedirects: IndexedSeq[ValidIO[Redirect]] = delayedNotFlushedWriteBack.filter(_.bits.redirect.nonEmpty).map(x => {
134    val out = Wire(Valid(new Redirect()))
135    out.valid := x.valid && x.bits.redirect.get.valid
136    out.bits := x.bits.redirect.get.bits
137    out
138  })
139
140  private val memViolation = io.fromMem.violation
141  val loadReplay = Wire(ValidIO(new Redirect))
142  loadReplay.valid := RegNext(memViolation.valid &&
143    !memViolation.bits.robIdx.needFlush(Seq(s1_s3_redirect, s2_s4_redirect))
144  )
145  loadReplay.bits := RegEnable(memViolation.bits, memViolation.valid)
146
147  //  val isCommitWriteVconfigVec = rob.io.commits.commitValid.zip(rob.io.commits.info).map{case (valid, info) => valid && info.ldest === 32.U}.reverse
148//  val isWalkWriteVconfigVec = rob.io.commits.walkValid.zip(rob.io.commits.info).map{case (valid, info) => valid && info.ldest === 32.U}.reverse
149  val pdestReverse = rob.io.commits.info.map(info => info.pdest).reverse
150//  val commitSel = PriorityMux(isCommitWriteVconfigVec, pdestReverse)
151//  val walkSel = PriorityMux(isWalkWriteVconfigVec, pdestReverse)
152//  val vconfigAddr = Mux(rob.io.commits.isCommit, commitSel, walkSel)
153//  decode.io.vconfig := io.vconfigReadPort.data
154//  decode.io.isVsetFlushPipe := rob.io.isVsetFlushPipe
155
156  pcMem.io.raddr(pcMemRdIndexes("redirect").head) := redirectGen.io.redirectPcRead.ptr.value
157  redirectGen.io.redirectPcRead.data := pcMem.io.rdata(pcMemRdIndexes("redirect").head).getPc(RegNext(redirectGen.io.redirectPcRead.offset))
158  pcMem.io.raddr(pcMemRdIndexes("memPred").head) := redirectGen.io.memPredPcRead.ptr.value
159  redirectGen.io.memPredPcRead.data := pcMem.io.rdata(pcMemRdIndexes("memPred").head).getPc(RegNext(redirectGen.io.memPredPcRead.offset))
160
161  for ((pcMemIdx, i) <- pcMemRdIndexes("load").zipWithIndex) {
162    pcMem.io.raddr(pcMemIdx) := io.memLdPcRead(i).ptr.value
163    io.memLdPcRead(i).data := pcMem.io.rdata(pcMemIdx).getPc(RegNext(io.memLdPcRead(i).offset))
164  }
165
166  redirectGen.io.hartId := io.fromTop.hartId
167  redirectGen.io.exuRedirect := exuRedirects
168  redirectGen.io.exuOutPredecode := exuPredecode // garded by exuRedirect.valid
169  redirectGen.io.loadReplay <> loadReplay
170
171  redirectGen.io.robFlush := s1_robFlushRedirect.valid
172
173  val s6_frontendFlushValid = DelayN(s1_robFlushRedirect.valid, 5)
174  val frontendFlushBits = RegEnable(s1_robFlushRedirect.bits, s1_robFlushRedirect.valid) // ??
175  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
176  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
177  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
178  for (i <- 0 until CommitWidth) {
179    // why flushOut: instructions with flushPipe are not commited to frontend
180    // If we commit them to frontend, it will cause flush after commit, which is not acceptable by frontend.
181    val s1_isCommit = rob.io.commits.commitValid(i) && rob.io.commits.isCommit && rob.io.commits.info(i).uopIdx.andR && !s0_robFlushRedirect.valid
182    io.frontend.toFtq.rob_commits(i).valid := RegNext(s1_isCommit)
183    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), s1_isCommit)
184  }
185  io.frontend.toFtq.redirect.valid := s6_frontendFlushValid || s3_redirectGen.valid
186  io.frontend.toFtq.redirect.bits := Mux(s6_frontendFlushValid, frontendFlushBits, s3_redirectGen.bits)
187  // Be careful here:
188  // T0: rob.io.flushOut, s0_robFlushRedirect
189  // T1: s1_robFlushRedirect, rob.io.exception.valid
190  // T2: csr.redirect.valid
191  // T3: csr.exception.valid
192  // T4: csr.trapTarget
193  // T5: ctrlBlock.trapTarget
194  // T6: io.frontend.toFtq.stage2Redirect.valid
195  val s2_robFlushPc = RegEnable(Mux(s1_robFlushRedirect.bits.flushItself(),
196    s1_robFlushPc, // replay inst
197    s1_robFlushPc + 4.U // flush pipe
198  ), s1_robFlushRedirect.valid)
199  private val s2_csrIsXRet = io.robio.csr.isXRet
200  private val s5_csrIsTrap = DelayN(rob.io.exception.valid, 4)
201  private val s2_s5_trapTargetFromCsr = io.robio.csr.trapTarget
202
203  val flushTarget = Mux(s2_csrIsXRet || s5_csrIsTrap, s2_s5_trapTargetFromCsr, s2_robFlushPc)
204  when (s6_frontendFlushValid) {
205    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
206    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegNext(flushTarget)
207  }
208
209  if (env.EnableTopDown) {
210    val stage2Redirect_valid_when_pending = s2_s4_pendingRedirectValid && s1_s3_redirect.valid
211
212    val stage2_redirect_cycles = RegInit(false.B)                                         // frontend_bound->fetch_lantency->stage2_redirect
213    val MissPredPending = RegInit(false.B); val branch_resteers_cycles = RegInit(false.B) // frontend_bound->fetch_lantency->stage2_redirect->branch_resteers
214    val RobFlushPending = RegInit(false.B); val robFlush_bubble_cycles = RegInit(false.B) // frontend_bound->fetch_lantency->stage2_redirect->robflush_bubble
215    val LdReplayPending = RegInit(false.B); val ldReplay_bubble_cycles = RegInit(false.B) // frontend_bound->fetch_lantency->stage2_redirect->ldReplay_bubble
216
217    when(redirectGen.io.isMisspreRedirect) { MissPredPending := true.B }
218    when(s1_robFlushRedirect.valid)              { RobFlushPending := true.B }
219    when(redirectGen.io.loadReplay.valid)  { LdReplayPending := true.B }
220
221    when (RegNext(io.frontend.toFtq.redirect.valid)) {
222      when(s2_s4_pendingRedirectValid) {                             stage2_redirect_cycles := true.B }
223      when(MissPredPending) { MissPredPending := false.B; branch_resteers_cycles := true.B }
224      when(RobFlushPending) { RobFlushPending := false.B; robFlush_bubble_cycles := true.B }
225      when(LdReplayPending) { LdReplayPending := false.B; ldReplay_bubble_cycles := true.B }
226    }
227
228    when(VecInit(decode.io.out.map(x => x.valid)).asUInt.orR){
229      when(stage2_redirect_cycles) { stage2_redirect_cycles := false.B }
230      when(branch_resteers_cycles) { branch_resteers_cycles := false.B }
231      when(robFlush_bubble_cycles) { robFlush_bubble_cycles := false.B }
232      when(ldReplay_bubble_cycles) { ldReplay_bubble_cycles := false.B }
233    }
234
235    XSPerfAccumulate("stage2_redirect_cycles", stage2_redirect_cycles)
236    XSPerfAccumulate("branch_resteers_cycles", branch_resteers_cycles)
237    XSPerfAccumulate("robFlush_bubble_cycles", robFlush_bubble_cycles)
238    XSPerfAccumulate("ldReplay_bubble_cycles", ldReplay_bubble_cycles)
239    XSPerfAccumulate("s2Redirect_pend_cycles", stage2Redirect_valid_when_pending)
240  }
241
242  decode.io.in.zip(io.frontend.cfVec).foreach { case (decodeIn, frontendCf) =>
243    decodeIn.valid := frontendCf.valid
244    frontendCf.ready := decodeIn.ready
245    decodeIn.bits.connectCtrlFlow(frontendCf.bits)
246  }
247  decode.io.csrCtrl := RegNext(io.csrCtrl)
248  decode.io.intRat <> rat.io.intReadPorts
249  decode.io.fpRat <> rat.io.fpReadPorts
250  decode.io.vecRat <> rat.io.vecReadPorts
251  decode.io.fusion := 0.U.asTypeOf(decode.io.fusion) // Todo
252//  decode.io.isRedirect <> stage2Redirect.valid
253//  decode.io.robCommits <> rob.io.commits
254
255  val decodeHasException = decode.io.out.map(x => x.bits.exceptionVec(instrPageFault) || x.bits.exceptionVec(instrAccessFault))
256  // fusion decoder
257  for (i <- 0 until DecodeWidth) {
258    fusionDecoder.io.in(i).valid := decode.io.out(i).valid && !(decodeHasException(i) || disableFusion)
259    fusionDecoder.io.in(i).bits := decode.io.out(i).bits.instr
260    if (i > 0) {
261      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
262    }
263  }
264
265  private val decodePipeRename = Wire(Vec(RenameWidth, DecoupledIO(new DecodedInst)))
266
267  for (i <- 0 until RenameWidth) {
268    PipelineConnect(decode.io.out(i), decodePipeRename(i), rename.io.in(i).ready,
269      s1_s3_redirect.valid || s2_s4_pendingRedirectValid, moduleName = Some("decodePipeRenameModule"))
270
271    decodePipeRename(i).ready := rename.io.in(i).ready
272    rename.io.in(i).valid := decodePipeRename(i).valid && !fusionDecoder.io.clear(i)
273    rename.io.in(i).bits := decodePipeRename(i).bits
274  }
275
276  for (i <- 0 until RenameWidth - 1) {
277    fusionDecoder.io.dec(i) := decodePipeRename(i).bits
278    rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
279
280    // update the first RenameWidth - 1 instructions
281    decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
282    when (fusionDecoder.io.out(i).valid) {
283      fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits)
284      // TODO: remove this dirty code for ftq update
285      val sameFtqPtr = rename.io.in(i).bits.ftqPtr.value === rename.io.in(i + 1).bits.ftqPtr.value
286      val ftqOffset0 = rename.io.in(i).bits.ftqOffset
287      val ftqOffset1 = rename.io.in(i + 1).bits.ftqOffset
288      val ftqOffsetDiff = ftqOffset1 - ftqOffset0
289      val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
290      val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
291      val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
292      val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
293      rename.io.in(i).bits.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
294      XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
295    }
296
297  }
298
299  // memory dependency predict
300  // when decode, send fold pc to mdp
301  private val mdpFlodPcVec = Wire(Vec(DecodeWidth, UInt(MemPredPCWidth.W)))
302  for (i <- 0 until DecodeWidth) {
303    mdpFlodPcVec(i) := Mux(
304      decode.io.out(i).fire,
305      decode.io.in(i).bits.foldpc,
306      rename.io.in(i).bits.foldpc
307    )
308  }
309
310  // currently, we only update mdp info when isReplay
311  memCtrl.io.redirect <> s1_s3_redirect
312  memCtrl.io.csrCtrl := io.csrCtrl                          // RegNext in memCtrl
313  memCtrl.io.stIn := io.fromMem.stIn                        // RegNext in memCtrl
314  memCtrl.io.memPredUpdate := redirectGen.io.memPredUpdate  // RegNext in memCtrl
315  memCtrl.io.mdpFlodPcVec := mdpFlodPcVec
316  memCtrl.io.dispatchLFSTio <> dispatch.io.lfst
317
318  rat.io.redirect := s1_s3_redirect.valid
319  rat.io.robCommits := rob.io.commits
320  rat.io.intRenamePorts := rename.io.intRenamePorts
321  rat.io.fpRenamePorts := rename.io.fpRenamePorts
322  rat.io.vecRenamePorts := rename.io.vecRenamePorts
323
324  rename.io.redirect := s1_s3_redirect
325  rename.io.robCommits <> rob.io.commits
326  rename.io.waittable := (memCtrl.io.waitTable2Rename zip decode.io.out).map{ case(waittable2rename, decodeOut) =>
327    RegEnable(waittable2rename, decodeOut.fire)
328  }
329  rename.io.ssit := memCtrl.io.ssit2Rename
330  rename.io.intReadPorts := VecInit(rat.io.intReadPorts.map(x => VecInit(x.map(_.data))))
331  rename.io.fpReadPorts := VecInit(rat.io.fpReadPorts.map(x => VecInit(x.map(_.data))))
332  rename.io.vecReadPorts := VecInit(rat.io.vecReadPorts.map(x => VecInit(x.map(_.data))))
333  rename.io.debug_int_rat := rat.io.debug_int_rat
334  rename.io.debug_fp_rat := rat.io.debug_fp_rat
335  rename.io.debug_vconfig_rat := rat.io.debug_vconfig_rat
336  rename.io.debug_vec_rat := rat.io.debug_vec_rat
337
338  // pipeline between rename and dispatch
339  for (i <- 0 until RenameWidth) {
340    PipelineConnect(rename.io.out(i), dispatch.io.fromRename(i), dispatch.io.recv(i), s1_s3_redirect.valid)
341  }
342
343  dispatch.io.hartId := io.fromTop.hartId
344  dispatch.io.redirect <> s1_s3_redirect
345  dispatch.io.enqRob <> rob.io.enq
346  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
347
348  intDq.io.enq <> dispatch.io.toIntDq
349  intDq.io.redirect <> s2_s4_redirect
350
351  fpDq.io.enq <> dispatch.io.toFpDq
352  fpDq.io.redirect <> s2_s4_redirect
353
354  lsDq.io.enq <> dispatch.io.toLsDq
355  lsDq.io.redirect <> s2_s4_redirect
356
357  io.toIssueBlock.intUops <> intDq.io.deq
358  io.toIssueBlock.vfUops  <> fpDq.io.deq
359  io.toIssueBlock.memUops <> lsDq.io.deq
360  io.toIssueBlock.allocPregs <> dispatch.io.allocPregs
361  io.toIssueBlock.flush   <> s2_s4_redirect
362
363  pcMem.io.wen.head   := RegNext(io.frontend.fromFtq.pc_mem_wen)
364  pcMem.io.waddr.head := RegNext(io.frontend.fromFtq.pc_mem_waddr)
365  pcMem.io.wdata.head := RegNext(io.frontend.fromFtq.pc_mem_wdata)
366  targetMem.io.wen.head := RegNext(io.frontend.fromFtq.pc_mem_wen)
367  targetMem.io.waddr.head := RegNext(io.frontend.fromFtq.pc_mem_waddr)
368  targetMem.io.wdata.head := RegNext(io.frontend.fromFtq.pc_mem_wdata.startAddr)
369
370  private val jumpPcVec         : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
371  private val jumpTargetReadVec : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
372  private val jumpTargetVec     : Vec[UInt] = Wire(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
373  io.toIssueBlock.pcVec := jumpPcVec
374  io.toIssueBlock.targetVec := jumpTargetVec
375
376  io.toDataPath.flush := s2_s4_redirect
377  io.toExuBlock.flush := s2_s4_redirect
378
379  for ((pcMemIdx, i) <- pcMemRdIndexes("exu").zipWithIndex) {
380    pcMem.io.raddr(pcMemIdx) := intDq.io.deqNext(i).ftqPtr.value
381    jumpPcVec(i) := pcMem.io.rdata(pcMemIdx).getPc(RegNext(intDq.io.deqNext(i).ftqOffset))
382  }
383
384  val dqOuts = Seq(io.toIssueBlock.intUops) ++ Seq(io.toIssueBlock.vfUops) ++ Seq(io.toIssueBlock.memUops)
385  dqOuts.zipWithIndex.foreach { case (dqOut, dqIdx) =>
386    dqOut.map(_.bits.pc).zipWithIndex.map{ case (pc, portIdx) =>
387      if(params.allSchdParams(dqIdx).numPcReadPort > 0){
388        val realJumpPcVec = jumpPcVec.drop(params.allSchdParams.take(dqIdx).map(_.numPcReadPort).sum).take(params.allSchdParams(dqIdx).numPcReadPort)
389        pc := realJumpPcVec(portIdx)
390      }
391    }
392  }
393
394  private val newestTarget: UInt = io.frontend.fromFtq.newest_entry_target
395  for (i <- 0 until numTargetMemRead) {
396    val targetPtr = intDq.io.deqNext(i).ftqPtr
397    // target pc stored in next entry
398    targetMem.io.raddr(i) := (targetPtr + 1.U).value
399    jumpTargetReadVec(i) := targetMem.io.rdata(i)
400    val needNewestTarget = RegNext(targetPtr === io.frontend.fromFtq.newest_entry_ptr)
401    jumpTargetVec(i) := Mux(
402      needNewestTarget,
403      RegNext(newestTarget),
404      jumpTargetReadVec(i)
405    )
406  }
407
408  rob.io.hartId := io.fromTop.hartId
409  rob.io.redirect <> s1_s3_redirect
410  rob.io.writeback := delayedNotFlushedWriteBack
411
412  io.redirect <> s1_s3_redirect
413
414  // rob to int block
415  io.robio.csr <> rob.io.csr
416  // When wfi is disabled, it will not block ROB commit.
417  rob.io.csr.wfiEvent := io.robio.csr.wfiEvent
418  rob.io.wfi_enable := decode.io.csrCtrl.wfi_enable
419
420  io.toTop.cpuHalt := DelayN(rob.io.cpu_halt, 5)
421
422  io.robio.csr.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
423  io.robio.exception := rob.io.exception
424  io.robio.exception.bits.pc := s1_robFlushPc
425
426//  io.robio.csr.vcsrFlag := RegNext(rob.io.commits.isCommit && Cat(isCommitWriteVconfigVec).orR)
427
428  // rob to mem block
429  io.robio.lsq <> rob.io.lsq
430
431  io.debug_int_rat := rat.io.debug_int_rat
432  io.debug_fp_rat := rat.io.debug_fp_rat
433  io.debug_vec_rat := rat.io.debug_vec_rat
434  io.debug_vconfig_rat := rat.io.debug_vconfig_rat
435
436  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
437  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
438  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
439  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
440
441  val pfevent = Module(new PFEvent)
442  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
443  val csrevents = pfevent.io.hpmevent.slice(8,16)
444
445  val perfinfo = IO(new Bundle(){
446    val perfEventsRs      = Input(Vec(params.IqCnt, new PerfEvent))
447    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
448    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
449  })
450
451  val allPerfEvents = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerf)
452  val hpmEvents = allPerfEvents ++ perfinfo.perfEventsEu0 ++ perfinfo.perfEventsEu1 ++ perfinfo.perfEventsRs
453  val perfEvents = HPerfMonitor(csrevents, hpmEvents).getPerfEvents
454  generatePerfEvent()
455}
456
457class CtrlBlockIO()(implicit p: Parameters, params: BackendParams) extends XSBundle {
458  val fromTop = new Bundle {
459    val hartId = Input(UInt(8.W))
460  }
461  val toTop = new Bundle {
462    val cpuHalt = Output(Bool())
463  }
464  val frontend = Flipped(new FrontendToCtrlIO())
465  val toIssueBlock = new Bundle {
466    val flush = ValidIO(new Redirect)
467    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
468    val intUops = Vec(dpParams.IntDqDeqWidth, DecoupledIO(new DynInst))
469    val vfUops = Vec(dpParams.FpDqDeqWidth, DecoupledIO(new DynInst))
470    val memUops = Vec(dpParams.LsDqDeqWidth, DecoupledIO(new DynInst))
471    val pcVec = Output(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
472    val targetVec = Output(Vec(params.numPcReadPort, UInt(VAddrData().dataWidth.W)))
473  }
474  val toDataPath = new Bundle {
475    val flush = ValidIO(new Redirect)
476  }
477  val toExuBlock = new Bundle {
478    val flush = ValidIO(new Redirect)
479  }
480  val fromWB = new Bundle {
481    val wbData = Flipped(MixedVec(params.genWrite2CtrlBundles))
482  }
483  val redirect = ValidIO(new Redirect)
484  val fromMem = new Bundle {
485    val stIn = Vec(params.StaCnt, Flipped(ValidIO(new DynInst))) // use storeSetHit, ssid, robIdx
486    val violation = Flipped(ValidIO(new Redirect))
487  }
488  val memLdPcRead = Vec(params.LduCnt, Flipped(new FtqRead(UInt(VAddrBits.W))))
489  val csrCtrl = Input(new CustomCSRCtrlIO)
490  val robio = new Bundle {
491    val csr = new RobCSRIO
492    val exception = ValidIO(new ExceptionInfo)
493    val lsq = new RobLsqIO
494  }
495
496  val perfInfo = Output(new Bundle{
497    val ctrlInfo = new Bundle {
498      val robFull   = Bool()
499      val intdqFull = Bool()
500      val fpdqFull  = Bool()
501      val lsdqFull  = Bool()
502    }
503  })
504  val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
505  val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
506  val debug_vec_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) // TODO: use me
507  val debug_vconfig_rat = Output(UInt(PhyRegIdxWidth.W)) // TODO: use me
508
509}
510
511class NamedIndexes(namedCnt: Seq[(String, Int)]) {
512  require(namedCnt.map(_._1).distinct.size == namedCnt.size, "namedCnt should not have the same name")
513
514  val maxIdx = namedCnt.map(_._2).sum
515  val nameRangeMap: Map[String, (Int, Int)] = namedCnt.indices.map { i =>
516    val begin = namedCnt.slice(0, i).map(_._2).sum
517    val end = begin + namedCnt(i)._2
518    (namedCnt(i)._1, (begin, end))
519  }.toMap
520
521  def apply(name: String): Seq[Int] = {
522    require(nameRangeMap.contains(name))
523    nameRangeMap(name)._1 until nameRangeMap(name)._2
524  }
525}
526