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