xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 0dc4893d7629b8ec8a9c78fd371177de2d6aac94)
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 utils._
24import xiangshan._
25import xiangshan.backend.decode.{DecodeStage, FusionDecoder, ImmUnion}
26import xiangshan.backend.dispatch.{Dispatch, DispatchQueue}
27import xiangshan.backend.fu.PFEvent
28import xiangshan.backend.rename.{Rename, RenameTableWrapper}
29import xiangshan.backend.rob.{Rob, RobCSRIO, RobLsqIO}
30import xiangshan.frontend.FtqRead
31import xiangshan.mem.mdp.{LFST, SSIT, WaitTable}
32import xiangshan.ExceptionNO._
33
34class CtrlToFtqIO(implicit p: Parameters) extends XSBundle {
35  def numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt
36  val rob_commits = Vec(CommitWidth, Valid(new RobCommitInfo))
37  val redirect = Valid(new Redirect)
38  val for_redirect_gen = new Bundle {
39    val rawRedirect = Valid(new Redirect)
40    val s1_redirect_onehot = Output(Vec(numRedirect+1, Bool()))
41    val s1_oldest_redirect = ValidIO(new Redirect)
42    val s1_oldest_exu_output = ValidIO(new ExuOutput)
43    val s1_jumpTarget = Output(UInt(VAddrBits.W))
44    val flushRedirect = Valid(new Redirect)
45    val frontendFlushTarget = Output(UInt(VAddrBits.W))
46  }
47}
48
49class RedirectGenerator(implicit p: Parameters) extends XSModule
50  with HasCircularQueuePtrHelper {
51
52  class RedirectGeneratorIO(implicit p: Parameters) extends XSBundle {
53    def numRedirect = exuParameters.JmpCnt + exuParameters.AluCnt
54    val hartId = Input(UInt(8.W))
55    val exuMispredict = Vec(numRedirect, Flipped(ValidIO(new ExuOutput)))
56    val loadReplay = Flipped(ValidIO(new Redirect))
57    val flush = Input(Bool())
58    val stage1PcRead = Vec(numRedirect+1, new FtqRead(UInt(VAddrBits.W)))
59    val stage2Redirect = ValidIO(new Redirect)
60    val stage3Redirect = ValidIO(new Redirect)
61    val memPredUpdate = Output(new MemPredUpdateReq)
62    val memPredPcRead = new FtqRead(UInt(VAddrBits.W)) // read req send form stage 2
63    val for_frontend_redirect_gen = new Bundle {
64      val s1_jumpTarget = Output(UInt(VAddrBits.W))
65      val s1_redirect_onehot = Output(Vec(numRedirect+1, Bool()))
66      val s1_oldest_redirect = ValidIO(new Redirect)
67      val s1_oldest_exu_output = ValidIO(new ExuOutput)
68      val s1_real_pc = Input(UInt(VAddrBits.W))
69    }
70  }
71  val io = IO(new RedirectGeneratorIO)
72  /*
73        LoadQueue  Jump  ALU0  ALU1  ALU2  ALU3   exception    Stage1
74          |         |      |    |     |     |         |
75          |============= reg & compare =====|         |       ========
76                            |                         |
77                            |                         |
78                            |                         |        Stage2
79                            |                         |
80                    redirect (flush backend)          |
81                    |                                 |
82               === reg ===                            |       ========
83                    |                                 |
84                    |----- mux (exception first) -----|        Stage3
85                            |
86                redirect (send to frontend)
87   */
88  private class Wrapper(val n: Int) extends Bundle {
89    val redirect = new Redirect
90    val valid = Bool()
91    val idx = UInt(log2Up(n).W)
92  }
93  def selectOldestRedirect(xs: Seq[Valid[Redirect]]): Vec[Bool] = {
94    val compareVec = (0 until xs.length).map(i => (0 until i).map(j => isAfter(xs(j).bits.robIdx, xs(i).bits.robIdx)))
95    val resultOnehot = VecInit((0 until xs.length).map(i => Cat((0 until xs.length).map(j =>
96      (if (j < i) !xs(j).valid || compareVec(i)(j)
97      else if (j == i) xs(i).valid
98      else !xs(j).valid || !compareVec(j)(i))
99    )).andR))
100    resultOnehot
101  }
102
103  val redirects = io.exuMispredict.map(_.bits.redirect) :+ io.loadReplay.bits
104  val stage1FtqReadPcs =
105    (io.stage1PcRead zip redirects).map{ case (r, redirect) =>
106      r(redirect.ftqIdx, redirect.ftqOffset)
107    }
108
109  def getRedirect(exuOut: Valid[ExuOutput]): ValidIO[Redirect] = {
110    val redirect = Wire(Valid(new Redirect))
111    redirect.valid := exuOut.valid && exuOut.bits.redirect.cfiUpdate.isMisPred
112    redirect.bits := exuOut.bits.redirect
113    redirect
114  }
115
116  val jumpOut = io.exuMispredict.head
117  val allRedirect = VecInit(io.exuMispredict.map(x => getRedirect(x)) :+ io.loadReplay)
118  val oldestOneHot = selectOldestRedirect(allRedirect)
119  val needFlushVec = VecInit(allRedirect.map(_.bits.robIdx.needFlush(io.stage2Redirect) || io.flush))
120  val oldestValid = VecInit(oldestOneHot.zip(needFlushVec).map{ case (v, f) => v && !f }).asUInt.orR
121  val oldestExuOutput = Mux1H(io.exuMispredict.indices.map(oldestOneHot), io.exuMispredict)
122  val oldestRedirect = Mux1H(oldestOneHot, allRedirect)
123
124  val s1_jumpTarget = RegEnable(jumpOut.bits.redirect.cfiUpdate.target, jumpOut.valid)
125  val s1_imm12_reg = RegNext(oldestExuOutput.bits.uop.ctrl.imm(11, 0))
126  val s1_pd = RegNext(oldestExuOutput.bits.uop.cf.pd)
127  val s1_redirect_bits_reg = RegNext(oldestRedirect.bits)
128  val s1_redirect_valid_reg = RegNext(oldestValid)
129  val s1_redirect_onehot = RegNext(oldestOneHot)
130  io.for_frontend_redirect_gen.s1_jumpTarget := s1_jumpTarget
131  io.for_frontend_redirect_gen.s1_redirect_onehot := s1_redirect_onehot
132  io.for_frontend_redirect_gen.s1_oldest_redirect.valid := s1_redirect_valid_reg
133  io.for_frontend_redirect_gen.s1_oldest_redirect.bits := s1_redirect_bits_reg
134  io.for_frontend_redirect_gen.s1_oldest_exu_output := RegNext(oldestExuOutput)
135
136  // stage1 -> stage2
137  io.stage2Redirect.valid := s1_redirect_valid_reg && !io.flush
138  io.stage2Redirect.bits := s1_redirect_bits_reg
139
140  val s1_isReplay = s1_redirect_onehot.last
141  val s1_isJump = s1_redirect_onehot.head
142  val real_pc = Mux1H(s1_redirect_onehot, stage1FtqReadPcs)
143  val brTarget = real_pc + SignExt(ImmUnion.B.toImm32(s1_imm12_reg), XLEN)
144  val snpc = real_pc + Mux(s1_pd.isRVC, 2.U, 4.U)
145  val target = Mux(s1_isReplay,
146    real_pc, // replay from itself
147    Mux(s1_redirect_bits_reg.cfiUpdate.taken,
148      Mux(s1_isJump, s1_jumpTarget, brTarget),
149      snpc
150    )
151  )
152
153  val stage2CfiUpdate = io.stage2Redirect.bits.cfiUpdate
154  stage2CfiUpdate.pc := real_pc
155  stage2CfiUpdate.pd := s1_pd
156  // stage2CfiUpdate.predTaken := s1_redirect_bits_reg.cfiUpdate.predTaken
157  stage2CfiUpdate.target := target
158  // stage2CfiUpdate.taken := s1_redirect_bits_reg.cfiUpdate.taken
159  // stage2CfiUpdate.isMisPred := s1_redirect_bits_reg.cfiUpdate.isMisPred
160
161  val s2_target = RegEnable(target, s1_redirect_valid_reg)
162  val s2_pc = RegEnable(real_pc, s1_redirect_valid_reg)
163  val s2_redirect_bits_reg = RegEnable(s1_redirect_bits_reg, s1_redirect_valid_reg)
164  val s2_redirect_valid_reg = RegNext(s1_redirect_valid_reg && !io.flush, init = false.B)
165
166  io.stage3Redirect.valid := s2_redirect_valid_reg
167  io.stage3Redirect.bits := s2_redirect_bits_reg
168
169  // get pc from ftq
170  // valid only if redirect is caused by load violation
171  // store_pc is used to update store set
172  val store_pc = io.memPredPcRead(s1_redirect_bits_reg.stFtqIdx, s1_redirect_bits_reg.stFtqOffset)
173
174  val s1_real_pc_from_frontend = io.for_frontend_redirect_gen.s1_real_pc
175  // update load violation predictor if load violation redirect triggered
176  io.memPredUpdate.valid := RegNext(s1_isReplay && s1_redirect_valid_reg, init = false.B)
177  // update wait table
178  io.memPredUpdate.waddr := RegNext(XORFold(s1_real_pc_from_frontend(VAddrBits-1, 1), MemPredPCWidth))
179  io.memPredUpdate.wdata := true.B
180  // update store set
181  io.memPredUpdate.ldpc := RegNext(XORFold(s1_real_pc_from_frontend(VAddrBits-1, 1), MemPredPCWidth))
182  // store pc is ready 1 cycle after s1_isReplay is judged
183  io.memPredUpdate.stpc := XORFold(store_pc(VAddrBits-1, 1), MemPredPCWidth)
184
185  XSError(io.memPredUpdate.valid && RegNext(s1_real_pc_from_frontend) =/= RegNext(real_pc), "s1_real_pc error")
186
187  // // recover runahead checkpoint if redirect
188  // if (!env.FPGAPlatform) {
189  //   val runahead_redirect = Module(new DifftestRunaheadRedirectEvent)
190  //   runahead_redirect.io.clock := clock
191  //   runahead_redirect.io.coreid := io.hartId
192  //   runahead_redirect.io.valid := io.stage3Redirect.valid
193  //   runahead_redirect.io.pc :=  s2_pc // for debug only
194  //   runahead_redirect.io.target_pc := s2_target // for debug only
195  //   runahead_redirect.io.checkpoint_id := io.stage3Redirect.bits.debug_runahead_checkpoint_id // make sure it is right
196  // }
197}
198
199class CtrlBlock(implicit p: Parameters) extends LazyModule
200  with HasWritebackSink with HasWritebackSource {
201  val rob = LazyModule(new Rob)
202
203  override def addWritebackSink(source: Seq[HasWritebackSource], index: Option[Seq[Int]]): HasWritebackSink = {
204    rob.addWritebackSink(Seq(this), Some(Seq(writebackSinks.length)))
205    super.addWritebackSink(source, index)
206  }
207
208  lazy val module = new CtrlBlockImp(this)
209
210  override lazy val writebackSourceParams: Seq[WritebackSourceParams] = {
211    writebackSinksParams
212  }
213  override lazy val writebackSourceImp: HasWritebackSourceImp = module
214
215  override def generateWritebackIO(
216    thisMod: Option[HasWritebackSource] = None,
217    thisModImp: Option[HasWritebackSourceImp] = None
218  ): Unit = {
219    module.io.writeback.zip(writebackSinksImp(thisMod, thisModImp)).foreach(x => x._1 := x._2)
220  }
221}
222
223class CtrlBlockImp(outer: CtrlBlock)(implicit p: Parameters) extends LazyModuleImp(outer)
224  with HasXSParameter
225  with HasCircularQueuePtrHelper
226  with HasWritebackSourceImp
227  with HasPerfEvents
228{
229  val writebackLengths = outer.writebackSinksParams.map(_.length)
230
231  val io = IO(new Bundle {
232    val hartId = Input(UInt(8.W))
233    val cpu_halt = Output(Bool())
234    val frontend = Flipped(new FrontendToCtrlIO)
235    val allocPregs = Vec(RenameWidth, Output(new ResetPregStateReq))
236    val dispatch = Vec(3*dpParams.IntDqDeqWidth, DecoupledIO(new MicroOp))
237    // from int block
238    val exuRedirect = Vec(exuParameters.AluCnt + exuParameters.JmpCnt, Flipped(ValidIO(new ExuOutput)))
239    val stIn = Vec(exuParameters.StuCnt, Flipped(ValidIO(new ExuInput)))
240    val memoryViolation = Flipped(ValidIO(new Redirect))
241    val jumpPc = Output(UInt(VAddrBits.W))
242    val jalr_target = Output(UInt(VAddrBits.W))
243    val robio = new Bundle {
244      // to int block
245      val toCSR = new RobCSRIO
246      val exception = ValidIO(new ExceptionInfo)
247      // to mem block
248      val lsq = new RobLsqIO
249    }
250    val csrCtrl = Input(new CustomCSRCtrlIO)
251    val perfInfo = Output(new Bundle{
252      val ctrlInfo = new Bundle {
253        val robFull   = Input(Bool())
254        val intdqFull = Input(Bool())
255        val fpdqFull  = Input(Bool())
256        val lsdqFull  = Input(Bool())
257      }
258    })
259    val writeback = MixedVec(writebackLengths.map(num => Vec(num, Flipped(ValidIO(new ExuOutput)))))
260    // redirect out
261    val redirect = ValidIO(new Redirect)
262    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
263    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
264  })
265
266  override def writebackSource: Option[Seq[Seq[Valid[ExuOutput]]]] = {
267    Some(io.writeback.map(writeback => {
268      val exuOutput = WireInit(writeback)
269      val timer = GTimer()
270      for ((wb_next, wb) <- exuOutput.zip(writeback)) {
271        wb_next.valid := RegNext(wb.valid && !wb.bits.uop.robIdx.needFlush(Seq(stage2Redirect, redirectForExu)))
272        wb_next.bits := RegNext(wb.bits)
273        wb_next.bits.uop.debugInfo.writebackTime := timer
274      }
275      exuOutput
276    }))
277  }
278
279  val decode = Module(new DecodeStage)
280  val fusionDecoder = Module(new FusionDecoder)
281  val rat = Module(new RenameTableWrapper)
282  val ssit = Module(new SSIT)
283  val waittable = Module(new WaitTable)
284  val rename = Module(new Rename)
285  val dispatch = Module(new Dispatch)
286  val intDq = Module(new DispatchQueue(dpParams.IntDqSize, RenameWidth, dpParams.IntDqDeqWidth))
287  val fpDq = Module(new DispatchQueue(dpParams.FpDqSize, RenameWidth, dpParams.FpDqDeqWidth))
288  val lsDq = Module(new DispatchQueue(dpParams.LsDqSize, RenameWidth, dpParams.LsDqDeqWidth))
289  val redirectGen = Module(new RedirectGenerator)
290
291  val rob = outer.rob.module
292
293  val robPcRead = io.frontend.fromFtq.getRobFlushPcRead
294  val flushPC = robPcRead(rob.io.flushOut.bits.ftqIdx, rob.io.flushOut.bits.ftqOffset)
295
296  val flushRedirect = Wire(Valid(new Redirect))
297  flushRedirect.valid := RegNext(rob.io.flushOut.valid)
298  flushRedirect.bits := RegEnable(rob.io.flushOut.bits, rob.io.flushOut.valid)
299
300  val flushRedirectReg = Wire(Valid(new Redirect))
301  flushRedirectReg.valid := RegNext(flushRedirect.valid, init = false.B)
302  flushRedirectReg.bits := RegEnable(flushRedirect.bits, flushRedirect.valid)
303
304  val stage2Redirect = Mux(flushRedirect.valid, flushRedirect, redirectGen.io.stage2Redirect)
305  // Redirect will be RegNext at ExuBlocks.
306  val redirectForExu = RegNextWithEnable(stage2Redirect)
307
308  val exuRedirect = io.exuRedirect.map(x => {
309    val valid = x.valid && x.bits.redirectValid
310    val killedByOlder = x.bits.uop.robIdx.needFlush(Seq(stage2Redirect, redirectForExu))
311    val delayed = Wire(Valid(new ExuOutput))
312    delayed.valid := RegNext(valid && !killedByOlder, init = false.B)
313    delayed.bits := RegEnable(x.bits, x.valid)
314    delayed
315  })
316  val loadReplay = Wire(Valid(new Redirect))
317  loadReplay.valid := RegNext(io.memoryViolation.valid &&
318    !io.memoryViolation.bits.robIdx.needFlush(Seq(stage2Redirect, redirectForExu)),
319    init = false.B
320  )
321  loadReplay.bits := RegEnable(io.memoryViolation.bits, io.memoryViolation.valid)
322  io.frontend.fromFtq.getRedirectPcRead <> redirectGen.io.stage1PcRead
323  io.frontend.fromFtq.getMemPredPcRead <> redirectGen.io.memPredPcRead
324  redirectGen.io.hartId := io.hartId
325  redirectGen.io.exuMispredict <> exuRedirect
326  redirectGen.io.loadReplay <> loadReplay
327  redirectGen.io.flush := flushRedirect.valid
328
329  val frontendFlushValid = DelayN(flushRedirect.valid, 5)
330  val frontendFlushBits = RegEnable(flushRedirect.bits, flushRedirect.valid)
331  // When ROB commits an instruction with a flush, we notify the frontend of the flush without the commit.
332  // Flushes to frontend may be delayed by some cycles and commit before flush causes errors.
333  // Thus, we make all flush reasons to behave the same as exceptions for frontend.
334  for (i <- 0 until CommitWidth) {
335    val is_commit = rob.io.commits.valid(i) && !rob.io.commits.isWalk && !rob.io.flushOut.valid
336    io.frontend.toFtq.rob_commits(i).valid := RegNext(is_commit)
337    io.frontend.toFtq.rob_commits(i).bits := RegEnable(rob.io.commits.info(i), is_commit)
338  }
339  io.frontend.toFtq.redirect.valid := frontendFlushValid || redirectGen.io.stage2Redirect.valid
340  io.frontend.toFtq.redirect.bits := Mux(frontendFlushValid, frontendFlushBits, redirectGen.io.stage2Redirect.bits)
341  // Be careful here:
342  // T0: flushRedirect.valid, exception.valid
343  // T1: csr.redirect.valid
344  // T2: csr.exception.valid
345  // T3: csr.trapTarget
346  // T4: ctrlBlock.trapTarget
347  // T5: io.frontend.toFtq.stage2Redirect.valid
348  val pc_from_csr = io.robio.toCSR.isXRet || DelayN(rob.io.exception.valid, 4)
349  val rob_flush_pc = RegEnable(Mux(flushRedirect.bits.flushItself(),
350    flushPC, // replay inst
351    flushPC + 4.U // flush pipe
352  ), flushRedirect.valid)
353  val flushTarget = Mux(pc_from_csr, io.robio.toCSR.trapTarget, rob_flush_pc)
354  when (frontendFlushValid) {
355    io.frontend.toFtq.redirect.bits.level := RedirectLevel.flush
356    io.frontend.toFtq.redirect.bits.cfiUpdate.target := RegNext(flushTarget)
357  }
358  redirectGen.io.for_frontend_redirect_gen.s1_real_pc := io.frontend.fromFtq.redirect_s1_real_pc
359  io.frontend.toFtq.for_redirect_gen.s1_oldest_redirect := redirectGen.io.for_frontend_redirect_gen.s1_oldest_redirect
360  io.frontend.toFtq.for_redirect_gen.s1_oldest_exu_output := redirectGen.io.for_frontend_redirect_gen.s1_oldest_exu_output
361  io.frontend.toFtq.for_redirect_gen.s1_redirect_onehot := redirectGen.io.for_frontend_redirect_gen.s1_redirect_onehot
362  io.frontend.toFtq.for_redirect_gen.s1_jumpTarget := redirectGen.io.for_frontend_redirect_gen.s1_jumpTarget
363  io.frontend.toFtq.for_redirect_gen.rawRedirect := redirectGen.io.stage2Redirect
364  io.frontend.toFtq.for_redirect_gen.flushRedirect.valid := frontendFlushValid
365  io.frontend.toFtq.for_redirect_gen.flushRedirect.bits := frontendFlushBits
366
367  io.frontend.toFtq.for_redirect_gen.frontendFlushTarget := RegNext(flushTarget)
368
369
370  val pendingRedirect = RegInit(false.B)
371  when (stage2Redirect.valid) {
372    pendingRedirect := true.B
373  }.elsewhen (RegNext(io.frontend.toFtq.redirect.valid)) {
374    pendingRedirect := false.B
375  }
376
377  decode.io.in <> io.frontend.cfVec
378  decode.io.csrCtrl := RegNext(io.csrCtrl)
379  decode.io.intRat <> rat.io.intReadPorts
380  decode.io.fpRat <> rat.io.fpReadPorts
381
382  // memory dependency predict
383  // when decode, send fold pc to mdp
384  for (i <- 0 until DecodeWidth) {
385    val mdp_foldpc = Mux(
386      decode.io.out(i).fire,
387      decode.io.in(i).bits.foldpc,
388      rename.io.in(i).bits.cf.foldpc
389    )
390    ssit.io.raddr(i) := mdp_foldpc
391    waittable.io.raddr(i) := mdp_foldpc
392  }
393  // currently, we only update mdp info when isReplay
394  ssit.io.update <> RegNext(redirectGen.io.memPredUpdate)
395  ssit.io.csrCtrl := RegNext(io.csrCtrl)
396  waittable.io.update <> RegNext(redirectGen.io.memPredUpdate)
397  waittable.io.csrCtrl := RegNext(io.csrCtrl)
398
399  // LFST lookup and update
400  val lfst = Module(new LFST)
401  lfst.io.redirect <> RegNext(io.redirect)
402  lfst.io.storeIssue <> RegNext(io.stIn)
403  lfst.io.csrCtrl <> RegNext(io.csrCtrl)
404  lfst.io.dispatch <> dispatch.io.lfst
405
406  rat.io.robCommits := rob.io.commits
407  rat.io.intRenamePorts := rename.io.intRenamePorts
408  rat.io.fpRenamePorts := rename.io.fpRenamePorts
409  rat.io.debug_int_rat <> io.debug_int_rat
410  rat.io.debug_fp_rat <> io.debug_fp_rat
411
412  // pipeline between decode and rename
413  for (i <- 0 until RenameWidth) {
414    // fusion decoder
415    val decodeHasException = io.frontend.cfVec(i).bits.exceptionVec(instrPageFault) || io.frontend.cfVec(i).bits.exceptionVec(instrAccessFault)
416    val disableFusion = decode.io.csrCtrl.singlestep
417    fusionDecoder.io.in(i).valid := io.frontend.cfVec(i).valid && !(decodeHasException || disableFusion)
418    fusionDecoder.io.in(i).bits := io.frontend.cfVec(i).bits.instr
419    if (i > 0) {
420      fusionDecoder.io.inReady(i - 1) := decode.io.out(i).ready
421    }
422
423    // Pipeline
424    val renamePipe = PipelineNext(decode.io.out(i), rename.io.in(i).ready,
425      stage2Redirect.valid || pendingRedirect)
426    renamePipe.ready := rename.io.in(i).ready
427    rename.io.in(i).valid := renamePipe.valid && !fusionDecoder.io.clear(i)
428    rename.io.in(i).bits := renamePipe.bits
429    rename.io.intReadPorts(i) := rat.io.intReadPorts(i).map(_.data)
430    rename.io.fpReadPorts(i) := rat.io.fpReadPorts(i).map(_.data)
431    rename.io.waittable(i) := RegEnable(waittable.io.rdata(i), decode.io.out(i).fire)
432
433    if (i < RenameWidth - 1) {
434      // fusion decoder sees the raw decode info
435      fusionDecoder.io.dec(i) := renamePipe.bits.ctrl
436      rename.io.fusionInfo(i) := fusionDecoder.io.info(i)
437
438      // update the first RenameWidth - 1 instructions
439      decode.io.fusion(i) := fusionDecoder.io.out(i).valid && rename.io.out(i).fire
440      when (fusionDecoder.io.out(i).valid) {
441        fusionDecoder.io.out(i).bits.update(rename.io.in(i).bits.ctrl)
442        // TODO: remove this dirty code for ftq update
443        val sameFtqPtr = rename.io.in(i).bits.cf.ftqPtr.value === rename.io.in(i + 1).bits.cf.ftqPtr.value
444        val ftqOffset0 = rename.io.in(i).bits.cf.ftqOffset
445        val ftqOffset1 = rename.io.in(i + 1).bits.cf.ftqOffset
446        val ftqOffsetDiff = ftqOffset1 - ftqOffset0
447        val cond1 = sameFtqPtr && ftqOffsetDiff === 1.U
448        val cond2 = sameFtqPtr && ftqOffsetDiff === 2.U
449        val cond3 = !sameFtqPtr && ftqOffset1 === 0.U
450        val cond4 = !sameFtqPtr && ftqOffset1 === 1.U
451        rename.io.in(i).bits.ctrl.commitType := Mux(cond1, 4.U, Mux(cond2, 5.U, Mux(cond3, 6.U, 7.U)))
452        XSError(!cond1 && !cond2 && !cond3 && !cond4, p"new condition $sameFtqPtr $ftqOffset0 $ftqOffset1\n")
453      }
454    }
455  }
456
457  rename.io.redirect <> stage2Redirect
458  rename.io.robCommits <> rob.io.commits
459  rename.io.ssit <> ssit.io.rdata
460
461  // pipeline between rename and dispatch
462  for (i <- 0 until RenameWidth) {
463    PipelineConnect(rename.io.out(i), dispatch.io.fromRename(i), dispatch.io.recv(i), stage2Redirect.valid)
464  }
465
466  dispatch.io.hartId := io.hartId
467  dispatch.io.redirect <> stage2Redirect
468  dispatch.io.enqRob <> rob.io.enq
469  dispatch.io.toIntDq <> intDq.io.enq
470  dispatch.io.toFpDq <> fpDq.io.enq
471  dispatch.io.toLsDq <> lsDq.io.enq
472  dispatch.io.allocPregs <> io.allocPregs
473  dispatch.io.singleStep := RegNext(io.csrCtrl.singlestep)
474
475  intDq.io.redirect <> redirectForExu
476  fpDq.io.redirect <> redirectForExu
477  lsDq.io.redirect <> redirectForExu
478
479  io.dispatch <> intDq.io.deq ++ lsDq.io.deq ++ fpDq.io.deq
480
481  val pingpong = RegInit(false.B)
482  pingpong := !pingpong
483  val jumpInst = Mux(pingpong && (exuParameters.AluCnt > 2).B, io.dispatch(2).bits, io.dispatch(0).bits)
484  val jumpPcRead = io.frontend.fromFtq.getJumpPcRead
485  io.jumpPc := jumpPcRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
486  val jumpTargetRead = io.frontend.fromFtq.target_read
487  io.jalr_target := jumpTargetRead(jumpInst.cf.ftqPtr, jumpInst.cf.ftqOffset)
488
489  rob.io.hartId := io.hartId
490  io.cpu_halt := DelayN(rob.io.cpu_halt, 5)
491  rob.io.redirect <> stage2Redirect
492  outer.rob.generateWritebackIO(Some(outer), Some(this))
493
494  io.redirect <> stage2Redirect
495
496  // rob to int block
497  io.robio.toCSR <> rob.io.csr
498  io.robio.toCSR.perfinfo.retiredInstr <> RegNext(rob.io.csr.perfinfo.retiredInstr)
499  io.robio.exception := rob.io.exception
500  io.robio.exception.bits.uop.cf.pc := flushPC
501
502  // rob to mem block
503  io.robio.lsq <> rob.io.lsq
504
505  io.perfInfo.ctrlInfo.robFull := RegNext(rob.io.robFull)
506  io.perfInfo.ctrlInfo.intdqFull := RegNext(intDq.io.dqFull)
507  io.perfInfo.ctrlInfo.fpdqFull := RegNext(fpDq.io.dqFull)
508  io.perfInfo.ctrlInfo.lsdqFull := RegNext(lsDq.io.dqFull)
509
510  val pfevent = Module(new PFEvent)
511  pfevent.io.distribute_csr := RegNext(io.csrCtrl.distribute_csr)
512  val csrevents = pfevent.io.hpmevent.slice(8,16)
513
514  val perfinfo = IO(new Bundle(){
515    val perfEventsRs      = Input(Vec(NumRs, new PerfEvent))
516    val perfEventsEu0     = Input(Vec(6, new PerfEvent))
517    val perfEventsEu1     = Input(Vec(6, new PerfEvent))
518  })
519
520  val allPerfEvents = Seq(decode, rename, dispatch, intDq, fpDq, lsDq, rob).flatMap(_.getPerf)
521  val hpmEvents = allPerfEvents ++ perfinfo.perfEventsEu0 ++ perfinfo.perfEventsEu1 ++ perfinfo.perfEventsRs
522  val perfEvents = HPerfMonitor(csrevents, hpmEvents).getPerfEvents
523  generatePerfEvent()
524}
525