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