xref: /XiangShan/src/main/scala/xiangshan/backend/issue/IssueQueue.scala (revision 10721ce6d3b3266c0a1e47257cefa3c9d65d73aa)
1package xiangshan.backend.issue
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6
7trait IQConst{
8  val iqSize = 8
9  val iqIdxWidth = log2Up(iqSize)
10  val layer1Size = iqSize
11  val layer2Size = iqSize/2
12}
13
14sealed abstract class IQBundle extends XSBundle with IQConst
15sealed abstract class IQModule extends XSModule with IQConst with NeedImpl
16
17sealed class CmpInputBundle extends IQBundle{
18  val instRdy = Input(Bool())
19  val roqIdx  = Input(UInt(RoqIdxWidth.W))
20  val iqIdx   = Input(UInt(iqIdxWidth.W))
21}
22
23
24sealed class CompareCircuitUnit(layer: Int = 0, id: Int = 0) extends IQModule {
25  val io = IO(new Bundle(){
26    val in1 = new CmpInputBundle
27    val in2 = new CmpInputBundle
28    val out = Flipped(new CmpInputBundle)
29  })
30
31  val roqIdx1 = io.in1.roqIdx
32  val roqIdx2 = io.in2.roqIdx
33  val iqIdx1  = io.in1.iqIdx
34  val iqIdx2  = io.in2.iqIdx
35
36  val inst1Rdy = io.in1.instRdy
37  val inst2Rdy = io.in2.instRdy
38
39  io.out.instRdy := inst1Rdy | inst2Rdy
40  io.out.roqIdx := roqIdx2
41  io.out.iqIdx := iqIdx2
42
43  when((inst1Rdy && !inst2Rdy) || (inst1Rdy && inst2Rdy && (roqIdx1 < roqIdx2))){
44    io.out.roqIdx := roqIdx1
45    io.out.iqIdx := iqIdx1
46  }
47
48}
49
50class IssueQueue(val fuTypeInt: BigInt, val wakeupCnt: Int, val bypassCnt: Int) extends IQModule {
51
52  val useBypass = bypassCnt > 0
53
54  val io = IO(new Bundle() {
55    // flush Issue Queue
56    val redirect = Flipped(ValidIO(new Redirect))
57
58    // enq Ctrl sigs at dispatch-2
59    val enqCtrl = Flipped(DecoupledIO(new MicroOp))
60    // enq Data at next cycle (regfile has 1 cycle latency)
61    val enqData = Flipped(ValidIO(new ExuInput))
62
63    //  broadcast selected uop to other issue queues which has bypasses
64    val selectedUop = if(useBypass) DecoupledIO(new MicroOp) else null
65
66    // send to exu
67    val deq = DecoupledIO(new ExuInput)
68
69    // listen to write back bus
70    val wakeUpPorts = Vec(wakeupCnt, Flipped(DecoupledIO(new ExuOutput)))
71
72    // use bypass uops to speculative wake-up
73    val bypassUops = if(useBypass) Vec(bypassCnt, Flipped(DecoupledIO(new MicroOp))) else null
74    val bypassData = if(useBypass) Vec(bypassCnt, Flipped(DecoupledIO(new ExuOutput))) else null
75  })
76  //---------------------------------------------------------
77  // Issue Queue
78  //---------------------------------------------------------
79
80  //Tag Queue
81  val ctrlFlow = Mem(iqSize,new CtrlFlow)
82  val ctrlSig = Mem(iqSize,new CtrlSignals)
83  val brMask  = RegInit(VecInit(Seq.fill(iqSize)(0.U(BrqSize.W))))
84  val valid   = RegInit(VecInit(Seq.fill(iqSize)(false.B)))
85  val src1Rdy = RegInit(VecInit(Seq.fill(iqSize)(false.B)))
86  val src2Rdy = RegInit(VecInit(Seq.fill(iqSize)(false.B)))
87  val src3Rdy = RegInit(VecInit(Seq.fill(iqSize)(false.B)))
88  val prfSrc1 = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
89  val prfSrc2 = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
90  val prfSrc3 = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
91  val prfDest = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
92  val oldPDest = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
93  val freelistAllocPtr = Reg(Vec(iqSize, UInt(PhyRegIdxWidth.W)))
94  val roqIdx  = Reg(Vec(iqSize, UInt(RoqIdxWidth.W)))
95
96  val instRdy = WireInit(VecInit(List.tabulate(iqSize)(i => src1Rdy(i) && src2Rdy(i) && valid(i))))
97
98
99  //tag enqueue
100  val iqEmty = !valid.asUInt.orR
101  val iqFull =  valid.asUInt.andR
102  val iqAllowIn = !iqFull
103  io.enqCtrl.ready := iqAllowIn
104
105  //enqueue pointer
106  val emptySlot = ~valid.asUInt
107  val enqueueSelect = PriorityEncoder(emptySlot)
108
109  when(io.enqCtrl.fire()){
110    ctrlFlow(enqueueSelect) := io.enqCtrl.bits.cf
111    ctrlSig(enqueueSelect) := io.enqCtrl.bits.ctrl
112    brMask(enqueueSelect) := io.enqCtrl.bits.brMask
113    valid(enqueueSelect) := true.B
114    src1Rdy(enqueueSelect) := io.enqCtrl.bits.src1State === SrcState.rdy
115    src2Rdy(enqueueSelect) := io.enqCtrl.bits.src2State === SrcState.rdy
116    src3Rdy(enqueueSelect) := io.enqCtrl.bits.src3State === SrcState.rdy
117    prfSrc1(enqueueSelect) := io.enqCtrl.bits.psrc1
118    prfSrc2(enqueueSelect) := io.enqCtrl.bits.psrc2
119    prfSrc3(enqueueSelect) := io.enqCtrl.bits.psrc3
120    prfDest(enqueueSelect) := io.enqCtrl.bits.pdest
121    oldPDest(enqueueSelect) := io.enqCtrl.bits.old_pdest
122    freelistAllocPtr(enqueueSelect) := io.enqCtrl.bits.freelistAllocPtr
123    roqIdx(enqueueSelect) := io.enqCtrl.bits.roqIdx
124
125  }
126
127  //Data Queue
128  val src1Data = Reg(Vec(iqSize, UInt(XLEN.W)))
129  val src2Data = Reg(Vec(iqSize, UInt(XLEN.W)))
130  val src3Data = Reg(Vec(iqSize, UInt(XLEN.W)))
131
132  val enqSelNext = RegNext(enqueueSelect)
133  val enqFireNext = RegNext(io.enqCtrl.fire())
134
135  // Read RegFile
136  when (enqFireNext) {
137    src1Data(enqSelNext) := io.enqData.bits.src1
138    src2Data(enqSelNext) := io.enqData.bits.src2
139    src3Data(enqSelNext) := io.enqData.bits.src3
140  }
141  // From Common Data Bus(wakeUpPort)
142  val cdbValid = List.tabulate(wakeupCnt)(i => io.wakeUpPorts(i).valid)
143  val cdbData = List.tabulate(wakeupCnt)(i => io.wakeUpPorts(i).bits.data)
144  val cdbPdest = List.tabulate(wakeupCnt)(i => io.wakeUpPorts(i).bits.uop.pdest)
145  List.tabulate(iqSize)(i =>
146    when (valid(i)) {
147      List.tabulate(wakeupCnt)(j =>
148        when(!src1Rdy(i) && prfSrc1(i) === cdbPdest(j) && cdbValid(j)) {
149          src1Rdy(i) := true.B
150          src1Data(i) := cdbData(j)
151        }
152      )
153      List.tabulate(wakeupCnt)(j =>
154        when(!src2Rdy(i) && prfSrc2(i) === cdbPdest(j) && cdbValid(j)) {
155          src2Rdy(i) := true.B
156          src2Data(i) := cdbData(j)
157        }
158      )
159      List.tabulate(wakeupCnt)(j =>
160        when(!src3Rdy(i) && prfSrc3(i) === cdbPdest(j) && cdbValid(j)) {
161          src3Rdy(i) := true.B
162          src3Data(i) := cdbData(j)
163        }
164      )
165    }
166  )
167
168  // From byPass [speculative] (just for ALU to listen to other ALU's res, include itself)
169  // just need Tag(Ctrl). send out Tag when Tag is decided. other ALUIQ listen to them and decide Tag
170
171
172  //---------------------------------------------------------
173  // Select Circuit
174  //---------------------------------------------------------
175  //layer 1
176  val layer1CCUs = (0 until layer1Size by 2) map { i =>
177    val CCU_1 = Module(new CompareCircuitUnit(layer = 1, id = i/2))
178    CCU_1.io.in1.instRdy := instRdy(i)
179    CCU_1.io.in1.roqIdx  := roqIdx(i)
180    CCU_1.io.in1.iqIdx   := i.U
181
182    CCU_1.io.in2.instRdy := instRdy(i+1)
183    CCU_1.io.in2.roqIdx  := roqIdx(i+1)
184    CCU_1.io.in2.iqIdx   := (i+1).U
185
186    CCU_1
187  }
188
189  //layer 2
190  val layer2CCUs = (0 until layer2Size by 2) map { i =>
191    val CCU_2 = Module(new CompareCircuitUnit(layer = 2, id = i/2))
192    CCU_2.io.in1.instRdy := layer1CCUs(i).io.out.instRdy
193    CCU_2.io.in1.roqIdx  := layer1CCUs(i).io.out.roqIdx
194    CCU_2.io.in1.iqIdx   := layer1CCUs(i).io.out.iqIdx
195
196    CCU_2.io.in2.instRdy := layer1CCUs(i+1).io.out.instRdy
197    CCU_2.io.in2.roqIdx  := layer1CCUs(i+1).io.out.roqIdx
198    CCU_2.io.in2.iqIdx   := layer1CCUs(i+1).io.out.iqIdx
199
200    CCU_2
201  }
202
203  //layer 3
204  val CCU_3 = Module(new CompareCircuitUnit(layer = 3, id = 0))
205  CCU_3.io.in1.instRdy := layer2CCUs(0).io.out.instRdy
206  CCU_3.io.in1.roqIdx  := layer2CCUs(0).io.out.roqIdx
207  CCU_3.io.in1.iqIdx   := layer2CCUs(0).io.out.iqIdx
208
209  CCU_3.io.in2.instRdy := layer2CCUs(1).io.out.instRdy
210  CCU_3.io.in2.roqIdx  := layer2CCUs(1).io.out.roqIdx
211  CCU_3.io.in2.iqIdx   := layer2CCUs(1).io.out.iqIdx
212
213
214  //Dequeue Logic
215  //hold the sel-index to wait for data
216  val selInstIdx = RegInit(0.U(iqIdxWidth.W))
217  val selInstRdy = RegInit(false.B)
218
219
220  selInstRdy := CCU_3.io.out.instRdy
221  selInstIdx := CCU_3.io.out.iqIdx
222
223  //issue the select instruction
224  val dequeueSelect = Wire(UInt(iqIdxWidth.W))
225  dequeueSelect := selInstIdx
226
227  val IQreadyGo = selInstRdy && enqFireNext
228
229  io.deq.valid := IQreadyGo
230
231  io.deq.bits.uop.psrc1 := prfSrc1(dequeueSelect)
232  io.deq.bits.uop.psrc2 := prfSrc2(dequeueSelect)
233  io.deq.bits.uop.psrc3 := prfSrc3(dequeueSelect)
234  io.deq.bits.uop.pdest := prfDest(dequeueSelect)
235  io.deq.bits.uop.old_pdest := oldPDest(dequeueSelect)
236  io.deq.bits.uop.src1State := SrcState.rdy
237  io.deq.bits.uop.src2State := SrcState.rdy
238  io.deq.bits.uop.src3State := SrcState.rdy
239  io.deq.bits.uop.freelistAllocPtr := freelistAllocPtr(dequeueSelect)
240  io.deq.bits.uop.roqIdx := roqIdx(dequeueSelect)
241
242  //TODO
243  io.deq.bits.redirect := DontCare
244
245  io.deq.bits.src1 := src1Data(dequeueSelect)
246  io.deq.bits.src2 := src2Data(dequeueSelect)
247  io.deq.bits.src3 := src3Data(dequeueSelect)
248
249
250
251
252
253
254}
255