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