xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision 8dedb8e72b90ded9f9412788026f27545772a2be)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import device.RAMHelper
6import xiangshan._
7import utils._
8import xiangshan.cache._
9import chisel3.experimental.chiselName
10
11trait HasIFUConst extends HasXSParameter {
12  val resetVector = 0x80000000L//TODO: set reset vec
13  def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W))
14  val groupBytes = FetchWidth * 4 * 2 // correspond to cache line size
15  val groupOffsetBits = log2Ceil(groupBytes)
16  val nBanksInPacket = 2
17  val bankBytes = PredictWidth * 2 / nBanksInPacket
18  val nBanksInGroup = groupBytes / bankBytes
19  val bankWidth = PredictWidth / nBanksInPacket
20  val bankOffsetBits = log2Ceil(bankBytes)
21  // (0, nBanksInGroup-1)
22  def bankInGroup(pc: UInt) = pc(groupOffsetBits-1,bankOffsetBits)
23  def isInLastBank(pc: UInt) = bankInGroup(pc) === (nBanksInGroup-1).U
24  // (0, bankBytes/2-1)
25  def offsetInBank(pc: UInt) = pc(bankOffsetBits-1,1)
26  def bankAligned(pc: UInt)  = align(pc, bankBytes)
27  def groupAligned(pc: UInt) = align(pc, groupBytes)
28  // each 1 bit in mask stands for 2 Bytes
29  // 8 bits, in which only the first 7 bits could be 0
30  def maskFirstHalf(pc: UInt): UInt = ((~(0.U(bankWidth.W))) >> offsetInBank(pc))(bankWidth-1,0)
31  // when in loop(buffer), we need to make use of the full packet
32  // and get the real mask in iCacheResp from loop buffer
33  // we may make predictions on more instructions than we could get from loop buffer
34  // and this will be handled in if4
35  def maskLastHalf(pc: UInt, inLoop: Bool = false.B): UInt = Mux(isInLastBank(pc) && !inLoop, 0.U(bankWidth.W), ~0.U(bankWidth.W))
36  def mask(pc: UInt, inLoop: Bool = false.B): UInt = Reverse(Cat(maskFirstHalf(pc), maskLastHalf(pc, inLoop)))
37  def snpc(pc: UInt, inLoop: Bool = false.B): UInt = pc + (PopCount(mask(pc, inLoop)) << 1)
38
39  val enableGhistRepair = true
40  val IFUDebug = true
41}
42
43class GlobalHistory extends XSBundle {
44  val predHist = UInt(HistoryLength.W)
45  // val sawNTBr = Bool()
46  // val takenOnBr = Bool()
47  // val saveHalfRVI = Bool()
48  // def shifted = takenOnBr || sawNTBr
49  // def newPtr(ptr: UInt = nowPtr): UInt = Mux(shifted, ptr - 1.U, ptr)
50  def update(sawNTBr: Bool, takenOnBr: Bool, hist: UInt = predHist): GlobalHistory = {
51    val g = Wire(new GlobalHistory)
52    val shifted = takenOnBr || sawNTBr
53    g.predHist := Mux(shifted, (hist << 1) | takenOnBr.asUInt, hist)
54    g
55  }
56
57  final def === (that: GlobalHistory): Bool = {
58    predHist === that.predHist
59  }
60
61  final def =/= (that: GlobalHistory): Bool = !(this === that)
62
63  implicit val name = "IFU"
64  def debug(where: String) = XSDebug(p"[${where}_GlobalHistory] hist=${Binary(predHist)}\n")
65  // override def toString(): String = "histPtr=%d, sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d".format(histPtr, sawNTBr, takenOnBr, saveHalfRVI)
66}
67
68
69class IFUIO extends XSBundle
70{
71  val fetchPacket = DecoupledIO(new FetchPacket)
72  val redirect = Flipped(ValidIO(UInt(VAddrBits.W)))
73  // val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo))
74  val cfiUpdateInfo = Flipped(ValidIO(new CfiUpdateInfo))
75  val icacheReq = DecoupledIO(new ICacheReq)
76  val icacheResp = Flipped(DecoupledIO(new ICacheResp))
77  val icacheFlush = Output(UInt(2.W))
78  // val loopBufPar = Flipped(new LoopBufferParameters)
79}
80
81class PrevHalfInstr extends XSBundle {
82  val valid = Bool()
83  val taken = Bool()
84  val ghInfo = new GlobalHistory()
85  val fetchpc = UInt(VAddrBits.W) // only for debug
86  val idx = UInt(VAddrBits.W) // only for debug
87  val pc = UInt(VAddrBits.W)
88  val npc = UInt(VAddrBits.W)
89  val target = UInt(VAddrBits.W)
90  val instr = UInt(16.W)
91  val ipf = Bool()
92  val newPtr = UInt(log2Up(ExtHistoryLength).W)
93}
94
95@chiselName
96class IFU extends XSModule with HasIFUConst
97{
98  val io = IO(new IFUIO)
99  val bpu = BPU(EnableBPU)
100  val pd = Module(new PreDecode)
101  val loopBuffer = if(EnableLB) { Module(new LoopBuffer) } else { Module(new FakeLoopBuffer) }
102
103  val if2_redirect, if3_redirect, if4_redirect = WireInit(false.B)
104  val if1_flush, if2_flush, if3_flush, if4_flush = WireInit(false.B)
105
106  val loopBufPar = loopBuffer.io.loopBufPar
107  val inLoop = WireInit(loopBuffer.io.out.valid)
108  val icacheResp = WireInit(Mux(inLoop, loopBuffer.io.out.bits, io.icacheResp.bits))
109
110  if4_flush := io.redirect.valid || loopBufPar.LBredirect.valid
111  if3_flush := if4_flush || if4_redirect
112  if2_flush := if3_flush || if3_redirect
113  if1_flush := if2_flush || if2_redirect
114
115  loopBuffer.io.flush := io.redirect.valid
116
117  //********************** IF1 ****************************//
118  val if1_valid = !reset.asBool && GTimer() > 500.U
119  val if1_npc = WireInit(0.U(VAddrBits.W))
120  val if2_ready = WireInit(false.B)
121  val if2_allReady = WireInit(if2_ready && (inLoop || io.icacheReq.ready))
122  val if1_fire = if1_valid && (if2_allReady || if1_flush)
123
124
125  // val if2_newPtr, if3_newPtr, if4_newPtr = Wire(UInt(log2Up(ExtHistoryLength).W))
126
127  val if1_gh, if2_gh, if3_gh, if4_gh = Wire(new GlobalHistory)
128  val if2_predicted_gh, if3_predicted_gh, if4_predicted_gh = Wire(new GlobalHistory)
129  val final_gh = RegInit(0.U.asTypeOf(new GlobalHistory))
130  val final_gh_bypass = WireInit(0.U.asTypeOf(new GlobalHistory))
131  val flush_final_gh = WireInit(false.B)
132
133  //********************** IF2 ****************************//
134  val if2_valid = RegInit(init = false.B)
135  val if3_ready = WireInit(false.B)
136  val if2_fire = if2_valid && if3_ready
137  val if2_pc = RegEnable(next = if1_npc, init = resetVector.U, enable = if1_fire)
138  val if2_snpc = snpc(if2_pc, inLoop)
139  val if2_predHist = RegEnable(if1_gh.predHist, enable=if1_fire)
140  if2_ready := if3_ready || !if2_valid
141  when (if1_fire)       { if2_valid := true.B }
142  .elsewhen (if2_flush) { if2_valid := false.B }
143  .elsewhen (if2_fire)  { if2_valid := false.B }
144
145  val npcGen = new PriorityMuxGenerator[UInt]
146  npcGen.register(true.B, RegNext(if1_npc))
147  npcGen.register(if2_fire, if2_snpc)
148  val if2_bp = bpu.io.out(0)
149
150  // if taken, bp_redirect should be true
151  // when taken on half RVI, we suppress this redirect signal
152  if2_redirect := if2_valid && if2_bp.taken
153  npcGen.register(if2_redirect, if2_bp.target)
154
155  if2_predicted_gh := if2_gh.update(if2_bp.hasNotTakenBrs, if2_bp.takenOnBr)
156
157  //********************** IF3 ****************************//
158  val if3_valid = RegInit(init = false.B)
159  val if4_ready = WireInit(false.B)
160  val if3_allValid = if3_valid && (inLoop || io.icacheResp.valid)
161  val if3_fire = if3_allValid && if4_ready
162  val if3_pc = RegEnable(if2_pc, if2_fire)
163  val if3_predHist = RegEnable(if2_predHist, enable=if2_fire)
164  if3_ready := if4_ready || !if3_valid
165  when (if3_flush)     { if3_valid := false.B }
166  .elsewhen (if2_fire) { if3_valid := true.B }
167  .elsewhen (if3_fire) { if3_valid := false.B }
168
169  val if3_bp = bpu.io.out(1)
170  if3_predicted_gh := if3_gh.update(if3_bp.hasNotTakenBrs, if3_bp.takenOnBr)
171
172
173  val prevHalfInstrReq = Wire(new PrevHalfInstr)
174  // only valid when if4_fire
175  val hasPrevHalfInstrReq = prevHalfInstrReq.valid
176
177  val if3_prevHalfInstr = RegInit(0.U.asTypeOf(new PrevHalfInstr))
178
179  // 32-bit instr crosses 2 pages, and the higher 16-bit triggers page fault
180  val crossPageIPF = WireInit(false.B)
181
182  val if3_pendingPrevHalfInstr = if3_prevHalfInstr.valid
183
184  // the previous half of RVI instruction waits until it meets its last half
185  val if3_prevHalfInstrMet = if3_pendingPrevHalfInstr && if3_prevHalfInstr.npc === if3_pc && if3_allValid
186  // set to invalid once consumed or redirect from backend
187  val if3_prevHalfConsumed = if3_prevHalfInstrMet && if3_fire
188  val if3_prevHalfFlush = if4_flush
189  when (hasPrevHalfInstrReq) {
190    if3_prevHalfInstr := prevHalfInstrReq
191  }.elsewhen (if3_prevHalfConsumed || if3_prevHalfFlush) {
192    if3_prevHalfInstr.valid := false.B
193  }
194
195  // when bp signal a redirect, we distinguish between taken and not taken
196  // if taken and saveHalfRVI is true, we do not redirect to the target
197
198  def if3_nextValidPCNotEquals(pc: UInt) = !if2_valid || if2_valid && if2_pc =/= pc
199  val if3_prevHalfMetRedirect    = if3_pendingPrevHalfInstr && if3_prevHalfInstrMet && if3_prevHalfInstr.taken && if3_nextValidPCNotEquals(if3_prevHalfInstr.target)
200  val if3_prevHalfNotMetRedirect = if3_pendingPrevHalfInstr && !if3_prevHalfInstrMet && if3_nextValidPCNotEquals(if3_prevHalfInstr.pc + 2.U)
201  val if3_predTakenRedirect    = !if3_pendingPrevHalfInstr && if3_bp.taken && if3_nextValidPCNotEquals(if3_bp.target)
202  val if3_predNotTakenRedirect = !if3_pendingPrevHalfInstr && !if3_bp.taken && if3_nextValidPCNotEquals(snpc(if3_pc, inLoop))
203  // when pendingPrevHalfInstr, if3_GHInfo is set to the info of last prev half instr
204  // val if3_ghInfoNotIdenticalRedirect = !if3_pendingPrevHalfInstr && if3_GHInfo =/= if3_lastGHInfo && enableGhistRepair.B
205
206  if3_redirect := if3_allValid && (
207                    // prevHalf is consumed but the next packet is not where it meant to be
208                    // we do not handle this condition because of the burden of building a correct GHInfo
209                    // prevHalfMetRedirect ||
210                    // prevHalf does not match if3_pc and the next fetch packet is not snpc
211                    if3_prevHalfNotMetRedirect ||
212                    // pred taken and next fetch packet is not the predicted target
213                    if3_predTakenRedirect ||
214                    // pred not taken and next fetch packet is not snpc
215                    if3_predNotTakenRedirect
216                    // GHInfo from last pred does not corresponds with this packet
217                    // if3_ghInfoNotIdenticalRedirect
218                  )
219
220  val if3_target = WireInit(snpc(if3_pc))
221
222  /* when (prevHalfMetRedirect) {
223    if1_npc := if3_prevHalfInstr.target
224  }.else */
225  when (if3_prevHalfNotMetRedirect) {
226    if3_target := if3_prevHalfInstr.pc + 2.U
227  }.elsewhen (if3_predTakenRedirect) {
228    if3_target := if3_bp.target
229  }.elsewhen (if3_predNotTakenRedirect) {
230    if3_target := snpc(if3_pc)
231  }
232  // }.elsewhen (if3_ghInfoNotIdenticalRedirect) {
233  //   if3_target := Mux(if3_bp.taken, if3_bp.target, snpc(if3_pc))
234  // }
235  npcGen.register(if3_redirect, if3_target)
236
237  // when (if3_redirect) {
238  //   if1_npc := if3_target
239  // }
240
241  //********************** IF4 ****************************//
242  val if4_pd = RegEnable(pd.io.out, if3_fire)
243  val if4_ipf = RegEnable(icacheResp.ipf || if3_prevHalfInstrMet && if3_prevHalfInstr.ipf, if3_fire)
244  val if4_acf = RegEnable(icacheResp.acf, if3_fire)
245  val if4_crossPageIPF = RegEnable(crossPageIPF, if3_fire)
246  val if4_valid = RegInit(false.B)
247  val if4_fire = if4_valid && io.fetchPacket.ready
248  val if4_pc = RegEnable(if3_pc, if3_fire)
249  // This is the real mask given from icache or loop buffer
250  val if4_mask = RegEnable(icacheResp.mask, if3_fire)
251  val if4_snpc = Mux(inLoop, if4_pc + (PopCount(if4_mask) << 1), snpc(if4_pc))
252
253
254  val if4_predHist = RegEnable(if3_predHist, enable=if3_fire)
255  // wait until prevHalfInstr written into reg
256  if4_ready := (io.fetchPacket.ready && !hasPrevHalfInstrReq || !if4_valid) && GTimer() > 500.U
257  when (if4_flush)     { if4_valid := false.B }
258  .elsewhen (if3_fire) { if4_valid := true.B }
259  .elsewhen (if4_fire) { if4_valid := false.B }
260
261  val if4_bp = Wire(new BranchPrediction)
262  if4_bp := bpu.io.out(2)
263  if4_bp.takens  := bpu.io.out(2).takens & if4_mask
264  if4_bp.brMask  := bpu.io.out(2).brMask & if4_mask
265  if4_bp.jalMask := bpu.io.out(2).jalMask & if4_mask
266
267  if4_predicted_gh := if4_gh.update(if4_bp.hasNotTakenBrs, if4_bp.takenOnBr)
268
269  def cal_jal_tgt(inst: UInt, rvc: Bool): UInt = {
270    Mux(rvc,
271      SignExt(Cat(inst(12), inst(8), inst(10, 9), inst(6), inst(7), inst(2), inst(11), inst(5, 3), 0.U(1.W)), XLEN),
272      SignExt(Cat(inst(31), inst(19, 12), inst(20), inst(30, 21), 0.U(1.W)), XLEN)
273    )
274  }
275  val if4_instrs = if4_pd.instrs
276  val if4_jals = if4_bp.jalMask
277  val if4_jal_tgts = VecInit((0 until PredictWidth).map(i => if4_pd.pc(i) + cal_jal_tgt(if4_instrs(i), if4_pd.pd(i).isRVC)))
278
279  (0 until PredictWidth).foreach {i =>
280    when (if4_jals(i)) {
281      if4_bp.targets(i) := if4_jal_tgts(i)
282    }
283  }
284
285  // we need this to tell BPU the prediction of prev half
286  // because the prediction is with the start of each inst
287  val if4_prevHalfInstr = RegInit(0.U.asTypeOf(new PrevHalfInstr))
288  val if4_pendingPrevHalfInstr = if4_prevHalfInstr.valid
289  val if4_prevHalfInstrMet = if4_pendingPrevHalfInstr && if4_prevHalfInstr.npc === if4_pc && if4_valid
290  val if4_prevHalfConsumed = if4_prevHalfInstrMet && if4_fire
291  val if4_prevHalfFlush = if4_flush
292
293  val if4_takenPrevHalf = WireInit(if4_prevHalfInstrMet && if4_prevHalfInstr.taken)
294  when (if3_prevHalfConsumed) {
295    if4_prevHalfInstr := if3_prevHalfInstr
296  }.elsewhen (if4_prevHalfConsumed || if4_prevHalfFlush) {
297    if4_prevHalfInstr.valid := false.B
298  }
299
300  prevHalfInstrReq := 0.U.asTypeOf(new PrevHalfInstr)
301  when (if4_fire && if4_bp.saveHalfRVI) {
302    val idx = if4_bp.lastHalfRVIIdx
303    prevHalfInstrReq.valid := true.B
304    // this is result of the last half RVI
305    prevHalfInstrReq.taken := if4_bp.lastHalfRVITaken
306    prevHalfInstrReq.ghInfo := if4_gh
307    prevHalfInstrReq.newPtr := DontCare
308    prevHalfInstrReq.fetchpc := if4_pc
309    prevHalfInstrReq.idx := idx
310    prevHalfInstrReq.pc := if4_pd.pc(idx)
311    prevHalfInstrReq.npc := if4_pd.pc(idx) + 2.U
312    prevHalfInstrReq.target := if4_bp.lastHalfRVITarget
313    prevHalfInstrReq.instr := if4_pd.instrs(idx)(15, 0)
314    prevHalfInstrReq.ipf := if4_ipf
315  }
316
317  def if4_nextValidPCNotEquals(pc: UInt) = if3_valid  && if3_pc =/= pc ||
318                                           !if3_valid && (if2_valid && if2_pc =/= pc) ||
319                                           !if3_valid && !if2_valid
320
321  val if4_prevHalfNextNotMet = hasPrevHalfInstrReq && if4_nextValidPCNotEquals(prevHalfInstrReq.pc+2.U)
322  val if4_predTakenRedirect = !hasPrevHalfInstrReq && if4_bp.taken && if4_nextValidPCNotEquals(if4_bp.target)
323  val if4_predNotTakenRedirect = !hasPrevHalfInstrReq && !if4_bp.taken && if4_nextValidPCNotEquals(if4_snpc)
324  // val if4_ghInfoNotIdenticalRedirect = if4_GHInfo =/= if4_lastGHInfo && enableGhistRepair.B
325
326  if4_redirect := if4_valid && (
327                    // when if4 has a lastHalfRVI, but the next fetch packet is not snpc
328                    // if4_prevHalfNextNotMet ||
329                    // when if4 preds taken, but the pc of next fetch packet is not the target
330                    if4_predTakenRedirect ||
331                    // when if4 preds not taken, but the pc of next fetch packet is not snpc
332                    if4_predNotTakenRedirect
333                    // GHInfo from last pred does not corresponds with this packet
334                    // if4_ghInfoNotIdenticalRedirect
335                  )
336
337  val if4_target = WireInit(if4_snpc)
338
339  // when (if4_prevHalfNextNotMet) {
340  //   if4_target := prevHalfInstrReq.pc+2.U
341  // }.else
342  when (if4_predTakenRedirect) {
343    if4_target := if4_bp.target
344  }.elsewhen (if4_predNotTakenRedirect) {
345    if4_target := if4_snpc
346  }
347  // }.elsewhen (if4_ghInfoNotIdenticalRedirect) {
348  //   if4_target := Mux(if4_bp.taken, if4_bp.target, if4_snpc)
349  // }
350  npcGen.register(if4_redirect, if4_target)
351
352  when (if4_fire) {
353    final_gh := if4_predicted_gh
354  }
355  if4_gh := Mux(flush_final_gh, final_gh_bypass, final_gh)
356  if3_gh := Mux(if4_valid && !if4_flush, if4_predicted_gh, if4_gh)
357  if2_gh := Mux(if3_valid && !if3_flush, if3_predicted_gh, if3_gh)
358  if1_gh := Mux(if2_valid && !if2_flush, if2_predicted_gh, if2_gh)
359
360
361
362
363  val cfiUpdate = io.cfiUpdateInfo
364  when (cfiUpdate.valid && (cfiUpdate.bits.isMisPred || cfiUpdate.bits.isReplay)) {
365    val b = cfiUpdate.bits
366    val oldGh = b.bpuMeta.hist
367    val sawNTBr = b.bpuMeta.sawNotTakenBranch
368    val isBr = b.pd.isBr
369    val taken = b.taken
370    val updatedGh = oldGh.update(sawNTBr, isBr && taken)
371    final_gh := updatedGh
372    final_gh_bypass := updatedGh
373    flush_final_gh := true.B
374  }
375
376  npcGen.register(loopBufPar.LBredirect.valid, loopBufPar.LBredirect.bits)
377  npcGen.register(io.redirect.valid, io.redirect.bits)
378  npcGen.register(RegNext(reset.asBool) && !reset.asBool, resetVector.U(VAddrBits.W))
379
380  if1_npc := npcGen()
381
382  when(inLoop) {
383    io.icacheReq.valid := if4_flush
384  }.otherwise {
385    io.icacheReq.valid := if1_valid && if2_ready
386  }
387  io.icacheResp.ready := if4_ready
388  io.icacheReq.bits.addr := if1_npc
389
390  // when(if4_bp.taken) {
391  //   when(if4_bp.saveHalfRVI) {
392  //     io.loopBufPar.LBReq := snpc(if4_pc)
393  //   }.otherwise {
394  //     io.loopBufPar.LBReq := if4_bp.target
395  //   }
396  // }.otherwise {
397  //   io.loopBufPar.LBReq := snpc(if4_pc)
398  //   XSDebug(p"snpc(if4_pc)=${Hexadecimal(snpc(if4_pc))}\n")
399  // }
400  loopBufPar.fetchReq := if3_pc
401
402  io.icacheReq.bits.mask := mask(if1_npc)
403
404  io.icacheFlush := Cat(if3_flush, if2_flush)
405
406  bpu.io.cfiUpdateInfo <> io.cfiUpdateInfo
407
408  // bpu.io.flush := Cat(if4_flush, if3_flush, if2_flush)
409  bpu.io.flush := VecInit(if2_flush, if3_flush, if4_flush)
410  bpu.io.inFire(0) := if1_fire
411  bpu.io.inFire(1) := if2_fire
412  bpu.io.inFire(2) := if3_fire
413  bpu.io.inFire(3) := if4_fire
414  bpu.io.in.pc := if1_npc
415  bpu.io.in.hist := if1_gh.asUInt
416  // bpu.io.in.histPtr := ptr
417  bpu.io.in.inMask := mask(if1_npc)
418  bpu.io.predecode.mask := if4_pd.mask
419  bpu.io.predecode.lastHalf := if4_pd.lastHalf
420  bpu.io.predecode.pd := if4_pd.pd
421  bpu.io.predecode.hasLastHalfRVI := if4_prevHalfInstrMet
422  bpu.io.realMask := if4_mask
423  bpu.io.prevHalf := if4_prevHalfInstr
424
425  pd.io.in := icacheResp
426  when(inLoop) {
427    pd.io.in.mask := loopBuffer.io.out.bits.mask // TODO: Maybe this is unnecessary
428    // XSDebug("Fetch from LB\n")
429    // XSDebug(p"pc=${Hexadecimal(io.loopBufPar.LBResp.pc)}\n")
430    // XSDebug(p"data=${Hexadecimal(io.loopBufPar.LBResp.data)}\n")
431    // XSDebug(p"mask=${Hexadecimal(io.loopBufPar.LBResp.mask)}\n")
432  }
433
434  pd.io.prev.valid := if3_prevHalfInstrMet
435  pd.io.prev.bits := if3_prevHalfInstr.instr
436  // if a fetch packet triggers page fault, set the pf instruction to nop
437  when (!if3_prevHalfInstrMet && icacheResp.ipf) {
438    val instrs = Wire(Vec(FetchWidth, UInt(32.W)))
439    (0 until FetchWidth).foreach(i => instrs(i) := ZeroExt("b0010011".U, 32)) // nop
440    pd.io.in.data := instrs.asUInt
441  }.elsewhen (if3_prevHalfInstrMet && (if3_prevHalfInstr.ipf || icacheResp.ipf)) {
442    pd.io.prev.bits := ZeroExt("b0010011".U, 16)
443    val instrs = Wire(Vec(FetchWidth, UInt(32.W)))
444    (0 until FetchWidth).foreach(i => instrs(i) := Cat(ZeroExt("b0010011".U, 16), Fill(16, 0.U(1.W))))
445    pd.io.in.data := instrs.asUInt
446
447    when (icacheResp.ipf && !if3_prevHalfInstr.ipf) { crossPageIPF := true.B } // higher 16 bits page fault
448  }
449
450  //Performance Counter
451  // if (!env.FPGAPlatform ) {
452  //   ExcitingUtils.addSource(io.fetchPacket.fire && !inLoop, "CntFetchFromICache", Perf)
453  //   ExcitingUtils.addSource(io.fetchPacket.fire && inLoop, "CntFetchFromLoopBuffer", Perf)
454  // }
455
456  val fetchPacketValid = if4_valid && !io.redirect.valid
457  val fetchPacketWire = Wire(new FetchPacket)
458
459  // io.fetchPacket.valid := if4_valid && !io.redirect.valid
460  fetchPacketWire.instrs := if4_pd.instrs
461  fetchPacketWire.mask := if4_pd.mask & (Fill(PredictWidth, !if4_bp.taken) | (Fill(PredictWidth, 1.U(1.W)) >> (~if4_bp.jmpIdx)))
462
463  loopBufPar.noTakenMask := if4_pd.mask
464  fetchPacketWire.pc := if4_pd.pc
465  (0 until PredictWidth).foreach(i => fetchPacketWire.pnpc(i) := if4_pd.pc(i) + Mux(if4_pd.pd(i).isRVC, 2.U, 4.U))
466  when (if4_bp.taken) {
467    fetchPacketWire.pnpc(if4_bp.jmpIdx) := if4_bp.target
468  }
469  fetchPacketWire.bpuMeta := bpu.io.bpuMeta
470  (0 until PredictWidth).foreach(i => fetchPacketWire.bpuMeta(i).hist := final_gh)
471  (0 until PredictWidth).foreach(i => fetchPacketWire.bpuMeta(i).predHist := if4_predHist.asTypeOf(new GlobalHistory))
472  fetchPacketWire.pd := if4_pd.pd
473  fetchPacketWire.ipf := if4_ipf
474  fetchPacketWire.acf := if4_acf
475  fetchPacketWire.crossPageIPFFix := if4_crossPageIPF
476
477  // predTaken Vec
478  fetchPacketWire.predTaken := if4_bp.taken
479
480  loopBuffer.io.in.bits := fetchPacketWire
481  io.fetchPacket.bits := fetchPacketWire
482  io.fetchPacket.valid := fetchPacketValid
483  loopBuffer.io.in.valid := io.fetchPacket.fire
484
485  // debug info
486  if (IFUDebug) {
487    XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n")
488    XSDebug(io.icacheFlush(0).asBool, "Flush icache stage2...\n")
489    XSDebug(io.icacheFlush(1).asBool, "Flush icache stage3...\n")
490    XSDebug(io.redirect.valid, p"Redirect from backend! target=${Hexadecimal(io.redirect.bits)}\n")
491
492    XSDebug("[IF1] v=%d     fire=%d            flush=%d pc=%x mask=%b\n", if1_valid, if1_fire, if1_flush, if1_npc, mask(if1_npc))
493    XSDebug("[IF2] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x snpc=%x\n", if2_valid, if2_ready, if2_fire, if2_redirect, if2_flush, if2_pc, if2_snpc)
494    XSDebug("[IF3] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if3_valid, if3_ready, if3_fire, if3_redirect, if3_flush, if3_pc, crossPageIPF, if3_bp.hasNotTakenBrs)
495    XSDebug("[IF4] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if4_valid, if4_ready, if4_fire, if4_redirect, if4_flush, if4_pc, if4_crossPageIPF, if4_bp.hasNotTakenBrs)
496    XSDebug("[IF1][icacheReq] v=%d r=%d addr=%x\n", io.icacheReq.valid, io.icacheReq.ready, io.icacheReq.bits.addr)
497    XSDebug("[IF1][ghr] hist=%b\n", if1_gh.asUInt)
498    XSDebug("[IF1][ghr] extHist=%b\n\n", if1_gh.asUInt)
499
500    XSDebug("[IF2][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n\n", if2_bp.taken, if2_bp.jmpIdx, if2_bp.hasNotTakenBrs, if2_bp.target, if2_bp.saveHalfRVI)
501    if2_gh.debug("if2")
502
503    XSDebug("[IF3][icacheResp] v=%d r=%d pc=%x mask=%b\n", io.icacheResp.valid, io.icacheResp.ready, io.icacheResp.bits.pc, io.icacheResp.bits.mask)
504    XSDebug("[IF3][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if3_bp.taken, if3_bp.jmpIdx, if3_bp.hasNotTakenBrs, if3_bp.target, if3_bp.saveHalfRVI)
505    XSDebug("[IF3][redirect]: v=%d, prevMet=%d, prevNMet=%d, predT=%d, predNT=%d\n", if3_redirect, if3_prevHalfMetRedirect, if3_prevHalfNotMetRedirect, if3_predTakenRedirect, if3_predNotTakenRedirect)
506    // XSDebug("[IF3][prevHalfInstr] v=%d redirect=%d fetchpc=%x idx=%d tgt=%x taken=%d instr=%x\n\n",
507    //   prev_half_valid, prev_half_redirect, prev_half_fetchpc, prev_half_idx, prev_half_tgt, prev_half_taken, prev_half_instr)
508    XSDebug("[IF3][    prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n",
509      if3_prevHalfInstr.valid, if3_prevHalfInstr.taken, if3_prevHalfInstr.fetchpc, if3_prevHalfInstr.idx, if3_prevHalfInstr.pc, if3_prevHalfInstr.target, if3_prevHalfInstr.instr, if3_prevHalfInstr.ipf)
510    XSDebug("[IF3][if3_prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n\n",
511      if3_prevHalfInstr.valid, if3_prevHalfInstr.taken, if3_prevHalfInstr.fetchpc, if3_prevHalfInstr.idx, if3_prevHalfInstr.pc, if3_prevHalfInstr.target, if3_prevHalfInstr.instr, if3_prevHalfInstr.ipf)
512    if3_gh.debug("if3")
513
514    XSDebug("[IF4][predecode] mask=%b\n", if4_pd.mask)
515    XSDebug("[IF4][snpc]: %x, realMask=%b\n", if4_snpc, if4_mask)
516    XSDebug("[IF4][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if4_bp.taken, if4_bp.jmpIdx, if4_bp.hasNotTakenBrs, if4_bp.target, if4_bp.saveHalfRVI)
517    XSDebug("[IF4][redirect]: v=%d, prevNotMet=%d, predT=%d, predNT=%d\n", if4_redirect, if4_prevHalfNextNotMet, if4_predTakenRedirect, if4_predNotTakenRedirect)
518    XSDebug(if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken, "[IF4] cfi is jal!  instr=%x target=%x\n", if4_instrs(if4_bp.jmpIdx), if4_jal_tgts(if4_bp.jmpIdx))
519    XSDebug("[IF4][if4_prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n",
520      if4_prevHalfInstr.valid, if4_prevHalfInstr.taken, if4_prevHalfInstr.fetchpc, if4_prevHalfInstr.idx, if4_prevHalfInstr.pc, if4_prevHalfInstr.target, if4_prevHalfInstr.instr, if4_prevHalfInstr.ipf)
521    if4_gh.debug("if4")
522    XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] v=%d r=%d mask=%b ipf=%d acf=%d crossPageIPF=%d\n",
523      io.fetchPacket.valid, io.fetchPacket.ready, io.fetchPacket.bits.mask, io.fetchPacket.bits.ipf, io.fetchPacket.bits.acf, io.fetchPacket.bits.crossPageIPFFix)
524    for (i <- 0 until PredictWidth) {
525      XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] %b %x pc=%x pnpc=%x pd: rvc=%d brType=%b call=%d ret=%d\n",
526        io.fetchPacket.bits.mask(i),
527        io.fetchPacket.bits.instrs(i),
528        io.fetchPacket.bits.pc(i),
529        io.fetchPacket.bits.pnpc(i),
530        io.fetchPacket.bits.pd(i).isRVC,
531        io.fetchPacket.bits.pd(i).brType,
532        io.fetchPacket.bits.pd(i).isCall,
533        io.fetchPacket.bits.pd(i).isRet
534      )
535    }
536  }
537}