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 org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 24import utility._ 25import utils._ 26import xiangshan._ 27import xiangshan.backend.BackendParams 28import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput} 29import xiangshan.backend.fu.{FuConfig, FuType} 30import xiangshan.frontend.FtqPtr 31import xiangshan.mem.{LqPtr, LsqEnqIO, SqPtr} 32import xiangshan.backend.Bundles.{DynInst, ExceptionInfo, ExuOutput} 33import xiangshan.backend.ctrlblock.{DebugLSIO, DebugLsInfo, LsTopdownInfo} 34import xiangshan.backend.fu.vector.Bundles.VType 35import xiangshan.backend.rename.SnapshotGenerator 36 37 38class RobPtr(entries: Int) extends CircularQueuePtr[RobPtr]( 39 entries 40) with HasCircularQueuePtrHelper { 41 42 def this()(implicit p: Parameters) = this(p(XSCoreParamsKey).RobSize) 43 44 def needFlush(redirect: Valid[Redirect]): Bool = { 45 val flushItself = redirect.bits.flushItself() && this === redirect.bits.robIdx 46 redirect.valid && (flushItself || isAfter(this, redirect.bits.robIdx)) 47 } 48 49 def needFlush(redirect: Seq[Valid[Redirect]]): Bool = VecInit(redirect.map(needFlush)).asUInt.orR 50} 51 52object RobPtr { 53 def apply(f: Bool, v: UInt)(implicit p: Parameters): RobPtr = { 54 val ptr = Wire(new RobPtr) 55 ptr.flag := f 56 ptr.value := v 57 ptr 58 } 59} 60 61class RobCSRIO(implicit p: Parameters) extends XSBundle { 62 val intrBitSet = Input(Bool()) 63 val trapTarget = Input(UInt(VAddrBits.W)) 64 val isXRet = Input(Bool()) 65 val wfiEvent = Input(Bool()) 66 67 val fflags = Output(Valid(UInt(5.W))) 68 val vxsat = Output(Valid(Bool())) 69 val vstart = Output(Valid(UInt(XLEN.W))) 70 val dirty_fs = Output(Bool()) 71 val perfinfo = new Bundle { 72 val retiredInstr = Output(UInt(3.W)) 73 } 74 75 val vcsrFlag = Output(Bool()) 76} 77 78class RobLsqIO(implicit p: Parameters) extends XSBundle { 79 val lcommit = Output(UInt(log2Up(CommitWidth + 1).W)) 80 val scommit = Output(UInt(log2Up(CommitWidth + 1).W)) 81 val pendingld = Output(Bool()) 82 val pendingst = Output(Bool()) 83 val commit = Output(Bool()) 84 val pendingPtr = Output(new RobPtr) 85 val pendingPtrNext = Output(new RobPtr) 86 87 val mmio = Input(Vec(LoadPipelineWidth, Bool())) 88 // Todo: what's this? 89 val uop = Input(Vec(LoadPipelineWidth, new DynInst)) 90} 91 92class RobEnqIO(implicit p: Parameters) extends XSBundle { 93 val canAccept = Output(Bool()) 94 val isEmpty = Output(Bool()) 95 // valid vector, for robIdx gen and walk 96 val needAlloc = Vec(RenameWidth, Input(Bool())) 97 val req = Vec(RenameWidth, Flipped(ValidIO(new DynInst))) 98 val resp = Vec(RenameWidth, Output(new RobPtr)) 99} 100 101class RobCoreTopDownIO(implicit p: Parameters) extends XSBundle { 102 val robHeadVaddr = Valid(UInt(VAddrBits.W)) 103 val robHeadPaddr = Valid(UInt(PAddrBits.W)) 104} 105 106class RobDispatchTopDownIO extends Bundle { 107 val robTrueCommit = Output(UInt(64.W)) 108 val robHeadLsIssue = Output(Bool()) 109} 110 111class RobDebugRollingIO extends Bundle { 112 val robTrueCommit = Output(UInt(64.W)) 113} 114 115class RobDispatchData(implicit p: Parameters) extends RobCommitInfo {} 116 117class RobDeqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 118 val io = IO(new Bundle { 119 // for commits/flush 120 val state = Input(UInt(2.W)) 121 val deq_v = Vec(CommitWidth, Input(Bool())) 122 val deq_w = Vec(CommitWidth, Input(Bool())) 123 val exception_state = Flipped(ValidIO(new RobExceptionInfo)) 124 // for flush: when exception occurs, reset deqPtrs to range(0, CommitWidth) 125 val intrBitSetReg = Input(Bool()) 126 val hasNoSpecExec = Input(Bool()) 127 val interrupt_safe = Input(Bool()) 128 val blockCommit = Input(Bool()) 129 // output: the CommitWidth deqPtr 130 val out = Vec(CommitWidth, Output(new RobPtr)) 131 val next_out = Vec(CommitWidth, Output(new RobPtr)) 132 val commitCnt = Output(UInt(log2Up(CommitWidth+1).W)) 133 val canCommitPriorityCond = Output(Vec(CommitWidth+1,Bool())) 134 val commitEn = Output(Bool()) 135 }) 136 137 val deqPtrVec = RegInit(VecInit((0 until CommitWidth).map(_.U.asTypeOf(new RobPtr)))) 138 139 // for exceptions (flushPipe included) and interrupts: 140 // only consider the first instruction 141 val intrEnable = io.intrBitSetReg && !io.hasNoSpecExec && io.interrupt_safe 142 val exceptionEnable = io.deq_w(0) && io.exception_state.valid && io.exception_state.bits.not_commit && io.exception_state.bits.robIdx === deqPtrVec(0) 143 val redirectOutValid = io.state === 0.U && io.deq_v(0) && (intrEnable || exceptionEnable) 144 145 // for normal commits: only to consider when there're no exceptions 146 // we don't need to consider whether the first instruction has exceptions since it wil trigger exceptions. 147 val commit_exception = io.exception_state.valid && !isAfter(io.exception_state.bits.robIdx, deqPtrVec.last) 148 val canCommit = VecInit((0 until CommitWidth).map(i => io.deq_v(i) && io.deq_w(i))) 149 val normalCommitCnt = PriorityEncoder(canCommit.map(c => !c) :+ true.B) 150 // when io.intrBitSetReg or there're possible exceptions in these instructions, 151 // only one instruction is allowed to commit 152 val allowOnlyOne = commit_exception || io.intrBitSetReg 153 val commitCnt = Mux(allowOnlyOne, canCommit(0), normalCommitCnt) 154 val allowOnlyOneCond = Wire(chiselTypeOf(io.canCommitPriorityCond)) 155 allowOnlyOneCond.zipWithIndex.map{ case (value,i) => value := (if (i==0) !canCommit(0) else true.B)} 156 io.canCommitPriorityCond := Mux(allowOnlyOne, allowOnlyOneCond, VecInit(canCommit.map(c => !c) :+ true.B)) 157 158 val commitDeqPtrAll = VecInit((0 until 2*CommitWidth).map{case i => deqPtrVec(0) + i.U}) 159 val commitDeqPtrVec = Wire(chiselTypeOf(deqPtrVec)) 160 for (i <- 0 until CommitWidth){ 161 commitDeqPtrVec(i) := PriorityMuxDefault(io.canCommitPriorityCond.zip(commitDeqPtrAll.drop(i).take(CommitWidth+1)), deqPtrVec(i)) 162 } 163 val deqPtrVec_next = Mux(io.state === 0.U && !redirectOutValid && !io.blockCommit, commitDeqPtrVec, deqPtrVec) 164 165 deqPtrVec := deqPtrVec_next 166 167 io.next_out := deqPtrVec_next 168 io.out := deqPtrVec 169 io.commitCnt := commitCnt 170 io.commitEn := io.state === 0.U && !redirectOutValid && !io.blockCommit 171 172 when (io.state === 0.U) { 173 XSInfo(io.state === 0.U && commitCnt > 0.U, "retired %d insts\n", commitCnt) 174 } 175 176} 177 178class RobEnqPtrWrapper(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 179 val io = IO(new Bundle { 180 // for input redirect 181 val redirect = Input(Valid(new Redirect)) 182 // for enqueue 183 val allowEnqueue = Input(Bool()) 184 val hasBlockBackward = Input(Bool()) 185 val enq = Vec(RenameWidth, Input(Bool())) 186 val out = Output(Vec(RenameWidth, new RobPtr)) 187 }) 188 189 val enqPtrVec = RegInit(VecInit.tabulate(RenameWidth)(_.U.asTypeOf(new RobPtr))) 190 191 // enqueue 192 val canAccept = io.allowEnqueue && !io.hasBlockBackward 193 val dispatchNum = Mux(canAccept, PopCount(io.enq), 0.U) 194 195 for ((ptr, i) <- enqPtrVec.zipWithIndex) { 196 when(io.redirect.valid) { 197 ptr := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U) 198 }.otherwise { 199 ptr := ptr + dispatchNum 200 } 201 } 202 203 io.out := enqPtrVec 204 205} 206 207class RobExceptionInfo(implicit p: Parameters) extends XSBundle { 208 // val valid = Bool() 209 val robIdx = new RobPtr 210 val exceptionVec = ExceptionVec() 211 val flushPipe = Bool() 212 val isVset = Bool() 213 val replayInst = Bool() // redirect to that inst itself 214 val singleStep = Bool() // TODO add frontend hit beneath 215 val crossPageIPFFix = Bool() 216 val trigger = new TriggerCf 217 val vstartEn = Bool() 218 val vstart = UInt(XLEN.W) 219 220 def has_exception = exceptionVec.asUInt.orR || flushPipe || singleStep || replayInst || trigger.canFire 221 def not_commit = exceptionVec.asUInt.orR || singleStep || replayInst || trigger.canFire 222 // only exceptions are allowed to writeback when enqueue 223 def can_writeback = exceptionVec.asUInt.orR || singleStep || trigger.canFire 224} 225 226class ExceptionGen(params: BackendParams)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 227 val io = IO(new Bundle { 228 val redirect = Input(Valid(new Redirect)) 229 val flush = Input(Bool()) 230 val enq = Vec(RenameWidth, Flipped(ValidIO(new RobExceptionInfo))) 231 // csr + load + store + varith + vload + vstore 232 val wb = Vec(params.numException, Flipped(ValidIO(new RobExceptionInfo))) 233 val out = ValidIO(new RobExceptionInfo) 234 val state = ValidIO(new RobExceptionInfo) 235 }) 236 237 val wbExuParams = params.allExuParams.filter(_.exceptionOut.nonEmpty) 238 239 def getOldest(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): RobExceptionInfo = { 240 def getOldest_recursion(valid: Seq[Bool], bits: Seq[RobExceptionInfo]): (Seq[Bool], Seq[RobExceptionInfo]) = { 241 assert(valid.length == bits.length) 242 if (valid.length == 1) { 243 (valid, bits) 244 } else if (valid.length == 2) { 245 val res = Seq.fill(2)(Wire(ValidIO(chiselTypeOf(bits(0))))) 246 for (i <- res.indices) { 247 res(i).valid := valid(i) 248 res(i).bits := bits(i) 249 } 250 val oldest = Mux(!valid(1) || valid(0) && isAfter(bits(1).robIdx, bits(0).robIdx), res(0), res(1)) 251 (Seq(oldest.valid), Seq(oldest.bits)) 252 } else { 253 val left = getOldest_recursion(valid.take(valid.length / 2), bits.take(valid.length / 2)) 254 val right = getOldest_recursion(valid.drop(valid.length / 2), bits.drop(valid.length / 2)) 255 getOldest_recursion(left._1 ++ right._1, left._2 ++ right._2) 256 } 257 } 258 getOldest_recursion(valid, bits)._2.head 259 } 260 261 262 val currentValid = RegInit(false.B) 263 val current = Reg(new RobExceptionInfo) 264 265 // orR the exceptionVec 266 val lastCycleFlush = RegNext(io.flush) 267 val in_enq_valid = VecInit(io.enq.map(e => e.valid && e.bits.has_exception && !lastCycleFlush)) 268 269 // s0: compare wb in 6 groups 270 val csr_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(t => t.isCsr).nonEmpty).map(_._1) 271 val load_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.fuType == FuType.ldu).nonEmpty).map(_._1) 272 val store_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(t => t.isSta || t.fuType == FuType.mou).nonEmpty).map(_._1) 273 val varith_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.isVecArith).nonEmpty).map(_._1) 274 val vload_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.fuType == FuType.vldu).nonEmpty).map(_._1) 275 val vstore_wb = io.wb.zip(wbExuParams).filter(_._2.fuConfigs.filter(_.fuType == FuType.vstu).nonEmpty).map(_._1) 276 277 val writebacks = Seq(csr_wb, load_wb, store_wb, varith_wb, vload_wb, vstore_wb) 278 val in_wb_valids = writebacks.map(_.map(w => w.valid && w.bits.has_exception && !lastCycleFlush)) 279 val wb_valid = in_wb_valids.zip(writebacks).map { case (valid, wb) => 280 valid.zip(wb.map(_.bits)).map { case (v, bits) => v && !(bits.robIdx.needFlush(io.redirect) || io.flush) }.reduce(_ || _) 281 } 282 val wb_bits = in_wb_valids.zip(writebacks).map { case (valid, wb) => getOldest(valid, wb.map(_.bits))} 283 284 val s0_out_valid = wb_valid.map(x => RegNext(x)) 285 val s0_out_bits = wb_bits.zip(wb_valid).map{ case(b, v) => RegEnable(b, v)} 286 287 // s1: compare last six and current flush 288 val s1_valid = VecInit(s0_out_valid.zip(s0_out_bits).map{ case (v, b) => v && !(b.robIdx.needFlush(io.redirect) || io.flush) }) 289 val s1_out_bits = RegEnable(getOldest(s0_out_valid, s0_out_bits), s1_valid.asUInt.orR) 290 val s1_out_valid = RegNext(s1_valid.asUInt.orR) 291 292 val enq_valid = RegNext(in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush) 293 val enq_bits = RegEnable(ParallelPriorityMux(in_enq_valid, io.enq.map(_.bits)), in_enq_valid.asUInt.orR && !io.redirect.valid && !io.flush) 294 295 // s2: compare the input exception with the current one 296 // priorities: 297 // (1) system reset 298 // (2) current is valid: flush, remain, merge, update 299 // (3) current is not valid: s1 or enq 300 val current_flush = current.robIdx.needFlush(io.redirect) || io.flush 301 val s1_flush = s1_out_bits.robIdx.needFlush(io.redirect) || io.flush 302 when (currentValid) { 303 when (current_flush) { 304 currentValid := Mux(s1_flush, false.B, s1_out_valid) 305 } 306 when (s1_out_valid && !s1_flush) { 307 when (isAfter(current.robIdx, s1_out_bits.robIdx)) { 308 current := s1_out_bits 309 }.elsewhen (current.robIdx === s1_out_bits.robIdx) { 310 current.exceptionVec := (s1_out_bits.exceptionVec.asUInt | current.exceptionVec.asUInt).asTypeOf(ExceptionVec()) 311 current.flushPipe := s1_out_bits.flushPipe || current.flushPipe 312 current.replayInst := s1_out_bits.replayInst || current.replayInst 313 current.singleStep := s1_out_bits.singleStep || current.singleStep 314 current.trigger := (s1_out_bits.trigger.asUInt | current.trigger.asUInt).asTypeOf(new TriggerCf) 315 } 316 } 317 }.elsewhen (s1_out_valid && !s1_flush) { 318 currentValid := true.B 319 current := s1_out_bits 320 }.elsewhen (enq_valid && !(io.redirect.valid || io.flush)) { 321 currentValid := true.B 322 current := enq_bits 323 } 324 325 io.out.valid := s1_out_valid || enq_valid && enq_bits.can_writeback 326 io.out.bits := Mux(s1_out_valid, s1_out_bits, enq_bits) 327 io.state.valid := currentValid 328 io.state.bits := current 329 330} 331 332class RobFlushInfo(implicit p: Parameters) extends XSBundle { 333 val ftqIdx = new FtqPtr 334 val robIdx = new RobPtr 335 val ftqOffset = UInt(log2Up(PredictWidth).W) 336 val replayInst = Bool() 337} 338 339class Rob(params: BackendParams)(implicit p: Parameters) extends LazyModule with HasXSParameter { 340 override def shouldBeInlined: Boolean = false 341 342 lazy val module = new RobImp(this)(p, params) 343} 344 345class RobImp(override val wrapper: Rob)(implicit p: Parameters, params: BackendParams) extends LazyModuleImp(wrapper) 346 with HasXSParameter with HasCircularQueuePtrHelper with HasPerfEvents { 347 348 private val LduCnt = params.LduCnt 349 private val StaCnt = params.StaCnt 350 private val HyuCnt = params.HyuCnt 351 352 val io = IO(new Bundle() { 353 val hartId = Input(UInt(8.W)) 354 val redirect = Input(Valid(new Redirect)) 355 val enq = new RobEnqIO 356 val flushOut = ValidIO(new Redirect) 357 val exception = ValidIO(new ExceptionInfo) 358 // exu + brq 359 val writeback: MixedVec[ValidIO[ExuOutput]] = Flipped(params.genWrite2CtrlBundles) 360 val commits = Output(new RobCommitIO) 361 val rabCommits = Output(new RabCommitIO) 362 val diffCommits = if (backendParams.debugEn) Some(Output(new DiffCommitIO)) else None 363 val isVsetFlushPipe = Output(Bool()) 364 val vconfigPdest = Output(UInt(PhyRegIdxWidth.W)) 365 val lsq = new RobLsqIO 366 val robDeqPtr = Output(new RobPtr) 367 val csr = new RobCSRIO 368 val snpt = Input(new SnapshotPort) 369 val robFull = Output(Bool()) 370 val headNotReady = Output(Bool()) 371 val cpu_halt = Output(Bool()) 372 val wfi_enable = Input(Bool()) 373 val toDecode = new Bundle { 374 val vtype = ValidIO(VType()) 375 } 376 377 val debug_ls = Flipped(new DebugLSIO) 378 val debugRobHead = Output(new DynInst) 379 val debugEnqLsq = Input(new LsqEnqIO) 380 val debugHeadLsIssue = Input(Bool()) 381 val lsTopdownInfo = Vec(LduCnt + HyuCnt, Input(new LsTopdownInfo)) 382 val debugTopDown = new Bundle { 383 val toCore = new RobCoreTopDownIO 384 val toDispatch = new RobDispatchTopDownIO 385 val robHeadLqIdx = Valid(new LqPtr) 386 } 387 val debugRolling = new RobDebugRollingIO 388 }) 389 390 val exuWBs: Seq[ValidIO[ExuOutput]] = io.writeback.filter(!_.bits.params.hasStdFu).toSeq 391 val stdWBs: Seq[ValidIO[ExuOutput]] = io.writeback.filter(_.bits.params.hasStdFu).toSeq 392 val fflagsWBs = io.writeback.filter(x => x.bits.fflags.nonEmpty) 393 val exceptionWBs = io.writeback.filter(x => x.bits.exceptionVec.nonEmpty) 394 val redirectWBs = io.writeback.filter(x => x.bits.redirect.nonEmpty) 395 val vxsatWBs = io.writeback.filter(x => x.bits.vxsat.nonEmpty) 396 397 val numExuWbPorts = exuWBs.length 398 val numStdWbPorts = stdWBs.length 399 400 401 println(s"Rob: size $RobSize, numExuWbPorts: $numExuWbPorts, numStdWbPorts: $numStdWbPorts, commitwidth: $CommitWidth") 402// println(s"exuPorts: ${exuWbs.map(_._1.map(_.name))}") 403// println(s"stdPorts: ${stdWbs.map(_._1.map(_.name))}") 404// println(s"fflagsPorts: ${fflagsWBs.map(_._1.map(_.name))}") 405 406 407 // instvalid field 408 val valid = RegInit(VecInit(Seq.fill(RobSize)(false.B))) 409 // writeback status 410 411 val stdWritebacked = Reg(Vec(RobSize, Bool())) 412 val commitTrigger = Mem(RobSize, Bool()) 413 val uopNumVec = RegInit(VecInit(Seq.fill(RobSize)(0.U(log2Up(MaxUopSize + 1).W)))) 414 val realDestSize = RegInit(VecInit(Seq.fill(RobSize)(0.U(log2Up(MaxUopSize + 1).W)))) 415 val fflagsDataModule = RegInit(VecInit(Seq.fill(RobSize)(0.U(5.W)))) 416 val vxsatDataModule = RegInit(VecInit(Seq.fill(RobSize)(false.B))) 417 val vls = RegInit(VecInit(Seq.fill(RobSize)(false.B))) 418 419 val stdWritebackedDeqGroup = Reg(Vec(CommitWidth, Bool())) 420 val uopNumVecDeqGroup = RegInit(VecInit(Seq.fill(CommitWidth)(0.U(log2Up(MaxUopSize + 1).W)))) 421 val realDestSizeDeqGroup = RegInit(VecInit(Seq.fill(CommitWidth)(0.U(log2Up(MaxUopSize + 1).W)))) 422 val fflagsDataModuleDeqGroup = RegInit(VecInit(Seq.fill(CommitWidth)(0.U(5.W)))) 423 val vxsatDataModuleDeqGroup = RegInit(VecInit(Seq.fill(CommitWidth)(false.B))) 424 def isWritebacked(ptr: UInt): Bool = { 425 !uopNumVec(ptr).orR && stdWritebacked(ptr) 426 } 427 428 def isUopWritebacked(ptr: UInt): Bool = { 429 !uopNumVec(ptr).orR 430 } 431 432 val mmio = RegInit(VecInit(Seq.fill(RobSize)(false.B))) 433 434 // data for redirect, exception, etc. 435 val flagBkup = Mem(RobSize, Bool()) 436 // some instructions are not allowed to trigger interrupts 437 // They have side effects on the states of the processor before they write back 438 val interrupt_safe = RegInit(VecInit(Seq.fill(RobSize)(true.B))) 439 val interrupt_safeDeqGroup = Reg(Vec(CommitWidth, Bool())) 440 441 // data for debug 442 // Warn: debug_* prefix should not exist in generated verilog. 443 val debug_microOp = DebugMem(RobSize, new DynInst) 444 val debug_exuData = Reg(Vec(RobSize, UInt(XLEN.W)))//for debug 445 val debug_exuDebug = Reg(Vec(RobSize, new DebugBundle))//for debug 446 val debug_lsInfo = RegInit(VecInit(Seq.fill(RobSize)(DebugLsInfo.init))) 447 val debug_lsTopdownInfo = RegInit(VecInit(Seq.fill(RobSize)(LsTopdownInfo.init))) 448 val debug_lqIdxValid = RegInit(VecInit.fill(RobSize)(false.B)) 449 val debug_lsIssued = RegInit(VecInit.fill(RobSize)(false.B)) 450 451 // pointers 452 // For enqueue ptr, we don't duplicate it since only enqueue needs it. 453 val enqPtrVec = Wire(Vec(RenameWidth, new RobPtr)) 454 val deqPtrVec = Wire(Vec(CommitWidth, new RobPtr)) 455 456 if(backendParams.debugEn) { 457 dontTouch(enqPtrVec) 458 dontTouch(deqPtrVec) 459 } 460 461 val walkPtrVec = Reg(Vec(CommitWidth, new RobPtr)) 462 val lastWalkPtr = Reg(new RobPtr) 463 val allowEnqueue = RegInit(true.B) 464 465 val enqPtr = enqPtrVec.head 466 val deqPtr = deqPtrVec(0) 467 val walkPtr = walkPtrVec(0) 468 469 val isEmpty = enqPtr === deqPtr 470 val isReplaying = io.redirect.valid && RedirectLevel.flushItself(io.redirect.bits.level) 471 472 val snptEnq = io.enq.canAccept && io.enq.req.map(x => x.valid && x.bits.snapshot).reduce(_ || _) 473 val snapshotPtrVec = Wire(Vec(RenameWidth, new RobPtr)) 474 snapshotPtrVec(0) := io.enq.req(0).bits.robIdx 475 for (i <- 1 until RenameWidth) { 476 snapshotPtrVec(i) := snapshotPtrVec(0) + i.U 477 } 478 val snapshots = SnapshotGenerator(snapshotPtrVec, snptEnq, io.snpt.snptDeq, io.redirect.valid, io.snpt.flushVec) 479 val debug_lsIssue = WireDefault(debug_lsIssued) 480 debug_lsIssue(deqPtr.value) := io.debugHeadLsIssue 481 482 /** 483 * states of Rob 484 */ 485 val s_idle :: s_walk :: Nil = Enum(2) 486 val state = RegInit(s_idle) 487 488 /** 489 * Data Modules 490 * 491 * CommitDataModule: data from dispatch 492 * (1) read: commits/walk/exception 493 * (2) write: enqueue 494 * 495 * WritebackData: data from writeback 496 * (1) read: commits/walk/exception 497 * (2) write: write back from exe units 498 */ 499 val dispatchData = Module(new SyncDataModuleTemplate(new RobDispatchData, RobSize, CommitWidth, RenameWidth)) 500 val dispatchDataRead = dispatchData.io.rdata 501 502 val exceptionGen = Module(new ExceptionGen(params)) 503 val exceptionDataRead = exceptionGen.io.state 504 val fflagsDataRead = Wire(Vec(CommitWidth, UInt(5.W))) 505 val vxsatDataRead = Wire(Vec(CommitWidth, Bool())) 506 507 io.robDeqPtr := deqPtr 508 io.debugRobHead := debug_microOp(deqPtr.value) 509 510 val rab = Module(new RenameBuffer(RabSize)) 511 val vtypeBuffer = Module(new VTypeBuffer(VTypeBufferSize)) 512 513 /** 514 * connection of [[rab]] 515 */ 516 rab.io.redirect.valid := io.redirect.valid 517 518 rab.io.req.zip(io.enq.req).map { case (dest, src) => 519 dest.bits := src.bits 520 dest.valid := src.valid && io.enq.canAccept 521 } 522 523 val commitDestSizeSeq = (0 until CommitWidth).map(i => realDestSize(deqPtrVec(i).value)) 524 val walkDestSizeSeq = (0 until CommitWidth).map(i => realDestSize(walkPtrVec(i).value)) 525 526 val walkDestSizeDeqGroup = RegInit(VecInit(Seq.fill(CommitWidth)(0.U(log2Up(MaxUopSize + 1).W)))) 527 val commitSizeSumSeq = (0 until CommitWidth).map(i => realDestSizeDeqGroup.take(i+1).reduce(_ +& _)) 528 val walkSizeSumSeq = (0 until CommitWidth).map(i => walkDestSizeDeqGroup.take(i+1).reduce(_ +& _)) 529 val commitSizeSumCond = io.commits.commitValid.map(_ && io.commits.isCommit) 530 val walkSizeSumCond = io.commits.walkValid.map(_ && io.commits.isWalk) 531 val commitSizeSum = PriorityMuxDefault(commitSizeSumCond.reverse.zip(commitSizeSumSeq.reverse), 0.U) 532 val walkSizeSum = PriorityMuxDefault(walkSizeSumCond.reverse.zip(walkSizeSumSeq.reverse), 0.U) 533 534 rab.io.fromRob.commitSize := commitSizeSum 535 rab.io.fromRob.walkSize := walkSizeSum 536 rab.io.snpt := io.snpt 537 rab.io.snpt.snptEnq := snptEnq 538 539 io.rabCommits := rab.io.commits 540 io.diffCommits.foreach(_ := rab.io.diffCommits.get) 541 542 /** 543 * connection of [[vtypeBuffer]] 544 */ 545 546 vtypeBuffer.io.redirect.valid := io.redirect.valid 547 548 vtypeBuffer.io.req.zip(io.enq.req).map { case (sink, source) => 549 sink.valid := source.valid && io.enq.canAccept 550 sink.bits := source.bits 551 } 552 553 private val commitIsVTypeVec = VecInit(io.commits.commitValid.zip(io.commits.info).map { case (valid, info) => io.commits.isCommit && valid && info.isVset }) 554 private val walkIsVTypeVec = VecInit(io.commits.walkValid.zip(io.commits.info).map { case (valid, info) => io.commits.isWalk && valid && info.isVset }) 555 vtypeBuffer.io.fromRob.commitSize := PopCount(commitIsVTypeVec) 556 vtypeBuffer.io.fromRob.walkSize := PopCount(walkIsVTypeVec) 557 vtypeBuffer.io.snpt := io.snpt 558 vtypeBuffer.io.snpt.snptEnq := snptEnq 559 io.toDecode.vtype := vtypeBuffer.io.toDecode.vtype 560 561 /** 562 * Enqueue (from dispatch) 563 */ 564 // special cases 565 val hasBlockBackward = RegInit(false.B) 566 val hasWaitForward = RegInit(false.B) 567 val doingSvinval = RegInit(false.B) 568 // When blockBackward instruction leaves Rob (commit or walk), hasBlockBackward should be set to false.B 569 // To reduce registers usage, for hasBlockBackward cases, we allow enqueue after ROB is empty. 570 when (isEmpty) { hasBlockBackward:= false.B } 571 // When any instruction commits, hasNoSpecExec should be set to false.B 572 when (io.commits.hasWalkInstr || io.commits.hasCommitInstr) { hasWaitForward:= false.B } 573 574 // The wait-for-interrupt (WFI) instruction waits in the ROB until an interrupt might need servicing. 575 // io.csr.wfiEvent will be asserted if the WFI can resume execution, and we change the state to s_wfi_idle. 576 // It does not affect how interrupts are serviced. Note that WFI is noSpecExec and it does not trigger interrupts. 577 val hasWFI = RegInit(false.B) 578 io.cpu_halt := hasWFI 579 // WFI Timeout: 2^20 = 1M cycles 580 val wfi_cycles = RegInit(0.U(20.W)) 581 when (hasWFI) { 582 wfi_cycles := wfi_cycles + 1.U 583 }.elsewhen (!hasWFI && RegNext(hasWFI)) { 584 wfi_cycles := 0.U 585 } 586 val wfi_timeout = wfi_cycles.andR 587 when (RegNext(RegNext(io.csr.wfiEvent)) || io.flushOut.valid || wfi_timeout) { 588 hasWFI := false.B 589 } 590 591 val allocatePtrVec = VecInit((0 until RenameWidth).map(i => enqPtrVec(PopCount(io.enq.req.take(i).map(req => req.valid && req.bits.firstUop))))) 592 io.enq.canAccept := allowEnqueue && !hasBlockBackward && rab.io.canEnq && vtypeBuffer.io.canEnq 593 io.enq.resp := allocatePtrVec 594 val canEnqueue = VecInit(io.enq.req.map(req => req.valid && req.bits.firstUop && io.enq.canAccept)) 595 val timer = GTimer() 596 for (i <- 0 until RenameWidth) { 597 // we don't check whether io.redirect is valid here since redirect has higher priority 598 when (canEnqueue(i)) { 599 val enqUop = io.enq.req(i).bits 600 val enqIndex = allocatePtrVec(i).value 601 // store uop in data module and debug_microOp Vec 602 debug_microOp(enqIndex) := enqUop 603 debug_microOp(enqIndex).debugInfo.dispatchTime := timer 604 debug_microOp(enqIndex).debugInfo.enqRsTime := timer 605 debug_microOp(enqIndex).debugInfo.selectTime := timer 606 debug_microOp(enqIndex).debugInfo.issueTime := timer 607 debug_microOp(enqIndex).debugInfo.writebackTime := timer 608 debug_microOp(enqIndex).debugInfo.tlbFirstReqTime := timer 609 debug_microOp(enqIndex).debugInfo.tlbRespTime := timer 610 debug_lsInfo(enqIndex) := DebugLsInfo.init 611 debug_lsTopdownInfo(enqIndex) := LsTopdownInfo.init 612 debug_lqIdxValid(enqIndex) := false.B 613 debug_lsIssued(enqIndex) := false.B 614 615 when (enqUop.blockBackward) { 616 hasBlockBackward := true.B 617 } 618 when (enqUop.waitForward) { 619 hasWaitForward := true.B 620 } 621 val enqHasTriggerCanFire = io.enq.req(i).bits.trigger.getFrontendCanFire 622 val enqHasException = ExceptionNO.selectFrontend(enqUop.exceptionVec).asUInt.orR 623 // the begin instruction of Svinval enqs so mark doingSvinval as true to indicate this process 624 when(!enqHasTriggerCanFire && !enqHasException && enqUop.isSvinvalBegin(enqUop.flushPipe)) 625 { 626 doingSvinval := true.B 627 } 628 // the end instruction of Svinval enqs so clear doingSvinval 629 when(!enqHasTriggerCanFire && !enqHasException && enqUop.isSvinvalEnd(enqUop.flushPipe)) 630 { 631 doingSvinval := false.B 632 } 633 // when we are in the process of Svinval software code area , only Svinval.vma and end instruction of Svinval can appear 634 assert(!doingSvinval || (enqUop.isSvinval(enqUop.flushPipe) || enqUop.isSvinvalEnd(enqUop.flushPipe))) 635 when (enqUop.isWFI && !enqHasException && !enqHasTriggerCanFire) { 636 hasWFI := true.B 637 } 638 639 mmio(enqIndex) := false.B 640 641 vls(enqIndex) := enqUop.vlsInstr 642 } 643 } 644 val dispatchNum = Mux(io.enq.canAccept, PopCount(io.enq.req.map(req => req.valid && req.bits.firstUop)), 0.U) 645 io.enq.isEmpty := RegNext(isEmpty && !VecInit(io.enq.req.map(_.valid)).asUInt.orR) 646 647 when (!io.wfi_enable) { 648 hasWFI := false.B 649 } 650 // sel vsetvl's flush position 651 val vs_idle :: vs_waitVinstr :: vs_waitFlush :: Nil = Enum(3) 652 val vsetvlState = RegInit(vs_idle) 653 654 val firstVInstrFtqPtr = RegInit(0.U.asTypeOf(new FtqPtr)) 655 val firstVInstrFtqOffset = RegInit(0.U.asTypeOf(UInt(log2Up(PredictWidth).W))) 656 val firstVInstrRobIdx = RegInit(0.U.asTypeOf(new RobPtr)) 657 658 val enq0 = io.enq.req(0) 659 val enq0IsVset = enq0.bits.isVset && enq0.bits.lastUop && canEnqueue(0) 660 val enq0IsVsetFlush = enq0IsVset && enq0.bits.flushPipe 661 val enqIsVInstrVec = io.enq.req.zip(canEnqueue).map{case (req, fire) => FuType.isVArith(req.bits.fuType) && fire} 662 // for vs_idle 663 val firstVInstrIdle = PriorityMux(enqIsVInstrVec.zip(io.enq.req).drop(1) :+ (true.B, 0.U.asTypeOf(io.enq.req(0).cloneType))) 664 // for vs_waitVinstr 665 val enqIsVInstrOrVset = (enqIsVInstrVec(0) || enq0IsVset) +: enqIsVInstrVec.drop(1) 666 val firstVInstrWait = PriorityMux(enqIsVInstrOrVset, io.enq.req) 667 when(vsetvlState === vs_idle){ 668 firstVInstrFtqPtr := firstVInstrIdle.bits.ftqPtr 669 firstVInstrFtqOffset := firstVInstrIdle.bits.ftqOffset 670 firstVInstrRobIdx := firstVInstrIdle.bits.robIdx 671 }.elsewhen(vsetvlState === vs_waitVinstr){ 672 when(Cat(enqIsVInstrOrVset).orR){ 673 firstVInstrFtqPtr := firstVInstrWait.bits.ftqPtr 674 firstVInstrFtqOffset := firstVInstrWait.bits.ftqOffset 675 firstVInstrRobIdx := firstVInstrWait.bits.robIdx 676 } 677 } 678 679 val hasVInstrAfterI = Cat(enqIsVInstrVec(0)).orR 680 when(vsetvlState === vs_idle && !io.redirect.valid){ 681 when(enq0IsVsetFlush){ 682 vsetvlState := Mux(hasVInstrAfterI, vs_waitFlush, vs_waitVinstr) 683 } 684 }.elsewhen(vsetvlState === vs_waitVinstr){ 685 when(io.redirect.valid){ 686 vsetvlState := vs_idle 687 }.elsewhen(Cat(enqIsVInstrOrVset).orR){ 688 vsetvlState := vs_waitFlush 689 } 690 }.elsewhen(vsetvlState === vs_waitFlush){ 691 when(io.redirect.valid){ 692 vsetvlState := vs_idle 693 } 694 } 695 696 // lqEnq 697 io.debugEnqLsq.needAlloc.map(_(0)).zip(io.debugEnqLsq.req).foreach { case (alloc, req) => 698 when(io.debugEnqLsq.canAccept && alloc && req.valid) { 699 debug_microOp(req.bits.robIdx.value).lqIdx := req.bits.lqIdx 700 debug_lqIdxValid(req.bits.robIdx.value) := true.B 701 } 702 } 703 704 // lsIssue 705 when(io.debugHeadLsIssue) { 706 debug_lsIssued(deqPtr.value) := true.B 707 } 708 709 /** 710 * Writeback (from execution units) 711 */ 712 for (wb <- exuWBs) { 713 when (wb.valid) { 714 val wbIdx = wb.bits.robIdx.value 715 debug_exuData(wbIdx) := wb.bits.data 716 debug_exuDebug(wbIdx) := wb.bits.debug 717 debug_microOp(wbIdx).debugInfo.enqRsTime := wb.bits.debugInfo.enqRsTime 718 debug_microOp(wbIdx).debugInfo.selectTime := wb.bits.debugInfo.selectTime 719 debug_microOp(wbIdx).debugInfo.issueTime := wb.bits.debugInfo.issueTime 720 debug_microOp(wbIdx).debugInfo.writebackTime := wb.bits.debugInfo.writebackTime 721 722 // debug for lqidx and sqidx 723 debug_microOp(wbIdx).lqIdx := wb.bits.lqIdx.getOrElse(0.U.asTypeOf(new LqPtr)) 724 debug_microOp(wbIdx).sqIdx := wb.bits.sqIdx.getOrElse(0.U.asTypeOf(new SqPtr)) 725 726 val debug_Uop = debug_microOp(wbIdx) 727 XSInfo(true.B, 728 p"writebacked pc 0x${Hexadecimal(debug_Uop.pc)} wen ${debug_Uop.rfWen} " + 729 p"data 0x${Hexadecimal(wb.bits.data)} ldst ${debug_Uop.ldest} pdst ${debug_Uop.pdest} " + 730 p"skip ${wb.bits.debug.isMMIO} robIdx: ${wb.bits.robIdx}\n" 731 ) 732 } 733 } 734 735 val writebackNum = PopCount(exuWBs.map(_.valid)) 736 XSInfo(writebackNum =/= 0.U, "writebacked %d insts\n", writebackNum) 737 738 for (i <- 0 until LoadPipelineWidth) { 739 when (RegNext(io.lsq.mmio(i))) { 740 mmio(RegEnable(io.lsq.uop(i).robIdx, io.lsq.mmio(i)).value) := true.B 741 } 742 } 743 744 /** 745 * RedirectOut: Interrupt and Exceptions 746 */ 747 val deqDispatchData = dispatchDataRead(0) 748 val debug_deqUop = debug_microOp(deqPtr.value) 749 750 val intrBitSetReg = RegNext(io.csr.intrBitSet) 751 val intrEnable = intrBitSetReg && !hasWaitForward && interrupt_safeDeqGroup(0) 752 val deqHasExceptionOrFlush = exceptionDataRead.valid && exceptionDataRead.bits.robIdx === deqPtr 753 val deqHasException = deqHasExceptionOrFlush && (exceptionDataRead.bits.exceptionVec.asUInt.orR || 754 exceptionDataRead.bits.singleStep || exceptionDataRead.bits.trigger.canFire) 755 val deqHasFlushPipe = deqHasExceptionOrFlush && exceptionDataRead.bits.flushPipe 756 val deqHasReplayInst = deqHasExceptionOrFlush && exceptionDataRead.bits.replayInst 757 val exceptionEnable = isWritebacked(deqPtr.value) && deqHasException 758 759 XSDebug(deqHasException && exceptionDataRead.bits.singleStep, "Debug Mode: Deq has singlestep exception\n") 760 XSDebug(deqHasException && exceptionDataRead.bits.trigger.getFrontendCanFire, "Debug Mode: Deq has frontend trigger exception\n") 761 XSDebug(deqHasException && exceptionDataRead.bits.trigger.getBackendCanFire, "Debug Mode: Deq has backend trigger exception\n") 762 763 val isFlushPipe = isWritebacked(deqPtr.value) && (deqHasFlushPipe || deqHasReplayInst) 764 765 val isVsetFlushPipe = isWritebacked(deqPtr.value) && deqHasFlushPipe && exceptionDataRead.bits.isVset 766// val needModifyFtqIdxOffset = isVsetFlushPipe && (vsetvlState === vs_waitFlush) 767 val needModifyFtqIdxOffset = false.B 768 io.isVsetFlushPipe := isVsetFlushPipe 769 io.vconfigPdest := rab.io.vconfigPdest 770 // io.flushOut will trigger redirect at the next cycle. 771 // Block any redirect or commit at the next cycle. 772 val lastCycleFlush = RegNext(io.flushOut.valid) 773 774 io.flushOut.valid := (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable || isFlushPipe) && !lastCycleFlush 775 io.flushOut.bits := DontCare 776 io.flushOut.bits.isRVC := deqDispatchData.isRVC 777 io.flushOut.bits.robIdx := Mux(needModifyFtqIdxOffset, firstVInstrRobIdx, deqPtr) 778 io.flushOut.bits.ftqIdx := Mux(needModifyFtqIdxOffset, firstVInstrFtqPtr, deqDispatchData.ftqIdx) 779 io.flushOut.bits.ftqOffset := Mux(needModifyFtqIdxOffset, firstVInstrFtqOffset, deqDispatchData.ftqOffset) 780 io.flushOut.bits.level := Mux(deqHasReplayInst || intrEnable || exceptionEnable || needModifyFtqIdxOffset, RedirectLevel.flush, RedirectLevel.flushAfter) // TODO use this to implement "exception next" 781 io.flushOut.bits.interrupt := true.B 782 XSPerfAccumulate("interrupt_num", io.flushOut.valid && intrEnable) 783 XSPerfAccumulate("exception_num", io.flushOut.valid && exceptionEnable) 784 XSPerfAccumulate("flush_pipe_num", io.flushOut.valid && isFlushPipe) 785 XSPerfAccumulate("replay_inst_num", io.flushOut.valid && isFlushPipe && deqHasReplayInst) 786 787 val exceptionHappen = (state === s_idle) && valid(deqPtr.value) && (intrEnable || exceptionEnable) && !lastCycleFlush 788 io.exception.valid := RegNext(exceptionHappen) 789 io.exception.bits.pc := RegEnable(debug_deqUop.pc, exceptionHappen) 790 io.exception.bits.instr := RegEnable(debug_deqUop.instr, exceptionHappen) 791 io.exception.bits.commitType := RegEnable(deqDispatchData.commitType, exceptionHappen) 792 io.exception.bits.exceptionVec := RegEnable(exceptionDataRead.bits.exceptionVec, exceptionHappen) 793 io.exception.bits.singleStep := RegEnable(exceptionDataRead.bits.singleStep, exceptionHappen) 794 io.exception.bits.crossPageIPFFix := RegEnable(exceptionDataRead.bits.crossPageIPFFix, exceptionHappen) 795 io.exception.bits.isInterrupt := RegEnable(intrEnable, exceptionHappen) 796 io.exception.bits.vls := RegEnable(vls(deqPtr.value), exceptionHappen) 797 io.exception.bits.trigger := RegEnable(exceptionDataRead.bits.trigger, exceptionHappen) 798 io.csr.vstart.valid := RegEnable(exceptionDataRead.bits.vstartEn, false.B, exceptionHappen) 799 io.csr.vstart.bits := RegEnable(exceptionDataRead.bits.vstart, exceptionHappen) 800 801 XSDebug(io.flushOut.valid, 802 p"generate redirect: pc 0x${Hexadecimal(io.exception.bits.pc)} intr $intrEnable " + 803 p"excp $exceptionEnable flushPipe $isFlushPipe " + 804 p"Trap_target 0x${Hexadecimal(io.csr.trapTarget)} exceptionVec ${Binary(exceptionDataRead.bits.exceptionVec.asUInt)}\n") 805 806 807 /** 808 * Commits (and walk) 809 * They share the same width. 810 */ 811 val shouldWalkVec = VecInit(walkPtrVec.map(_ <= lastWalkPtr)) 812 val walkFinished = VecInit(walkPtrVec.map(_ >= lastWalkPtr)).asUInt.orR 813 rab.io.fromRob.walkEnd := state === s_walk && walkFinished 814 vtypeBuffer.io.fromRob.walkEnd := state === s_walk && walkFinished 815 816 require(RenameWidth <= CommitWidth) 817 818 // wiring to csr 819 val (wflags, dirtyFs) = (0 until CommitWidth).map(i => { 820 val v = io.commits.commitValid(i) 821 val info = io.commits.info(i) 822 (v & info.wflags, v & info.dirtyFs) 823 }).unzip 824 val fflags = Wire(Valid(UInt(5.W))) 825 fflags.valid := io.commits.isCommit && VecInit(wflags).asUInt.orR 826 fflags.bits := wflags.zip(fflagsDataRead).map({ 827 case (w, f) => Mux(w, f, 0.U) 828 }).reduce(_|_) 829 val dirty_fs = io.commits.isCommit && VecInit(dirtyFs).asUInt.orR 830 831 val vxsat = Wire(Valid(Bool())) 832 vxsat.valid := io.commits.isCommit && vxsat.bits 833 vxsat.bits := io.commits.commitValid.zip(vxsatDataRead).map { 834 case (valid, vxsat) => valid & vxsat 835 }.reduce(_ | _) 836 837 // when mispredict branches writeback, stop commit in the next 2 cycles 838 // TODO: don't check all exu write back 839 val misPredWb = Cat(VecInit(redirectWBs.map(wb => 840 wb.bits.redirect.get.bits.cfiUpdate.isMisPred && wb.bits.redirect.get.valid && wb.valid 841 ).toSeq)).orR 842 val misPredBlockCounter = Reg(UInt(3.W)) 843 misPredBlockCounter := Mux(misPredWb, 844 "b111".U, 845 misPredBlockCounter >> 1.U 846 ) 847 val misPredBlock = misPredBlockCounter(0) 848 val blockCommit = misPredBlock || isReplaying || lastCycleFlush || hasWFI || io.redirect.valid 849 850 io.commits.isWalk := state === s_walk 851 io.commits.isCommit := state === s_idle && !blockCommit 852 val walk_v = VecInit(walkPtrVec.map(ptr => valid(ptr.value))) 853 val commit_v = VecInit(deqPtrVec.map(ptr => valid(ptr.value))) 854 dontTouch(commit_v) 855 val commit_vDeqGroup = Reg(chiselTypeOf(walk_v)) 856 // store will be commited iff both sta & std have been writebacked 857 val commit_w = VecInit(deqPtrVec.map(ptr => isWritebacked(ptr.value) && commitTrigger(ptr.value))) 858 val commit_wDeqGroup = Reg(chiselTypeOf(walk_v)) 859 val commit_exception = exceptionDataRead.valid && !isAfter(exceptionDataRead.bits.robIdx, deqPtrVec.last) 860 val commit_block = VecInit((0 until CommitWidth).map(i => !commit_wDeqGroup(i))) 861 val allowOnlyOneCommit = commit_exception || intrBitSetReg 862 // for instructions that may block others, we don't allow them to commit 863 for (i <- 0 until CommitWidth) { 864 // defaults: state === s_idle and instructions commit 865 // when intrBitSetReg, allow only one instruction to commit at each clock cycle 866 val isBlocked = if (i != 0) Cat(commit_block.take(i)).orR || allowOnlyOneCommit else intrEnable || deqHasException || deqHasReplayInst 867 io.commits.commitValid(i) := commit_vDeqGroup(i) && commit_wDeqGroup(i) && !isBlocked 868 io.commits.info(i) := dispatchDataRead(i) 869 io.commits.robIdx(i) := deqPtrVec(i) 870 871 io.commits.walkValid(i) := shouldWalkVec(i) 872 when (state === s_walk) { 873 when (io.commits.isWalk && state === s_walk && shouldWalkVec(i)) { 874 XSError(!walk_v(i), s"The walking entry($i) should be valid\n") 875 } 876 } 877 878 XSInfo(io.commits.isCommit && io.commits.commitValid(i), 879 "retired pc %x wen %d ldest %d pdest %x data %x fflags: %b vxsat: %b\n", 880 debug_microOp(deqPtrVec(i).value).pc, 881 io.commits.info(i).rfWen, 882 io.commits.info(i).ldest, 883 io.commits.info(i).pdest, 884 debug_exuData(deqPtrVec(i).value), 885 fflagsDataRead(i), 886 vxsatDataRead(i) 887 ) 888 XSInfo(state === s_walk && io.commits.walkValid(i), "walked pc %x wen %d ldst %d data %x\n", 889 debug_microOp(walkPtrVec(i).value).pc, 890 io.commits.info(i).rfWen, 891 io.commits.info(i).ldest, 892 debug_exuData(walkPtrVec(i).value) 893 ) 894 } 895 if (env.EnableDifftest) { 896 io.commits.info.map(info => dontTouch(info.pc)) 897 } 898 899 // sync fflags/dirty_fs/vxsat to csr 900 io.csr.fflags := RegNext(fflags) 901 io.csr.dirty_fs := RegNext(dirty_fs) 902 io.csr.vxsat := RegNext(vxsat) 903 904 // sync v csr to csr 905 // for difftest 906 if(env.AlwaysBasicDiff || env.EnableDifftest) { 907 val isDiffWriteVconfigVec = io.diffCommits.get.commitValid.zip(io.diffCommits.get.info).map { case (valid, info) => valid && info.ldest === VCONFIG_IDX.U && info.vecWen }.reverse 908 io.csr.vcsrFlag := RegNext(io.diffCommits.get.isCommit && Cat(isDiffWriteVconfigVec).orR) 909 } 910 else{ 911 io.csr.vcsrFlag := false.B 912 } 913 914 // commit load/store to lsq 915 val ldCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.LOAD)) 916 val stCommitVec = VecInit((0 until CommitWidth).map(i => io.commits.commitValid(i) && io.commits.info(i).commitType === CommitType.STORE)) 917 val deqPtrVec_next = Wire(Vec(CommitWidth, Output(new RobPtr))) 918 io.lsq.lcommit := RegNext(Mux(io.commits.isCommit, PopCount(ldCommitVec), 0.U)) 919 io.lsq.scommit := RegNext(Mux(io.commits.isCommit, PopCount(stCommitVec), 0.U)) 920 // indicate a pending load or store 921 io.lsq.pendingld := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.LOAD && valid(deqPtr.value) && mmio(deqPtr.value)) 922 io.lsq.pendingst := RegNext(io.commits.isCommit && io.commits.info(0).commitType === CommitType.STORE && valid(deqPtr.value)) 923 io.lsq.commit := RegNext(io.commits.isCommit && io.commits.commitValid(0)) 924 io.lsq.pendingPtr := RegNext(deqPtr) 925 io.lsq.pendingPtrNext := RegNext(deqPtrVec_next.head) 926 927 /** 928 * state changes 929 * (1) redirect: switch to s_walk 930 * (2) walk: when walking comes to the end, switch to s_idle 931 */ 932 val state_next = Mux( 933 io.redirect.valid, s_walk, 934 Mux( 935 state === s_walk && walkFinished && rab.io.status.walkEnd && vtypeBuffer.io.status.walkEnd, s_idle, 936 state 937 ) 938 ) 939 XSPerfAccumulate("s_idle_to_idle", state === s_idle && state_next === s_idle) 940 XSPerfAccumulate("s_idle_to_walk", state === s_idle && state_next === s_walk) 941 XSPerfAccumulate("s_walk_to_idle", state === s_walk && state_next === s_idle) 942 XSPerfAccumulate("s_walk_to_walk", state === s_walk && state_next === s_walk) 943 state := state_next 944 945 /** 946 * pointers and counters 947 */ 948 val deqPtrGenModule = Module(new RobDeqPtrWrapper) 949 deqPtrGenModule.io.state := state 950 deqPtrGenModule.io.deq_v := commit_vDeqGroup 951 deqPtrGenModule.io.deq_w := commit_wDeqGroup 952 deqPtrGenModule.io.exception_state := exceptionDataRead 953 deqPtrGenModule.io.intrBitSetReg := intrBitSetReg 954 deqPtrGenModule.io.hasNoSpecExec := hasWaitForward 955 deqPtrGenModule.io.interrupt_safe := interrupt_safeDeqGroup(0) 956 deqPtrGenModule.io.blockCommit := blockCommit 957 deqPtrVec := deqPtrGenModule.io.out 958 deqPtrVec_next := deqPtrGenModule.io.next_out 959 960 val enqPtrGenModule = Module(new RobEnqPtrWrapper) 961 enqPtrGenModule.io.redirect := io.redirect 962 enqPtrGenModule.io.allowEnqueue := allowEnqueue && rab.io.canEnq 963 enqPtrGenModule.io.hasBlockBackward := hasBlockBackward 964 enqPtrGenModule.io.enq := VecInit(io.enq.req.map(req => req.valid && req.bits.firstUop)) 965 enqPtrVec := enqPtrGenModule.io.out 966 967 // next walkPtrVec: 968 // (1) redirect occurs: update according to state 969 // (2) walk: move forwards 970 val walkPtrVec_next = Mux(io.redirect.valid, 971 Mux(io.snpt.useSnpt, snapshots(io.snpt.snptSelect), deqPtrVec_next), 972 Mux(state === s_walk, VecInit(walkPtrVec.map(_ + CommitWidth.U)), walkPtrVec) 973 ) 974 walkPtrVec := walkPtrVec_next 975 walkDestSizeDeqGroup.zip(walkPtrVec_next).map{ 976 case (reg, ptrNext) => reg := realDestSize(ptrNext.value) 977 } 978 val numValidEntries = distanceBetween(enqPtr, deqPtr) 979 val commitCnt = PopCount(io.commits.commitValid) 980 981 allowEnqueue := numValidEntries + dispatchNum <= (RobSize - RenameWidth).U 982 983 val redirectWalkDistance = distanceBetween(io.redirect.bits.robIdx, deqPtrVec_next(0)) 984 when (io.redirect.valid) { 985 lastWalkPtr := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx - 1.U, io.redirect.bits.robIdx) 986 } 987 988 989 /** 990 * States 991 * We put all the stage bits changes here. 992 993 * All events: (1) enqueue (dispatch); (2) writeback; (3) cancel; (4) dequeue (commit); 994 * All states: (1) valid; (2) writebacked; (3) flagBkup 995 */ 996 997 // update commit_vDeqGroup 998 val deqPtrValue = Wire(Vec(2 * CommitWidth, new RobPtr)) 999 deqPtrValue.zipWithIndex.map{case (deq, i) => deq := deqPtrVec(0) + i.U} 1000 val commit_vReadVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(commit_v(0)))) 1001 val commit_vNextVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(commit_v(0)))) 1002 dontTouch(commit_vDeqGroup) 1003 dontTouch(commit_vReadVec) 1004 dontTouch(commit_vNextVec) 1005 dontTouch(deqPtrValue) 1006 for (i <- 0 until 2 * CommitWidth) { 1007 commit_vReadVec(i) := valid(deqPtrValue(i).value) 1008 commit_vNextVec(i) := commit_vReadVec(i) 1009 } 1010 (0 until CommitWidth).map { case i => 1011 val nextVec = commit_vNextVec 1012 val commitEn = deqPtrGenModule.io.commitEn 1013 val canCommitPriorityCond = deqPtrGenModule.io.canCommitPriorityCond 1014 val commit_wNextThis = nextVec.drop(i).take(CommitWidth+1) 1015 val originValue = nextVec(i) 1016 val ifCommitEnValue = PriorityMuxDefault(canCommitPriorityCond.zip(commit_wNextThis), originValue) 1017 commit_vDeqGroup(i) := Mux(commitEn, ifCommitEnValue, originValue) 1018 } 1019 // update commit_wDeqGroup 1020 val commit_wReadVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(commit_w(0)))) 1021 val commit_wNextVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(commit_w(0)))) 1022 dontTouch(commit_wDeqGroup) 1023 dontTouch(commit_wReadVec) 1024 dontTouch(commit_wNextVec) 1025 dontTouch(commit_w) 1026 for (i <- 0 until 2 * CommitWidth) { 1027 commit_wReadVec(i) := isWritebacked(deqPtrValue(i).value) 1028 commit_wNextVec(i) := commit_vReadVec(i) 1029 } 1030 (0 until CommitWidth).map { case i => 1031 val nextVec = commit_wNextVec 1032 val commitEn = deqPtrGenModule.io.commitEn 1033 val canCommitPriorityCond = deqPtrGenModule.io.canCommitPriorityCond 1034 val commit_wNextThis = nextVec.drop(i).take(CommitWidth+1) 1035 val originValue = nextVec(i) 1036 val ifCommitEnValue = PriorityMuxDefault(canCommitPriorityCond.zip(commit_wNextThis),originValue) 1037 commit_wDeqGroup(i) := Mux(commitEn, ifCommitEnValue, originValue) 1038 } 1039 val commitReadAddr = Mux(state === s_idle, VecInit(deqPtrVec.map(_.value)), VecInit(walkPtrVec.map(_.value))) 1040 1041 // redirect logic writes 6 valid 1042 val redirectHeadVec = Reg(Vec(RenameWidth, new RobPtr)) 1043 val redirectTail = Reg(new RobPtr) 1044 val redirectIdle :: redirectBusy :: Nil = Enum(2) 1045 val redirectState = RegInit(redirectIdle) 1046 val invMask = redirectHeadVec.map(redirectHead => isBefore(redirectHead, redirectTail)) 1047 when(redirectState === redirectBusy) { 1048 redirectHeadVec.foreach(redirectHead => redirectHead := redirectHead + RenameWidth.U) 1049 redirectHeadVec zip invMask foreach { 1050 case (redirectHead, inv) => when(inv) { 1051 valid(redirectHead.value) := false.B 1052 for (j <- 0 until 2 * CommitWidth) { 1053 when(redirectHead.value === deqPtrValue(j).value) { 1054 commit_vNextVec(j) := false.B 1055 } 1056 } 1057 } 1058 } 1059 when(!invMask.last) { 1060 redirectState := redirectIdle 1061 } 1062 } 1063 when(io.redirect.valid) { 1064 redirectState := redirectBusy 1065 when(redirectState === redirectIdle) { 1066 redirectTail := enqPtr 1067 } 1068 redirectHeadVec.zipWithIndex.foreach { case (redirectHead, i) => 1069 redirectHead := Mux(io.redirect.bits.flushItself(), io.redirect.bits.robIdx + i.U, io.redirect.bits.robIdx + (i + 1).U) 1070 } 1071 } 1072 // enqueue logic writes 6 valid 1073 for (i <- 0 until RenameWidth) { 1074 when (canEnqueue(i) && !io.redirect.valid) { 1075 valid(allocatePtrVec(i).value) := true.B 1076 for (j <- 0 until 2*CommitWidth) { 1077 when(allocatePtrVec(i).value === deqPtrValue(j).value){ 1078 commit_vNextVec(j) := true.B 1079 } 1080 } 1081 } 1082 } 1083 // dequeue logic writes 6 valid 1084 for (i <- 0 until CommitWidth) { 1085 val commitValid = io.commits.isCommit && io.commits.commitValid(i) 1086 when (commitValid) { 1087 valid(commitReadAddr(i)) := false.B 1088 for (j <- 0 until 2 * CommitWidth) { 1089 when(commitReadAddr(i) === deqPtrValue(j).value) { 1090 commit_vNextVec(j) := false.B 1091 } 1092 } 1093 } 1094 } 1095 1096 // debug_inst update 1097 for(i <- 0 until (LduCnt + StaCnt)) { 1098 debug_lsInfo(io.debug_ls.debugLsInfo(i).s1_robIdx).s1SignalEnable(io.debug_ls.debugLsInfo(i)) 1099 debug_lsInfo(io.debug_ls.debugLsInfo(i).s2_robIdx).s2SignalEnable(io.debug_ls.debugLsInfo(i)) 1100 } 1101 for (i <- 0 until LduCnt) { 1102 debug_lsTopdownInfo(io.lsTopdownInfo(i).s1.robIdx).s1SignalEnable(io.lsTopdownInfo(i)) 1103 debug_lsTopdownInfo(io.lsTopdownInfo(i).s2.robIdx).s2SignalEnable(io.lsTopdownInfo(i)) 1104 } 1105 1106 // status field: writebacked 1107 // enqueue logic set 6 writebacked to false 1108 for (i <- 0 until RenameWidth) { 1109 when(canEnqueue(i)) { 1110 val enqHasException = ExceptionNO.selectFrontend(io.enq.req(i).bits.exceptionVec).asUInt.orR 1111 val enqHasTriggerCanFire = io.enq.req(i).bits.trigger.getFrontendCanFire 1112 val enqIsWritebacked = io.enq.req(i).bits.eliminatedMove 1113 val isStu = FuType.isStore(io.enq.req(i).bits.fuType) 1114 commitTrigger(allocatePtrVec(i).value) := enqIsWritebacked && !enqHasException && !enqHasTriggerCanFire && !isStu 1115 } 1116 } 1117 when(exceptionGen.io.out.valid) { 1118 val wbIdx = exceptionGen.io.out.bits.robIdx.value 1119 commitTrigger(wbIdx) := true.B 1120 } 1121 1122 // writeback logic set numWbPorts writebacked to true 1123 val blockWbSeq = Wire(Vec(exuWBs.length, Bool())) 1124 blockWbSeq.map(_ := false.B) 1125 for ((wb, blockWb) <- exuWBs.zip(blockWbSeq)) { 1126 when(wb.valid) { 1127 val wbIdx = wb.bits.robIdx.value 1128 val wbHasException = wb.bits.exceptionVec.getOrElse(0.U).asUInt.orR 1129 val wbHasTriggerCanFire = wb.bits.trigger.getOrElse(0.U).asTypeOf(io.enq.req(0).bits.trigger).getBackendCanFire //Todo: wb.bits.trigger.getHitBackend 1130 val wbHasFlushPipe = wb.bits.flushPipe.getOrElse(false.B) 1131 val wbHasReplayInst = wb.bits.replay.getOrElse(false.B) //Todo: && wb.bits.replayInst 1132 blockWb := wbHasException || wbHasFlushPipe || wbHasReplayInst || wbHasTriggerCanFire 1133 commitTrigger(wbIdx) := !blockWb 1134 } 1135 } 1136 1137 // if the first uop of an instruction is valid , write writebackedCounter 1138 val uopEnqValidSeq = io.enq.req.map(req => io.enq.canAccept && req.valid) 1139 val instEnqValidSeq = io.enq.req.map (req => io.enq.canAccept && req.valid && req.bits.firstUop) 1140 val enqNeedWriteRFSeq = io.enq.req.map(_.bits.needWriteRf) 1141 val enqRobIdxSeq = io.enq.req.map(req => req.bits.robIdx.value) 1142 val enqUopNumVec = VecInit(io.enq.req.map(req => req.bits.numUops)) 1143 val enqWBNumVec = VecInit(io.enq.req.map(req => req.bits.numWB)) 1144 val enqEliminatedMoveVec = VecInit(io.enq.req.map(req => req.bits.eliminatedMove)) 1145 1146 private val enqWriteStdVec: Vec[Bool] = VecInit(io.enq.req.map { 1147 req => FuType.isAMO(req.bits.fuType) || FuType.isStore(req.bits.fuType) 1148 }) 1149 val fflags_wb = fflagsWBs 1150 val vxsat_wb = vxsatWBs 1151 for(i <- 0 until RobSize){ 1152 1153 val robIdxMatchSeq = io.enq.req.map(_.bits.robIdx.value === i.U) 1154 val uopCanEnqSeq = uopEnqValidSeq.zip(robIdxMatchSeq).map{ case(valid, isMatch) => valid && isMatch } 1155 val instCanEnqSeq = instEnqValidSeq.zip(robIdxMatchSeq).map{ case(valid, isMatch) => valid && isMatch } 1156 val instCanEnqFlag = Cat(instCanEnqSeq).orR 1157 1158 realDestSize(i) := Mux(!valid(i) && instCanEnqFlag || valid(i), realDestSize(i) + PopCount(enqNeedWriteRFSeq.zip(uopCanEnqSeq).map{ case(writeFlag, valid) => writeFlag && valid }), 0.U) 1159 1160 val enqUopNum = PriorityMux(instCanEnqSeq, enqUopNumVec) 1161 val enqWBNum = PriorityMux(instCanEnqSeq, enqWBNumVec) 1162 val enqEliminatedMove = PriorityMux(instCanEnqSeq, enqEliminatedMoveVec) 1163 val enqWriteStd = PriorityMux(instCanEnqSeq, enqWriteStdVec) 1164 1165 val canWbSeq = exuWBs.map(writeback => writeback.valid && writeback.bits.robIdx.value === i.U) 1166 val canWbNoBlockSeq = canWbSeq.zip(blockWbSeq).map{ case(canWb, blockWb) => canWb && !blockWb } 1167 val canStdWbSeq = VecInit(stdWBs.map(writeback => writeback.valid && writeback.bits.robIdx.value === i.U)) 1168 val wbCnt = PopCount(canWbNoBlockSeq) 1169 1170 val exceptionHas = RegInit(false.B) 1171 val exceptionHasWire = Wire(Bool()) 1172 exceptionHasWire := MuxCase(exceptionHas, Seq( 1173 (valid(i) && exceptionGen.io.out.valid && exceptionGen.io.out.bits.robIdx.value === i.U) -> true.B, 1174 !valid(i) -> false.B 1175 )) 1176 exceptionHas := exceptionHasWire 1177 1178 when (exceptionHas || exceptionHasWire) { 1179 // exception flush 1180 uopNumVec(i) := 0.U 1181 stdWritebacked(i) := true.B 1182 for (j <- 0 until 2 * CommitWidth) { 1183 when(i.U === deqPtrValue(j).value) { 1184 commit_wNextVec(j) := true.B 1185 } 1186 } 1187 }.elsewhen(!valid(i) && instCanEnqFlag) { 1188 // enq set num of uops 1189 uopNumVec(i) := enqWBNum 1190 stdWritebacked(i) := Mux(enqWriteStd, false.B, true.B) 1191 for (j <- 0 until 2 * CommitWidth) { 1192 when(i.U === deqPtrValue(j).value) { 1193 commit_wNextVec(j) := !enqUopNum.orR && Mux(enqWriteStd, false.B, true.B) 1194 } 1195 } 1196 }.elsewhen(valid(i)) { 1197 // update by writing back 1198 uopNumVec(i) := uopNumVec(i) - wbCnt 1199 assert(!(uopNumVec(i) - wbCnt > uopNumVec(i)), "Overflow!") 1200 for (j <- 0 until 2 * CommitWidth) { 1201 when(i.U === deqPtrValue(j).value) { 1202 commit_wNextVec(j) := (uopNumVec(i) === wbCnt) && stdWritebacked(i) 1203 } 1204 } 1205 when (canStdWbSeq.asUInt.orR) { 1206 stdWritebacked(i) := true.B 1207 for (j <- 0 until 2 * CommitWidth) { 1208 when(i.U === deqPtrValue(j).value) { 1209 commit_wNextVec(j) := uopNumVec(i) === wbCnt 1210 } 1211 } 1212 } 1213 }.otherwise { 1214 uopNumVec(i) := 0.U 1215 for (j <- 0 until 2 * CommitWidth) { 1216 when(i.U === deqPtrValue(j).value) { 1217 commit_wNextVec(j) := stdWritebacked(i) 1218 } 1219 } 1220 } 1221 1222 val fflagsCanWbSeq = fflags_wb.map(writeback => writeback.valid && writeback.bits.robIdx.value === i.U && writeback.bits.wflags.getOrElse(false.B)) 1223 val fflagsRes = fflagsCanWbSeq.zip(fflags_wb).map { case (canWb, wb) => Mux(canWb, wb.bits.fflags.get, 0.U) }.fold(false.B)(_ | _) 1224 fflagsDataModule(i) := Mux(!valid(i) && instCanEnqFlag, 0.U, fflagsDataModule(i) | fflagsRes) 1225 1226 val vxsatCanWbSeq = vxsat_wb.map(writeback => writeback.valid && writeback.bits.robIdx.value === i.U) 1227 val vxsatRes = vxsatCanWbSeq.zip(vxsat_wb).map { case (canWb, wb) => Mux(canWb, wb.bits.vxsat.get, 0.U) }.fold(false.B)(_ | _) 1228 vxsatDataModule(i) := Mux(!valid(i) && instCanEnqFlag, 0.U, vxsatDataModule(i) | vxsatRes) 1229 } 1230 // update uopNumVecDeqGroup 1231 val realDestSizeReadVec = Wire(Vec(2*CommitWidth, chiselTypeOf(realDestSize(0)))) 1232 val realDestSizeNextVec = Wire(Vec(2*CommitWidth, chiselTypeOf(realDestSize(0)))) 1233 for(i <- 0 until 2*CommitWidth) { 1234 val robIdxMatchSeq = io.enq.req.map(_.bits.robIdx.value === deqPtrValue(i).value) 1235 val uopCanEnqSeq = uopEnqValidSeq.zip(robIdxMatchSeq).map { case (valid, isMatch) => valid && isMatch } 1236 val instCanEnqSeq = instEnqValidSeq.zip(robIdxMatchSeq).map { case (valid, isMatch) => valid && isMatch } 1237 val instCanEnqFlag = Cat(instCanEnqSeq).orR 1238 realDestSizeReadVec(i) := realDestSize(deqPtrValue(i).value) 1239 realDestSizeNextVec(i) := Mux(valid(deqPtrValue(i).value) || instCanEnqFlag, realDestSizeReadVec(i) + PopCount(enqNeedWriteRFSeq.zip(uopCanEnqSeq).map { case (writeFlag, valid) => writeFlag && valid }), 0.U) 1240 } 1241 (0 until CommitWidth).map{ case i => 1242 val nextVec = realDestSizeNextVec 1243 val commitEn = deqPtrGenModule.io.commitEn 1244 val canCommitPriorityCond = deqPtrGenModule.io.canCommitPriorityCond 1245 val commit_wNextThis = nextVec.drop(i).take(CommitWidth+1) 1246 val originValue = nextVec(i) 1247 val ifCommitEnValue = PriorityMuxDefault(canCommitPriorityCond.zip(commit_wNextThis), originValue) 1248 realDestSizeDeqGroup(i) := Mux(commitEn, ifCommitEnValue, originValue) 1249 } 1250 // flagBkup 1251 // enqueue logic set 6 flagBkup at most 1252 for (i <- 0 until RenameWidth) { 1253 when (canEnqueue(i)) { 1254 flagBkup(allocatePtrVec(i).value) := allocatePtrVec(i).flag 1255 } 1256 } 1257 1258 // interrupt_safe 1259 1260 val interrupt_safeReadVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(interrupt_safe(0)))) 1261 val interrupt_safeNextVec = Wire(Vec(2 * CommitWidth, chiselTypeOf(interrupt_safe(0)))) 1262 dontTouch(interrupt_safeDeqGroup) 1263 dontTouch(interrupt_safeReadVec) 1264 dontTouch(interrupt_safeNextVec) 1265 for (i <- 0 until 2 * CommitWidth) { 1266 interrupt_safeReadVec(i) := interrupt_safe(deqPtrValue(i).value) 1267 interrupt_safeNextVec(i) := interrupt_safeReadVec(i) 1268 } 1269 (0 until CommitWidth).map { case i => 1270 val nextVec = interrupt_safeNextVec 1271 val commitEn = deqPtrGenModule.io.commitEn 1272 val canCommitPriorityCond = deqPtrGenModule.io.canCommitPriorityCond 1273 val commit_wNextThis = nextVec.drop(i).take(CommitWidth+1) 1274 val originValue = nextVec(i) 1275 val ifCommitEnValue = PriorityMuxDefault(canCommitPriorityCond.zip(commit_wNextThis), originValue) 1276 interrupt_safeDeqGroup(i) := Mux(commitEn, ifCommitEnValue, originValue) 1277 } 1278 for (i <- 0 until RenameWidth) { 1279 // We RegNext the updates for better timing. 1280 // Note that instructions won't change the system's states in this cycle. 1281 when (RegNext(canEnqueue(i))) { 1282 // For now, we allow non-load-store instructions to trigger interrupts 1283 // For MMIO instructions, they should not trigger interrupts since they may 1284 // be sent to lower level before it writes back. 1285 // However, we cannot determine whether a load/store instruction is MMIO. 1286 // Thus, we don't allow load/store instructions to trigger an interrupt. 1287 // TODO: support non-MMIO load-store instructions to trigger interrupts 1288 val allow_interrupts = !CommitType.isLoadStore(io.enq.req(i).bits.commitType) 1289 interrupt_safe(RegEnable(allocatePtrVec(i).value, canEnqueue(i))) := RegEnable(allow_interrupts, canEnqueue(i)) 1290 for (j <- 0 until 2 * CommitWidth) { 1291 when(RegNext(allocatePtrVec(i).value) === deqPtrValue(j).value) { 1292 interrupt_safeNextVec(j) := RegNext(allow_interrupts) 1293 } 1294 } 1295 } 1296 } 1297 1298 /** 1299 * read and write of data modules 1300 */ 1301 val commitReadAddr_next = Mux(state_next === s_idle, 1302 VecInit(deqPtrVec_next.map(_.value)), 1303 VecInit(walkPtrVec_next.map(_.value)) 1304 ) 1305 dispatchData.io.wen := canEnqueue 1306 dispatchData.io.waddr := allocatePtrVec.map(_.value) 1307 dispatchData.io.wdata.zip(io.enq.req.map(_.bits)).zipWithIndex.foreach { case ((wdata, req), portIdx) => 1308 wdata.ldest := req.ldest 1309 wdata.rfWen := req.rfWen 1310 wdata.dirtyFs := req.dirtyFs 1311 wdata.vecWen := req.vecWen 1312 wdata.wflags := req.wfflags 1313 wdata.commitType := req.commitType 1314 wdata.pdest := req.pdest 1315 wdata.ftqIdx := req.ftqPtr 1316 wdata.ftqOffset := req.ftqOffset 1317 wdata.isMove := req.eliminatedMove 1318 wdata.isRVC := req.preDecodeInfo.isRVC 1319 wdata.pc := req.pc 1320 wdata.vtype := req.vpu.vtype 1321 wdata.isVset := req.isVset 1322 wdata.instrSize := req.instrSize 1323 } 1324 dispatchData.io.raddr := commitReadAddr_next 1325 1326 exceptionGen.io.redirect <> io.redirect 1327 exceptionGen.io.flush := io.flushOut.valid 1328 1329 val canEnqueueEG = VecInit(io.enq.req.map(req => req.valid && io.enq.canAccept)) 1330 for (i <- 0 until RenameWidth) { 1331 exceptionGen.io.enq(i).valid := canEnqueueEG(i) 1332 exceptionGen.io.enq(i).bits.robIdx := io.enq.req(i).bits.robIdx 1333 exceptionGen.io.enq(i).bits.exceptionVec := ExceptionNO.selectFrontend(io.enq.req(i).bits.exceptionVec) 1334 exceptionGen.io.enq(i).bits.flushPipe := io.enq.req(i).bits.flushPipe 1335 exceptionGen.io.enq(i).bits.isVset := io.enq.req(i).bits.isVset 1336 exceptionGen.io.enq(i).bits.replayInst := false.B 1337 XSError(canEnqueue(i) && io.enq.req(i).bits.replayInst, "enq should not set replayInst") 1338 exceptionGen.io.enq(i).bits.singleStep := io.enq.req(i).bits.singleStep 1339 exceptionGen.io.enq(i).bits.crossPageIPFFix := io.enq.req(i).bits.crossPageIPFFix 1340 exceptionGen.io.enq(i).bits.trigger.clear() 1341 exceptionGen.io.enq(i).bits.trigger.frontendHit := io.enq.req(i).bits.trigger.frontendHit 1342 exceptionGen.io.enq(i).bits.trigger.frontendCanFire := io.enq.req(i).bits.trigger.frontendCanFire 1343 exceptionGen.io.enq(i).bits.vstartEn := false.B //DontCare 1344 exceptionGen.io.enq(i).bits.vstart := 0.U //DontCare 1345 } 1346 1347 println(s"ExceptionGen:") 1348 println(s"num of exceptions: ${params.numException}") 1349 require(exceptionWBs.length == exceptionGen.io.wb.length, 1350 f"exceptionWBs.length: ${exceptionWBs.length}, " + 1351 f"exceptionGen.io.wb.length: ${exceptionGen.io.wb.length}") 1352 for (((wb, exc_wb), i) <- exceptionWBs.zip(exceptionGen.io.wb).zipWithIndex) { 1353 exc_wb.valid := wb.valid 1354 exc_wb.bits.robIdx := wb.bits.robIdx 1355 exc_wb.bits.exceptionVec := wb.bits.exceptionVec.get 1356 exc_wb.bits.flushPipe := wb.bits.flushPipe.getOrElse(false.B) 1357 exc_wb.bits.isVset := false.B 1358 exc_wb.bits.replayInst := wb.bits.replay.getOrElse(false.B) 1359 exc_wb.bits.singleStep := false.B 1360 exc_wb.bits.crossPageIPFFix := false.B 1361 // TODO: make trigger configurable 1362 val trigger = wb.bits.trigger.getOrElse(0.U).asTypeOf(exc_wb.bits.trigger) 1363 exc_wb.bits.trigger.clear() // Don't care frontend timing, chain, hit and canFire 1364 exc_wb.bits.trigger.backendHit := trigger.backendHit 1365 exc_wb.bits.trigger.backendCanFire := trigger.backendCanFire 1366 exc_wb.bits.vstartEn := false.B //wb.bits.vstartEn.getOrElse(false.B) // todo need add vstart in ExuOutput 1367 exc_wb.bits.vstart := 0.U //wb.bits.vstart.getOrElse(0.U) 1368// println(s" [$i] ${configs.map(_.name)}: exception ${exceptionCases(i)}, " + 1369// s"flushPipe ${configs.exists(_.flushPipe)}, " + 1370// s"replayInst ${configs.exists(_.replayInst)}") 1371 } 1372 1373 fflagsDataRead := (0 until CommitWidth).map(i => fflagsDataModule(deqPtrVec(i).value)) 1374 vxsatDataRead := (0 until CommitWidth).map(i => vxsatDataModule(deqPtrVec(i).value)) 1375 1376 val instrCntReg = RegInit(0.U(64.W)) 1377 val fuseCommitCnt = PopCount(io.commits.commitValid.zip(io.commits.info).map{ case (v, i) => RegNext(v && CommitType.isFused(i.commitType)) }) 1378 val trueCommitCnt = RegNext(io.commits.commitValid.zip(io.commits.info).map{ case (v, i) => Mux(v, i.instrSize, 0.U) }.reduce(_ +& _)) +& fuseCommitCnt 1379 val retireCounter = Mux(RegNext(io.commits.isCommit), trueCommitCnt, 0.U) 1380 val instrCnt = instrCntReg + retireCounter 1381 instrCntReg := instrCnt 1382 io.csr.perfinfo.retiredInstr := retireCounter 1383 io.robFull := !allowEnqueue 1384 io.headNotReady := commit_vDeqGroup.head && !commit_wDeqGroup.head 1385 1386 /** 1387 * debug info 1388 */ 1389 XSDebug(p"enqPtr ${enqPtr} deqPtr ${deqPtr}\n") 1390 XSDebug("") 1391 XSError(isBefore(enqPtr, deqPtr) && !isFull(enqPtr, deqPtr), "\ndeqPtr is older than enqPtr!\n") 1392 for(i <- 0 until RobSize) { 1393 XSDebug(false, !valid(i), "-") 1394 XSDebug(false, valid(i) && isWritebacked(i.U), "w") 1395 XSDebug(false, valid(i) && !isWritebacked(i.U), "v") 1396 } 1397 XSDebug(false, true.B, "\n") 1398 1399 for(i <- 0 until RobSize) { 1400 if (i % 4 == 0) XSDebug("") 1401 XSDebug(false, true.B, "%x ", debug_microOp(i).pc) 1402 XSDebug(false, !valid(i), "- ") 1403 XSDebug(false, valid(i) && isWritebacked(i.U), "w ") 1404 XSDebug(false, valid(i) && !isWritebacked(i.U), "v ") 1405 if (i % 4 == 3) XSDebug(false, true.B, "\n") 1406 } 1407 1408 def ifCommit(counter: UInt): UInt = Mux(io.commits.isCommit, counter, 0.U) 1409 def ifCommitReg(counter: UInt): UInt = Mux(RegNext(io.commits.isCommit), counter, 0.U) 1410 1411 val commitDebugUop = deqPtrVec.map(_.value).map(debug_microOp(_)) 1412 XSPerfAccumulate("clock_cycle", 1.U) 1413 QueuePerf(RobSize, numValidEntries, numValidEntries === RobSize.U) 1414 XSPerfAccumulate("commitUop", ifCommit(commitCnt)) 1415 XSPerfAccumulate("commitInstr", ifCommitReg(trueCommitCnt)) 1416 XSPerfRolling("ipc", ifCommitReg(trueCommitCnt), 1000, clock, reset) 1417 XSPerfRolling("cpi", perfCnt = 1.U/*Cycle*/, eventTrigger = ifCommitReg(trueCommitCnt), granularity = 1000, clock, reset) 1418 val commitIsMove = commitDebugUop.map(_.isMove) 1419 XSPerfAccumulate("commitInstrMove", ifCommit(PopCount(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m }))) 1420 val commitMoveElim = commitDebugUop.map(_.debugInfo.eliminatedMove) 1421 XSPerfAccumulate("commitInstrMoveElim", ifCommit(PopCount(io.commits.commitValid zip commitMoveElim map { case (v, e) => v && e }))) 1422 XSPerfAccumulate("commitInstrFused", ifCommitReg(fuseCommitCnt)) 1423 val commitIsLoad = io.commits.info.map(_.commitType).map(_ === CommitType.LOAD) 1424 val commitLoadValid = io.commits.commitValid.zip(commitIsLoad).map{ case (v, t) => v && t } 1425 XSPerfAccumulate("commitInstrLoad", ifCommit(PopCount(commitLoadValid))) 1426 val commitIsBranch = io.commits.info.map(_.commitType).map(_ === CommitType.BRANCH) 1427 val commitBranchValid = io.commits.commitValid.zip(commitIsBranch).map{ case (v, t) => v && t } 1428 XSPerfAccumulate("commitInstrBranch", ifCommit(PopCount(commitBranchValid))) 1429 val commitLoadWaitBit = commitDebugUop.map(_.loadWaitBit) 1430 XSPerfAccumulate("commitInstrLoadWait", ifCommit(PopCount(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w }))) 1431 val commitIsStore = io.commits.info.map(_.commitType).map(_ === CommitType.STORE) 1432 XSPerfAccumulate("commitInstrStore", ifCommit(PopCount(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t }))) 1433 XSPerfAccumulate("writeback", PopCount((0 until RobSize).map(i => valid(i) && isWritebacked(i.U)))) 1434 // XSPerfAccumulate("enqInstr", PopCount(io.dp1Req.map(_.fire))) 1435 // XSPerfAccumulate("d2rVnR", PopCount(io.dp1Req.map(p => p.valid && !p.ready))) 1436 XSPerfAccumulate("walkInstr", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U)) 1437 XSPerfAccumulate("walkCycleTotal", state === s_walk) 1438 XSPerfAccumulate("waitRabWalkEnd", state === s_walk && walkFinished && !rab.io.status.walkEnd) 1439 private val walkCycle = RegInit(0.U(8.W)) 1440 private val waitRabWalkCycle = RegInit(0.U(8.W)) 1441 walkCycle := Mux(io.redirect.valid, 0.U, Mux(state === s_walk, walkCycle + 1.U, 0.U)) 1442 waitRabWalkCycle := Mux(state === s_walk && walkFinished, 0.U, Mux(state === s_walk, walkCycle + 1.U, 0.U)) 1443 1444 XSPerfHistogram("walkRobCycleHist", walkCycle, state === s_walk && walkFinished, 0, 32) 1445 XSPerfHistogram("walkRabExtraCycleHist", waitRabWalkCycle, state === s_walk && walkFinished && rab.io.status.walkEnd, 0, 32) 1446 XSPerfHistogram("walkTotalCycleHist", walkCycle, state === s_walk && state_next === s_idle, 0, 32) 1447 1448 private val deqNotWritebacked = valid(deqPtr.value) && !isWritebacked(deqPtr.value) 1449 private val deqStdNotWritebacked = valid(deqPtr.value) && !stdWritebacked(deqPtr.value) 1450 private val deqUopNotWritebacked = valid(deqPtr.value) && !isUopWritebacked(deqPtr.value) 1451 private val deqHeadInfo = debug_microOp(deqPtr.value) 1452 val deqUopCommitType = io.commits.info(0).commitType 1453 1454 XSPerfAccumulate("waitAluCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.alu.U) 1455 XSPerfAccumulate("waitMulCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.mul.U) 1456 XSPerfAccumulate("waitDivCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.div.U) 1457 XSPerfAccumulate("waitBrhCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.brh.U) 1458 XSPerfAccumulate("waitJmpCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.jmp.U) 1459 XSPerfAccumulate("waitCsrCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.csr.U) 1460 XSPerfAccumulate("waitFenCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.fence.U) 1461 XSPerfAccumulate("waitBkuCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.bku.U) 1462 XSPerfAccumulate("waitLduCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.ldu.U) 1463 XSPerfAccumulate("waitStuCycle", deqNotWritebacked && deqHeadInfo.fuType === FuType.stu.U) 1464 XSPerfAccumulate("waitStaCycle", deqUopNotWritebacked && deqHeadInfo.fuType === FuType.stu.U) 1465 XSPerfAccumulate("waitStdCycle", deqStdNotWritebacked && deqHeadInfo.fuType === FuType.stu.U) 1466 XSPerfAccumulate("waitAtmCycle", deqStdNotWritebacked && deqHeadInfo.fuType === FuType.mou.U) 1467 1468 XSPerfAccumulate("waitNormalCycle", deqNotWritebacked && deqUopCommitType === CommitType.NORMAL) 1469 XSPerfAccumulate("waitBranchCycle", deqNotWritebacked && deqUopCommitType === CommitType.BRANCH) 1470 XSPerfAccumulate("waitLoadCycle", deqNotWritebacked && deqUopCommitType === CommitType.LOAD) 1471 XSPerfAccumulate("waitStoreCycle", deqNotWritebacked && deqUopCommitType === CommitType.STORE) 1472 XSPerfAccumulate("robHeadPC", io.commits.info(0).pc) 1473 XSPerfAccumulate("commitCompressCntAll", PopCount(io.commits.commitValid.zip(io.commits.info).map{case(valid, info) => io.commits.isCommit && valid && info.instrSize > 1.U})) 1474 (2 to RenameWidth).foreach(i => 1475 XSPerfAccumulate(s"commitCompressCnt${i}", PopCount(io.commits.commitValid.zip(io.commits.info).map{case(valid, info) => io.commits.isCommit && valid && info.instrSize === i.U})) 1476 ) 1477 XSPerfAccumulate("compressSize", io.commits.commitValid.zip(io.commits.info).map { case (valid, info) => Mux(io.commits.isCommit && valid && info.instrSize > 1.U, info.instrSize, 0.U) }.reduce(_ +& _)) 1478 val dispatchLatency = commitDebugUop.map(uop => uop.debugInfo.dispatchTime - uop.debugInfo.renameTime) 1479 val enqRsLatency = commitDebugUop.map(uop => uop.debugInfo.enqRsTime - uop.debugInfo.dispatchTime) 1480 val selectLatency = commitDebugUop.map(uop => uop.debugInfo.selectTime - uop.debugInfo.enqRsTime) 1481 val issueLatency = commitDebugUop.map(uop => uop.debugInfo.issueTime - uop.debugInfo.selectTime) 1482 val executeLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.issueTime) 1483 val rsFuLatency = commitDebugUop.map(uop => uop.debugInfo.writebackTime - uop.debugInfo.enqRsTime) 1484 val commitLatency = commitDebugUop.map(uop => timer - uop.debugInfo.writebackTime) 1485 def latencySum(cond: Seq[Bool], latency: Seq[UInt]): UInt = { 1486 cond.zip(latency).map(x => Mux(x._1, x._2, 0.U)).reduce(_ +& _) 1487 } 1488 for (fuType <- FuType.functionNameMap.keys) { 1489 val fuName = FuType.functionNameMap(fuType) 1490 val commitIsFuType = io.commits.commitValid.zip(commitDebugUop).map(x => x._1 && x._2.fuType === fuType.U ) 1491 XSPerfRolling(s"ipc_futype_${fuName}", ifCommit(PopCount(commitIsFuType)), 1000, clock, reset) 1492 XSPerfAccumulate(s"${fuName}_instr_cnt", ifCommit(PopCount(commitIsFuType))) 1493 XSPerfAccumulate(s"${fuName}_latency_dispatch", ifCommit(latencySum(commitIsFuType, dispatchLatency))) 1494 XSPerfAccumulate(s"${fuName}_latency_enq_rs", ifCommit(latencySum(commitIsFuType, enqRsLatency))) 1495 XSPerfAccumulate(s"${fuName}_latency_select", ifCommit(latencySum(commitIsFuType, selectLatency))) 1496 XSPerfAccumulate(s"${fuName}_latency_issue", ifCommit(latencySum(commitIsFuType, issueLatency))) 1497 XSPerfAccumulate(s"${fuName}_latency_execute", ifCommit(latencySum(commitIsFuType, executeLatency))) 1498 XSPerfAccumulate(s"${fuName}_latency_enq_rs_execute", ifCommit(latencySum(commitIsFuType, rsFuLatency))) 1499 XSPerfAccumulate(s"${fuName}_latency_commit", ifCommit(latencySum(commitIsFuType, commitLatency))) 1500 } 1501 XSPerfAccumulate(s"redirect_use_snapshot", io.redirect.valid && io.snpt.useSnpt) 1502 1503 // top-down info 1504 io.debugTopDown.toCore.robHeadVaddr.valid := debug_lsTopdownInfo(deqPtr.value).s1.vaddr_valid 1505 io.debugTopDown.toCore.robHeadVaddr.bits := debug_lsTopdownInfo(deqPtr.value).s1.vaddr_bits 1506 io.debugTopDown.toCore.robHeadPaddr.valid := debug_lsTopdownInfo(deqPtr.value).s2.paddr_valid 1507 io.debugTopDown.toCore.robHeadPaddr.bits := debug_lsTopdownInfo(deqPtr.value).s2.paddr_bits 1508 io.debugTopDown.toDispatch.robTrueCommit := ifCommitReg(trueCommitCnt) 1509 io.debugTopDown.toDispatch.robHeadLsIssue := debug_lsIssue(deqPtr.value) 1510 io.debugTopDown.robHeadLqIdx.valid := debug_lqIdxValid(deqPtr.value) 1511 io.debugTopDown.robHeadLqIdx.bits := debug_microOp(deqPtr.value).lqIdx 1512 1513 // rolling 1514 io.debugRolling.robTrueCommit := ifCommitReg(trueCommitCnt) 1515 1516 /** 1517 * DataBase info: 1518 * log trigger is at writeback valid 1519 * */ 1520 1521 /** 1522 * @todo add InstInfoEntry back 1523 * @author Maxpicca-Li 1524 */ 1525 1526 //difftest signals 1527 val firstValidCommit = (deqPtr + PriorityMux(io.commits.commitValid, VecInit(List.tabulate(CommitWidth)(_.U(log2Up(CommitWidth).W))))).value 1528 1529 val wdata = Wire(Vec(CommitWidth, UInt(XLEN.W))) 1530 val wpc = Wire(Vec(CommitWidth, UInt(XLEN.W))) 1531 1532 for(i <- 0 until CommitWidth) { 1533 val idx = deqPtrVec(i).value 1534 wdata(i) := debug_exuData(idx) 1535 wpc(i) := SignExt(commitDebugUop(i).pc, XLEN) 1536 } 1537 1538 if (env.EnableDifftest || env.AlwaysBasicDiff) { 1539 // These are the structures used by difftest only and should be optimized after synthesis. 1540 val dt_eliminatedMove = Mem(RobSize, Bool()) 1541 val dt_isRVC = Mem(RobSize, Bool()) 1542 val dt_exuDebug = Reg(Vec(RobSize, new DebugBundle)) 1543 for (i <- 0 until RenameWidth) { 1544 when (canEnqueue(i)) { 1545 dt_eliminatedMove(allocatePtrVec(i).value) := io.enq.req(i).bits.eliminatedMove 1546 dt_isRVC(allocatePtrVec(i).value) := io.enq.req(i).bits.preDecodeInfo.isRVC 1547 } 1548 } 1549 for (wb <- exuWBs) { 1550 when (wb.valid) { 1551 val wbIdx = wb.bits.robIdx.value 1552 dt_exuDebug(wbIdx) := wb.bits.debug 1553 } 1554 } 1555 // Always instantiate basic difftest modules. 1556 for (i <- 0 until CommitWidth) { 1557 val uop = commitDebugUop(i) 1558 val commitInfo = io.commits.info(i) 1559 val ptr = deqPtrVec(i).value 1560 val exuOut = dt_exuDebug(ptr) 1561 val eliminatedMove = dt_eliminatedMove(ptr) 1562 val isRVC = dt_isRVC(ptr) 1563 1564 val difftest = DifftestModule(new DiffInstrCommit(MaxPhyPregs), delay = 3, dontCare = true) 1565 difftest.coreid := io.hartId 1566 difftest.index := i.U 1567 difftest.valid := io.commits.commitValid(i) && io.commits.isCommit 1568 difftest.skip := Mux(eliminatedMove, false.B, exuOut.isMMIO || exuOut.isPerfCnt) 1569 difftest.isRVC := isRVC 1570 difftest.rfwen := io.commits.commitValid(i) && commitInfo.rfWen && commitInfo.ldest =/= 0.U 1571 difftest.fpwen := io.commits.commitValid(i) && uop.fpWen 1572 difftest.wpdest := commitInfo.pdest 1573 difftest.wdest := commitInfo.ldest 1574 difftest.nFused := CommitType.isFused(commitInfo.commitType).asUInt + commitInfo.instrSize - 1.U 1575 when(difftest.valid) { 1576 assert(CommitType.isFused(commitInfo.commitType).asUInt + commitInfo.instrSize >= 1.U) 1577 } 1578 if (env.EnableDifftest) { 1579 val uop = commitDebugUop(i) 1580 difftest.pc := SignExt(uop.pc, XLEN) 1581 difftest.instr := uop.instr 1582 difftest.robIdx := ZeroExt(ptr, 10) 1583 difftest.lqIdx := ZeroExt(uop.lqIdx.value, 7) 1584 difftest.sqIdx := ZeroExt(uop.sqIdx.value, 7) 1585 difftest.isLoad := io.commits.info(i).commitType === CommitType.LOAD 1586 difftest.isStore := io.commits.info(i).commitType === CommitType.STORE 1587 } 1588 } 1589 } 1590 1591 if (env.EnableDifftest) { 1592 for (i <- 0 until CommitWidth) { 1593 val difftest = DifftestModule(new DiffLoadEvent, delay = 3) 1594 difftest.coreid := io.hartId 1595 difftest.index := i.U 1596 1597 val ptr = deqPtrVec(i).value 1598 val uop = commitDebugUop(i) 1599 val exuOut = debug_exuDebug(ptr) 1600 difftest.valid := io.commits.commitValid(i) && io.commits.isCommit 1601 difftest.paddr := exuOut.paddr 1602 difftest.opType := uop.fuOpType 1603 difftest.fuType := uop.fuType 1604 } 1605 } 1606 1607 if (env.EnableDifftest || env.AlwaysBasicDiff) { 1608 val dt_isXSTrap = Mem(RobSize, Bool()) 1609 for (i <- 0 until RenameWidth) { 1610 when (canEnqueue(i)) { 1611 dt_isXSTrap(allocatePtrVec(i).value) := io.enq.req(i).bits.isXSTrap 1612 } 1613 } 1614 val trapVec = io.commits.commitValid.zip(deqPtrVec).map{ case (v, d) => 1615 io.commits.isCommit && v && dt_isXSTrap(d.value) 1616 } 1617 val hitTrap = trapVec.reduce(_||_) 1618 val difftest = DifftestModule(new DiffTrapEvent, dontCare = true) 1619 difftest.coreid := io.hartId 1620 difftest.hasTrap := hitTrap 1621 difftest.cycleCnt := timer 1622 difftest.instrCnt := instrCnt 1623 difftest.hasWFI := hasWFI 1624 1625 if (env.EnableDifftest) { 1626 val trapCode = PriorityMux(wdata.zip(trapVec).map(x => x._2 -> x._1)) 1627 val trapPC = SignExt(PriorityMux(wpc.zip(trapVec).map(x => x._2 ->x._1)), XLEN) 1628 difftest.code := trapCode 1629 difftest.pc := trapPC 1630 } 1631 } 1632 1633 val validEntriesBanks = (0 until (RobSize + 31) / 32).map(i => RegNext(PopCount(valid.drop(i * 32).take(32)))) 1634 val validEntries = RegNext(VecInit(validEntriesBanks).reduceTree(_ +& _)) 1635 val commitMoveVec = VecInit(io.commits.commitValid.zip(commitIsMove).map{ case (v, m) => v && m }) 1636 val commitLoadVec = VecInit(commitLoadValid) 1637 val commitBranchVec = VecInit(commitBranchValid) 1638 val commitLoadWaitVec = VecInit(commitLoadValid.zip(commitLoadWaitBit).map{ case (v, w) => v && w }) 1639 val commitStoreVec = VecInit(io.commits.commitValid.zip(commitIsStore).map{ case (v, t) => v && t }) 1640 val perfEvents = Seq( 1641 ("rob_interrupt_num ", io.flushOut.valid && intrEnable ), 1642 ("rob_exception_num ", io.flushOut.valid && exceptionEnable ), 1643 ("rob_flush_pipe_num ", io.flushOut.valid && isFlushPipe ), 1644 ("rob_replay_inst_num ", io.flushOut.valid && isFlushPipe && deqHasReplayInst ), 1645 ("rob_commitUop ", ifCommit(commitCnt) ), 1646 ("rob_commitInstr ", ifCommitReg(trueCommitCnt) ), 1647 ("rob_commitInstrMove ", ifCommitReg(PopCount(RegNext(commitMoveVec))) ), 1648 ("rob_commitInstrFused ", ifCommitReg(fuseCommitCnt) ), 1649 ("rob_commitInstrLoad ", ifCommitReg(PopCount(RegNext(commitLoadVec))) ), 1650 ("rob_commitInstrBranch ", ifCommitReg(PopCount(RegNext(commitBranchVec))) ), 1651 ("rob_commitInstrLoadWait", ifCommitReg(PopCount(RegNext(commitLoadWaitVec))) ), 1652 ("rob_commitInstrStore ", ifCommitReg(PopCount(RegNext(commitStoreVec))) ), 1653 ("rob_walkInstr ", Mux(io.commits.isWalk, PopCount(io.commits.walkValid), 0.U) ), 1654 ("rob_walkCycle ", (state === s_walk) ), 1655 ("rob_1_4_valid ", validEntries <= (RobSize / 4).U ), 1656 ("rob_2_4_valid ", validEntries > (RobSize / 4).U && validEntries <= (RobSize / 2).U ), 1657 ("rob_3_4_valid ", validEntries > (RobSize / 2).U && validEntries <= (RobSize * 3 / 4).U), 1658 ("rob_4_4_valid ", validEntries > (RobSize * 3 / 4).U ), 1659 ) 1660 generatePerfEvent() 1661} 1662