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