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