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