1package xiangshan.backend 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.backend.decode.DecodeStage 8import xiangshan.backend.rename.{BusyTable, Rename} 9import xiangshan.backend.brq.{Brq, BrqPcRead} 10import xiangshan.backend.dispatch.Dispatch 11import xiangshan.backend.exu._ 12import xiangshan.backend.exu.Exu.exuConfigs 13import xiangshan.backend.regfile.RfReadPort 14import xiangshan.backend.roq.{Roq, RoqCSRIO, RoqPtr} 15import xiangshan.mem.LsqEnqIO 16 17class CtrlToIntBlockIO extends XSBundle { 18 val enqIqCtrl = Vec(exuParameters.IntExuCnt, DecoupledIO(new MicroOp)) 19 val readRf = Vec(NRIntReadPorts, Output(UInt(PhyRegIdxWidth.W))) 20 val jumpPc = Output(UInt(VAddrBits.W)) 21 // int block only uses port 0~7 22 val readPortIndex = Vec(exuParameters.IntExuCnt, Output(UInt(log2Ceil(8 / 2).W))) // TODO parameterize 8 here 23 val redirect = ValidIO(new Redirect) 24} 25 26class CtrlToFpBlockIO extends XSBundle { 27 val enqIqCtrl = Vec(exuParameters.FpExuCnt, DecoupledIO(new MicroOp)) 28 val readRf = Vec(NRFpReadPorts, Output(UInt(PhyRegIdxWidth.W))) 29 // fp block uses port 0~11 30 val readPortIndex = Vec(exuParameters.FpExuCnt, Output(UInt(log2Ceil((NRFpReadPorts - exuParameters.StuCnt) / 3).W))) 31 val redirect = ValidIO(new Redirect) 32} 33 34class CtrlToLsBlockIO extends XSBundle { 35 val enqIqCtrl = Vec(exuParameters.LsExuCnt, DecoupledIO(new MicroOp)) 36 val enqLsq = Flipped(new LsqEnqIO) 37 val redirect = ValidIO(new Redirect) 38} 39 40class CtrlBlock extends XSModule with HasCircularQueuePtrHelper { 41 val io = IO(new Bundle { 42 val frontend = Flipped(new FrontendToBackendIO) 43 val fromIntBlock = Flipped(new IntBlockToCtrlIO) 44 val fromFpBlock = Flipped(new FpBlockToCtrlIO) 45 val fromLsBlock = Flipped(new LsBlockToCtrlIO) 46 val toIntBlock = new CtrlToIntBlockIO 47 val toFpBlock = new CtrlToFpBlockIO 48 val toLsBlock = new CtrlToLsBlockIO 49 val roqio = new Bundle { 50 // to int block 51 val toCSR = new RoqCSRIO 52 val exception = ValidIO(new MicroOp) 53 val isInterrupt = Output(Bool()) 54 // to mem block 55 val commits = new RoqCommitIO 56 val roqDeqPtr = Output(new RoqPtr) 57 } 58 }) 59 60 val difftestIO = IO(new Bundle() { 61 val fromRoq = new Bundle() { 62 val commit = Output(UInt(32.W)) 63 val thisPC = Output(UInt(XLEN.W)) 64 val thisINST = Output(UInt(32.W)) 65 val skip = Output(UInt(32.W)) 66 val wen = Output(UInt(32.W)) 67 val wdata = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6 68 val wdst = Output(Vec(CommitWidth, UInt(32.W))) // set difftest width to 6 69 val wpc = Output(Vec(CommitWidth, UInt(XLEN.W))) // set difftest width to 6 70 val isRVC = Output(UInt(32.W)) 71 val scFailed = Output(Bool()) 72 } 73 }) 74 difftestIO <> DontCare 75 76 val decode = Module(new DecodeStage) 77 val brq = Module(new Brq) 78 val rename = Module(new Rename) 79 val dispatch = Module(new Dispatch) 80 val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts)) 81 val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts)) 82 83 val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt + 1 84 85 val roq = Module(new Roq(roqWbSize)) 86 87 // When replay and mis-prediction have the same roqIdx, 88 // mis-prediction should have higher priority, since mis-prediction flushes the load instruction. 89 // Thus, only when mis-prediction roqIdx is after replay roqIdx, replay should be valid. 90 val brqIsAfterLsq = isAfter(brq.io.redirectOut.bits.roqIdx, io.fromLsBlock.replay.bits.roqIdx) 91 val redirectArb = Mux(io.fromLsBlock.replay.valid && (!brq.io.redirectOut.valid || brqIsAfterLsq), 92 io.fromLsBlock.replay.bits, brq.io.redirectOut.bits) 93 val redirectValid = roq.io.redirectOut.valid || brq.io.redirectOut.valid || io.fromLsBlock.replay.valid 94 val redirect = Mux(roq.io.redirectOut.valid, roq.io.redirectOut.bits, redirectArb) 95 96 io.frontend.redirect.valid := RegNext(redirectValid) 97 io.frontend.redirect.bits := RegNext(Mux(roq.io.redirectOut.valid, roq.io.redirectOut.bits.target, redirectArb.target)) 98 io.frontend.cfiUpdateInfo <> brq.io.cfiInfo 99 100 decode.io.in <> io.frontend.cfVec 101 decode.io.enqBrq <> brq.io.enq 102 103 brq.io.redirect.valid <> redirectValid 104 brq.io.redirect.bits <> redirect 105 brq.io.bcommit <> roq.io.bcommit 106 brq.io.exuRedirectWb <> io.fromIntBlock.exuRedirect 107 brq.io.pcReadReq.brqIdx := dispatch.io.enqIQCtrl(0).bits.brTag // jump 108 io.toIntBlock.jumpPc := brq.io.pcReadReq.pc 109 110 // pipeline between decode and dispatch 111 val lastCycleRedirect = RegNext(redirectValid) 112 for (i <- 0 until RenameWidth) { 113 PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready, redirectValid || lastCycleRedirect) 114 } 115 116 rename.io.redirect.valid <> redirectValid 117 rename.io.redirect.bits <> redirect 118 rename.io.roqCommits <> roq.io.commits 119 rename.io.out <> dispatch.io.fromRename 120 rename.io.renameBypass <> dispatch.io.renameBypass 121 122 dispatch.io.redirect.valid <> redirectValid 123 dispatch.io.redirect.bits <> redirect 124 dispatch.io.enqRoq <> roq.io.enq 125 dispatch.io.enqLsq <> io.toLsBlock.enqLsq 126 dispatch.io.readIntRf <> io.toIntBlock.readRf 127 dispatch.io.readFpRf <> io.toFpBlock.readRf 128 dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) => 129 intBusyTable.io.allocPregs(i).valid := preg.isInt 130 fpBusyTable.io.allocPregs(i).valid := preg.isFp 131 intBusyTable.io.allocPregs(i).bits := preg.preg 132 fpBusyTable.io.allocPregs(i).bits := preg.preg 133 } 134 dispatch.io.numExist <> io.fromIntBlock.numExist ++ io.fromFpBlock.numExist ++ io.fromLsBlock.numExist 135 dispatch.io.enqIQCtrl <> io.toIntBlock.enqIqCtrl ++ io.toFpBlock.enqIqCtrl ++ io.toLsBlock.enqIqCtrl 136// dispatch.io.enqIQData <> io.toIntBlock.enqIqData ++ io.toFpBlock.enqIqData ++ io.toLsBlock.enqIqData 137 138 139 val flush = redirectValid && RedirectLevel.isUnconditional(redirect.level) 140 fpBusyTable.io.flush := flush 141 intBusyTable.io.flush := flush 142 for((wb, setPhyRegRdy) <- io.fromIntBlock.wbRegs.zip(intBusyTable.io.wbPregs)){ 143 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen 144 setPhyRegRdy.bits := wb.bits.uop.pdest 145 } 146 for((wb, setPhyRegRdy) <- io.fromFpBlock.wbRegs.zip(fpBusyTable.io.wbPregs)){ 147 setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen 148 setPhyRegRdy.bits := wb.bits.uop.pdest 149 } 150 intBusyTable.io.read <> dispatch.io.readIntState 151 fpBusyTable.io.read <> dispatch.io.readFpState 152 153 roq.io.redirect.valid := brq.io.redirectOut.valid || io.fromLsBlock.replay.valid 154 roq.io.redirect.bits <> redirectArb 155 roq.io.exeWbResults.take(roqWbSize-1).zip( 156 io.fromIntBlock.wbRegs ++ io.fromFpBlock.wbRegs ++ io.fromLsBlock.stOut 157 ).foreach{ 158 case(x, y) => 159 x.bits := y.bits 160 x.valid := y.valid && !y.bits.redirectValid 161 } 162 roq.io.exeWbResults.last := brq.io.out 163 164 if (env.DualCoreDifftest) { 165 difftestIO.fromRoq <> roq.difftestIO 166 } 167 168 io.toIntBlock.redirect.valid := redirectValid 169 io.toIntBlock.redirect.bits := redirect 170 io.toFpBlock.redirect.valid := redirectValid 171 io.toFpBlock.redirect.bits := redirect 172 io.toLsBlock.redirect.valid := redirectValid 173 io.toLsBlock.redirect.bits := redirect 174 175 dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex 176 dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex 177 178 // roq to int block 179 io.roqio.toCSR <> roq.io.csr 180 io.roqio.exception.valid := roq.io.redirectOut.valid && roq.io.redirectOut.bits.isException() 181 io.roqio.exception.bits := roq.io.exception 182 io.roqio.isInterrupt := roq.io.redirectOut.bits.interrupt 183 // roq to mem block 184 io.roqio.roqDeqPtr := roq.io.roqDeqPtr 185 io.roqio.commits := roq.io.commits 186} 187