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