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 chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import utility.ParallelPriorityMux 23import utils.XSError 24import xiangshan._ 25 26abstract class RegType 27case object Reg_I extends RegType 28case object Reg_F extends RegType 29case object Reg_V extends RegType 30 31class RatReadPort(implicit p: Parameters) extends XSBundle { 32 val hold = Input(Bool()) 33 val addr = Input(UInt(6.W)) 34 val data = Output(UInt(PhyRegIdxWidth.W)) 35} 36 37class RatWritePort(implicit p: Parameters) extends XSBundle { 38 val wen = Bool() 39 val addr = UInt(6.W) 40 val data = UInt(PhyRegIdxWidth.W) 41} 42 43class RenameTable(reg_t: RegType)(implicit p: Parameters) extends XSModule { 44 45 // params alias 46 private val numVecRegSrc = backendParams.numVecRegSrc 47 private val numVecRatPorts = numVecRegSrc + 1 // +1 dst 48 49 val readPortsNum = reg_t match { 50 case Reg_I => 3 51 case Reg_F => 4 52 case Reg_V => numVecRatPorts // +1 ldest 53 } 54 val io = IO(new Bundle { 55 val redirect = Input(Bool()) 56 val readPorts = Vec(readPortsNum * RenameWidth, new RatReadPort) 57 val specWritePorts = Vec(CommitWidth, Input(new RatWritePort)) 58 val archWritePorts = Vec(CommitWidth, Input(new RatWritePort)) 59 val diffWritePorts = Vec(CommitWidth * MaxUopSize, Input(new RatWritePort)) 60 val debug_rdata = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 61 val debug_vconfig = reg_t match { // vconfig is implemented as int reg[32] 62 case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W))) 63 case _ => None 64 } 65 val diff_rdata = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 66 val diff_vconfig = reg_t match { 67 case Reg_V => Some(Output(UInt(PhyRegIdxWidth.W))) 68 case _ => None 69 } 70 }) 71 72 // speculative rename table 73 // fp and vec share the same free list, so the first init value of vecRAT is 32 74 val rename_table_init = reg_t match { 75 case Reg_I => VecInit.fill (IntLogicRegs)(0.U(PhyRegIdxWidth.W)) 76 case Reg_F => VecInit.tabulate(FpLogicRegs)(_.U(PhyRegIdxWidth.W)) 77 case Reg_V => VecInit.tabulate(VecLogicRegs)(x => (x + FpLogicRegs).U(PhyRegIdxWidth.W)) 78 } 79 val spec_table = RegInit(rename_table_init) 80 val spec_table_next = WireInit(spec_table) 81 // arch state rename table 82 val arch_table = RegInit(rename_table_init) 83 val arch_table_next = WireDefault(arch_table) 84 85 // For better timing, we optimize reading and writing to RenameTable as follows: 86 // (1) Writing at T0 will be actually processed at T1. 87 // (2) Reading is synchronous now. 88 // (3) RAddr at T0 will be used to access the table and get data at T0. 89 // (4) WData at T0 is bypassed to RData at T1. 90 val t1_redirect = RegNext(io.redirect, false.B) 91 val t1_rdata = io.readPorts.map(p => RegNext(Mux(p.hold, p.data, spec_table_next(p.addr)))) 92 val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold)) 93 val t1_wSpec = RegNext(Mux(io.redirect, 0.U.asTypeOf(io.specWritePorts), io.specWritePorts)) 94 95 // WRITE: when instruction commits or walking 96 val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U)) 97 for ((next, i) <- spec_table_next.zipWithIndex) { 98 val matchVec = t1_wSpec_addr.map(w => w(i)) 99 val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse) 100 // When there's a flush, we use arch_table to update spec_table. 101 next := Mux(t1_redirect, arch_table(i), Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i))) 102 } 103 spec_table := spec_table_next 104 105 // READ: decode-rename stage 106 for ((r, i) <- io.readPorts.zipWithIndex) { 107 // We use two comparisons here because r.hold has bad timing but addrs have better timing. 108 val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr)) 109 val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass))) 110 val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse) 111 r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata(i)) 112 } 113 114 for (w <- io.archWritePorts) { 115 when (w.wen) { 116 arch_table_next(w.addr) := w.data 117 } 118 } 119 arch_table := arch_table_next 120 121 io.debug_rdata := arch_table.take(32) 122 io.debug_vconfig match { 123 case None => Unit 124 case x => x.get := arch_table.last 125 } 126 if (env.EnableDifftest || env.AlwaysBasicDiff) { 127 val difftest_table = RegInit(rename_table_init) 128 val difftest_table_next = WireDefault(difftest_table) 129 130 for (w <- io.diffWritePorts) { 131 when(w.wen) { 132 difftest_table_next(w.addr) := w.data 133 } 134 } 135 difftest_table := difftest_table_next 136 137 io.diff_rdata := difftest_table.take(32) 138 io.diff_vconfig match { 139 case None => Unit 140 case x => x.get := difftest_table(VCONFIG_IDX) 141 } 142 } 143 else { 144 io.diff_rdata := 0.U.asTypeOf(io.debug_rdata) 145 io.diff_vconfig match { 146 case None => Unit 147 case x => x.get := 0.U 148 } 149 } 150} 151 152class RenameTableWrapper(implicit p: Parameters) extends XSModule { 153 154 // params alias 155 private val numVecRegSrc = backendParams.numVecRegSrc 156 private val numVecRatPorts = numVecRegSrc + 1 // +1 dst 157 158 val io = IO(new Bundle() { 159 val redirect = Input(Bool()) 160 val robCommits = Input(new RobCommitIO) 161 val diffCommits = Input(new DiffCommitIO) 162 val intReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort)) 163 val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 164 val fpReadPorts = Vec(RenameWidth, Vec(4, new RatReadPort)) 165 val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 166 val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, new RatReadPort)) 167 val vecRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 168 // for debug printing 169 val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 170 val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 171 val debug_vec_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 172 val debug_vconfig_rat = Output(UInt(PhyRegIdxWidth.W)) 173 174 val diff_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 175 val diff_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 176 val diff_vec_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 177 val diff_vconfig_rat = Output(UInt(PhyRegIdxWidth.W)) 178 }) 179 180 val intRat = Module(new RenameTable(Reg_I)) 181 val fpRat = Module(new RenameTable(Reg_F)) 182 val vecRat = Module(new RenameTable(Reg_V)) 183 184 io.debug_int_rat := intRat.io.debug_rdata 185 io.diff_int_rat := intRat.io.diff_rdata 186 intRat.io.readPorts <> io.intReadPorts.flatten 187 intRat.io.redirect := io.redirect 188 val intDestValid = io.robCommits.info.map(_.rfWen) 189 for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) { 190 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && intDestValid(i) 191 arch.addr := io.robCommits.info(i).ldest 192 arch.data := io.robCommits.info(i).pdest 193 XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n") 194 } 195 for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) { 196 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && intDestValid(i) 197 spec.addr := io.robCommits.info(i).ldest 198 spec.data := io.robCommits.info(i).pdest 199 XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n") 200 } 201 for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) { 202 when (rename.wen) { 203 spec.wen := true.B 204 spec.addr := rename.addr 205 spec.data := rename.data 206 } 207 } 208 for ((diff, i) <- intRat.io.diffWritePorts.zipWithIndex) { 209 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).rfWen 210 diff.addr := io.diffCommits.info(i).ldest 211 diff.data := io.diffCommits.info(i).pdest 212 } 213 214 // debug read ports for difftest 215 io.debug_fp_rat := fpRat.io.debug_rdata 216 io.diff_fp_rat := fpRat.io.diff_rdata 217 fpRat.io.readPorts <> io.fpReadPorts.flatten 218 fpRat.io.redirect := io.redirect 219 for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) { 220 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).fpWen 221 arch.addr := io.robCommits.info(i).ldest 222 arch.data := io.robCommits.info(i).pdest 223 } 224 for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) { 225 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).fpWen 226 spec.addr := io.robCommits.info(i).ldest 227 spec.data := io.robCommits.info(i).pdest 228 } 229 for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) { 230 when (rename.wen) { 231 spec.wen := true.B 232 spec.addr := rename.addr 233 spec.data := rename.data 234 } 235 } 236 for ((diff, i) <- fpRat.io.diffWritePorts.zipWithIndex) { 237 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).fpWen 238 diff.addr := io.diffCommits.info(i).ldest 239 diff.data := io.diffCommits.info(i).pdest 240 } 241 242 // debug read ports for difftest 243 io.debug_vec_rat := vecRat.io.debug_rdata 244 io.debug_vconfig_rat := vecRat.io.debug_vconfig.get 245 io.diff_vec_rat := vecRat.io.diff_rdata 246 io.diff_vconfig_rat := vecRat.io.diff_vconfig.get 247 vecRat.io.readPorts <> io.vecReadPorts.flatten 248 vecRat.io.redirect := io.redirect 249 //TODO: RM the donTouch 250 dontTouch(vecRat.io) 251 for ((arch, i) <- vecRat.io.archWritePorts.zipWithIndex) { 252 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).vecWen 253 arch.addr := io.robCommits.info(i).ldest 254 arch.data := io.robCommits.info(i).pdest 255 } 256 for ((spec, i) <- vecRat.io.specWritePorts.zipWithIndex) { 257 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).vecWen 258 spec.addr := io.robCommits.info(i).ldest 259 spec.data := io.robCommits.info(i).pdest 260 } 261 for ((spec, rename) <- vecRat.io.specWritePorts.zip(io.vecRenamePorts)) { 262 when (rename.wen) { 263 spec.wen := true.B 264 spec.addr := rename.addr 265 spec.data := rename.data 266 } 267 } 268 for ((diff, i) <- vecRat.io.diffWritePorts.zipWithIndex) { 269 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).vecWen 270 diff.addr := io.diffCommits.info(i).ldest 271 diff.data := io.diffCommits.info(i).pdest 272 } 273 274} 275