xref: /XiangShan/src/main/scala/xiangshan/backend/CtrlBlock.scala (revision 10aac6e797f4a8acd3fd415838768f54151fec5b)
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, RoqLsqIO, 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 lsq = new RoqLsqIO
56    }
57  })
58
59  val decode = Module(new DecodeStage)
60  val brq = Module(new Brq)
61  val rename = Module(new Rename)
62  val dispatch = Module(new Dispatch)
63  val intBusyTable = Module(new BusyTable(NRIntReadPorts, NRIntWritePorts))
64  val fpBusyTable = Module(new BusyTable(NRFpReadPorts, NRFpWritePorts))
65
66  val roqWbSize = NRIntWritePorts + NRFpWritePorts + exuParameters.StuCnt + 1
67
68  val roq = Module(new Roq(roqWbSize))
69
70  // When replay and mis-prediction have the same roqIdx,
71  // mis-prediction should have higher priority, since mis-prediction flushes the load instruction.
72  // Thus, only when mis-prediction roqIdx is after replay roqIdx, replay should be valid.
73  val brqIsAfterLsq = isAfter(brq.io.redirectOut.bits.roqIdx, io.fromLsBlock.replay.bits.roqIdx)
74  val redirectArb = Mux(io.fromLsBlock.replay.valid && (!brq.io.redirectOut.valid || brqIsAfterLsq),
75    io.fromLsBlock.replay.bits, brq.io.redirectOut.bits)
76  val redirectValid = roq.io.redirectOut.valid || brq.io.redirectOut.valid || io.fromLsBlock.replay.valid
77  val redirect = Mux(roq.io.redirectOut.valid, roq.io.redirectOut.bits, redirectArb)
78
79  io.frontend.redirect.valid := RegNext(redirectValid)
80  io.frontend.redirect.bits := RegNext(Mux(roq.io.redirectOut.valid, roq.io.redirectOut.bits.target, redirectArb.target))
81  io.frontend.cfiUpdateInfo <> brq.io.cfiInfo
82
83  decode.io.in <> io.frontend.cfVec
84  decode.io.enqBrq <> brq.io.enq
85
86  brq.io.redirect.valid <> redirectValid
87  brq.io.redirect.bits <> redirect
88  brq.io.bcommit <> roq.io.bcommit
89  brq.io.exuRedirectWb <> io.fromIntBlock.exuRedirect
90  brq.io.pcReadReq.brqIdx := dispatch.io.enqIQCtrl(0).bits.brTag // jump
91  io.toIntBlock.jumpPc := brq.io.pcReadReq.pc
92
93  // pipeline between decode and dispatch
94  val lastCycleRedirect = RegNext(redirectValid)
95  for (i <- 0 until RenameWidth) {
96    PipelineConnect(decode.io.out(i), rename.io.in(i), rename.io.in(i).ready, redirectValid || lastCycleRedirect)
97  }
98
99  rename.io.redirect.valid <> redirectValid
100  rename.io.redirect.bits <> redirect
101  rename.io.roqCommits <> roq.io.commits
102  rename.io.out <> dispatch.io.fromRename
103  rename.io.renameBypass <> dispatch.io.renameBypass
104
105  dispatch.io.redirect.valid <> redirectValid
106  dispatch.io.redirect.bits <> redirect
107  dispatch.io.enqRoq <> roq.io.enq
108  dispatch.io.enqLsq <> io.toLsBlock.enqLsq
109  dispatch.io.readIntRf <> io.toIntBlock.readRf
110  dispatch.io.readFpRf <> io.toFpBlock.readRf
111  dispatch.io.allocPregs.zipWithIndex.foreach { case (preg, i) =>
112    intBusyTable.io.allocPregs(i).valid := preg.isInt
113    fpBusyTable.io.allocPregs(i).valid := preg.isFp
114    intBusyTable.io.allocPregs(i).bits := preg.preg
115    fpBusyTable.io.allocPregs(i).bits := preg.preg
116  }
117  dispatch.io.numExist <> io.fromIntBlock.numExist ++ io.fromFpBlock.numExist ++ io.fromLsBlock.numExist
118  dispatch.io.enqIQCtrl <> io.toIntBlock.enqIqCtrl ++ io.toFpBlock.enqIqCtrl ++ io.toLsBlock.enqIqCtrl
119//  dispatch.io.enqIQData <> io.toIntBlock.enqIqData ++ io.toFpBlock.enqIqData ++ io.toLsBlock.enqIqData
120
121
122  val flush = redirectValid && RedirectLevel.isUnconditional(redirect.level)
123  fpBusyTable.io.flush := flush
124  intBusyTable.io.flush := flush
125  for((wb, setPhyRegRdy) <- io.fromIntBlock.wbRegs.zip(intBusyTable.io.wbPregs)){
126    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.rfWen
127    setPhyRegRdy.bits := wb.bits.uop.pdest
128  }
129  for((wb, setPhyRegRdy) <- io.fromFpBlock.wbRegs.zip(fpBusyTable.io.wbPregs)){
130    setPhyRegRdy.valid := wb.valid && wb.bits.uop.ctrl.fpWen
131    setPhyRegRdy.bits := wb.bits.uop.pdest
132  }
133  intBusyTable.io.read <> dispatch.io.readIntState
134  fpBusyTable.io.read <> dispatch.io.readFpState
135
136  roq.io.redirect.valid := brq.io.redirectOut.valid || io.fromLsBlock.replay.valid
137  roq.io.redirect.bits <> redirectArb
138  roq.io.exeWbResults.take(roqWbSize-1).zip(
139    io.fromIntBlock.wbRegs ++ io.fromFpBlock.wbRegs ++ io.fromLsBlock.stOut
140  ).foreach{
141    case(x, y) =>
142      x.bits := y.bits
143      x.valid := y.valid && !y.bits.redirectValid
144  }
145  roq.io.exeWbResults.last := brq.io.out
146
147  io.toIntBlock.redirect.valid := redirectValid
148  io.toIntBlock.redirect.bits := redirect
149  io.toFpBlock.redirect.valid := redirectValid
150  io.toFpBlock.redirect.bits := redirect
151  io.toLsBlock.redirect.valid := redirectValid
152  io.toLsBlock.redirect.bits := redirect
153
154  dispatch.io.readPortIndex.intIndex <> io.toIntBlock.readPortIndex
155  dispatch.io.readPortIndex.fpIndex <> io.toFpBlock.readPortIndex
156
157  // roq to int block
158  io.roqio.toCSR <> roq.io.csr
159  io.roqio.exception.valid := roq.io.redirectOut.valid && roq.io.redirectOut.bits.isException()
160  io.roqio.exception.bits := roq.io.exception
161  io.roqio.isInterrupt := roq.io.redirectOut.bits.interrupt
162  // roq to mem block
163  io.roqio.lsq <> roq.io.lsq
164}
165