1package xiangshan.backend.rename 2 3import chisel3._ 4import chisel3.util._ 5import xiangshan._ 6import utils.{ParallelOR, XSInfo} 7 8class Rename extends XSModule { 9 val io = IO(new Bundle() { 10 val redirect = Flipped(ValidIO(new Redirect)) 11 val roqCommits = Vec(CommitWidth, Flipped(ValidIO(new RoqCommit))) 12 val wbIntResults = Vec(NRIntWritePorts, Flipped(ValidIO(new ExuOutput))) 13 val wbFpResults = Vec(NRFpWritePorts, Flipped(ValidIO(new ExuOutput))) 14 val intRfReadAddr = Vec(NRIntReadPorts + NRMemReadPorts, Input(UInt(PhyRegIdxWidth.W))) 15 val fpRfReadAddr = Vec(NRFpReadPorts, Input(UInt(PhyRegIdxWidth.W))) 16 val intPregRdy = Vec(NRIntReadPorts + NRMemReadPorts, Output(Bool())) 17 val fpPregRdy = Vec(NRFpReadPorts, Output(Bool())) 18 // from decode buffer 19 val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl))) 20 // to dispatch1 21 val out = Vec(RenameWidth, DecoupledIO(new MicroOp)) 22 }) 23 24 val isWalk = ParallelOR(io.roqCommits.map(x => x.valid && x.bits.isWalk)).asBool() 25 26 val debug_exception = io.redirect.valid && io.redirect.bits.isException 27 val debug_walk = isWalk 28 val debug_norm = !(debug_exception || debug_walk) 29 30 def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = { 31 XSInfo( 32 debug_norm && in.valid && in.ready, 33 p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " + 34 p"lsrc1:${in.bits.ctrl.lsrc1} -> psrc1:${out.bits.psrc1} " + 35 p"lsrc2:${in.bits.ctrl.lsrc2} -> psrc2:${out.bits.psrc2} " + 36 p"lsrc3:${in.bits.ctrl.lsrc3} -> psrc3:${out.bits.psrc3} " + 37 p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " + 38 p"old_pdest:${out.bits.old_pdest} " + 39 p"out v:${out.valid} r:${out.ready}\n" 40 ) 41 } 42 43 for((x,y) <- io.in.zip(io.out)){ 44 printRenameInfo(x, y) 45 } 46 47 val fpFreeList, intFreeList = Module(new FreeList).io 48 val fpRat = Module(new RenameTable(float = true)).io 49 val intRat = Module(new RenameTable(float = false)).io 50 val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts + CommitWidth)).io 51 val intBusyTable = Module(new BusyTable(NRIntReadPorts+NRMemReadPorts, NRIntWritePorts + CommitWidth)).io 52 53 fpFreeList.redirect := io.redirect 54 intFreeList.redirect := io.redirect 55 56 val flush = io.redirect.valid && io.redirect.bits.isException 57 fpRat.flush := flush 58 intRat.flush := flush 59 fpBusyTable.flush := flush 60 intBusyTable.flush := flush 61 62 def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = { 63 {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)} 64 } 65 66 val uops = Wire(Vec(RenameWidth, new MicroOp)) 67 68 uops.foreach( uop => { 69// uop.brMask := DontCare 70// uop.brTag := DontCare 71 uop.src1State := DontCare 72 uop.src2State := DontCare 73 uop.src3State := DontCare 74 uop.roqIdx := DontCare 75 uop.moqIdx := DontCare 76 }) 77 78 var lastReady = WireInit(true.B) 79 for(i <- 0 until RenameWidth) { 80 uops(i).cf := io.in(i).bits.cf 81 uops(i).ctrl := io.in(i).bits.ctrl 82 uops(i).brTag := io.in(i).bits.brTag 83 84 val inValid = io.in(i).valid && !isWalk 85 86 // alloc a new phy reg 87 val needFpDest = inValid && needDestReg(fp = true, io.in(i).bits) 88 val needIntDest = inValid && needDestReg(fp = false, io.in(i).bits) 89 fpFreeList.allocReqs(i) := needFpDest && lastReady && io.out(i).ready 90 intFreeList.allocReqs(i) := needIntDest && lastReady && io.out(i).ready 91 val fpCanAlloc = fpFreeList.canAlloc(i) 92 val intCanAlloc = intFreeList.canAlloc(i) 93 val this_can_alloc = Mux( 94 needIntDest, 95 intCanAlloc, 96 Mux( 97 needFpDest, 98 fpCanAlloc, 99 true.B 100 ) 101 ) 102 io.in(i).ready := lastReady && io.out(i).ready && this_can_alloc && !isWalk 103 104 // do checkpoints when a branch inst come 105 for(fl <- Seq(fpFreeList, intFreeList)){ 106 fl.cpReqs(i).valid := inValid 107 fl.cpReqs(i).bits := io.in(i).bits.brTag 108 } 109 110 lastReady = io.in(i).ready 111 112 uops(i).pdest := Mux(needIntDest, 113 intFreeList.pdests(i), 114 Mux( 115 uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 116 0.U, fpFreeList.pdests(i) 117 ) 118 ) 119 120 io.out(i).valid := io.in(i).fire() 121 io.out(i).bits := uops(i) 122 123 // write rename table 124 def writeRat(fp: Boolean) = { 125 val rat = if(fp) fpRat else intRat 126 val freeList = if(fp) fpFreeList else intFreeList 127 val busyTable = if(fp) fpBusyTable else intBusyTable 128 // speculative inst write 129 val specWen = freeList.allocReqs(i) && freeList.canAlloc(i) 130 // walk back write 131 val commitDestValid = io.roqCommits(i).valid && needDestReg(fp, io.roqCommits(i).bits.uop) 132 val walkWen = commitDestValid && io.roqCommits(i).bits.isWalk 133 134 rat.specWritePorts(i).wen := specWen || walkWen 135 rat.specWritePorts(i).addr := Mux(specWen, uops(i).ctrl.ldest, io.roqCommits(i).bits.uop.ctrl.ldest) 136 rat.specWritePorts(i).wdata := Mux(specWen, freeList.pdests(i), io.roqCommits(i).bits.uop.old_pdest) 137 138 val numRfWritePorts = if(fp) NRFpWritePorts else NRIntWritePorts 139 140 busyTable.wbPregs(numRfWritePorts + i).valid := walkWen 141 busyTable.wbPregs(numRfWritePorts + i).bits := io.roqCommits(i).bits.uop.pdest 142 143 XSInfo(walkWen, 144 {if(fp) p"fp" else p"int "} + p"walk: pc:${Hexadecimal(io.roqCommits(i).bits.uop.cf.pc)}" + 145 p" ldest:${rat.specWritePorts(i).addr} old_pdest:${rat.specWritePorts(i).wdata}\n" 146 ) 147 148 rat.archWritePorts(i).wen := commitDestValid && !io.roqCommits(i).bits.isWalk 149 rat.archWritePorts(i).addr := io.roqCommits(i).bits.uop.ctrl.ldest 150 rat.archWritePorts(i).wdata := io.roqCommits(i).bits.uop.pdest 151 152 XSInfo(rat.archWritePorts(i).wen, 153 {if(fp) p"fp" else p"int "} + p" rat arch: ldest:${rat.archWritePorts(i).addr}" + 154 p" pdest:${rat.archWritePorts(i).wdata}\n" 155 ) 156 157 freeList.deallocReqs(i) := rat.archWritePorts(i).wen 158 freeList.deallocPregs(i) := io.roqCommits(i).bits.uop.old_pdest 159 160 // set phy reg status to busy 161 busyTable.allocPregs(i).valid := specWen 162 busyTable.allocPregs(i).bits := freeList.pdests(i) 163 } 164 165 writeRat(fp = false) 166 writeRat(fp = true) 167 168 // read rename table 169 def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = { 170 val rat = if(fp) fpRat else intRat 171 val srcCnt = lsrcList.size 172 val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W))) 173 val old_pdest = Wire(UInt(PhyRegIdxWidth.W)) 174 for(k <- 0 until srcCnt+1){ 175 val rportIdx = i * (srcCnt+1) + k 176 if(k != srcCnt){ 177 rat.readPorts(rportIdx).addr := lsrcList(k) 178 psrcVec(k) := rat.readPorts(rportIdx).rdata 179 } else { 180 rat.readPorts(rportIdx).addr := ldest 181 old_pdest := rat.readPorts(rportIdx).rdata 182 } 183 } 184 (psrcVec, old_pdest) 185 } 186 val lsrcList = List(uops(i).ctrl.lsrc1, uops(i).ctrl.lsrc2, uops(i).ctrl.lsrc3) 187 val ldest = uops(i).ctrl.ldest 188 val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false) 189 val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true) 190 uops(i).psrc1 := Mux(uops(i).ctrl.src1Type === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0)) 191 uops(i).psrc2 := Mux(uops(i).ctrl.src2Type === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1)) 192 uops(i).psrc3 := fpPhySrcVec(2) 193 uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest) 194 } 195 196 197 def updateBusyTable(fp: Boolean) = { 198 val wbResults = if(fp) io.wbFpResults else io.wbIntResults 199 val busyTable = if(fp) fpBusyTable else intBusyTable 200 for( 201 (wb, setPhyRegRdy) <- wbResults.zip(busyTable.wbPregs.take(if(fp) NRFpWritePorts else NRIntWritePorts)) 202 ){ 203 setPhyRegRdy.valid := wb.valid && needDestReg(fp, wb.bits.uop) 204 setPhyRegRdy.bits := wb.bits.uop.pdest 205 } 206 } 207 208 updateBusyTable(false) 209 updateBusyTable(true) 210 211 intBusyTable.rfReadAddr <> io.intRfReadAddr 212 intBusyTable.pregRdy <> io.intPregRdy 213 fpBusyTable.rfReadAddr <> io.fpRfReadAddr 214 fpBusyTable.pregRdy <> io.fpPregRdy 215} 216