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