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