xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision fdf3b10b3a6a6cb7476f5d29b2dda84dbc5cefde)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.backend.ALUOpType
8import xiangshan.backend.JumpOpType
9import chisel3.experimental.chiselName
10
11trait HasBPUParameter extends HasXSParameter {
12  val BPUDebug = true && !env.FPGAPlatform
13  val EnableCFICommitLog = true
14  val EnbaleCFIPredLog = true
15  val EnableBPUTimeRecord = (EnableCFICommitLog || EnbaleCFIPredLog) && !env.FPGAPlatform
16  val EnableCommit = false
17}
18
19class TableAddr(val idxBits: Int, val banks: Int) extends XSBundle with HasIFUConst {
20  def tagBits = VAddrBits - idxBits - instOffsetBits
21
22  val tag = UInt(tagBits.W)
23  val idx = UInt(idxBits.W)
24  val offset = UInt(instOffsetBits.W)
25
26  def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this)
27  def getTag(x: UInt) = fromUInt(x).tag
28  def getIdx(x: UInt) = fromUInt(x).idx
29  def getBank(x: UInt) = getIdx(x)(log2Up(banks) - 1, 0)
30  def getBankIdx(x: UInt) = getIdx(x)(idxBits - 1, log2Up(banks))
31}
32
33class PredictorResponse extends XSBundle {
34  class UbtbResp extends XSBundle {
35  // the valid bits indicates whether a target is hit
36    val targets = Vec(PredictWidth, UInt(VAddrBits.W))
37    val hits = Vec(PredictWidth, Bool())
38    val takens = Vec(PredictWidth, Bool())
39    val brMask = Vec(PredictWidth, Bool())
40    val is_RVC = Vec(PredictWidth, Bool())
41  }
42  class BtbResp extends XSBundle {
43  // the valid bits indicates whether a target is hit
44    val targets = Vec(PredictWidth, UInt(VAddrBits.W))
45    val hits = Vec(PredictWidth, Bool())
46    val isBrs = Vec(PredictWidth, Bool())
47    val isRVC = Vec(PredictWidth, Bool())
48  }
49  class BimResp extends XSBundle {
50    val ctrs = Vec(PredictWidth, UInt(2.W))
51  }
52  class TageResp extends XSBundle {
53  // the valid bits indicates whether a prediction is hit
54    val takens = Vec(PredictWidth, Bool())
55    val hits = Vec(PredictWidth, Bool())
56  }
57  class LoopResp extends XSBundle {
58    val exit = Vec(PredictWidth, Bool())
59  }
60
61  val ubtb = new UbtbResp
62  val btb = new BtbResp
63  val bim = new BimResp
64  val tage = new TageResp
65  val loop = new LoopResp
66}
67
68trait PredictorUtils {
69  // circular shifting
70  def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = {
71    val res = Wire(UInt(len.W))
72    val higher = source << shamt
73    val lower = source >> (len.U - shamt)
74    res := higher | lower
75    res
76  }
77
78  def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = {
79    val res = Wire(UInt(len.W))
80    val higher = source << (len.U - shamt)
81    val lower = source >> shamt
82    res := higher | lower
83    res
84  }
85
86  // To be verified
87  def satUpdate(old: UInt, len: Int, taken: Bool): UInt = {
88    val oldSatTaken = old === ((1 << len)-1).U
89    val oldSatNotTaken = old === 0.U
90    Mux(oldSatTaken && taken, ((1 << len)-1).U,
91      Mux(oldSatNotTaken && !taken, 0.U,
92        Mux(taken, old + 1.U, old - 1.U)))
93  }
94
95  def signedSatUpdate(old: SInt, len: Int, taken: Bool): SInt = {
96    val oldSatTaken = old === ((1 << (len-1))-1).S
97    val oldSatNotTaken = old === (-(1 << (len-1))).S
98    Mux(oldSatTaken && taken, ((1 << (len-1))-1).S,
99      Mux(oldSatNotTaken && !taken, (-(1 << (len-1))).S,
100        Mux(taken, old + 1.S, old - 1.S)))
101  }
102}
103
104trait HasIFUFire { this: MultiIOModule =>
105  val fires = IO(Input(Vec(4, Bool())))
106  val s1_fire  = fires(0)
107  val s2_fire  = fires(1)
108  val s3_fire  = fires(2)
109  val out_fire = fires(3)
110}
111abstract class BasePredictor extends XSModule
112  with HasBPUParameter with HasIFUConst with PredictorUtils
113  with HasIFUFire {
114  val metaLen = 0
115
116  // An implementation MUST extend the IO bundle with a response
117  // and the special input from other predictors, as well as
118  // the metas to store in BRQ
119  abstract class Resp extends XSBundle {}
120  abstract class FromOthers extends XSBundle {}
121  abstract class Meta extends XSBundle {}
122
123  class DefaultBasePredictorIO extends XSBundle {
124    val pc = Flipped(ValidIO(UInt(VAddrBits.W)))
125    val hist = Input(UInt(HistoryLength.W))
126    val inMask = Input(UInt(PredictWidth.W))
127    val update = Flipped(ValidIO(new FtqEntry))
128  }
129
130  val io = new DefaultBasePredictorIO
131  val debug = true
132}
133
134class BrInfo extends XSBundle {
135  val metas = Vec(PredictWidth, new BpuMeta)
136  val rasSp = UInt(log2Ceil(RasSize).W)
137  val rasTop = new RASEntry
138  val specCnt = Vec(PredictWidth, UInt(10.W))
139}
140class BPUStageIO extends XSBundle {
141  val pc = UInt(VAddrBits.W)
142  val mask = UInt(PredictWidth.W)
143  val resp = new PredictorResponse
144  val brInfo = new BrInfo
145}
146
147
148abstract class BPUStage extends XSModule with HasBPUParameter
149  with HasIFUConst with HasIFUFire {
150  class DefaultIO extends XSBundle {
151    val in = Input(new BPUStageIO)
152    val inFire = Input(Bool())
153    val pred = Output(new BranchPrediction) // to ifu
154    val out = Output(new BPUStageIO)        // to the next stage
155    val outFire = Input(Bool())
156
157    val debug_hist = Input(UInt((if (BPUDebug) (HistoryLength) else 0).W))
158  }
159  val io = IO(new DefaultIO)
160
161  val inLatch = RegEnable(io.in, io.inFire)
162
163  // Each stage has its own logic to decide
164  // takens, brMask, jalMask, targets and hasHalfRVI
165  val takens = Wire(Vec(PredictWidth, Bool()))
166  val brMask = Wire(Vec(PredictWidth, Bool()))
167  val jalMask = Wire(Vec(PredictWidth, Bool()))
168  val targets = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
169  val hasHalfRVI = Wire(Bool())
170
171  io.pred <> DontCare
172  io.pred.takens := takens.asUInt
173  io.pred.brMask := brMask.asUInt
174  io.pred.jalMask := jalMask.asUInt
175  io.pred.targets := targets
176  io.pred.hasHalfRVI := hasHalfRVI
177
178  io.out <> DontCare
179  io.out.pc := inLatch.pc
180  io.out.mask := inLatch.mask
181  io.out.resp <> inLatch.resp
182  io.out.brInfo := inLatch.brInfo
183
184  if (BPUDebug) {
185    val jmpIdx = io.pred.jmpIdx
186    val taken  = io.pred.taken
187    val target = Mux(taken, io.pred.targets(jmpIdx), snpc(inLatch.pc))
188    XSDebug("in(%d): pc=%x, mask=%b\n", io.inFire, io.in.pc, io.in.mask)
189    XSDebug("inLatch: pc=%x, mask=%b\n", inLatch.pc, inLatch.mask)
190    XSDebug("out(%d): pc=%x, mask=%b, taken=%d, jmpIdx=%d, target=%x, hasHalfRVI=%d\n",
191      io.outFire, io.out.pc, io.out.mask, taken, jmpIdx, target, hasHalfRVI)
192    val p = io.pred
193  }
194}
195
196@chiselName
197class BPUStage1 extends BPUStage {
198
199  // ubtb is accessed with inLatch pc in s1,
200  // so we use io.in instead of inLatch
201  val ubtbResp = io.in.resp.ubtb
202  // the read operation is already masked, so we do not need to mask here
203  takens    := VecInit((0 until PredictWidth).map(i => ubtbResp.takens(i)))
204  // notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && !ubtbResp.takens(i) && ubtbResp.brMask(i)))
205  brMask := ubtbResp.brMask
206  jalMask := DontCare
207  targets := ubtbResp.targets
208
209  hasHalfRVI := ubtbResp.hits(PredictWidth-1) && !ubtbResp.is_RVC(PredictWidth-1) && HasCExtension.B
210
211  // resp and brInfo are from the components,
212  // so it does not need to be latched
213  io.out.resp <> io.in.resp
214  io.out.brInfo := io.in.brInfo
215
216  // For perf counters
217  if (!env.FPGAPlatform && env.EnablePerfDebug) {
218    io.out.brInfo.metas.zipWithIndex.foreach{case (meta, i) =>
219      // record ubtb pred result
220      meta.ubtbAns.hit := ubtbResp.hits(i)
221      meta.ubtbAns.taken := ubtbResp.takens(i)
222      meta.ubtbAns.target := ubtbResp.targets(i)
223    }
224  }
225
226  if (BPUDebug) {
227    XSDebug(io.outFire, "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n",
228      ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ~ubtbResp.takens.asUInt & brMask.asUInt, ubtbResp.is_RVC.asUInt)
229  }
230  if (EnableBPUTimeRecord) {
231    io.out.brInfo.metas.map(_.debug_ubtb_cycle := GTimer())
232  }
233}
234@chiselName
235class BPUStage2 extends BPUStage {
236  // Use latched response from s1
237  val btbResp = inLatch.resp.btb
238  val bimResp = inLatch.resp.bim
239  takens    := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.isBrs(i) && bimResp.ctrs(i)(1) || !btbResp.isBrs(i))))
240  targets := btbResp.targets
241  brMask  := VecInit((0 until PredictWidth).map(i => btbResp.isBrs(i) && btbResp.hits(i)))
242  jalMask := DontCare
243
244  hasHalfRVI  := btbResp.hits(PredictWidth-1) && !btbResp.isRVC(PredictWidth-1) && HasCExtension.B
245
246  // For perf counters
247  if (!env.FPGAPlatform && env.EnablePerfDebug) {
248    io.out.brInfo.metas.zipWithIndex.foreach{case (meta, i) =>
249      // record btb pred result
250      meta.btbAns.hit := btbResp.hits(i)
251      meta.btbAns.taken := takens(i)
252      meta.btbAns.target := btbResp.targets(i)
253    }
254  }
255
256  if (BPUDebug) {
257    XSDebug(io.outFire, "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n",
258      btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt)
259  }
260  if (EnableBPUTimeRecord) {
261    io.out.brInfo.metas.map(_.debug_btb_cycle := GTimer())
262  }
263}
264@chiselName
265class BPUStage3 extends BPUStage {
266  class S3IO extends XSBundle {
267
268    val predecode = Input(new Predecode)
269    val redirect =  Flipped(ValidIO(new Redirect))
270  }
271  val s3IO = IO(new S3IO)
272  // TAGE has its own pipelines and the
273  // response comes directly from s3,
274  // so we do not use those from inLatch
275  val tageResp = io.in.resp.tage
276  val tageTakens = tageResp.takens
277
278  val loopResp = io.in.resp.loop.exit
279
280  val pdMask     = s3IO.predecode.mask
281  val pdLastHalf = s3IO.predecode.lastHalf
282  val pds        = s3IO.predecode.pd
283
284  val btbResp   = WireInit(inLatch.resp.btb)
285  val btbHits   = WireInit(btbResp.hits.asUInt)
286  val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1)))
287
288  val brs   = pdMask & Reverse(Cat(pds.map(_.isBr)))
289  val jals  = pdMask & Reverse(Cat(pds.map(_.isJal)))
290  val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr)))
291  val calls = pdMask & Reverse(Cat(pds.map(_.isCall)))
292  val rets  = pdMask & Reverse(Cat(pds.map(_.isRet)))
293  val RVCs  = pdMask & Reverse(Cat(pds.map(_.isRVC)))
294
295  val callIdx = PriorityEncoder(calls)
296  val retIdx  = PriorityEncoder(rets)
297
298  val brPred = (if(EnableBPD) tageTakens else bimTakens).asUInt
299  val loopRes = (if (EnableLoop) loopResp else VecInit(Fill(PredictWidth, 0.U(1.W)))).asUInt
300  val brTakens = ((brs & brPred) & ~loopRes)
301  // we should provide btb resp as well
302  btbHits := btbResp.hits.asUInt
303
304  // predict taken only if btb has a target, jal and br targets will be provided by IFU
305  takens := VecInit((0 until PredictWidth).map(i => jalrs(i) && btbHits(i) || (jals(i) || brTakens(i))))
306
307
308  targets := inLatch.resp.btb.targets
309
310  brMask  := WireInit(brs.asTypeOf(Vec(PredictWidth, Bool())))
311  jalMask := WireInit(jals.asTypeOf(Vec(PredictWidth, Bool())))
312
313  hasHalfRVI  := pdLastHalf && HasCExtension.B
314
315  //RAS
316  if(EnableRAS){
317    val ras = Module(new RAS)
318    ras.io <> DontCare
319    ras.io.pc.bits := packetAligned(inLatch.pc)
320    ras.io.pc.valid := io.outFire//predValid
321    ras.io.is_ret := rets.orR  && (retIdx === io.pred.jmpIdx)
322    ras.io.callIdx.valid := calls.orR && (callIdx === io.pred.jmpIdx)
323    ras.io.callIdx.bits := callIdx
324    ras.io.isRVC := (calls & RVCs).orR   //TODO: this is ugly
325    ras.io.isLastHalfRVI := s3IO.predecode.hasLastHalfRVI
326    ras.io.redirect := s3IO.redirect
327    ras.fires <> fires
328
329    for(i <- 0 until PredictWidth){
330      io.out.brInfo.rasSp :=  ras.io.meta.rasSp
331      io.out.brInfo.rasTop :=  ras.io.meta.rasTop
332    }
333    takens := VecInit((0 until PredictWidth).map(i => {
334      (jalrs(i) && btbHits(i)) ||
335          jals(i) || brTakens(i) ||
336          (ras.io.out.valid && rets(i)) ||
337          (!ras.io.out.valid && rets(i) && btbHits(i))
338      }
339    ))
340
341    for (i <- 0 until PredictWidth) {
342      when(rets(i) && ras.io.out.valid){
343        targets(i) := ras.io.out.bits.target
344      }
345    }
346
347    // For perf counters
348    if (!env.FPGAPlatform && env.EnablePerfDebug) {
349      io.out.brInfo.metas.zipWithIndex.foreach{case (meta, i) =>
350        // record tage pred result
351        meta.tageAns.hit := tageResp.hits(i)
352        meta.tageAns.taken := tageResp.takens(i)
353        meta.tageAns.target := DontCare
354
355        // record ras pred result
356        meta.rasAns.hit := ras.io.out.valid
357        meta.rasAns.taken := true.B
358        meta.rasAns.target := ras.io.out.bits.target
359
360        // record loop pred result
361        meta.loopAns.hit := loopRes(i)
362        meta.loopAns.taken := false.B
363        meta.loopAns.target := DontCare
364      }
365    }
366  }
367
368
369  // Wrap tage resp and tage meta in
370  // This is ugly
371  io.out.resp.tage <> io.in.resp.tage
372  io.out.resp.loop <> io.in.resp.loop
373  for (i <- 0 until PredictWidth) {
374    io.out.brInfo.metas(i).tageMeta := io.in.brInfo.metas(i).tageMeta
375    io.out.brInfo.specCnt(i) := io.in.brInfo.specCnt(i)
376  }
377
378  if (BPUDebug) {
379    XSDebug(io.inFire, "predecode: pc:%x, mask:%b\n", inLatch.pc, s3IO.predecode.mask)
380    for (i <- 0 until PredictWidth) {
381      val p = s3IO.predecode.pd(i)
382      XSDebug(io.inFire && s3IO.predecode.mask(i), "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n",
383        i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType)
384    }
385    XSDebug(p"brs:${Binary(brs)} jals:${Binary(jals)} jalrs:${Binary(jalrs)} calls:${Binary(calls)} rets:${Binary(rets)} rvcs:${Binary(RVCs)}\n")
386    XSDebug(p"callIdx:${callIdx} retIdx:${retIdx}\n")
387    XSDebug(p"brPred:${Binary(brPred)} loopRes:${Binary(loopRes)} brTakens:${Binary(brTakens)}\n")
388  }
389
390  if (EnbaleCFIPredLog) {
391    val out = io.out
392    XSDebug(io.outFire, p"cfi_pred: fetchpc(${Hexadecimal(out.pc)}) mask(${out.mask}) brmask(${brMask.asUInt}) hist(${Hexadecimal(io.debug_hist)})\n")
393  }
394
395  if (EnableBPUTimeRecord) {
396    io.out.brInfo.metas.map(_.debug_tage_cycle := GTimer())
397  }
398}
399
400trait BranchPredictorComponents extends HasXSParameter {
401  val ubtb = Module(new MicroBTB)
402  val btb = Module(new BTB)
403  val bim = Module(new BIM)
404  val tage = (if(EnableBPD) { if (EnableSC) Module(new Tage_SC)
405                              else          Module(new Tage) }
406              else          { Module(new FakeTage) })
407  val loop = Module(new LoopPredictor)
408  val preds = Seq(ubtb, btb, bim, tage, loop)
409  preds.map(_.io := DontCare)
410}
411
412class BPUReq extends XSBundle {
413  val pc = UInt(VAddrBits.W)
414  val hist = UInt(HistoryLength.W)
415  val inMask = UInt(PredictWidth.W)
416}
417
418abstract class BaseBPU extends XSModule with BranchPredictorComponents
419  with HasBPUParameter with HasIFUConst {
420  val io = IO(new Bundle() {
421    // from backend
422    val redirect = Flipped(ValidIO(new Redirect))
423    val commit   = Flipped(ValidIO(new FtqEntry))
424    // from if1
425    val in = Input(new BPUReq)
426    val inFire = Input(Vec(4, Bool()))
427    // to if2/if3/if4
428    val out = Vec(3, Output(new BranchPrediction))
429    // from if4
430    val predecode = Input(new Predecode)
431    // to if4, some bpu info used for updating
432    val brInfo = Output(new BrInfo)
433  })
434
435  preds.map(p => {
436    p.io.update <> io.commit
437    p.fires <> io.inFire
438  })
439
440  val s1 = Module(new BPUStage1)
441  val s2 = Module(new BPUStage2)
442  val s3 = Module(new BPUStage3)
443
444  Seq(s1, s2, s3).foreach(s => s.fires <> io.inFire)
445
446  val s1_fire = io.inFire(0)
447  val s2_fire = io.inFire(1)
448  val s3_fire = io.inFire(2)
449  val s4_fire = io.inFire(3)
450
451  s1.io.in <> DontCare
452  s2.io.in <> s1.io.out
453  s3.io.in <> s2.io.out
454
455  s1.io.inFire := s1_fire
456  s2.io.inFire := s2_fire
457  s3.io.inFire := s3_fire
458
459  s1.io.outFire := s2_fire
460  s2.io.outFire := s3_fire
461  s3.io.outFire := s4_fire
462
463  io.out(0) <> s1.io.pred
464  io.out(1) <> s2.io.pred
465  io.out(2) <> s3.io.pred
466
467  io.brInfo := s3.io.out.brInfo
468
469  if (BPUDebug) {
470    XSDebug(io.inFire(3), "bpuMeta sent!\n")
471    for (i <- 0 until PredictWidth) {
472      val b = io.brInfo.metas(i)
473      XSDebug(io.inFire(3), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, bimCtr:%d\n",
474        i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.bimCtr)
475      val t = b.tageMeta
476      XSDebug(io.inFire(3), "  tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n",
477        t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits)
478    }
479  }
480  val debug_verbose = false
481}
482
483
484class FakeBPU extends BaseBPU {
485  io.out.foreach(i => {
486    // Provide not takens
487    i <> DontCare
488    i.takens := 0.U
489  })
490  io.brInfo <> DontCare
491}
492@chiselName
493class BPU extends BaseBPU {
494
495  //**********************Stage 1****************************//
496
497  val s1_resp_in = Wire(new PredictorResponse)
498  val s1_brInfo_in = Wire(new BrInfo)
499
500  s1_resp_in.tage := DontCare
501  s1_resp_in.loop := DontCare
502  s1_brInfo_in    := DontCare
503
504  val s1_inLatch = RegEnable(io.in, s1_fire)
505  ubtb.io.pc.valid := s2_fire
506  ubtb.io.pc.bits := s1_inLatch.pc
507  ubtb.io.inMask := s1_inLatch.inMask
508
509
510
511  // Wrap ubtb response into resp_in and brInfo_in
512  s1_resp_in.ubtb <> ubtb.io.out
513  for (i <- 0 until PredictWidth) {
514    s1_brInfo_in.metas(i).ubtbWriteWay := ubtb.io.uBTBMeta.writeWay(i)
515    s1_brInfo_in.metas(i).ubtbHits := ubtb.io.uBTBMeta.hits(i)
516  }
517
518  btb.io.pc.valid := s1_fire
519  btb.io.pc.bits := io.in.pc
520  btb.io.inMask := io.in.inMask
521
522
523
524  // Wrap btb response into resp_in and brInfo_in
525  s1_resp_in.btb <> btb.io.resp
526  for (i <- 0 until PredictWidth) {
527    s1_brInfo_in.metas(i).btbWriteWay := btb.io.meta.writeWay(i)
528  }
529
530  bim.io.pc.valid := s1_fire
531  bim.io.pc.bits := io.in.pc
532  bim.io.inMask := io.in.inMask
533
534
535  // Wrap bim response into resp_in and brInfo_in
536  s1_resp_in.bim <> bim.io.resp
537  for (i <- 0 until PredictWidth) {
538    s1_brInfo_in.metas(i).bimCtr := bim.io.meta.ctrs(i)
539  }
540
541
542  s1.io.inFire := s1_fire
543  s1.io.in.pc := io.in.pc
544  s1.io.in.mask := io.in.inMask
545  s1.io.in.resp <> s1_resp_in
546  s1.io.in.brInfo <> s1_brInfo_in
547
548  val s1_hist = RegEnable(io.in.hist, enable=s1_fire)
549  val s2_hist = RegEnable(s1_hist, enable=s2_fire)
550  val s3_hist = RegEnable(s2_hist, enable=s3_fire)
551
552  s1.io.debug_hist := s1_hist
553  s2.io.debug_hist := s2_hist
554  s3.io.debug_hist := s3_hist
555
556  //**********************Stage 2****************************//
557  tage.io.pc.valid := s2_fire
558  tage.io.pc.bits := s2.io.in.pc // PC from s1
559  tage.io.hist := s1_hist // The inst is from s1
560  tage.io.inMask := s2.io.in.mask
561  tage.io.bim <> s1.io.out.resp.bim // Use bim results from s1
562
563  //**********************Stage 3****************************//
564  // Wrap tage response and meta into s3.io.in.bits
565  // This is ugly
566
567  loop.io.pc.valid := s2_fire
568  loop.io.if3_fire := s3_fire
569  loop.io.pc.bits := s2.io.in.pc
570  loop.io.inMask := io.predecode.mask
571  loop.io.respIn.taken := s3.io.pred.taken
572  loop.io.respIn.jmpIdx := s3.io.pred.jmpIdx
573  loop.io.redirect := s3.s3IO.redirect
574
575
576  s3.io.in.resp.tage <> tage.io.resp
577  s3.io.in.resp.loop <> loop.io.resp
578  for (i <- 0 until PredictWidth) {
579    s3.io.in.brInfo.metas(i).tageMeta := tage.io.meta(i)
580    s3.io.in.brInfo.specCnt(i) := loop.io.meta.specCnts(i)
581  }
582
583  s3.s3IO.predecode <> io.predecode
584
585  s3.s3IO.redirect <> io.redirect
586
587  if (BPUDebug) {
588    if (debug_verbose) {
589      val uo = ubtb.io.out
590      XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, ~uo.takens.asUInt & uo.brMask.asUInt)
591      val bio = bim.io.resp
592      XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt)
593      val bo = btb.io.resp
594      XSDebug("debug: btb hits:%b\n", bo.hits.asUInt)
595    }
596  }
597
598
599
600  if (EnableCFICommitLog) {
601    val buValid = io.commit.valid
602    val buinfo  = io.commit.bits
603    for (i <- 0 until PredictWidth) {
604      val cfi_idx = buinfo.cfiIndex
605      val isTaken = cfi_idx.valid && cfi_idx.bits === i.U
606      val isCfi = buinfo.valids(i) && (buinfo.br_mask(i) || cfi_idx.valid && cfi_idx.bits === i.U)
607      val isBr = buinfo.br_mask(i)
608      val pc = packetAligned(buinfo.ftqPC) + (i * instBytes).U - Mux((i==0).B && buinfo.hasLastPrev, 2.U, 0.U)
609      val tage_cycle = buinfo.metas(i).debug_tage_cycle
610      XSDebug(buValid && isCfi, p"cfi_update: isBr(${isBr}) pc(${Hexadecimal(pc)}) taken(${isTaken}) mispred(${buinfo.mispred(i)}) cycle($tage_cycle) hist(${Hexadecimal(buinfo.predHist.asUInt)})\n")
611    }
612  }
613
614}
615
616object BPU{
617  def apply(enableBPU: Boolean = true) = {
618      if(enableBPU) {
619        val BPU = Module(new BPU)
620        BPU
621      }
622      else {
623        val FakeBPU = Module(new FakeBPU)
624        FakeBPU
625      }
626  }
627}
628