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