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