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