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