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