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