xref: /XiangShan/src/main/scala/xiangshan/backend/rob/Rob.scala (revision 67ba96b4871c459c09df20e3052738174021a830)
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.rob
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import difftest._
23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
24import utils._
25import utility._
26import xiangshan._
27import xiangshan.backend.exu.ExuConfig
28import xiangshan.frontend.FtqPtr
29
30class RobPtr(implicit p: Parameters) extends CircularQueuePtr[RobPtr](
31  p => p(XSCoreParamsKey).RobSize
32) with HasCircularQueuePtrHelper {
33
34  def needFlush(redirect: Valid[Redirect]): Bool = {
35    val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx
36    redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx))
37  }
38
39  def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR
40}
41
42object RobPtr {
43  def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = {
44    val ptr = Wire(new RobPtr)
45    ptr.flag := f
46    ptr.value := v
47    ptr
48  }
49}
50
51class RobCSRIO(implicit p: Parameters) extends XSBundle {
52  val intrBitSet = Input(Bool())
53  val trapTarget = Input(UInt(VAddrBits.W))
54  val isXRet     = Input(Bool())
55  val wfiEvent   = Input(Bool())
56
57  val fflags     = Output(Valid(UInt(5.W)))
58  val dirty_fs   = Output(Bool())
59  val perfinfo   = new Bundle {
60    val retiredInstr = Output(UInt(3.W))
61  }
62}
63
64class RobLsqIO(implicit p: Parameters) extends XSBundle {
65  val lcommit = Output(UInt(log2Up(CommitWidth + 1).W))
66  val scommit = Output(UInt(log2Up(CommitWidth + 1).W))
67  val pendingld = Output(Bool())
68  val pendingst = Output(Bool())
69  val commit = Output(Bool())
70}
71
72class RobEnqIO(implicit p: Parameters) extends XSBundle {
73  val canAccept = Output(Bool())
74  val isEmpty = Output(Bool())
75  // valid vector, for robIdx gen and walk
76  val needAlloc = Vec(RenameWidth, Input(Bool()))
77  val req = Vec(RenameWidth, Flipped(ValidIO(new MicroOp)))
78  val resp = Vec(RenameWidth, Output(new RobPtr))
79}
80
81class RobDispatchData(implicit p: Parameters) extends RobCommitInfo
82
83class RobDeqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
84  val io = IO(new Bundle {
85    // for commits/flush
86    val state = Input(UInt(2.W))
87    val deq_v = Vec(CommitWidth, Input(Bool()))
88    val deq_w = Vec(CommitWidth, Input(Bool()))
89    val exception_state = Flipped(ValidIO(new RobExceptionInfo))
90    // for flush: when exception occurs, reset deqPtrs to range(0, CommitWidth)
91    val intrBitSetReg = Input(Bool())
92    val hasNoSpecExec = Input(Bool())
93    val interrupt_safe = Input(Bool())
94    val blockCommit = Input(Bool())
95    // output: the CommitWidth deqPtr
96    val out = Vec(CommitWidth, Output(new RobPtr))
97    val next_out = Vec(CommitWidth, Output(new RobPtr))
98  })
99
100  val deqPtrVec = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new RobPtr))))
101
102  // for exceptions (flushPipe included) and interrupts:
103  // only consider the first instruction
104  val intrEnable = io.intrBitSetReg && !io.hasNoSpecExec && io.interrupt_safe
105  val exceptionEnable = io.deq_w(0) && io.exception_state.valid && io.exception_state.bits.not_commit && io.exception_state.bits.robIdx === deqPtrVec(0)
106  val redirectOutValid = io.state === 0.U && io.deq_v(0) && (intrEnable || exceptionEnable)
107
108  // for normal commits: only to consider when there're no exceptions
109  // we don't need to consider whether the first instruction has exceptions since it wil trigger exceptions.
110  val commit_exception = io.exception_state.valid && !isAfter(io.exception_state.bits.robIdx, deqPtrVec.last)
111  val canCommit = VecInit((0 until CommitWidth).map(i => io.deq_v(i) && io.deq_w(i)))
112  val normalCommitCnt = PriorityEncoder(canCommit.map(c => !c) :+ true.B)
113  // when io.intrBitSetReg or there're possible exceptions in these instructions,
114  // only one instruction is allowed to commit
115  val allowOnlyOne = commit_exception || io.intrBitSetReg
116  val commitCnt = Mux(allowOnlyOne, canCommit(0), normalCommitCnt)
117
118  val commitDeqPtrVec = VecInit(deqPtrVec.map(_ + commitCnt))
119  val deqPtrVec_next = Mux(io.state === 0.U && !redirectOutValid && !io.blockCommit, commitDeqPtrVec, deqPtrVec)
120
121  deqPtrVec := deqPtrVec_next
122
123  io.next_out := deqPtrVec_next
124  io.out      := deqPtrVec
125
126  when (io.state === 0.U) {
127    XSInfo(io.state === 0.U && commitCnt > 0.U, "retired %d insts\n", commitCnt)
128  }
129
130}
131
132class RobEnqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
133  val io = IO(new Bundle {
134    // for input redirect
135    val redirect = Input(Valid(new Redirect))
136    // for enqueue
137    val allowEnqueue = Input(Bool())
138    val hasBlockBackward = Input(Bool())
139    val enq = Vec(RenameWidth, Input(Bool()))
140    val out = Output(Vec(RenameWidth, new RobPtr))
141  })
142
143  val enqPtrVec = RegInit(VecInit.tabulate(RenameWidth)(_.U.asTypeOf(new RobPtr)))
144
145  // enqueue
146  val canAccept = io.allowEnqueue && !io.hasBlockBackward
147  val dispatchNum = Mux(canAccept, PopCount(io.enq), 0.U)
148
149  for ((ptr, i) <- enqPtrVec.zipWithIndex) {
150    when(io.redirect.valid) {
151      ptr := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U)
152    }.otherwise {
153      ptr := ptr + dispatchNum
154    }
155  }
156
157  io.out := enqPtrVec
158
159}
160
161class RobExceptionInfo(implicit p: Parameters) extends XSBundle {
162  // val valid = Bool()
163  val robIdx = new RobPtr
164  val exceptionVec = ExceptionVec()
165  val flushPipe = Bool()
166  val replayInst = Bool() // redirect to that inst itself
167  val singleStep = Bool() // TODO add frontend hit beneath
168  val crossPageIPFFix = Bool()
169  val trigger = new TriggerCf
170
171//  def trigger_before = !trigger.getTimingBackend && trigger.getHitBackend
172//  def trigger_after = trigger.getTimingBackend && trigger.getHitBackend
173  def has_exception = exceptionVec.asUInt.orR || flushPipe || singleStep || replayInst || trigger.hit
174  def not_commit = exceptionVec.asUInt.orR || singleStep || replayInst || trigger.hit
175  // only exceptions are allowed to writeback when enqueue
176  def can_writeback = exceptionVec.asUInt.orR || singleStep || trigger.hit
177}
178
179class ExceptionGen(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper {
180  val io = IO(new Bundle {
181    val redirect = Input(Valid(new Redirect))
182    val flush = Input(Bool())
183    val enq = Vec(RenameWidth, Flipped(ValidIO(new RobExceptionInfo)))
184    val wb = Vec(1 + LoadPipelineWidth + StorePipelineWidth, Flipped(ValidIO(new RobExceptionInfo)))
185    val out = ValidIO(new RobExceptionInfo)
186    val state = ValidIO(new RobExceptionInfo)
187  })
188
189  def getOldest(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): (Seq[Bool], Seq[RobExceptionInfo]) = {
190    assert(valid.length == bits.length)
191    assert(isPow2(valid.length))
192    if (valid.length == 1) {
193      (valid, bits)
194    } else if (valid.length == 2) {
195      val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0)))))
196      for (i <- res.indices) {
197        res(i).valid := valid(i)
198        res(i).bits := bits(i)
199      }
200      val oldest = Mux(!valid(1) || valid(0) && isAfter(bits(1).robIdx, bits(0).robIdx), res(0), res(1))
201      (Seq(oldest.valid), Seq(oldest.bits))
202    } else {
203      val left = getOldest(valid.take(valid.length / 2), bits.take(valid.length / 2))
204      val right = getOldest(valid.takeRight(valid.length / 2), bits.takeRight(valid.length / 2))
205      getOldest(left._1 ++ right._1, left._2 ++ right._2)
206    }
207  }
208
209  val currentValid = RegInit(false.B)
210  val current = Reg(new RobExceptionInfo)
211
212  // orR the exceptionVec
213  val lastCycleFlush = RegNext(io.flush)
214  val in_enq_valid = VecInit(io.enq.map(e => e.valid && e.bits.has_exception && !lastCycleFlush))
215  val in_wb_valid = io.wb.map(w => w.valid && w.bits.has_exception && !lastCycleFlush)
216
217  // s0: compare wb(1)~wb(LoadPipelineWidth) and wb(1 + LoadPipelineWidth)~wb(LoadPipelineWidth + StorePipelineWidth)
218  val wb_valid = in_wb_valid.zip(io.wb.map(_.bits)).map{ case (v, bits) => v && !(bits.robIdx.needFlush(io.redirect) || io.flush) }
219  val csr_wb_bits = io.wb(0).bits
220  val load_wb_bits = getOldest(in_wb_valid.slice(1, 1 + LoadPipelineWidth), io.wb.map(_.bits).slice(1, 1 + LoadPipelineWidth))._2(0)
221  val store_wb_bits = getOldest(in_wb_valid.slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth), io.wb.map(_.bits).slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth))._2(0)
222  val s0_out_valid = RegNext(VecInit(Seq(wb_valid(0), wb_valid.slice(1, 1 + LoadPipelineWidth).reduce(_ || _), wb_valid.slice(1 + LoadPipelineWidth, 1 + LoadPipelineWidth + StorePipelineWidth).reduce(_ || _))))
223  val s0_out_bits = RegNext(VecInit(Seq(csr_wb_bits, load_wb_bits, store_wb_bits)))
224
225  // s1: compare last four and current flush
226  val s1_valid = VecInit(s0_out_valid.zip(s0_out_bits).map{ case (v, b) => v && !(b.robIdx.needFlush(io.redirect) || io.flush) })
227  val compare_01_valid = s0_out_valid(0) || s0_out_valid(1)
228  val compare_01_bits = Mux(!s0_out_valid(0) || s0_out_valid(1) && isAfter(s0_out_bits(0).robIdx, s0_out_bits(1).robIdx), s0_out_bits(1), s0_out_bits(0))
229  val compare_bits = Mux(!s0_out_valid(2) || compare_01_valid && isAfter(s0_out_bits(2).robIdx, compare_01_bits.robIdx), compare_01_bits, s0_out_bits(2))
230  val s1_out_bits = RegNext(compare_bits)
231  val s1_out_valid = RegNext(s1_valid.asUInt.orR)
232
233  val enq_valid = RegNext(in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush)
234  val enq_bits = RegNext(ParallelPriorityMux(in_enq_valid, io.enq.map(_.bits)))
235
236  // s2: compare the input exception with the current one
237  // priorities:
238  // (1) system reset
239  // (2) current is valid: flush, remain, merge, update
240  // (3) current is not valid: s1 or enq
241  val current_flush = current.robIdx.needFlush(io.redirect) || io.flush
242  val s1_flush = s1_out_bits.robIdx.needFlush(io.redirect) || io.flush
243  when (currentValid) {
244    when (current_flush) {
245      currentValid := Mux(s1_flush, false.B, s1_out_valid)
246    }
247    when (s1_out_valid && !s1_flush) {
248      when (isAfter(current.robIdx, s1_out_bits.robIdx)) {
249        current := s1_out_bits
250      }.elsewhen (current.robIdx === s1_out_bits.robIdx) {
251        current.exceptionVec := (s1_out_bits.exceptionVec.asUInt | current.exceptionVec.asUInt).asTypeOf(ExceptionVec())
252        current.flushPipe := s1_out_bits.flushPipe || current.flushPipe
253        current.replayInst := s1_out_bits.replayInst || current.replayInst
254        current.singleStep := s1_out_bits.singleStep || current.singleStep
255        current.trigger := (s1_out_bits.trigger.asUInt | current.trigger.asUInt).asTypeOf(new TriggerCf)
256      }
257    }
258  }.elsewhen (s1_out_valid && !s1_flush) {
259    currentValid := true.B
260    current := s1_out_bits
261  }.elsewhen (enq_valid && !(io.redirect.valid || io.flush)) {
262    currentValid := true.B
263    current := enq_bits
264  }
265
266  io.out.valid   := s1_out_valid || enq_valid && enq_bits.can_writeback
267  io.out.bits    := Mux(s1_out_valid, s1_out_bits, enq_bits)
268  io.state.valid := currentValid
269  io.state.bits  := current
270
271}
272
273class RobFlushInfo(implicit p: Parameters) extends XSBundle {
274  val ftqIdx = new FtqPtr
275  val robIdx = new RobPtr
276  val ftqOffset = UInt(log2Up(PredictWidth).W)
277  val replayInst = Bool()
278}
279
280class Rob(implicit p: Parameters) extends LazyModule with HasWritebackSink with HasXSParameter {
281
282  lazy val module = new RobImp(this)
283
284  override def generateWritebackIO(
285    thisMod: Option[HasWritebackSource] = None,
286    thisModImp: Option[HasWritebackSourceImp] = None
287  ): Unit = {
288    val sources = writebackSinksImp(thisMod, thisModImp)
289    module.io.writeback.zip(sources).foreach(x => x._1 := x._2)
290  }
291}
292
293class RobImp(outer: Rob)(implicit p: Parameters) extends LazyModuleImp(outer)
294  with HasXSParameter with HasCircularQueuePtrHelper with HasPerfEvents {
295  val wbExuConfigs = outer.writebackSinksParams.map(_.exuConfigs)
296  val numWbPorts = wbExuConfigs.map(_.length)
297
298  val io = IO(new Bundle() {
299    val hartId = Input(UInt(8.W))
300    val redirect = Input(Valid(new Redirect))
301    val enq = new RobEnqIO
302    val flushOut = ValidIO(new Redirect)
303    val exception = ValidIO(new ExceptionInfo)
304    // exu + brq
305    val writeback = MixedVec(numWbPorts.map(num => Vec(num, Flipped(ValidIO(new ExuOutput)))))
306    val commits = Output(new RobCommitIO)
307    val lsq = new RobLsqIO
308    val robDeqPtr = Output(new RobPtr)
309    val csr = new RobCSRIO
310    val robFull = Output(Bool())
311    val cpu_halt = Output(Bool())
312    val wfi_enable = Input(Bool())
313  })
314
315  def selectWb(index: Int, func: Seq[ExuConfig] => Boolean): Seq[(Seq[ExuConfig], ValidIO[ExuOutput])] = {
316    wbExuConfigs(index).zip(io.writeback(index)).filter(x => func(x._1))
317  }
318  val exeWbSel = outer.selWritebackSinks(_.exuConfigs.length)
319  val fflagsWbSel = outer.selWritebackSinks(_.exuConfigs.count(_.exists(_.writeFflags)))
320  val fflagsPorts = selectWb(fflagsWbSel, _.exists(_.writeFflags))
321  val exceptionWbSel = outer.selWritebackSinks(_.exuConfigs.count(_.exists(_.needExceptionGen)))
322  val exceptionPorts = selectWb(fflagsWbSel, _.exists(_.needExceptionGen))
323  val exuWbPorts = selectWb(exeWbSel, _.forall(_ != StdExeUnitCfg))
324  val stdWbPorts = selectWb(exeWbSel, _.contains(StdExeUnitCfg))
325  println(s"Rob: size $RobSize, numWbPorts: $numWbPorts, commitwidth: $CommitWidth")
326  println(s"exuPorts: ${exuWbPorts.map(_._1.map(_.name))}")
327  println(s"stdPorts: ${stdWbPorts.map(_._1.map(_.name))}")
328  println(s"fflags: ${fflagsPorts.map(_._1.map(_.name))}")
329
330
331  val exuWriteback = exuWbPorts.map(_._2)
332  val stdWriteback = stdWbPorts.map(_._2)
333
334  // instvalid field
335  val valid = RegInit(VecInit(Seq.fill(RobSize)(false.B)))
336  // writeback status
337  val writebacked = Mem(RobSize, Bool())
338  val store_data_writebacked = Mem(RobSize, Bool())
339  // data for redirect, exception, etc.
340  val flagBkup = Mem(RobSize, Bool())
341  // some instructions are not allowed to trigger interrupts
342  // They have side effects on the states of the processor before they write back
343  val interrupt_safe = Mem(RobSize, Bool())
344
345  // data for debug
346  // Warn: debug_* prefix should not exist in generated verilog.
347  val debug_microOp = Mem(RobSize, new MicroOp)
348  val debug_exuData = Reg(Vec(RobSize, UInt(XLEN.W)))//for debug
349  val debug_exuDebug = Reg(Vec(RobSize, new DebugBundle))//for debug
350
351  // pointers
352  // For enqueue ptr, we don't duplicate it since only enqueue needs it.
353  val enqPtrVec = Wire(Vec(RenameWidth, new RobPtr))
354  val deqPtrVec = Wire(Vec(CommitWidth, new RobPtr))
355
356  val walkPtrVec = Reg(Vec(CommitWidth, new RobPtr))
357  val allowEnqueue = RegInit(true.B)
358
359  val enqPtr = enqPtrVec.head
360  val deqPtr = deqPtrVec(0)
361  val walkPtr = walkPtrVec(0)
362
363  val isEmpty = enqPtr === deqPtr
364  val isReplaying = io.redirect.valid && RedirectLevel.flushItself(io.redirect.bits.level)
365
366  /**
367    * states of Rob
368    */
369  val s_idle :: s_walk :: Nil = Enum(2)
370  val state = RegInit(s_idle)
371
372  /**
373    * Data Modules
374    *
375    * CommitDataModule: data from dispatch
376    * (1) read: commits/walk/exception
377    * (2) write: enqueue
378    *
379    * WritebackData: data from writeback
380    * (1) read: commits/walk/exception
381    * (2) write: write back from exe units
382    */
383  val dispatchData = Module(new SyncDataModuleTemplate(new RobDispatchData, RobSize, CommitWidth, RenameWidth))
384  val dispatchDataRead = dispatchData.io.rdata
385
386  val exceptionGen = Module(new ExceptionGen)
387  val exceptionDataRead = exceptionGen.io.state
388  val fflagsDataRead = Wire(Vec(CommitWidth, UInt(5.W)))
389
390  io.robDeqPtr := deqPtr
391
392  /**
393    * Enqueue (from dispatch)
394    */
395  // special cases
396  val hasBlockBackward = RegInit(false.B)
397  val hasNoSpecExec = RegInit(false.B)
398  val doingSvinval = RegInit(false.B)
399  // When blockBackward instruction leaves Rob (commit or walk), hasBlockBackward should be set to false.B
400  // To reduce registers usage, for hasBlockBackward cases, we allow enqueue after ROB is empty.
401  when (isEmpty) { hasBlockBackward:= false.B }
402  // When any instruction commits, hasNoSpecExec should be set to false.B
403  when (io.commits.hasWalkInstr || io.commits.hasCommitInstr) { hasNoSpecExec:= false.B }
404
405  // The wait-for-interrupt (WFI) instruction waits in the ROB until an interrupt might need servicing.
406  // io.csr.wfiEvent will be asserted if the WFI can resume execution, and we change the state to s_wfi_idle.
407  // It does not affect how interrupts are serviced. Note that WFI is noSpecExec and it does not trigger interrupts.
408  val hasWFI = RegInit(false.B)
409  io.cpu_halt := hasWFI
410  // WFI Timeout: 2^20 = 1M cycles
411  val wfi_cycles = RegInit(0.U(20.W))
412  when (hasWFI) {
413    wfi_cycles := wfi_cycles + 1.U
414  }.elsewhen (!hasWFI && RegNext(hasWFI)) {
415    wfi_cycles := 0.U
416  }
417  val wfi_timeout = wfi_cycles.andR
418  when (RegNext(RegNext(io.csr.wfiEvent)) || io.flushOut.valid || wfi_timeout) {
419    hasWFI := false.B
420  }
421
422  val allocatePtrVec = VecInit((0 until RenameWidth).map(i => enqPtrVec(PopCount(io.enq.needAlloc.take(i)))))
423  io.enq.canAccept := allowEnqueue && !hasBlockBackward
424  io.enq.resp      := allocatePtrVec
425  val canEnqueue = VecInit(io.enq.req.map(_.valid && io.enq.canAccept))
426  val timer = GTimer()
427  for (i <- 0 until RenameWidth) {
428    // we don't check whether io.redirect is valid here since redirect has higher priority
429    when (canEnqueue(i)) {
430      val enqUop = io.enq.req(i).bits
431      val enqIndex = allocatePtrVec(i).value
432      // store uop in data module and debug_microOp Vec
433      debug_microOp(enqIndex) := enqUop
434      debug_microOp(enqIndex).debugInfo.dispatchTime := timer
435      debug_microOp(enqIndex).debugInfo.enqRsTime := timer
436      debug_microOp(enqIndex).debugInfo.selectTime := timer
437      debug_microOp(enqIndex).debugInfo.issueTime := timer
438      debug_microOp(enqIndex).debugInfo.writebackTime := timer
439      when (enqUop.ctrl.blockBackward) {
440        hasBlockBackward := true.B
441      }
442      when (enqUop.ctrl.noSpecExec) {
443        hasNoSpecExec := true.B
444      }
445      val enqHasTriggerHit = io.enq.req(i).bits.cf.trigger.getHitFrontend
446      val enqHasException = ExceptionNO.selectFrontend(enqUop.cf.exceptionVec).asUInt.orR
447      // the begin instruction of Svinval enqs so mark doingSvinval as true to indicate this process
448      when(!enqHasTriggerHit && !enqHasException && FuType.isSvinvalBegin(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe))
449      {
450        doingSvinval := true.B
451      }
452      // the end instruction of Svinval enqs so clear doingSvinval
453      when(!enqHasTriggerHit && !enqHasException && FuType.isSvinvalEnd(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe))
454      {
455        doingSvinval := false.B
456      }
457      // when we are in the process of Svinval software code area , only Svinval.vma and end instruction of Svinval can appear
458      assert(!doingSvinval || (FuType.isSvinval(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe) ||
459        FuType.isSvinvalEnd(enqUop.ctrl.fuType, enqUop.ctrl.fuOpType, enqUop.ctrl.flushPipe)))
460      when (enqUop.ctrl.isWFI && !enqHasException && !enqHasTriggerHit) {
461        hasWFI := true.B
462      }
463    }
464  }
465  val dispatchNum = Mux(io.enq.canAccept, PopCount(io.enq.req.map(_.valid)), 0.U)
466  io.enq.isEmpty   := RegNext(isEmpty && !VecInit(io.enq.req.map(_.valid)).asUInt.orR)
467
468  when (!io.wfi_enable) {
469    hasWFI := false.B
470  }
471
472  /**
473    * Writeback (from execution units)
474    */
475  for (wb <- exuWriteback) {
476    when (wb.valid) {
477      val wbIdx = wb.bits.uop.robIdx.value
478      debug_exuData(wbIdx) := wb.bits.data
479      debug_exuDebug(wbIdx) := wb.bits.debug
480      debug_microOp(wbIdx).debugInfo.enqRsTime := wb.bits.uop.debugInfo.enqRsTime
481      debug_microOp(wbIdx).debugInfo.selectTime := wb.bits.uop.debugInfo.selectTime
482      debug_microOp(wbIdx).debugInfo.issueTime := wb.bits.uop.debugInfo.issueTime
483      debug_microOp(wbIdx).debugInfo.writebackTime := wb.bits.uop.debugInfo.writebackTime
484
485      // debug for lqidx and sqidx
486      debug_microOp(wbIdx).lqIdx := wb.bits.uop.lqIdx
487      debug_microOp(wbIdx).sqIdx := wb.bits.uop.sqIdx
488
489      val debug_Uop = debug_microOp(wbIdx)
490      XSInfo(true.B,
491        p"writebacked pc 0x${Hexadecimal(debug_Uop.cf.pc)} wen ${debug_Uop.ctrl.rfWen} " +
492        p"data 0x${Hexadecimal(wb.bits.data)} ldst ${debug_Uop.ctrl.ldest} pdst ${debug_Uop.pdest} " +
493        p"skip ${wb.bits.debug.isMMIO} robIdx: ${wb.bits.uop.robIdx}\n"
494      )
495    }
496  }
497  val writebackNum = PopCount(exuWriteback.map(_.valid))
498  XSInfo(writebackNum =/= 0.U, "writebacked %d insts\n", writebackNum)
499
500
501  /**
502    * RedirectOut: Interrupt and Exceptions
503    */
504  val deqDispatchData = dispatchDataRead(0)
505  val debug_deqUop = debug_microOp(deqPtr.value)
506
507  val intrBitSetReg = RegNext(io.csr.intrBitSet)
508  val intrEnable = intrBitSetReg && !hasNoSpecExec && interrupt_safe(deqPtr.value)
509  val deqHasExceptionOrFlush = exceptionDataRead.valid && exceptionDataRead.bits.robIdx === deqPtr
510  val deqHasException = deqHasExceptionOrFlush && (exceptionDataRead.bits.exceptionVec.asUInt.orR ||
511    exceptionDataRead.bits.singleStep || exceptionDataRead.bits.trigger.hit)
512  val deqHasFlushPipe = deqHasExceptionOrFlush && exceptionDataRead.bits.flushPipe
513  val deqHasReplayInst = deqHasExceptionOrFlush && exceptionDataRead.bits.replayInst
514  val exceptionEnable = writebacked(deqPtr.value) && deqHasException
515
516  XSDebug(deqHasException && exceptionDataRead.bits.singleStep, "Debug Mode: Deq has singlestep exception\n")
517  XSDebug(deqHasException && exceptionDataRead.bits.trigger.getHitFrontend, "Debug Mode: Deq has frontend trigger exception\n")
518  XSDebug(deqHasException && exceptionDataRead.bits.trigger.getHitBackend, "Debug Mode: Deq has backend trigger exception\n")
519
520  val isFlushPipe = writebacked(deqPtr.value) && (deqHasFlushPipe || deqHasReplayInst)
521
522  // io.flushOut will trigger redirect at the next cycle.
523  // Block any redirect or commit at the next cycle.
524  val lastCycleFlush = RegNext(io.flushOut.valid)
525
526  io.flushOut.valid := (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable || isFlushPipe) && !lastCycleFlush
527  io.flushOut.bits := DontCare
528  io.flushOut.bits.robIdx := deqPtr
529  io.flushOut.bits.ftqIdx := deqDispatchData.ftqIdx
530  io.flushOut.bits.ftqOffset := deqDispatchData.ftqOffset
531  io.flushOut.bits.level := Mux(deqHasReplayInst || intrEnable || exceptionEnable, RedirectLevel.flush, RedirectLevel.flushAfter) // TODO use this to implement "exception next"
532  io.flushOut.bits.interrupt := true.B
533  XSPerfAccumulate("interrupt_num", io.flushOut.valid && intrEnable)
534  XSPerfAccumulate("exception_num", io.flushOut.valid && exceptionEnable)
535  XSPerfAccumulate("flush_pipe_num", io.flushOut.valid && isFlushPipe)
536  XSPerfAccumulate("replay_inst_num", io.flushOut.valid && isFlushPipe && deqHasReplayInst)
537
538  val exceptionHappen = (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable) && !lastCycleFlush
539  io.exception.valid := RegNext(exceptionHappen)
540  io.exception.bits.uop := RegEnable(debug_deqUop, exceptionHappen)
541  io.exception.bits.uop.ctrl.commitType := RegEnable(deqDispatchData.commitType, exceptionHappen)
542  io.exception.bits.uop.cf.exceptionVec := RegEnable(exceptionDataRead.bits.exceptionVec, exceptionHappen)
543  io.exception.bits.uop.ctrl.singleStep := RegEnable(exceptionDataRead.bits.singleStep, exceptionHappen)
544  io.exception.bits.uop.cf.crossPageIPFFix := RegEnable(exceptionDataRead.bits.crossPageIPFFix, exceptionHappen)
545  io.exception.bits.isInterrupt := RegEnable(intrEnable, exceptionHappen)
546  io.exception.bits.uop.cf.trigger := RegEnable(exceptionDataRead.bits.trigger, exceptionHappen)
547
548  XSDebug(io.flushOut.valid,
549    p"generate redirect: pc 0x${Hexadecimal(io.exception.bits.uop.cf.pc)} intr $intrEnable " +
550    p"excp $exceptionEnable flushPipe $isFlushPipe " +
551    p"Trap_target 0x${Hexadecimal(io.csr.trapTarget)} exceptionVec ${Binary(exceptionDataRead.bits.exceptionVec.asUInt)}\n")
552
553
554  /**
555    * Commits (and walk)
556    * They share the same width.
557    */
558  val walkCounter = Reg(UInt(log2Up(RobSize + 1).W))
559  val shouldWalkVec = VecInit((0 until CommitWidth).map(_.U < walkCounter))
560  val walkFinished = walkCounter <= CommitWidth.U
561
562  require(RenameWidth <= CommitWidth)
563
564  // wiring to csr
565  val (wflags, fpWen) = (0 until CommitWidth).map(i => {
566    val v = io.commits.commitValid(i)
567    val info = io.commits.info(i)
568    (v & info.wflags, v & info.fpWen)
569  }).unzip
570  val fflags = Wire(Valid(UInt(5.W)))
571  fflags.valid := io.commits.isCommit && VecInit(wflags).asUInt.orR
572  fflags.bits := wflags.zip(fflagsDataRead).map({
573    case (w, f) => Mux(w, f, 0.U)
574  }).reduce(_|_)
575  val dirty_fs = io.commits.isCommit && VecInit(fpWen).asUInt.orR
576
577  // when mispredict branches writeback, stop commit in the next 2 cycles
578  // TODO: don't check all exu write back
579  val misPredWb = Cat(VecInit(exuWriteback.map(wb =>
580    wb.bits.redirect.cfiUpdate.isMisPred && wb.bits.redirectValid
581  ))).orR
582  val misPredBlockCounter = Reg(UInt(3.W))
583  misPredBlockCounter := Mux(misPredWb,
584    "b111".U,
585    misPredBlockCounter >> 1.U
586  )
587  val misPredBlock = misPredBlockCounter(0)
588  val blockCommit = misPredBlock || isReplaying || lastCycleFlush || hasWFI
589
590  io.commits.isWalk := state === s_walk
591  io.commits.isCommit := state === s_idle && !blockCommit
592  val walk_v = VecInit(walkPtrVec.map(ptr => valid(ptr.value)))
593  val commit_v = VecInit(deqPtrVec.map(ptr => valid(ptr.value)))
594  // store will be commited iff both sta & std have been writebacked
595  val commit_w = VecInit(deqPtrVec.map(ptr => writebacked(ptr.value) && store_data_writebacked(ptr.value)))
596  val commit_exception = exceptionDataRead.valid && !isAfter(exceptionDataRead.bits.robIdx, deqPtrVec.last)
597  val commit_block = VecInit((0 until CommitWidth).map(i => !commit_w(i)))
598  val allowOnlyOneCommit = commit_exception || intrBitSetReg
599  // for instructions that may block others, we don't allow them to commit
600  for (i <- 0 until CommitWidth) {
601    // defaults: state === s_idle and instructions commit
602    // when intrBitSetReg, allow only one instruction to commit at each clock cycle
603    val isBlocked = if (i != 0) Cat(commit_block.take(i)).orR || allowOnlyOneCommit else intrEnable || deqHasException || deqHasReplayInst
604    io.commits.commitValid(i) := commit_v(i) && commit_w(i) && !isBlocked
605    io.commits.info(i)  := dispatchDataRead(i)
606
607    when (state === s_walk) {
608      io.commits.walkValid(i) := shouldWalkVec(i)
609      when (io.commits.isWalk && state === s_walk && shouldWalkVec(i)) {
610        XSError(!walk_v(i), s"why not $i???\n")
611      }
612    }
613
614    XSInfo(io.commits.isCommit && io.commits.commitValid(i),
615      "retired pc %x wen %d ldest %d pdest %x old_pdest %x data %x fflags: %b\n",
616      debug_microOp(deqPtrVec(i).value).cf.pc,
617      io.commits.info(i).rfWen,
618      io.commits.info(i).ldest,
619      io.commits.info(i).pdest,
620      io.commits.info(i).old_pdest,
621      debug_exuData(deqPtrVec(i).value),
622      fflagsDataRead(i)
623    )
624    XSInfo(state === s_walk && io.commits.walkValid(i), "walked pc %x wen %d ldst %d data %x\n",
625      debug_microOp(walkPtrVec(i).value).cf.pc,
626      io.commits.info(i).rfWen,
627      io.commits.info(i).ldest,
628      debug_exuData(walkPtrVec(i).value)
629    )
630  }
631  if (env.EnableDifftest) {
632    io.commits.info.map(info => dontTouch(info.pc))
633  }
634
635  // sync fflags/dirty_fs to csr
636  io.csr.fflags := RegNext(fflags)
637  io.csr.dirty_fs := RegNext(dirty_fs)
638
639  // commit load/store to lsq
640  val ldCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.LOAD))
641  val stCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.STORE))
642  io.lsq.lcommit := RegNext(Mux(io.commits.isCommit, PopCount(ldCommitVec), 0.U))
643  io.lsq.scommit := RegNext(Mux(io.commits.isCommit, PopCount(stCommitVec), 0.U))
644  // indicate a pending load or store
645  io.lsq.pendingld := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.LOAD && valid(deqPtr.value))
646  io.lsq.pendingst := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.STORE && valid(deqPtr.value))
647  io.lsq.commit := RegNext(io.commits.isCommit && io.commits.commitValid(0))
648
649  /**
650    * state changes
651    * (1) redirect: switch to s_walk
652    * (2) walk: when walking comes to the end, switch to s_idle
653    */
654  val state_next = Mux(io.redirect.valid, s_walk, Mux(state === s_walk && walkFinished, s_idle, state))
655  XSPerfAccumulate("s_idle_to_idle",            state === s_idle && state_next === s_idle)
656  XSPerfAccumulate("s_idle_to_walk",            state === s_idle && state_next === s_walk)
657  XSPerfAccumulate("s_walk_to_idle",            state === s_walk && state_next === s_idle)
658  XSPerfAccumulate("s_walk_to_walk",            state === s_walk && state_next === s_walk)
659  state := state_next
660
661  /**
662    * pointers and counters
663    */
664  val deqPtrGenModule = Module(new RobDeqPtrWrapper)
665  deqPtrGenModule.io.state := state
666  deqPtrGenModule.io.deq_v := commit_v
667  deqPtrGenModule.io.deq_w := commit_w
668  deqPtrGenModule.io.exception_state := exceptionDataRead
669  deqPtrGenModule.io.intrBitSetReg := intrBitSetReg
670  deqPtrGenModule.io.hasNoSpecExec := hasNoSpecExec
671  deqPtrGenModule.io.interrupt_safe := interrupt_safe(deqPtr.value)
672  deqPtrGenModule.io.blockCommit := blockCommit
673  deqPtrVec := deqPtrGenModule.io.out
674  val deqPtrVec_next = deqPtrGenModule.io.next_out
675
676  val enqPtrGenModule = Module(new RobEnqPtrWrapper)
677  enqPtrGenModule.io.redirect := io.redirect
678  enqPtrGenModule.io.allowEnqueue := allowEnqueue
679  enqPtrGenModule.io.hasBlockBackward := hasBlockBackward
680  enqPtrGenModule.io.enq := VecInit(io.enq.req.map(_.valid))
681  enqPtrVec := enqPtrGenModule.io.out
682
683  val thisCycleWalkCount = Mux(walkFinished, walkCounter, CommitWidth.U)
684  // next walkPtrVec:
685  // (1) redirect occurs: update according to state
686  // (2) walk: move forwards
687  val walkPtrVec_next = Mux(io.redirect.valid,
688    deqPtrVec_next,
689    Mux(state === s_walk, VecInit(walkPtrVec.map(_ + CommitWidth.U)), walkPtrVec)
690  )
691  walkPtrVec := walkPtrVec_next
692
693  val numValidEntries = distanceBetween(enqPtr, deqPtr)
694  val commitCnt = PopCount(io.commits.commitValid)
695
696  allowEnqueue := numValidEntries + dispatchNum <= (RobSize - RenameWidth).U
697
698  val currentWalkPtr = Mux(state === s_walk, walkPtr, deqPtrVec_next(0))
699  val redirectWalkDistance = distanceBetween(io.redirect.bits.robIdx, deqPtrVec_next(0))
700  when (io.redirect.valid) {
701    // full condition:
702    // +& is used here because:
703    // When rob is full and the tail instruction causes a misprediction,
704    // the redirect robIdx is the deqPtr - 1. In this case, redirectWalkDistance
705    // is RobSize - 1.
706    // Since misprediction does not flush the instruction itself, flushItSelf is false.B.
707    // Previously we use `+` to count the walk distance and it causes overflows
708    // when RobSize is power of 2. We change it to `+&` to allow walkCounter to be RobSize.
709    // The width of walkCounter also needs to be changed.
710    // empty condition:
711    // When the last instruction in ROB commits and causes a flush, a redirect
712    // will be raised later. In such circumstances, the redirect robIdx is before
713    // the deqPtrVec_next(0) and will cause underflow.
714    walkCounter := Mux(isBefore(io.redirect.bits.robIdx, deqPtrVec_next(0)), 0.U,
715                       redirectWalkDistance +& !io.redirect.bits.flushItself())
716  }.elsewhen (state === s_walk) {
717    walkCounter := walkCounter - thisCycleWalkCount
718    XSInfo(p"rolling back: $enqPtr $deqPtr walk $walkPtr walkcnt $walkCounter\n")
719  }
720
721
722  /**
723    * States
724    * We put all the stage bits changes here.
725
726    * All events: (1) enqueue (dispatch); (2) writeback; (3) cancel; (4) dequeue (commit);
727    * All states: (1) valid; (2) writebacked; (3) flagBkup
728    */
729  val commitReadAddr = Mux(state === s_idle, VecInit(deqPtrVec.map(_.value)), VecInit(walkPtrVec.map(_.value)))
730
731  // redirect logic writes 6 valid
732  val redirectHeadVec = Reg(Vec(RenameWidth, new RobPtr))
733  val redirectTail = Reg(new RobPtr)
734  val redirectIdle :: redirectBusy :: Nil = Enum(2)
735  val redirectState = RegInit(redirectIdle)
736  val invMask = redirectHeadVec.map(redirectHead => isBefore(redirectHead, redirectTail))
737  when(redirectState === redirectBusy) {
738    redirectHeadVec.foreach(redirectHead => redirectHead := redirectHead + RenameWidth.U)
739    redirectHeadVec zip invMask foreach {
740      case (redirectHead, inv) => when(inv) {
741        valid(redirectHead.value) := false.B
742      }
743    }
744    when(!invMask.last) {
745      redirectState := redirectIdle
746    }
747  }
748  when(io.redirect.valid) {
749    redirectState := redirectBusy
750    when(redirectState === redirectIdle) {
751      redirectTail := enqPtr
752    }
753    redirectHeadVec.zipWithIndex.foreach { case (redirectHead, i) =>
754      redirectHead := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U)
755    }
756  }
757  // enqueue logic writes 6 valid
758  for (i <- 0 until RenameWidth) {
759    when (canEnqueue(i) && !io.redirect.valid) {
760      valid(allocatePtrVec(i).value) := true.B
761    }
762  }
763  // dequeue logic writes 6 valid
764  for (i <- 0 until CommitWidth) {
765    val commitValid = io.commits.isCommit && io.commits.commitValid(i)
766    when (commitValid) {
767      valid(commitReadAddr(i)) := false.B
768    }
769  }
770
771  // status field: writebacked
772  // enqueue logic set 6 writebacked to false
773  for (i <- 0 until RenameWidth) {
774    when (canEnqueue(i)) {
775      val enqHasException = ExceptionNO.selectFrontend(io.enq.req(i).bits.cf.exceptionVec).asUInt.orR
776      val enqHasTriggerHit = io.enq.req(i).bits.cf.trigger.getHitFrontend
777      val enqIsWritebacked = io.enq.req(i).bits.eliminatedMove
778      writebacked(allocatePtrVec(i).value) := enqIsWritebacked && !enqHasException && !enqHasTriggerHit
779      val isStu = io.enq.req(i).bits.ctrl.fuType === FuType.stu
780      store_data_writebacked(allocatePtrVec(i).value) := !isStu
781    }
782  }
783  when (exceptionGen.io.out.valid) {
784    val wbIdx = exceptionGen.io.out.bits.robIdx.value
785    writebacked(wbIdx) := true.B
786    store_data_writebacked(wbIdx) := true.B
787  }
788  // writeback logic set numWbPorts writebacked to true
789  for ((wb, cfgs) <- exuWriteback.zip(wbExuConfigs(exeWbSel))) {
790    when (wb.valid) {
791      val wbIdx = wb.bits.uop.robIdx.value
792      val wbHasException = ExceptionNO.selectByExu(wb.bits.uop.cf.exceptionVec, cfgs).asUInt.orR
793      val wbHasTriggerHit = wb.bits.uop.cf.trigger.getHitBackend
794      val wbHasFlushPipe = cfgs.exists(_.flushPipe).B && wb.bits.uop.ctrl.flushPipe
795      val wbHasReplayInst = cfgs.exists(_.replayInst).B && wb.bits.uop.ctrl.replayInst
796      val block_wb = wbHasException || wbHasFlushPipe || wbHasReplayInst || wbHasTriggerHit
797      writebacked(wbIdx) := !block_wb
798    }
799  }
800  // store data writeback logic mark store as data_writebacked
801  for (wb <- stdWriteback) {
802    when(RegNext(wb.valid)) {
803      store_data_writebacked(RegNext(wb.bits.uop.robIdx.value)) := true.B
804    }
805  }
806
807  // flagBkup
808  // enqueue logic set 6 flagBkup at most
809  for (i <- 0 until RenameWidth) {
810    when (canEnqueue(i)) {
811      flagBkup(allocatePtrVec(i).value) := allocatePtrVec(i).flag
812    }
813  }
814
815  // interrupt_safe
816  for (i <- 0 until RenameWidth) {
817    // We RegNext the updates for better timing.
818    // Note that instructions won't change the system's states in this cycle.
819    when (RegNext(canEnqueue(i))) {
820      // For now, we allow non-load-store instructions to trigger interrupts
821      // For MMIO instructions, they should not trigger interrupts since they may
822      // be sent to lower level before it writes back.
823      // However, we cannot determine whether a load/store instruction is MMIO.
824      // Thus, we don't allow load/store instructions to trigger an interrupt.
825      // TODO: support non-MMIO load-store instructions to trigger interrupts
826      val allow_interrupts = !CommitType.isLoadStore(io.enq.req(i).bits.ctrl.commitType)
827      interrupt_safe(RegNext(allocatePtrVec(i).value)) := RegNext(allow_interrupts)
828    }
829  }
830
831  /**
832    * read and write of data modules
833    */
834  val commitReadAddr_next = Mux(state_next === s_idle,
835    VecInit(deqPtrVec_next.map(_.value)),
836    VecInit(walkPtrVec_next.map(_.value))
837  )
838  dispatchData.io.wen := canEnqueue
839  dispatchData.io.waddr := allocatePtrVec.map(_.value)
840  dispatchData.io.wdata.zip(io.enq.req.map(_.bits)).foreach{ case (wdata, req) =>
841    wdata.ldest := req.ctrl.ldest
842    wdata.rfWen := req.ctrl.rfWen
843    wdata.fpWen := req.ctrl.fpWen
844    wdata.wflags := req.ctrl.fpu.wflags
845    wdata.commitType := req.ctrl.commitType
846    wdata.pdest := req.pdest
847    wdata.old_pdest := req.old_pdest
848    wdata.ftqIdx := req.cf.ftqPtr
849    wdata.ftqOffset := req.cf.ftqOffset
850    wdata.isMove := req.eliminatedMove
851    wdata.pc := req.cf.pc
852  }
853  dispatchData.io.raddr := commitReadAddr_next
854
855  exceptionGen.io.redirect <> io.redirect
856  exceptionGen.io.flush := io.flushOut.valid
857  for (i <- 0 until RenameWidth) {
858    exceptionGen.io.enq(i).valid := canEnqueue(i)
859    exceptionGen.io.enq(i).bits.robIdx := io.enq.req(i).bits.robIdx
860    exceptionGen.io.enq(i).bits.exceptionVec := ExceptionNO.selectFrontend(io.enq.req(i).bits.cf.exceptionVec)
861    exceptionGen.io.enq(i).bits.flushPipe := io.enq.req(i).bits.ctrl.flushPipe
862    exceptionGen.io.enq(i).bits.replayInst := false.B
863    XSError(canEnqueue(i) && io.enq.req(i).bits.ctrl.replayInst, "enq should not set replayInst")
864    exceptionGen.io.enq(i).bits.singleStep := io.enq.req(i).bits.ctrl.singleStep
865    exceptionGen.io.enq(i).bits.crossPageIPFFix := io.enq.req(i).bits.cf.crossPageIPFFix
866    exceptionGen.io.enq(i).bits.trigger.clear()
867    exceptionGen.io.enq(i).bits.trigger.frontendHit := io.enq.req(i).bits.cf.trigger.frontendHit
868  }
869
870  println(s"ExceptionGen:")
871  val exceptionCases = exceptionPorts.map(_._1.flatMap(_.exceptionOut).distinct.sorted)
872  require(exceptionCases.length == exceptionGen.io.wb.length)
873  for ((((configs, wb), exc_wb), i) <- exceptionPorts.zip(exceptionGen.io.wb).zipWithIndex) {
874    exc_wb.valid                := wb.valid
875    exc_wb.bits.robIdx          := wb.bits.uop.robIdx
876    exc_wb.bits.exceptionVec    := ExceptionNO.selectByExu(wb.bits.uop.cf.exceptionVec, configs)
877    exc_wb.bits.flushPipe       := configs.exists(_.flushPipe).B && wb.bits.uop.ctrl.flushPipe
878    exc_wb.bits.replayInst      := configs.exists(_.replayInst).B && wb.bits.uop.ctrl.replayInst
879    exc_wb.bits.singleStep      := false.B
880    exc_wb.bits.crossPageIPFFix := false.B
881    // TODO: make trigger configurable
882    exc_wb.bits.trigger.clear()
883    exc_wb.bits.trigger.backendHit := wb.bits.uop.cf.trigger.backendHit
884    println(s"  [$i] ${configs.map(_.name)}: exception ${exceptionCases(i)}, " +
885      s"flushPipe ${configs.exists(_.flushPipe)}, " +
886      s"replayInst ${configs.exists(_.replayInst)}")
887  }
888
889  val fflags_wb = fflagsPorts.map(_._2)
890  val fflagsDataModule = Module(new SyncDataModuleTemplate(
891    UInt(5.W), RobSize, CommitWidth, fflags_wb.size)
892  )
893  for(i <- fflags_wb.indices){
894    fflagsDataModule.io.wen  (i) := fflags_wb(i).valid
895    fflagsDataModule.io.waddr(i) := fflags_wb(i).bits.uop.robIdx.value
896    fflagsDataModule.io.wdata(i) := fflags_wb(i).bits.fflags
897  }
898  fflagsDataModule.io.raddr := VecInit(deqPtrVec_next.map(_.value))
899  fflagsDataRead := fflagsDataModule.io.rdata
900
901
902  val instrCntReg = RegInit(0.U(64.W))
903  val fuseCommitCnt = PopCount(io.commits.commitValid.zip(io.commits.info).map{ case (v, i) => RegNext(v && CommitType.isFused(i.commitType)) })
904  val trueCommitCnt = RegNext(commitCnt) +& fuseCommitCnt
905  val retireCounter = Mux(RegNext(io.commits.isCommit), trueCommitCnt, 0.U)
906  val instrCnt = instrCntReg + retireCounter
907  instrCntReg := instrCnt
908  io.csr.perfinfo.retiredInstr := retireCounter
909  io.robFull := !allowEnqueue
910
911  /**
912    * debug info
913    */
914  XSDebug(p"enqPtr ${enqPtr} deqPtr ${deqPtr}\n")
915  XSDebug("")
916  for(i <- 0 until RobSize){
917    XSDebug(false, !valid(i), "-")
918    XSDebug(false, valid(i) && writebacked(i), "w")
919    XSDebug(false, valid(i) && !writebacked(i), "v")
920  }
921  XSDebug(false, true.B, "\n")
922
923  for(i <- 0 until RobSize) {
924    if(i % 4 == 0) XSDebug("")
925    XSDebug(false, true.B, "%x ", debug_microOp(i).cf.pc)
926    XSDebug(false, !valid(i), "- ")
927    XSDebug(false, valid(i) && writebacked(i), "w ")
928    XSDebug(false, valid(i) && !writebacked(i), "v ")
929    if(i % 4 == 3) XSDebug(false, true.B, "\n")
930  }
931
932  def ifCommit(counter: UInt): UInt = Mux(io.commits.isCommit, counter, 0.U)
933  def ifCommitReg(counter: UInt): UInt = Mux(RegNext(io.commits.isCommit), counter, 0.U)
934
935  val commitDebugUop = deqPtrVec.map(_.value).map(debug_microOp(_))
936  XSPerfAccumulate("clock_cycle", 1.U)
937  QueuePerf(RobSize, PopCount((0 until RobSize).map(valid(_))), !allowEnqueue)
938  XSPerfAccumulate("commitUop", ifCommit(commitCnt))
939  XSPerfAccumulate("commitInstr", ifCommitReg(trueCommitCnt))
940  val commitIsMove = commitDebugUop.map(_.ctrl.isMove)
941  XSPerfAccumulate("commitInstrMove", ifCommit(PopCount(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m })))
942  val commitMoveElim = commitDebugUop.map(_.debugInfo.eliminatedMove)
943  XSPerfAccumulate("commitInstrMoveElim", ifCommit(PopCount(io.commits.commitValid zip commitMoveElim map { case (v, e) => v && e })))
944  XSPerfAccumulate("commitInstrFused", ifCommitReg(fuseCommitCnt))
945  val commitIsLoad = io.commits.info.map(_.commitType).map(_ === CommitType.LOAD)
946  val commitLoadValid = io.commits.commitValid.zip(commitIsLoad).map{ case (v, t) => v && t }
947  XSPerfAccumulate("commitInstrLoad", ifCommit(PopCount(commitLoadValid)))
948  val commitIsBranch = io.commits.info.map(_.commitType).map(_ === CommitType.BRANCH)
949  val commitBranchValid = io.commits.commitValid.zip(commitIsBranch).map{ case (v, t) => v && t }
950  XSPerfAccumulate("commitInstrBranch", ifCommit(PopCount(commitBranchValid)))
951  val commitLoadWaitBit = commitDebugUop.map(_.cf.loadWaitBit)
952  XSPerfAccumulate("commitInstrLoadWait", ifCommit(PopCount(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w })))
953  val commitIsStore = io.commits.info.map(_.commitType).map(_ === CommitType.STORE)
954  XSPerfAccumulate("commitInstrStore", ifCommit(PopCount(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t })))
955  XSPerfAccumulate("writeback", PopCount((0 until RobSize).map(i => valid(i) && writebacked(i))))
956  // XSPerfAccumulate("enqInstr", PopCount(io.dp1Req.map(_.fire)))
957  // XSPerfAccumulate("d2rVnR", PopCount(io.dp1Req.map(p => p.valid && !p.ready)))
958  XSPerfAccumulate("walkInstr", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U))
959  XSPerfAccumulate("walkCycle", state === s_walk)
960  val deqNotWritebacked = valid(deqPtr.value) && !writebacked(deqPtr.value)
961  val deqUopCommitType = io.commits.info(0).commitType
962  XSPerfAccumulate("waitNormalCycle", deqNotWritebacked && deqUopCommitType === CommitType.NORMAL)
963  XSPerfAccumulate("waitBranchCycle", deqNotWritebacked && deqUopCommitType === CommitType.BRANCH)
964  XSPerfAccumulate("waitLoadCycle", deqNotWritebacked && deqUopCommitType === CommitType.LOAD)
965  XSPerfAccumulate("waitStoreCycle", deqNotWritebacked && deqUopCommitType === CommitType.STORE)
966  XSPerfAccumulate("robHeadPC", io.commits.info(0).pc)
967  val dispatchLatency = commitDebugUop.map(uop => uop.debugInfo.dispatchTime - uop.debugInfo.renameTime)
968  val enqRsLatency = commitDebugUop.map(uop => uop.debugInfo.enqRsTime - uop.debugInfo.dispatchTime)
969  val selectLatency = commitDebugUop.map(uop => uop.debugInfo.selectTime - uop.debugInfo.enqRsTime)
970  val issueLatency = commitDebugUop.map(uop => uop.debugInfo.issueTime - uop.debugInfo.selectTime)
971  val executeLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.issueTime)
972  val rsFuLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.enqRsTime)
973  val commitLatency = commitDebugUop.map(uop => timer - uop.debugInfo.writebackTime)
974  def latencySum(cond: Seq[Bool], latency: Seq[UInt]): UInt = {
975    cond.zip(latency).map(x => Mux(x._1, x._2, 0.U)).reduce(_ +& _)
976  }
977  for (fuType <- FuType.functionNameMap.keys) {
978    val fuName = FuType.functionNameMap(fuType)
979    val commitIsFuType = io.commits.commitValid.zip(commitDebugUop).map(x => x._1 && x._2.ctrl.fuType === fuType.U )
980    XSPerfAccumulate(s"${fuName}_instr_cnt", ifCommit(PopCount(commitIsFuType)))
981    XSPerfAccumulate(s"${fuName}_latency_dispatch", ifCommit(latencySum(commitIsFuType, dispatchLatency)))
982    XSPerfAccumulate(s"${fuName}_latency_enq_rs", ifCommit(latencySum(commitIsFuType, enqRsLatency)))
983    XSPerfAccumulate(s"${fuName}_latency_select", ifCommit(latencySum(commitIsFuType, selectLatency)))
984    XSPerfAccumulate(s"${fuName}_latency_issue", ifCommit(latencySum(commitIsFuType, issueLatency)))
985    XSPerfAccumulate(s"${fuName}_latency_execute", ifCommit(latencySum(commitIsFuType, executeLatency)))
986    XSPerfAccumulate(s"${fuName}_latency_enq_rs_execute", ifCommit(latencySum(commitIsFuType, rsFuLatency)))
987    XSPerfAccumulate(s"${fuName}_latency_commit", ifCommit(latencySum(commitIsFuType, commitLatency)))
988    if (fuType == FuType.fmac.litValue) {
989      val commitIsFma = commitIsFuType.zip(commitDebugUop).map(x => x._1 && x._2.ctrl.fpu.ren3 )
990      XSPerfAccumulate(s"${fuName}_instr_cnt_fma", ifCommit(PopCount(commitIsFma)))
991      XSPerfAccumulate(s"${fuName}_latency_enq_rs_execute_fma", ifCommit(latencySum(commitIsFma, rsFuLatency)))
992      XSPerfAccumulate(s"${fuName}_latency_execute_fma", ifCommit(latencySum(commitIsFma, executeLatency)))
993    }
994  }
995
996  //difftest signals
997  val firstValidCommit = (deqPtr + PriorityMux(io.commits.commitValid, VecInit(List.tabulate(CommitWidth)(_.U(log2Up(CommitWidth).W))))).value
998
999  val wdata = Wire(Vec(CommitWidth, UInt(XLEN.W)))
1000  val wpc = Wire(Vec(CommitWidth, UInt(XLEN.W)))
1001
1002  for(i <- 0 until CommitWidth) {
1003    val idx = deqPtrVec(i).value
1004    wdata(i) := debug_exuData(idx)
1005    wpc(i) := SignExt(commitDebugUop(i).cf.pc, XLEN)
1006  }
1007
1008  if (env.EnableDifftest) {
1009    for (i <- 0 until CommitWidth) {
1010      val difftest = Module(new DifftestInstrCommit)
1011      // assgin default value
1012      difftest.io := DontCare
1013
1014      difftest.io.clock    := clock
1015      difftest.io.coreid   := io.hartId
1016      difftest.io.index    := i.U
1017
1018      val ptr = deqPtrVec(i).value
1019      val uop = commitDebugUop(i)
1020      val exuOut = debug_exuDebug(ptr)
1021      val exuData = debug_exuData(ptr)
1022      difftest.io.valid    := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit)))
1023      difftest.io.pc       := RegNext(RegNext(RegNext(SignExt(uop.cf.pc, XLEN))))
1024      difftest.io.instr    := RegNext(RegNext(RegNext(uop.cf.instr)))
1025      difftest.io.robIdx   := RegNext(RegNext(RegNext(ZeroExt(ptr, 10))))
1026      difftest.io.lqIdx    := RegNext(RegNext(RegNext(ZeroExt(uop.lqIdx.value, 7))))
1027      difftest.io.sqIdx    := RegNext(RegNext(RegNext(ZeroExt(uop.sqIdx.value, 7))))
1028      difftest.io.isLoad   := RegNext(RegNext(RegNext(io.commits.info(i).commitType === CommitType.LOAD)))
1029      difftest.io.isStore  := RegNext(RegNext(RegNext(io.commits.info(i).commitType === CommitType.STORE)))
1030      difftest.io.special  := RegNext(RegNext(RegNext(CommitType.isFused(io.commits.info(i).commitType))))
1031      // when committing an eliminated move instruction,
1032      // we must make sure that skip is properly set to false (output from EXU is random value)
1033      difftest.io.skip     := RegNext(RegNext(RegNext(Mux(uop.eliminatedMove, false.B, exuOut.isMMIO || exuOut.isPerfCnt))))
1034      difftest.io.isRVC    := RegNext(RegNext(RegNext(uop.cf.pd.isRVC)))
1035      difftest.io.rfwen    := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).rfWen && io.commits.info(i).ldest =/= 0.U)))
1036      difftest.io.fpwen    := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.info(i).fpWen)))
1037      difftest.io.wpdest   := RegNext(RegNext(RegNext(io.commits.info(i).pdest)))
1038      difftest.io.wdest    := RegNext(RegNext(RegNext(io.commits.info(i).ldest)))
1039
1040      // // runahead commit hint
1041      // val runahead_commit = Module(new DifftestRunaheadCommitEvent)
1042      // runahead_commit.io.clock := clock
1043      // runahead_commit.io.coreid := io.hartId
1044      // runahead_commit.io.index := i.U
1045      // runahead_commit.io.valid := difftest.io.valid &&
1046      //   (commitBranchValid(i) || commitIsStore(i))
1047      // // TODO: is branch or store
1048      // runahead_commit.io.pc    := difftest.io.pc
1049    }
1050  }
1051  else if (env.AlwaysBasicDiff) {
1052    // These are the structures used by difftest only and should be optimized after synthesis.
1053    val dt_eliminatedMove = Mem(RobSize, Bool())
1054    val dt_isRVC = Mem(RobSize, Bool())
1055    val dt_exuDebug = Reg(Vec(RobSize, new DebugBundle))
1056    for (i <- 0 until RenameWidth) {
1057      when (canEnqueue(i)) {
1058        dt_eliminatedMove(allocatePtrVec(i).value) := io.enq.req(i).bits.eliminatedMove
1059        dt_isRVC(allocatePtrVec(i).value) := io.enq.req(i).bits.cf.pd.isRVC
1060      }
1061    }
1062    for (wb <- exuWriteback) {
1063      when (wb.valid) {
1064        val wbIdx = wb.bits.uop.robIdx.value
1065        dt_exuDebug(wbIdx) := wb.bits.debug
1066      }
1067    }
1068    // Always instantiate basic difftest modules.
1069    for (i <- 0 until CommitWidth) {
1070      val commitInfo = io.commits.info(i)
1071      val ptr = deqPtrVec(i).value
1072      val exuOut = dt_exuDebug(ptr)
1073      val eliminatedMove = dt_eliminatedMove(ptr)
1074      val isRVC = dt_isRVC(ptr)
1075
1076      val difftest = Module(new DifftestBasicInstrCommit)
1077      difftest.io.clock   := clock
1078      difftest.io.coreid  := io.hartId
1079      difftest.io.index   := i.U
1080      difftest.io.valid   := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit)))
1081      difftest.io.special := RegNext(RegNext(RegNext(CommitType.isFused(commitInfo.commitType))))
1082      difftest.io.skip    := RegNext(RegNext(RegNext(Mux(eliminatedMove, false.B, exuOut.isMMIO || exuOut.isPerfCnt))))
1083      difftest.io.isRVC   := RegNext(RegNext(RegNext(isRVC)))
1084      difftest.io.rfwen   := RegNext(RegNext(RegNext(io.commits.commitValid(i) && commitInfo.rfWen && commitInfo.ldest =/= 0.U)))
1085      difftest.io.fpwen   := RegNext(RegNext(RegNext(io.commits.commitValid(i) && commitInfo.fpWen)))
1086      difftest.io.wpdest  := RegNext(RegNext(RegNext(commitInfo.pdest)))
1087      difftest.io.wdest   := RegNext(RegNext(RegNext(commitInfo.ldest)))
1088    }
1089  }
1090
1091  if (env.EnableDifftest) {
1092    for (i <- 0 until CommitWidth) {
1093      val difftest = Module(new DifftestLoadEvent)
1094      difftest.io.clock  := clock
1095      difftest.io.coreid := io.hartId
1096      difftest.io.index  := i.U
1097
1098      val ptr = deqPtrVec(i).value
1099      val uop = commitDebugUop(i)
1100      val exuOut = debug_exuDebug(ptr)
1101      difftest.io.valid  := RegNext(RegNext(RegNext(io.commits.commitValid(i) && io.commits.isCommit)))
1102      difftest.io.paddr  := RegNext(RegNext(RegNext(exuOut.paddr)))
1103      difftest.io.opType := RegNext(RegNext(RegNext(uop.ctrl.fuOpType)))
1104      difftest.io.fuType := RegNext(RegNext(RegNext(uop.ctrl.fuType)))
1105    }
1106  }
1107
1108  // Always instantiate basic difftest modules.
1109  if (env.EnableDifftest) {
1110    val dt_isXSTrap = Mem(RobSize, Bool())
1111    for (i <- 0 until RenameWidth) {
1112      when (canEnqueue(i)) {
1113        dt_isXSTrap(allocatePtrVec(i).value) := io.enq.req(i).bits.ctrl.isXSTrap
1114      }
1115    }
1116    val trapVec = io.commits.commitValid.zip(deqPtrVec).map{ case (v, d) => io.commits.isCommit && v && dt_isXSTrap(d.value) }
1117    val hitTrap = trapVec.reduce(_||_)
1118    val trapCode = PriorityMux(wdata.zip(trapVec).map(x => x._2 -> x._1))
1119    val trapPC = SignExt(PriorityMux(wpc.zip(trapVec).map(x => x._2 ->x._1)), XLEN)
1120    val difftest = Module(new DifftestTrapEvent)
1121    difftest.io.clock    := clock
1122    difftest.io.coreid   := io.hartId
1123    difftest.io.valid    := hitTrap
1124    difftest.io.code     := trapCode
1125    difftest.io.pc       := trapPC
1126    difftest.io.cycleCnt := timer
1127    difftest.io.instrCnt := instrCnt
1128    difftest.io.hasWFI   := hasWFI
1129  }
1130  else if (env.AlwaysBasicDiff) {
1131    val dt_isXSTrap = Mem(RobSize, Bool())
1132    for (i <- 0 until RenameWidth) {
1133      when (canEnqueue(i)) {
1134        dt_isXSTrap(allocatePtrVec(i).value) := io.enq.req(i).bits.ctrl.isXSTrap
1135      }
1136    }
1137    val trapVec = io.commits.commitValid.zip(deqPtrVec).map{ case (v, d) => io.commits.isCommit && v && dt_isXSTrap(d.value) }
1138    val hitTrap = trapVec.reduce(_||_)
1139    val difftest = Module(new DifftestBasicTrapEvent)
1140    difftest.io.clock    := clock
1141    difftest.io.coreid   := io.hartId
1142    difftest.io.valid    := hitTrap
1143    difftest.io.cycleCnt := timer
1144    difftest.io.instrCnt := instrCnt
1145  }
1146
1147  val validEntriesBanks = (0 until (RobSize + 63) / 64).map(i => RegNext(PopCount(valid.drop(i * 64).take(64))))
1148  val validEntries = RegNext(ParallelOperation(validEntriesBanks, (a: UInt, b: UInt) => a +& b))
1149  val commitMoveVec = VecInit(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m })
1150  val commitLoadVec = VecInit(commitLoadValid)
1151  val commitBranchVec = VecInit(commitBranchValid)
1152  val commitLoadWaitVec = VecInit(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w })
1153  val commitStoreVec = VecInit(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t })
1154  val perfEvents = Seq(
1155    ("rob_interrupt_num      ", io.flushOut.valid && intrEnable                                       ),
1156    ("rob_exception_num      ", io.flushOut.valid && exceptionEnable                                  ),
1157    ("rob_flush_pipe_num     ", io.flushOut.valid && isFlushPipe                                      ),
1158    ("rob_replay_inst_num    ", io.flushOut.valid && isFlushPipe && deqHasReplayInst                  ),
1159    ("rob_commitUop          ", ifCommit(commitCnt)                                                   ),
1160    ("rob_commitInstr        ", ifCommitReg(trueCommitCnt)                                            ),
1161    ("rob_commitInstrMove    ", ifCommitReg(PopCount(RegNext(commitMoveVec)))                         ),
1162    ("rob_commitInstrFused   ", ifCommitReg(fuseCommitCnt)                                            ),
1163    ("rob_commitInstrLoad    ", ifCommitReg(PopCount(RegNext(commitLoadVec)))                         ),
1164    ("rob_commitInstrBranch  ", ifCommitReg(PopCount(RegNext(commitBranchVec)))                       ),
1165    ("rob_commitInstrLoadWait", ifCommitReg(PopCount(RegNext(commitLoadWaitVec)))                     ),
1166    ("rob_commitInstrStore   ", ifCommitReg(PopCount(RegNext(commitStoreVec)))                        ),
1167    ("rob_walkInstr          ", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U)           ),
1168    ("rob_walkCycle          ", (state === s_walk)                                                    ),
1169    ("rob_1_4_valid          ", validEntries <= (RobSize / 4).U                                       ),
1170    ("rob_2_4_valid          ", validEntries >  (RobSize / 4).U && validEntries <= (RobSize / 2).U    ),
1171    ("rob_3_4_valid          ", validEntries >  (RobSize / 2).U && validEntries <= (RobSize * 3 / 4).U),
1172    ("rob_4_4_valid          ", validEntries >  (RobSize * 3 / 4).U                                   ),
1173  )
1174  generatePerfEvent()
1175}
1176