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