xref: /XiangShan/src/main/scala/xiangshan/frontend/IFU.scala (revision 9311d1bb09a8608bba4d19c4bae06fb96336c7e2)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import chisel3.util.experimental.BoringUtils
6import device.RAMHelper
7import xiangshan._
8import utils._
9import xiangshan.cache._
10import chisel3.ExcitingUtils._
11
12trait HasIFUConst { this: XSModule =>
13  val resetVector = 0x80000000L//TODO: set reset vec
14  val groupAlign = log2Up(FetchWidth * 4 * 2)
15  def groupPC(pc: UInt): UInt = Cat(pc(VAddrBits-1, groupAlign), 0.U(groupAlign.W))
16  // each 1 bit in mask stands for 2 Bytes
17  def mask(pc: UInt): UInt = (Fill(PredictWidth * 2, 1.U(1.W)) >> pc(groupAlign - 1, 1))(PredictWidth - 1, 0)
18  def snpc(pc: UInt): UInt = pc + (PopCount(mask(pc)) << 1)
19
20  val IFUDebug = true
21}
22
23class GlobalHistoryInfo() extends XSBundle {
24  val sawNTBr = Bool()
25  val takenOnBr = Bool()
26  val saveHalfRVI = Bool()
27  def shifted = takenOnBr || sawNTBr
28  def newPtr(ptr: UInt) = Mux(shifted, ptr - 1.U, ptr)
29  implicit val name = "IFU"
30  def debug = XSDebug("[GHInfo] sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d\n", sawNTBr, takenOnBr, saveHalfRVI)
31  // override def toString(): String = "histPtr=%d, sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d".format(histPtr, sawNTBr, takenOnBr, saveHalfRVI)
32}
33
34class IFUIO extends XSBundle
35{
36  val fetchPacket = DecoupledIO(new FetchPacket)
37  val redirect = Flipped(ValidIO(new Redirect))
38  val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
39  val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo))
40  val icacheReq = DecoupledIO(new ICacheReq)
41  val icacheResp = Flipped(DecoupledIO(new ICacheResp))
42  val icacheFlush = Output(UInt(2.W))
43  // val loopBufPar = Flipped(new LoopBufferParameters)
44}
45
46class IFU extends XSModule with HasIFUConst
47{
48  val io = IO(new IFUIO)
49  val bpu = BPU(EnableBPU)
50  val pd = Module(new PreDecode)
51  val loopBuffer = if(EnableLB) { Module(new NewLoopBuffer) } else { Module(new FakeLoopBuffer) }
52
53  val if2_redirect, if3_redirect, if4_redirect = WireInit(false.B)
54  val if1_flush, if2_flush, if3_flush, if4_flush = WireInit(false.B)
55
56  val loopBufPar = loopBuffer.io.loopBufPar
57  val inLoop = WireInit(loopBuffer.io.out.valid)
58  val icacheResp = WireInit(Mux(inLoop, loopBuffer.io.out.bits, io.icacheResp.bits))
59
60  if4_flush := io.redirect.valid// || loopBufPar.LBredirect.valid
61  if3_flush := if4_flush || if4_redirect
62  if2_flush := if3_flush || if3_redirect
63  if1_flush := if2_flush || if2_redirect
64
65  loopBuffer.io.flush := io.redirect.valid
66
67  //********************** IF1 ****************************//
68  val if1_valid = !reset.asBool && GTimer() > 500.U
69  val if1_npc = WireInit(0.U(VAddrBits.W))
70  val if2_ready = WireInit(false.B)
71  val if1_fire = if1_valid && (if2_ready || if1_flush) && (inLoop || io.icacheReq.ready)
72
73
74  val if1_histPtr, if2_histPtr, if3_histPtr, if4_histPtr = Wire(UInt(log2Up(ExtHistoryLength).W))
75  val if2_newPtr, if3_newPtr, if4_newPtr = Wire(UInt(log2Up(ExtHistoryLength).W))
76
77  val extHist = RegInit(VecInit(Seq.fill(ExtHistoryLength)(0.U(1.W))))
78  val shiftPtr = WireInit(false.B)
79  val newPtr = Wire(UInt(log2Up(ExtHistoryLength).W))
80  val ptr = Mux(shiftPtr, newPtr, if1_histPtr)
81  val hist = Wire(Vec(HistoryLength, UInt(1.W)))
82  for (i <- 0 until HistoryLength) {
83    hist(i) := extHist(ptr + i.U)
84  }
85
86  shiftPtr := false.B
87  newPtr := if1_histPtr
88
89
90
91  val if1_GHInfo = Wire(new GlobalHistoryInfo())
92  if1_GHInfo := 0.U.asTypeOf(new GlobalHistoryInfo)
93
94  //********************** IF2 ****************************//
95  val if2_valid = RegEnable(next = if1_valid, init = false.B, enable = if1_fire)
96  val if3_ready = WireInit(false.B)
97  val if2_fire = if2_valid && if3_ready && !if2_flush
98  val if2_pc = RegEnable(next = if1_npc, init = resetVector.U, enable = if1_fire)
99  val if2_snpc = snpc(if2_pc)
100  val if2_GHInfo = RegEnable(if1_GHInfo, if1_fire)
101  val if2_predHistPtr = RegEnable(ptr, enable=if1_fire)
102  if2_ready := if2_fire || !if2_valid || if2_flush
103  when (if2_flush) { if2_valid := if1_fire }
104  .elsewhen (if1_fire) { if2_valid := if1_valid }
105  .elsewhen (if2_fire) { if2_valid := false.B }
106
107  when (RegNext(reset.asBool) && !reset.asBool) {
108    if1_npc := resetVector.U(VAddrBits.W)
109  }.elsewhen (if2_fire) {
110    if1_npc := if2_snpc
111  }.otherwise {
112    if1_npc := RegNext(if1_npc)
113  }
114
115  val if2_bp = bpu.io.out(0).bits
116  // if taken, bp_redirect should be true
117  // when taken on half RVI, we suppress this redirect signal
118  if2_redirect := if2_fire && bpu.io.out(0).valid && if2_bp.redirect && !if2_bp.saveHalfRVI
119  when (if2_redirect) {
120    if1_npc := if2_bp.target
121  }
122
123  val if2_realGHInfo = Wire(new GlobalHistoryInfo())
124  if2_realGHInfo.sawNTBr     := if2_bp.hasNotTakenBrs
125  if2_realGHInfo.takenOnBr   := if2_bp.takenOnBr
126  if2_realGHInfo.saveHalfRVI := if2_bp.saveHalfRVI
127
128  when (if2_fire && if2_realGHInfo.shifted) {
129    shiftPtr := true.B
130    newPtr := if2_newPtr
131  }
132  when (if2_realGHInfo.shifted && if2_newPtr >= ptr) {
133    hist(if2_newPtr-ptr) := if2_realGHInfo.takenOnBr.asUInt
134  }
135
136
137
138  //********************** IF3 ****************************//
139  val if3_valid = RegEnable(next = if2_valid, init = false.B, enable = if2_fire)
140  val if4_ready = WireInit(false.B)
141  val if3_fire = if3_valid && if4_ready && (inLoop || io.icacheResp.valid) && !if3_flush
142  val if3_pc = RegEnable(if2_pc, if2_fire)
143  val if3_GHInfo = RegEnable(if2_realGHInfo, if2_fire)
144  val if3_predHistPtr = RegEnable(if2_predHistPtr, enable=if2_fire)
145  if3_ready := if3_fire || !if3_valid || if3_flush
146  when (if3_flush) { if3_valid := false.B }
147  .elsewhen (if2_fire) { if3_valid := if2_valid }
148  .elsewhen (if3_fire) { if3_valid := false.B }
149
150  val if3_bp = bpu.io.out(1).bits
151
152  val if3_realGHInfo = Wire(new GlobalHistoryInfo())
153  if3_realGHInfo.sawNTBr     := if3_bp.hasNotTakenBrs
154  if3_realGHInfo.takenOnBr   := if3_bp.takenOnBr
155  if3_realGHInfo.saveHalfRVI := if3_bp.saveHalfRVI
156
157  class PrevHalfInstr extends Bundle {
158    val valid = Bool()
159    val taken = Bool()
160    val ghInfo = new GlobalHistoryInfo()
161    val fetchpc = UInt(VAddrBits.W) // only for debug
162    val idx = UInt(VAddrBits.W) // only for debug
163    val pc = UInt(VAddrBits.W)
164    val target = UInt(VAddrBits.W)
165    val instr = UInt(16.W)
166    val ipf = Bool()
167    val newPtr = UInt(log2Up(ExtHistoryLength).W)
168  }
169
170  val if3_prevHalfInstr = RegInit(0.U.asTypeOf(new PrevHalfInstr))
171  val if4_prevHalfInstr = Wire(new PrevHalfInstr)
172  // 32-bit instr crosses 2 pages, and the higher 16-bit triggers page fault
173  val crossPageIPF = WireInit(false.B)
174  when (if4_prevHalfInstr.valid) {
175    if3_prevHalfInstr := if4_prevHalfInstr
176  }
177  val prevHalfInstr = Mux(if4_prevHalfInstr.valid, if4_prevHalfInstr, if3_prevHalfInstr)
178
179  // the previous half of RVI instruction waits until it meets its last half
180  val if3_hasPrevHalfInstr = prevHalfInstr.valid && (prevHalfInstr.pc + 2.U) === if3_pc
181  // set to invalid once consumed or redirect from backend
182  val prevHalfConsumed = if3_hasPrevHalfInstr && if3_fire || if4_flush
183  when (prevHalfConsumed) {
184    if3_prevHalfInstr.valid := false.B
185  }
186
187  // when bp signal a redirect, we distinguish between taken and not taken
188  // if taken and saveHalfRVI is true, we do not redirect to the target
189  if3_redirect := if3_fire && bpu.io.out(1).valid && (if3_hasPrevHalfInstr && prevHalfInstr.taken || if3_bp.redirect && (if3_bp.taken && !if3_bp.saveHalfRVI || !if3_bp.taken) )
190
191  when (if3_redirect) {
192    when (!(if3_hasPrevHalfInstr && prevHalfInstr.taken)) {
193      if1_npc := if3_bp.target
194      when (if3_realGHInfo.shifted){
195        shiftPtr := true.B
196        newPtr := if3_newPtr
197      }
198    }
199  }
200
201  // when it does not redirect, we still need to modify hist(wire)
202  when(if3_realGHInfo.shifted && if3_newPtr >= ptr) {
203    hist(if3_newPtr-ptr) := if3_realGHInfo.takenOnBr
204  }
205  when (if3_hasPrevHalfInstr && prevHalfInstr.ghInfo.shifted && prevHalfInstr.newPtr >= ptr) {
206    hist(prevHalfInstr.newPtr-ptr) := prevHalfInstr.ghInfo.takenOnBr
207  }
208
209  //********************** IF4 ****************************//
210  val if4_pd = RegEnable(pd.io.out, if3_fire)
211  val if4_ipf = RegEnable(icacheResp.ipf || if3_hasPrevHalfInstr && prevHalfInstr.ipf, if3_fire)
212  val if4_crossPageIPF = RegEnable(crossPageIPF, if3_fire)
213  val if4_valid = RegInit(false.B)
214  val if4_fire = if4_valid && io.fetchPacket.ready
215  val if4_pc = RegEnable(if3_pc, if3_fire)
216
217  val if4_GHInfo = RegEnable(if3_realGHInfo, if3_fire)
218  val if4_predHistPtr = RegEnable(if3_predHistPtr, enable=if3_fire)
219  if4_ready := (if4_fire || !if4_valid || if4_flush) && GTimer() > 500.U
220  when (if4_flush)     { if4_valid := false.B }
221  .elsewhen (if3_fire) { if4_valid := if3_valid }
222  .elsewhen(if4_fire)  { if4_valid := false.B }
223
224  val if4_bp = Wire(new BranchPrediction)
225  if4_bp := bpu.io.out(2).bits
226
227  val if4_realGHInfo = Wire(new GlobalHistoryInfo())
228  if4_realGHInfo.sawNTBr     := if4_bp.hasNotTakenBrs
229  if4_realGHInfo.takenOnBr   := if4_bp.takenOnBr
230  if4_realGHInfo.saveHalfRVI := if4_bp.saveHalfRVI
231
232
233  val if4_cfi_jal = if4_pd.instrs(if4_bp.jmpIdx)
234  val if4_cfi_jal_tgt = if4_pd.pc(if4_bp.jmpIdx) + Mux(if4_pd.pd(if4_bp.jmpIdx).isRVC,
235    SignExt(Cat(if4_cfi_jal(12), if4_cfi_jal(8), if4_cfi_jal(10, 9), if4_cfi_jal(6), if4_cfi_jal(7), if4_cfi_jal(2), if4_cfi_jal(11), if4_cfi_jal(5, 3), 0.U(1.W)), XLEN),
236    SignExt(Cat(if4_cfi_jal(31), if4_cfi_jal(19, 12), if4_cfi_jal(20), if4_cfi_jal(30, 21), 0.U(1.W)), XLEN))
237  if4_bp.target := Mux(if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken, if4_cfi_jal_tgt, bpu.io.out(2).bits.target)
238  if4_bp.redirect := bpu.io.out(2).bits.redirect || if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken && if4_cfi_jal_tgt =/= bpu.io.out(2).bits.target
239
240  if4_prevHalfInstr := 0.U.asTypeOf(new PrevHalfInstr)
241  when (bpu.io.out(2).valid && if4_fire && if4_bp.saveHalfRVI) {
242    if4_prevHalfInstr.valid := true.B
243    if4_prevHalfInstr.taken := if4_bp.taken
244    if4_prevHalfInstr.ghInfo := if4_realGHInfo
245    // Make sure shifted can work
246    if4_prevHalfInstr.ghInfo.saveHalfRVI := false.B
247    if4_prevHalfInstr.newPtr := if4_newPtr
248    if4_prevHalfInstr.fetchpc := if4_pc
249    if4_prevHalfInstr.idx := PopCount(mask(if4_pc)) - 1.U
250    if4_prevHalfInstr.pc := if4_pd.pc(if4_prevHalfInstr.idx)
251    if4_prevHalfInstr.target := if4_bp.target
252    if4_prevHalfInstr.instr := if4_pd.instrs(if4_prevHalfInstr.idx)(15, 0)
253    if4_prevHalfInstr.ipf := if4_ipf
254  }
255
256  // Redirect and npc logic for if4
257  when (bpu.io.out(2).valid && if4_fire && if4_bp.redirect) {
258    if4_redirect := true.B
259    when (if4_bp.saveHalfRVI) {
260      if1_npc := snpc(if4_pc)
261    }.otherwise {
262      if1_npc := if4_bp.target
263    }
264  }
265  // }.elsewhen (bpu.io.out(2).valid && if4_fire/* && !if4_bp.redirect*/) {
266  //   // We redirect the pipeline to the next fetch packet,
267  //   // which contains the last half of the RVI instruction
268  //   when (if4_bp.saveHalfRVI && if4_bp.taken) {
269  //     if4_redirect := true.B
270  //     if1_npc := snpc(if4_pc)
271  //   }
272  // }
273
274  // This should cover the if4 redirect to snpc when saveHalfRVI
275  when (if3_redirect) {
276    when (if3_hasPrevHalfInstr && prevHalfInstr.taken) {
277      if1_npc := prevHalfInstr.target
278    }
279  }
280
281  // history logic for if4
282  when (bpu.io.out(2).valid && if4_fire && if4_bp.redirect) {
283    shiftPtr := true.B
284    newPtr := if4_newPtr
285  // }.elsewhen (bpu.io.out(2).valid && if4_fire/* && !if4_bp.redirect*/) {
286  //   // only if we hasn't seen not taken branches and
287  //   // see a not taken branch in if4 should we tell
288  //   // if3 and if4 to update histptr
289  //   // We do not shift global history pointer unless we have the full
290  //   // RVI instruction
291  //   when (if4_newSawNTBrs && !if4_bp.takenOnBr) {
292  //     shiftPtr := true.B
293  //     // newPtr := if4_realGHInfo.newPtr
294  //   }
295  }
296
297  when (if4_realGHInfo.shifted && if4_newPtr >= ptr) {
298    hist(if4_newPtr-ptr) := if4_realGHInfo.takenOnBr
299  }
300
301  when (if3_redirect) {
302    // when redirect and if3_hasPrevHalfInstr, this prevHalfInstr should only be taken
303    when (if3_hasPrevHalfInstr && prevHalfInstr.ghInfo.shifted) {
304      shiftPtr := true.B
305      newPtr := prevHalfInstr.newPtr
306      extHist(prevHalfInstr.newPtr) := prevHalfInstr.ghInfo.takenOnBr
307    }
308  }
309
310  // modify GHR at the end of a prediction lifetime
311  when (if4_fire && if4_realGHInfo.shifted) {
312    extHist(if4_newPtr) := if4_realGHInfo.takenOnBr
313  }
314
315  // This is a histPtr which is only modified when a prediction
316  // is sent, so that it can get the final prediction info
317  val finalPredHistPtr = RegInit(0.U(log2Up(ExtHistoryLength).W))
318  if4_histPtr := finalPredHistPtr
319  if4_newPtr  := if3_histPtr
320  when (if4_fire && if4_realGHInfo.shifted) {
321    finalPredHistPtr := if4_newPtr
322  }
323
324  if3_histPtr := Mux(if4_realGHInfo.shifted && if4_valid && !if4_flush, if4_histPtr - 1.U, if4_histPtr)
325  if3_newPtr  := if2_histPtr
326
327  if2_histPtr := Mux(if3_realGHInfo.shifted && if3_valid && !if3_flush, if3_histPtr - 1.U, if3_histPtr)
328  if2_newPtr  := if1_histPtr
329
330  if1_histPtr := Mux(if2_realGHInfo.shifted && if2_valid && !if2_flush, if2_histPtr - 1.U, if2_histPtr)
331
332
333
334
335  when (io.outOfOrderBrInfo.valid && io.outOfOrderBrInfo.bits.isMisPred) {
336    val b = io.outOfOrderBrInfo.bits
337    val oldPtr = b.brInfo.histPtr
338    shiftPtr := true.B
339    when (!b.pd.isBr && !b.brInfo.sawNotTakenBranch) {
340      // If mispredicted cfi is not a branch,
341      // and there wasn't any not taken branch before it,
342      // we should only recover the pointer to an unshifted state
343      newPtr := oldPtr
344      finalPredHistPtr := oldPtr
345    }.otherwise {
346      newPtr := oldPtr - 1.U
347      finalPredHistPtr := oldPtr - 1.U
348      hist(0) := Mux(b.pd.isBr, b.taken, 0.U)
349      extHist(newPtr) := Mux(b.pd.isBr, b.taken, 0.U)
350    }
351  }
352
353  // when (loopBufPar.LBredirect.valid) {
354  //   if1_npc := loopBufPar.LBredirect.bits
355  // }
356
357  when (io.redirect.valid) {
358    if1_npc := io.redirect.bits.target
359  }
360
361  when(inLoop) {
362    io.icacheReq.valid := if4_flush
363  }.otherwise {
364    io.icacheReq.valid := if1_valid && if2_ready
365    // io.icacheResp.ready := if3_ready
366  //io.icacheResp.ready := if3_valid
367  }
368  io.icacheResp.ready := if4_ready
369  io.icacheReq.bits.addr := if1_npc
370
371  // when(if4_bp.taken) {
372  //   when(if4_bp.saveHalfRVI) {
373  //     io.loopBufPar.LBReq := snpc(if4_pc)
374  //   }.otherwise {
375  //     io.loopBufPar.LBReq := if4_bp.target
376  //   }
377  // }.otherwise {
378  //   io.loopBufPar.LBReq := snpc(if4_pc)
379  //   XSDebug(p"snpc(if4_pc)=${Hexadecimal(snpc(if4_pc))}\n")
380  // }
381  loopBufPar.fetchReq := if3_pc
382
383  io.icacheReq.bits.mask := mask(if1_npc)
384
385  io.icacheFlush := Cat(if3_flush, if2_flush)
386
387  val inOrderBrHist = Wire(Vec(HistoryLength, UInt(1.W)))
388  (0 until HistoryLength).foreach(i => inOrderBrHist(i) := extHist(i.U + io.inOrderBrInfo.bits.brInfo.predHistPtr))
389  bpu.io.inOrderBrInfo.valid := io.inOrderBrInfo.valid
390  bpu.io.inOrderBrInfo.bits := BranchUpdateInfoWithHist(io.inOrderBrInfo.bits, inOrderBrHist.asUInt)
391  bpu.io.outOfOrderBrInfo.valid := io.outOfOrderBrInfo.valid
392  bpu.io.outOfOrderBrInfo.bits := BranchUpdateInfoWithHist(io.outOfOrderBrInfo.bits, inOrderBrHist.asUInt) // Dont care about hist
393
394  // bpu.io.flush := Cat(if4_flush, if3_flush, if2_flush)
395  bpu.io.flush := VecInit(if2_flush, if3_flush, if4_flush)
396  bpu.io.cacheValid := (inLoop || io.icacheResp.valid)
397  bpu.io.in.valid := if1_fire
398  bpu.io.in.bits.pc := if1_npc
399  bpu.io.in.bits.hist := hist.asUInt
400  bpu.io.in.bits.histPtr := ptr
401  bpu.io.in.bits.inMask := mask(if1_npc)
402  bpu.io.out(0).ready := if2_fire
403  bpu.io.out(1).ready := if3_fire
404  bpu.io.out(2).ready := if4_fire
405  bpu.io.predecode.valid := if4_valid
406  bpu.io.predecode.bits.mask := if4_pd.mask
407  bpu.io.predecode.bits.pd := if4_pd.pd
408  bpu.io.predecode.bits.isFetchpcEqualFirstpc := if4_pc === if4_pd.pc(0)
409  bpu.io.branchInfo.ready := if4_fire
410
411  pd.io.in := icacheResp
412  when(inLoop) {
413    pd.io.in.mask := loopBuffer.io.out.bits.mask & mask(loopBuffer.io.out.bits.pc) // TODO: Maybe this is unnecessary
414    // XSDebug("Fetch from LB\n")
415    // XSDebug(p"pc=${Hexadecimal(io.loopBufPar.LBResp.pc)}\n")
416    // XSDebug(p"data=${Hexadecimal(io.loopBufPar.LBResp.data)}\n")
417    // XSDebug(p"mask=${Hexadecimal(io.loopBufPar.LBResp.mask)}\n")
418  }
419
420  pd.io.prev.valid := if3_hasPrevHalfInstr
421  pd.io.prev.bits := prevHalfInstr.instr
422  // if a fetch packet triggers page fault, set the pf instruction to nop
423  when (!if3_hasPrevHalfInstr && icacheResp.ipf) {
424    val instrs = Wire(Vec(FetchWidth, UInt(32.W)))
425    (0 until FetchWidth).foreach(i => instrs(i) := ZeroExt("b0010011".U, 32)) // nop
426    pd.io.in.data := instrs.asUInt
427  }.elsewhen (if3_hasPrevHalfInstr && (prevHalfInstr.ipf || icacheResp.ipf)) {
428    pd.io.prev.bits := ZeroExt("b0010011".U, 16)
429    val instrs = Wire(Vec(FetchWidth, UInt(32.W)))
430    (0 until FetchWidth).foreach(i => instrs(i) := Cat(ZeroExt("b0010011".U, 16), Fill(16, 0.U(1.W))))
431    pd.io.in.data := instrs.asUInt
432
433    when (icacheResp.ipf && !prevHalfInstr.ipf) { crossPageIPF := true.B } // higher 16 bits page fault
434  }
435
436  //Performance Counter
437  if (!env.FPGAPlatform ) {
438    ExcitingUtils.addSource(io.fetchPacket.fire && !inLoop, "CntFetchFromICache", Perf)
439    ExcitingUtils.addSource(io.fetchPacket.fire && inLoop, "CntFetchFromLoopBuffer", Perf)
440  }
441
442  val fetchPacketValid = if4_valid && !io.redirect.valid
443  val fetchPacketWire = Wire(new FetchPacket)
444
445  // io.fetchPacket.valid := if4_valid && !io.redirect.valid
446  fetchPacketWire.instrs := if4_pd.instrs
447  fetchPacketWire.mask := if4_pd.mask & (Fill(PredictWidth, !if4_bp.taken) | (Fill(PredictWidth, 1.U(1.W)) >> (~if4_bp.jmpIdx)))
448  loopBufPar.noTakenMask := if4_pd.mask
449  fetchPacketWire.pc := if4_pd.pc
450  (0 until PredictWidth).foreach(i => fetchPacketWire.pnpc(i) := if4_pd.pc(i) + Mux(if4_pd.pd(i).isRVC, 2.U, 4.U))
451  when (if4_bp.taken) {
452    fetchPacketWire.pnpc(if4_bp.jmpIdx) := if4_bp.target
453  }
454  fetchPacketWire.brInfo := bpu.io.branchInfo.bits
455  (0 until PredictWidth).foreach(i => fetchPacketWire.brInfo(i).histPtr := finalPredHistPtr)
456  (0 until PredictWidth).foreach(i => fetchPacketWire.brInfo(i).predHistPtr := if4_predHistPtr)
457  fetchPacketWire.pd := if4_pd.pd
458  fetchPacketWire.ipf := if4_ipf
459  fetchPacketWire.crossPageIPFFix := if4_crossPageIPF
460
461  // predTaken Vec
462  fetchPacketWire.predTaken := if4_bp.taken
463
464  loopBuffer.io.in.bits := fetchPacketWire
465  io.fetchPacket.bits := fetchPacketWire
466  io.fetchPacket.valid := fetchPacketValid
467  loopBuffer.io.in.valid := io.fetchPacket.fire
468
469  // debug info
470  if (IFUDebug) {
471    XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n")
472    XSDebug(io.icacheFlush(0).asBool, "Flush icache stage2...\n")
473    XSDebug(io.icacheFlush(1).asBool, "Flush icache stage3...\n")
474    XSDebug(io.redirect.valid, "Redirect from backend! isExcp=%d isFpp:%d isMisPred=%d isReplay=%d pc=%x\n",
475      io.redirect.bits.isException, io.redirect.bits.isFlushPipe, io.redirect.bits.isMisPred, io.redirect.bits.isReplay, io.redirect.bits.pc)
476    XSDebug(io.redirect.valid, p"Redirect from backend! target=${Hexadecimal(io.redirect.bits.target)} brTag=${io.redirect.bits.brTag}\n")
477
478    XSDebug("[IF1] v=%d     fire=%d            flush=%d pc=%x ptr=%d mask=%b\n", if1_valid, if1_fire, if1_flush, if1_npc, ptr, mask(if1_npc))
479    XSDebug("[IF2] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x ptr=%d snpc=%x\n", if2_valid, if2_ready, if2_fire, if2_redirect, if2_flush, if2_pc, if2_histPtr, if2_snpc)
480    XSDebug("[IF3] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x ptr=%d crossPageIPF=%d sawNTBrs=%d\n", if3_valid, if3_ready, if3_fire, if3_redirect, if3_flush, if3_pc, if3_histPtr, crossPageIPF, if3_realGHInfo.sawNTBr)
481    XSDebug("[IF4] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x ptr=%d crossPageIPF=%d sawNTBrs=%d\n", if4_valid, if4_ready, if4_fire, if4_redirect, if4_flush, if4_pc, if4_histPtr, if4_crossPageIPF, if4_realGHInfo.sawNTBr)
482    XSDebug("[IF1][icacheReq] v=%d r=%d addr=%x\n", io.icacheReq.valid, io.icacheReq.ready, io.icacheReq.bits.addr)
483    XSDebug("[IF1][ghr] headPtr=%d shiftPtr=%d newPtr=%d ptr=%d\n", if1_histPtr, shiftPtr, newPtr, ptr)
484    XSDebug("[IF1][ghr] hist=%b\n", hist.asUInt)
485    XSDebug("[IF1][ghr] extHist=%b\n\n", extHist.asUInt)
486
487    XSDebug("[IF2][bp] redirect=%d taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n\n", if2_bp.redirect, if2_bp.taken, if2_bp.jmpIdx, if2_bp.hasNotTakenBrs, if2_bp.target, if2_bp.saveHalfRVI)
488    // XSDebug("[IF2][GHInfo]: %s\n", if2_realGHInfo)
489    if2_realGHInfo.debug
490
491    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)
492    XSDebug("[IF3][bp] redirect=%d taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if3_bp.redirect, if3_bp.taken, if3_bp.jmpIdx, if3_bp.hasNotTakenBrs, if3_bp.target, if3_bp.saveHalfRVI)
493    // XSDebug("[IF3][prevHalfInstr] v=%d redirect=%d fetchpc=%x idx=%d tgt=%x taken=%d instr=%x\n\n",
494    //   prev_half_valid, prev_half_redirect, prev_half_fetchpc, prev_half_idx, prev_half_tgt, prev_half_taken, prev_half_instr)
495    XSDebug("[IF3][    prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n",
496      prevHalfInstr.valid, prevHalfInstr.taken, prevHalfInstr.fetchpc, prevHalfInstr.idx, prevHalfInstr.pc, prevHalfInstr.target, prevHalfInstr.instr, prevHalfInstr.ipf)
497    XSDebug("[IF3][if3_prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n\n",
498      if3_prevHalfInstr.valid, if3_prevHalfInstr.taken, if3_prevHalfInstr.fetchpc, if3_prevHalfInstr.idx, if3_prevHalfInstr.pc, if3_prevHalfInstr.target, if3_prevHalfInstr.instr, if3_prevHalfInstr.ipf)
499    // XSDebug("[IF3][GHInfo]: %s\n", if3_realGHInfo)
500    if3_realGHInfo.debug
501
502    XSDebug("[IF4][predecode] mask=%b\n", if4_pd.mask)
503    XSDebug("[IF4][bp] redirect=%d taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if4_bp.redirect, if4_bp.taken, if4_bp.jmpIdx, if4_bp.hasNotTakenBrs, if4_bp.target, if4_bp.saveHalfRVI)
504    XSDebug(if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken, "[IF4] cfi is jal!  instr=%x target=%x\n", if4_cfi_jal, if4_cfi_jal_tgt)
505    XSDebug("[IF4][if4_prevHalfInstr] v=%d taken=%d fetchpc=%x idx=%d pc=%x tgt=%x instr=%x ipf=%d\n",
506      if4_prevHalfInstr.valid, if4_prevHalfInstr.taken, if4_prevHalfInstr.fetchpc, if4_prevHalfInstr.idx, if4_prevHalfInstr.pc, if4_prevHalfInstr.target, if4_prevHalfInstr.instr, if4_prevHalfInstr.ipf)
507    // XSDebug("[IF4][GHInfo]: %s\n", if4_realGHInfo)
508    if4_realGHInfo.debug
509    XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] v=%d r=%d mask=%b ipf=%d crossPageIPF=%d\n",
510      io.fetchPacket.valid, io.fetchPacket.ready, io.fetchPacket.bits.mask, io.fetchPacket.bits.ipf, io.fetchPacket.bits.crossPageIPFFix)
511    for (i <- 0 until PredictWidth) {
512      XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] %b %x pc=%x pnpc=%x pd: rvc=%d brType=%b call=%d ret=%d\n",
513        io.fetchPacket.bits.mask(i),
514        io.fetchPacket.bits.instrs(i),
515        io.fetchPacket.bits.pc(i),
516        io.fetchPacket.bits.pnpc(i),
517        io.fetchPacket.bits.pd(i).isRVC,
518        io.fetchPacket.bits.pd(i).brType,
519        io.fetchPacket.bits.pd(i).isCall,
520        io.fetchPacket.bits.pd(i).isRet
521      )
522    }
523  }
524}