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.rename 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utility.HasCircularQueuePtrHelper 23import utility.ParallelPriorityMux 24import utility.GatedValidRegNext 25import utility.XSError 26import xiangshan._ 27 28abstract class RegType 29case object Reg_I extends RegType 30case object Reg_F extends RegType 31case object Reg_V extends RegType 32case object Reg_V0 extends RegType 33case object Reg_Vl extends RegType 34 35class RatReadPort(ratAddrWidth: Int)(implicit p: Parameters) extends XSBundle { 36 val hold = Input(Bool()) 37 val addr = Input(UInt(ratAddrWidth.W)) 38 val data = Output(UInt(PhyRegIdxWidth.W)) 39} 40 41class RatWritePort(ratAddrWidth: Int)(implicit p: Parameters) extends XSBundle { 42 val wen = Bool() 43 val addr = UInt(ratAddrWidth.W) 44 val data = UInt(PhyRegIdxWidth.W) 45} 46 47class RenameTable(reg_t: RegType)(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper { 48 49 // params alias 50 private val numVecRegSrc = backendParams.numVecRegSrc 51 private val numVecRatPorts = numVecRegSrc 52 53 val readPortsNum = reg_t match { 54 case Reg_I => 2 55 case Reg_F => 3 56 case Reg_V => 3 57 case Reg_V0 => 1 58 case Reg_Vl => 1 59 } 60 val rdataNums = reg_t match { 61 case Reg_I => 32 62 case Reg_F => 32 63 case Reg_V => 31 // no v0 64 case Reg_V0 => 1 // v0 65 case Reg_Vl => 1 // vl 66 } 67 val renameTableWidth = reg_t match { 68 case Reg_I => log2Ceil(IntLogicRegs) 69 case Reg_F => log2Ceil(FpLogicRegs) 70 case Reg_V => log2Ceil(VecLogicRegs) 71 case Reg_V0 => log2Ceil(V0LogicRegs) 72 case Reg_Vl => log2Ceil(VlLogicRegs) 73 } 74 75 val io = IO(new Bundle { 76 val redirect = Input(Bool()) 77 val readPorts = Vec(readPortsNum * RenameWidth, new RatReadPort(renameTableWidth)) 78 val specWritePorts = Vec(RabCommitWidth, Input(new RatWritePort(renameTableWidth))) 79 val archWritePorts = Vec(RabCommitWidth, Input(new RatWritePort(renameTableWidth))) 80 val old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 81 val need_free = Vec(RabCommitWidth, Output(Bool())) 82 val snpt = Input(new SnapshotPort) 83 val diffWritePorts = if (backendParams.debugEn) Some(Vec(RabCommitWidth * MaxUopSize, Input(new RatWritePort(renameTableWidth)))) else None 84 val debug_rdata = if (backendParams.debugEn) Some(Vec(rdataNums, Output(UInt(PhyRegIdxWidth.W)))) else None 85 val diff_rdata = if (backendParams.debugEn) Some(Vec(rdataNums, Output(UInt(PhyRegIdxWidth.W)))) else None 86 val debug_v0 = if (backendParams.debugEn) reg_t match { 87 case Reg_V0 => Some(Output(UInt(PhyRegIdxWidth.W))) 88 case _ => None 89 } else None 90 val diff_v0 = if (backendParams.debugEn) reg_t match { 91 case Reg_V0 => Some(Output(UInt(PhyRegIdxWidth.W))) 92 case _ => None 93 } else None 94 val debug_vl = if (backendParams.debugEn) reg_t match { 95 case Reg_Vl => Some(Output(UInt(PhyRegIdxWidth.W))) 96 case _ => None 97 } else None 98 val diff_vl = if (backendParams.debugEn) reg_t match { 99 case Reg_Vl => Some(Output(UInt(PhyRegIdxWidth.W))) 100 case _ => None 101 } else None 102 }) 103 104 // speculative rename table 105 val rename_table_init = reg_t match { 106 case Reg_I => VecInit.fill (IntLogicRegs)(0.U(PhyRegIdxWidth.W)) 107 case Reg_F => VecInit.tabulate(FpLogicRegs)(_.U(PhyRegIdxWidth.W)) 108 case Reg_V => VecInit.tabulate(VecLogicRegs)(_.U(PhyRegIdxWidth.W)) 109 case Reg_V0 => VecInit.tabulate(V0LogicRegs)(_.U(PhyRegIdxWidth.W)) 110 case Reg_Vl => VecInit.tabulate(VlLogicRegs)(_.U(PhyRegIdxWidth.W)) 111 } 112 val spec_table = RegInit(rename_table_init) 113 val spec_table_next = WireInit(spec_table) 114 // arch state rename table 115 val arch_table = RegInit(rename_table_init) 116 val arch_table_next = WireDefault(arch_table) 117 // old_pdest 118 val old_pdest = RegInit(VecInit.fill(RabCommitWidth)(0.U(PhyRegIdxWidth.W))) 119 val need_free = RegInit(VecInit.fill(RabCommitWidth)(false.B)) 120 121 // For better timing, we optimize reading and writing to RenameTable as follows: 122 // (1) Writing at T0 will be actually processed at T1. 123 // (2) Reading is synchronous now. 124 // (3) RAddr at T0 will be used to access the table and get data at T0. 125 // (4) WData at T0 is bypassed to RData at T1. 126 val t1_redirect = GatedValidRegNext(io.redirect, false.B) 127 val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold)) 128 val t1_rdata_use_t1_raddr = VecInit(t1_raddr.map(spec_table(_))) 129 val t1_wSpec = RegNext(Mux(io.redirect, 0.U.asTypeOf(io.specWritePorts), io.specWritePorts)) 130 131 val t1_snpt = RegNext(io.snpt, 0.U.asTypeOf(io.snpt)) 132 133 val snapshots = SnapshotGenerator(spec_table, t1_snpt.snptEnq, t1_snpt.snptDeq, t1_redirect, t1_snpt.flushVec) 134 135 // WRITE: when instruction commits or walking 136 val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U)) 137 for ((next, i) <- spec_table_next.zipWithIndex) { 138 val matchVec = t1_wSpec_addr.map(w => w(i)) 139 val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse) 140 // When there's a flush, we use arch_table to update spec_table. 141 next := Mux( 142 t1_redirect, 143 Mux(t1_snpt.useSnpt, snapshots(t1_snpt.snptSelect)(i), arch_table(i)), 144 Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i)) 145 ) 146 } 147 spec_table := spec_table_next 148 149 // READ: decode-rename stage 150 for ((r, i) <- io.readPorts.zipWithIndex) { 151 val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr)) 152 val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass))) 153 val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse) 154 r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata_use_t1_raddr(i)) 155 } 156 157 for ((w, i) <- io.archWritePorts.zipWithIndex) { 158 when (w.wen) { 159 arch_table_next(w.addr) := w.data 160 } 161 val arch_mask = VecInit.fill(PhyRegIdxWidth)(w.wen).asUInt 162 old_pdest(i) := 163 MuxCase(arch_table(w.addr) & arch_mask, 164 io.archWritePorts.take(i).reverse.map(x => (x.wen && x.addr === w.addr, x.data & arch_mask))) 165 } 166 arch_table := arch_table_next 167 168 for (((old, free), i) <- (old_pdest zip need_free).zipWithIndex) { 169 val hasDuplicate = old_pdest.take(i).map(_ === old) 170 val blockedByDup = if (i == 0) false.B else VecInit(hasDuplicate).asUInt.orR 171 free := VecInit(arch_table.map(_ =/= old)).asUInt.andR && !blockedByDup 172 } 173 174 io.old_pdest := old_pdest 175 io.need_free := need_free 176 io.debug_rdata.foreach{ x => reg_t match { 177 case Reg_V => x := arch_table.drop(1).take(rdataNums) 178 case _ => x := arch_table.take(rdataNums) 179 } 180 } 181 io.debug_v0.foreach(_ := arch_table(0)) 182 io.debug_vl.foreach(_ := arch_table(0)) 183 if (env.EnableDifftest || env.AlwaysBasicDiff) { 184 val difftest_table = RegInit(rename_table_init) 185 val difftest_table_next = WireDefault(difftest_table) 186 187 for (w <- io.diffWritePorts.get) { 188 when(w.wen) { 189 difftest_table_next(w.addr) := w.data 190 } 191 } 192 difftest_table := difftest_table_next 193 194 io.diff_rdata.foreach{ x => reg_t match { 195 case Reg_V => x := difftest_table.drop(1).take(rdataNums) 196 case _ => x := difftest_table.take(rdataNums) 197 } 198 } 199 io.diff_v0.foreach(_ := difftest_table(0)) 200 io.diff_vl.foreach(_ := difftest_table(0)) 201 } 202 else { 203 io.diff_rdata.foreach(_ := 0.U.asTypeOf(io.debug_rdata.get)) 204 io.diff_v0.foreach(_ := 0.U) 205 io.diff_vl.foreach(_ := 0.U) 206 } 207} 208 209class RenameTableWrapper(implicit p: Parameters) extends XSModule { 210 211 // params alias 212 private val numVecRegSrc = backendParams.numVecRegSrc 213 private val numVecRatPorts = numVecRegSrc 214 215 val io = IO(new Bundle() { 216 val redirect = Input(Bool()) 217 val rabCommits = Input(new RabCommitIO) 218 val diffCommits = if (backendParams.debugEn) Some(Input(new DiffCommitIO)) else None 219 val intReadPorts = Vec(RenameWidth, Vec(2, new RatReadPort(IntLogicRegs))) 220 val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort(IntLogicRegs))) 221 val fpReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort(FpLogicRegs))) 222 val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort(FpLogicRegs))) 223 val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, new RatReadPort(VecLogicRegs))) 224 val vecRenamePorts = Vec(RenameWidth, Input(new RatWritePort(VecLogicRegs))) 225 val v0ReadPorts = Vec(RenameWidth, new RatReadPort(V0LogicRegs)) 226 val v0RenamePorts = Vec(RenameWidth, Input(new RatWritePort(V0LogicRegs))) 227 val vlReadPorts = Vec(RenameWidth, new RatReadPort(VlLogicRegs)) 228 val vlRenamePorts = Vec(RenameWidth, Input(new RatWritePort(VlLogicRegs))) 229 230 val int_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 231 val fp_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 232 val vec_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 233 val v0_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 234 val vl_old_pdest = Vec(RabCommitWidth, Output(UInt(PhyRegIdxWidth.W))) 235 val int_need_free = Vec(RabCommitWidth, Output(Bool())) 236 val snpt = Input(new SnapshotPort) 237 238 // for debug printing 239 val debug_int_rat = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 240 val debug_fp_rat = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 241 val debug_vec_rat = if (backendParams.debugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None 242 val debug_v0_rat = if (backendParams.debugEn) Some(Vec(1,Output(UInt(PhyRegIdxWidth.W)))) else None 243 val debug_vl_rat = if (backendParams.debugEn) Some(Vec(1,Output(UInt(PhyRegIdxWidth.W)))) else None 244 245 val diff_int_rat = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 246 val diff_fp_rat = if (backendParams.debugEn) Some(Vec(32, Output(UInt(PhyRegIdxWidth.W)))) else None 247 val diff_vec_rat = if (backendParams.debugEn) Some(Vec(31, Output(UInt(PhyRegIdxWidth.W)))) else None 248 val diff_v0_rat = if (backendParams.debugEn) Some(Vec(1,Output(UInt(PhyRegIdxWidth.W)))) else None 249 val diff_vl_rat = if (backendParams.debugEn) Some(Vec(1,Output(UInt(PhyRegIdxWidth.W)))) else None 250 }) 251 252 val intRat = Module(new RenameTable(Reg_I)) 253 val fpRat = Module(new RenameTable(Reg_F)) 254 val vecRat = Module(new RenameTable(Reg_V)) 255 val v0Rat = Module(new RenameTable(Reg_V0)) 256 val vlRat = Module(new RenameTable(Reg_Vl)) 257 258 io.debug_int_rat .foreach(_ := intRat.io.debug_rdata.get) 259 io.diff_int_rat .foreach(_ := intRat.io.diff_rdata.get) 260 intRat.io.readPorts <> io.intReadPorts.flatten 261 intRat.io.redirect := io.redirect 262 intRat.io.snpt := io.snpt 263 io.int_old_pdest := intRat.io.old_pdest 264 io.int_need_free := intRat.io.need_free 265 val intDestValid = io.rabCommits.info.map(_.rfWen) 266 for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) { 267 arch.wen := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && intDestValid(i) 268 arch.addr := io.rabCommits.info(i).ldest 269 arch.data := io.rabCommits.info(i).pdest 270 XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n") 271 } 272 for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) { 273 spec.wen := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && intDestValid(i) 274 spec.addr := io.rabCommits.info(i).ldest 275 spec.data := io.rabCommits.info(i).pdest 276 XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n") 277 } 278 for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) { 279 when (rename.wen) { 280 spec.wen := true.B 281 spec.addr := rename.addr 282 spec.data := rename.data 283 } 284 } 285 if (backendParams.debugEn) { 286 for ((diff, i) <- intRat.io.diffWritePorts.get.zipWithIndex) { 287 diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).rfWen 288 diff.addr := io.diffCommits.get.info(i).ldest 289 diff.data := io.diffCommits.get.info(i).pdest 290 } 291 } 292 293 // debug read ports for difftest 294 io.debug_fp_rat.foreach(_ := fpRat.io.debug_rdata.get) 295 io.diff_fp_rat .foreach(_ := fpRat.io.diff_rdata.get) 296 fpRat.io.readPorts <> io.fpReadPorts.flatten 297 fpRat.io.redirect := io.redirect 298 fpRat.io.snpt := io.snpt 299 io.fp_old_pdest := fpRat.io.old_pdest 300 301 for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) { 302 arch.wen := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).fpWen 303 arch.addr := io.rabCommits.info(i).ldest 304 arch.data := io.rabCommits.info(i).pdest 305 } 306 for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) { 307 spec.wen := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).fpWen 308 spec.addr := io.rabCommits.info(i).ldest 309 spec.data := io.rabCommits.info(i).pdest 310 } 311 for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) { 312 when (rename.wen) { 313 spec.wen := true.B 314 spec.addr := rename.addr 315 spec.data := rename.data 316 } 317 } 318 if (backendParams.debugEn) { 319 for ((diff, i) <- fpRat.io.diffWritePorts.get.zipWithIndex) { 320 diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).fpWen 321 diff.addr := io.diffCommits.get.info(i).ldest 322 diff.data := io.diffCommits.get.info(i).pdest 323 } 324 } 325 326 // debug read ports for difftest 327 io.debug_vec_rat .foreach(_ := vecRat.io.debug_rdata.get) 328 io.diff_vec_rat .foreach(_ := vecRat.io.diff_rdata.get) 329 vecRat.io.readPorts <> io.vecReadPorts.flatten 330 vecRat.io.redirect := io.redirect 331 vecRat.io.snpt := io.snpt 332 io.vec_old_pdest := vecRat.io.old_pdest 333 334 //TODO: RM the donTouch 335 if(backendParams.debugEn) { 336 dontTouch(vecRat.io) 337 } 338 for ((arch, i) <- vecRat.io.archWritePorts.zipWithIndex) { 339 arch.wen := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).vecWen 340 arch.addr := io.rabCommits.info(i).ldest 341 arch.data := io.rabCommits.info(i).pdest 342 } 343 for ((spec, i) <- vecRat.io.specWritePorts.zipWithIndex) { 344 spec.wen := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).vecWen 345 spec.addr := io.rabCommits.info(i).ldest 346 spec.data := io.rabCommits.info(i).pdest 347 } 348 for ((spec, rename) <- vecRat.io.specWritePorts.zip(io.vecRenamePorts)) { 349 when (rename.wen) { 350 spec.wen := true.B 351 spec.addr := rename.addr 352 spec.data := rename.data 353 } 354 } 355 if (backendParams.debugEn) { 356 for ((diff, i) <- vecRat.io.diffWritePorts.get.zipWithIndex) { 357 diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).vecWen 358 diff.addr := io.diffCommits.get.info(i).ldest 359 diff.data := io.diffCommits.get.info(i).pdest 360 } 361 } 362 363 // debug read ports for difftest 364 io.debug_v0_rat.foreach(_ := v0Rat.io.debug_rdata.get) 365 io.diff_v0_rat.foreach(_ := v0Rat.io.diff_rdata.get) 366 v0Rat.io.readPorts <> io.v0ReadPorts 367 v0Rat.io.redirect := io.redirect 368 v0Rat.io.snpt := io.snpt 369 io.v0_old_pdest := v0Rat.io.old_pdest 370 371 if (backendParams.debugEn) { 372 dontTouch(v0Rat.io) 373 } 374 for ((arch, i) <- v0Rat.io.archWritePorts.zipWithIndex) { 375 arch.wen := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).v0Wen 376 arch.addr := io.rabCommits.info(i).ldest 377 arch.data := io.rabCommits.info(i).pdest 378 } 379 for ((spec, i) <- v0Rat.io.specWritePorts.zipWithIndex) { 380 spec.wen := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).v0Wen 381 spec.addr := io.rabCommits.info(i).ldest 382 spec.data := io.rabCommits.info(i).pdest 383 } 384 for ((spec, rename) <- v0Rat.io.specWritePorts.zip(io.v0RenamePorts)) { 385 when(rename.wen) { 386 spec.wen := true.B 387 spec.addr := rename.addr 388 spec.data := rename.data 389 } 390 } 391 if (backendParams.debugEn) { 392 for ((diff, i) <- v0Rat.io.diffWritePorts.get.zipWithIndex) { 393 diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).v0Wen 394 diff.addr := io.diffCommits.get.info(i).ldest 395 diff.data := io.diffCommits.get.info(i).pdest 396 } 397 } 398 399 // debug read ports for difftest 400 io.debug_vl_rat.foreach(_ := vlRat.io.debug_rdata.get) 401 io.diff_vl_rat.foreach(_ := vlRat.io.diff_rdata.get) 402 vlRat.io.readPorts <> io.vlReadPorts 403 vlRat.io.redirect := io.redirect 404 vlRat.io.snpt := io.snpt 405 io.vl_old_pdest := vlRat.io.old_pdest 406 407 if (backendParams.debugEn) { 408 dontTouch(vlRat.io) 409 } 410 for ((arch, i) <- vlRat.io.archWritePorts.zipWithIndex) { 411 arch.wen := io.rabCommits.isCommit && io.rabCommits.commitValid(i) && io.rabCommits.info(i).vlWen 412 arch.addr := io.rabCommits.info(i).ldest 413 arch.data := io.rabCommits.info(i).pdest 414 } 415 for ((spec, i) <- vlRat.io.specWritePorts.zipWithIndex) { 416 spec.wen := io.rabCommits.isWalk && io.rabCommits.walkValid(i) && io.rabCommits.info(i).vlWen 417 spec.addr := io.rabCommits.info(i).ldest 418 spec.data := io.rabCommits.info(i).pdest 419 } 420 for ((spec, rename) <- vlRat.io.specWritePorts.zip(io.vlRenamePorts)) { 421 when(rename.wen) { 422 spec.wen := true.B 423 spec.addr := rename.addr 424 spec.data := rename.data 425 } 426 } 427 if (backendParams.debugEn) { 428 for ((diff, i) <- vlRat.io.diffWritePorts.get.zipWithIndex) { 429 diff.wen := io.diffCommits.get.isCommit && io.diffCommits.get.commitValid(i) && io.diffCommits.get.info(i).vlWen 430 diff.addr := io.diffCommits.get.info(i).ldest 431 diff.data := io.diffCommits.get.info(i).pdest 432 } 433 } 434} 435