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 val difftest_table = RegInit(rename_table_init) 86 val difftest_table_next = WireDefault(difftest_table) 87 88 // For better timing, we optimize reading and writing to RenameTable as follows: 89 // (1) Writing at T0 will be actually processed at T1. 90 // (2) Reading is synchronous now. 91 // (3) RAddr at T0 will be used to access the table and get data at T0. 92 // (4) WData at T0 is bypassed to RData at T1. 93 val t1_redirect = RegNext(io.redirect, false.B) 94 val t1_rdata = io.readPorts.map(p => RegNext(Mux(p.hold, p.data, spec_table_next(p.addr)))) 95 val t1_raddr = io.readPorts.map(p => RegEnable(p.addr, !p.hold)) 96 val t1_wSpec = RegNext(Mux(io.redirect, 0.U.asTypeOf(io.specWritePorts), io.specWritePorts)) 97 98 // WRITE: when instruction commits or walking 99 val t1_wSpec_addr = t1_wSpec.map(w => Mux(w.wen, UIntToOH(w.addr), 0.U)) 100 for ((next, i) <- spec_table_next.zipWithIndex) { 101 val matchVec = t1_wSpec_addr.map(w => w(i)) 102 val wMatch = ParallelPriorityMux(matchVec.reverse, t1_wSpec.map(_.data).reverse) 103 // When there's a flush, we use arch_table to update spec_table. 104 next := Mux(t1_redirect, arch_table(i), Mux(VecInit(matchVec).asUInt.orR, wMatch, spec_table(i))) 105 } 106 spec_table := spec_table_next 107 108 // READ: decode-rename stage 109 for ((r, i) <- io.readPorts.zipWithIndex) { 110 // We use two comparisons here because r.hold has bad timing but addrs have better timing. 111 val t0_bypass = io.specWritePorts.map(w => w.wen && Mux(r.hold, w.addr === t1_raddr(i), w.addr === r.addr)) 112 val t1_bypass = RegNext(Mux(io.redirect, 0.U.asTypeOf(VecInit(t0_bypass)), VecInit(t0_bypass))) 113 val bypass_data = ParallelPriorityMux(t1_bypass.reverse, t1_wSpec.map(_.data).reverse) 114 r.data := Mux(t1_bypass.asUInt.orR, bypass_data, t1_rdata(i)) 115 } 116 117 for (w <- io.archWritePorts) { 118 when (w.wen) { 119 arch_table_next(w.addr) := w.data 120 } 121 } 122 arch_table := arch_table_next 123 124 for (w <- io.diffWritePorts) { 125 when(w.wen) { 126 difftest_table_next(w.addr) := w.data 127 } 128 } 129 difftest_table := difftest_table_next 130 131 io.debug_rdata := arch_table.take(32) 132 io.debug_vconfig match { 133 case None => Unit 134 case x => x.get := arch_table.last 135 } 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 144class RenameTableWrapper(implicit p: Parameters) extends XSModule { 145 146 // params alias 147 private val numVecRegSrc = backendParams.numVecRegSrc 148 private val numVecRatPorts = numVecRegSrc + 1 // +1 dst 149 150 val io = IO(new Bundle() { 151 val redirect = Input(Bool()) 152 val robCommits = Input(new RobCommitIO) 153 val diffCommits = Input(new DiffCommitIO) 154 val intReadPorts = Vec(RenameWidth, Vec(3, new RatReadPort)) 155 val intRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 156 val fpReadPorts = Vec(RenameWidth, Vec(4, new RatReadPort)) 157 val fpRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 158 val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, new RatReadPort)) 159 val vecRenamePorts = Vec(RenameWidth, Input(new RatWritePort)) 160 // for debug printing 161 val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 162 val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 163 val debug_vec_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 164 val debug_vconfig_rat = Output(UInt(PhyRegIdxWidth.W)) 165 166 val diff_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 167 val diff_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 168 val diff_vec_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W))) 169 val diff_vconfig_rat = Output(UInt(PhyRegIdxWidth.W)) 170 }) 171 172 val intRat = Module(new RenameTable(Reg_I)) 173 val fpRat = Module(new RenameTable(Reg_F)) 174 val vecRat = Module(new RenameTable(Reg_V)) 175 176 io.debug_int_rat := intRat.io.debug_rdata 177 io.diff_int_rat := intRat.io.diff_rdata 178 intRat.io.readPorts <> io.intReadPorts.flatten 179 intRat.io.redirect := io.redirect 180 val intDestValid = io.robCommits.info.map(_.rfWen) 181 for ((arch, i) <- intRat.io.archWritePorts.zipWithIndex) { 182 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && intDestValid(i) 183 arch.addr := io.robCommits.info(i).ldest 184 arch.data := io.robCommits.info(i).pdest 185 XSError(arch.wen && arch.addr === 0.U && arch.data =/= 0.U, "pdest for $0 should be 0\n") 186 } 187 for ((spec, i) <- intRat.io.specWritePorts.zipWithIndex) { 188 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && intDestValid(i) 189 spec.addr := io.robCommits.info(i).ldest 190 spec.data := io.robCommits.info(i).pdest 191 XSError(spec.wen && spec.addr === 0.U && spec.data =/= 0.U, "pdest for $0 should be 0\n") 192 } 193 for ((spec, rename) <- intRat.io.specWritePorts.zip(io.intRenamePorts)) { 194 when (rename.wen) { 195 spec.wen := true.B 196 spec.addr := rename.addr 197 spec.data := rename.data 198 } 199 } 200 for ((diff, i) <- intRat.io.diffWritePorts.zipWithIndex) { 201 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).rfWen 202 diff.addr := io.diffCommits.info(i).ldest 203 diff.data := io.diffCommits.info(i).pdest 204 } 205 206 // debug read ports for difftest 207 io.debug_fp_rat := fpRat.io.debug_rdata 208 io.diff_fp_rat := fpRat.io.diff_rdata 209 fpRat.io.readPorts <> io.fpReadPorts.flatten 210 fpRat.io.redirect := io.redirect 211 for ((arch, i) <- fpRat.io.archWritePorts.zipWithIndex) { 212 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).fpWen 213 arch.addr := io.robCommits.info(i).ldest 214 arch.data := io.robCommits.info(i).pdest 215 } 216 for ((spec, i) <- fpRat.io.specWritePorts.zipWithIndex) { 217 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).fpWen 218 spec.addr := io.robCommits.info(i).ldest 219 spec.data := io.robCommits.info(i).pdest 220 } 221 for ((spec, rename) <- fpRat.io.specWritePorts.zip(io.fpRenamePorts)) { 222 when (rename.wen) { 223 spec.wen := true.B 224 spec.addr := rename.addr 225 spec.data := rename.data 226 } 227 } 228 for ((diff, i) <- fpRat.io.diffWritePorts.zipWithIndex) { 229 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).fpWen 230 diff.addr := io.diffCommits.info(i).ldest 231 diff.data := io.diffCommits.info(i).pdest 232 } 233 234 // debug read ports for difftest 235 io.debug_vec_rat := vecRat.io.debug_rdata 236 io.debug_vconfig_rat := vecRat.io.debug_vconfig.get 237 io.diff_vec_rat := vecRat.io.diff_rdata 238 io.diff_vconfig_rat := vecRat.io.diff_vconfig.get 239 vecRat.io.readPorts <> io.vecReadPorts.flatten 240 vecRat.io.redirect := io.redirect 241 //TODO: RM the donTouch 242 dontTouch(vecRat.io) 243 for ((arch, i) <- vecRat.io.archWritePorts.zipWithIndex) { 244 arch.wen := io.robCommits.isCommit && io.robCommits.commitValid(i) && io.robCommits.info(i).vecWen 245 arch.addr := io.robCommits.info(i).ldest 246 arch.data := io.robCommits.info(i).pdest 247 } 248 for ((spec, i) <- vecRat.io.specWritePorts.zipWithIndex) { 249 spec.wen := io.robCommits.isWalk && io.robCommits.walkValid(i) && io.robCommits.info(i).vecWen 250 spec.addr := io.robCommits.info(i).ldest 251 spec.data := io.robCommits.info(i).pdest 252 } 253 for ((spec, rename) <- vecRat.io.specWritePorts.zip(io.vecRenamePorts)) { 254 when (rename.wen) { 255 spec.wen := true.B 256 spec.addr := rename.addr 257 spec.data := rename.data 258 } 259 } 260 for ((diff, i) <- vecRat.io.diffWritePorts.zipWithIndex) { 261 diff.wen := io.diffCommits.isCommit && io.diffCommits.commitValid(i) && io.diffCommits.info(i).vecWen 262 diff.addr := io.diffCommits.info(i).ldest 263 diff.data := io.diffCommits.info(i).pdest 264 } 265 266} 267