xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision 2199a01c65d5a7bf503c4b40771336a50a6f1122)
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 types = Vec(PredictWidth, UInt(2.W))
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 CfiUpdateInfo))
127  }
128
129  val io = new DefaultBasePredictorIO
130  val debug = true
131}
132
133class BPUStageIO extends XSBundle {
134  val pc = UInt(VAddrBits.W)
135  val mask = UInt(PredictWidth.W)
136  val resp = new PredictorResponse
137  val brInfo = Vec(PredictWidth, new BpuMeta)
138}
139
140
141abstract class BPUStage extends XSModule with HasBPUParameter
142  with HasIFUConst with HasIFUFire {
143  class DefaultIO extends XSBundle {
144    val in = Input(new BPUStageIO)
145    val inFire = Input(Bool())
146    val pred = Output(new BranchPrediction) // to ifu
147    val out = Output(new BPUStageIO)        // to the next stage
148    val outFire = Input(Bool())
149
150    val debug_hist = Input(UInt((if (BPUDebug) (HistoryLength) else 0).W))
151  }
152  val io = IO(new DefaultIO)
153
154  val inLatch = RegEnable(io.in, io.inFire)
155
156  // Each stage has its own logic to decide
157  // takens, brMask, jalMask, targets and hasHalfRVI
158  val takens = Wire(Vec(PredictWidth, Bool()))
159  val brMask = Wire(Vec(PredictWidth, Bool()))
160  val jalMask = Wire(Vec(PredictWidth, Bool()))
161  val targets = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
162  val hasHalfRVI = Wire(Bool())
163
164  io.pred <> DontCare
165  io.pred.takens := takens.asUInt
166  io.pred.brMask := brMask.asUInt
167  io.pred.jalMask := jalMask.asUInt
168  io.pred.targets := targets
169  io.pred.hasHalfRVI := hasHalfRVI
170
171  io.out <> DontCare
172  io.out.pc := inLatch.pc
173  io.out.mask := inLatch.mask
174  io.out.resp <> inLatch.resp
175  io.out.brInfo := inLatch.brInfo
176  (0 until PredictWidth).map(i => io.out.brInfo(i).sawNotTakenBranch := io.pred.sawNotTakenBr(i))
177
178  if (BPUDebug) {
179    val jmpIdx = io.pred.jmpIdx
180    val taken  = io.pred.taken
181    val target = Mux(taken, io.pred.targets(jmpIdx), snpc(inLatch.pc))
182    XSDebug("in(%d): pc=%x, mask=%b\n", io.inFire, io.in.pc, io.in.mask)
183    XSDebug("inLatch: pc=%x, mask=%b\n", inLatch.pc, inLatch.mask)
184    XSDebug("out(%d): pc=%x, mask=%b, taken=%d, jmpIdx=%d, target=%x, hasHalfRVI=%d\n",
185      io.outFire, io.out.pc, io.out.mask, taken, jmpIdx, target, hasHalfRVI)
186    val p = io.pred
187  }
188}
189
190@chiselName
191class BPUStage1 extends BPUStage {
192
193  // ubtb is accessed with inLatch pc in s1,
194  // so we use io.in instead of inLatch
195  val ubtbResp = io.in.resp.ubtb
196  // the read operation is already masked, so we do not need to mask here
197  takens    := VecInit((0 until PredictWidth).map(i => ubtbResp.takens(i)))
198  // notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && !ubtbResp.takens(i) && ubtbResp.brMask(i)))
199  brMask := ubtbResp.brMask
200  jalMask := DontCare
201  targets := ubtbResp.targets
202
203  hasHalfRVI := ubtbResp.hits(PredictWidth-1) && !ubtbResp.is_RVC(PredictWidth-1) && HasCExtension.B
204
205  // resp and brInfo are from the components,
206  // so it does not need to be latched
207  io.out.resp <> io.in.resp
208  io.out.brInfo := io.in.brInfo
209
210  if (BPUDebug) {
211    XSDebug(io.outFire, "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n",
212      ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ~ubtbResp.takens.asUInt & brMask.asUInt, ubtbResp.is_RVC.asUInt)
213  }
214  if (EnableBPUTimeRecord) {
215    io.out.brInfo.map(_.debug_ubtb_cycle := GTimer())
216  }
217}
218@chiselName
219class BPUStage2 extends BPUStage {
220  // Use latched response from s1
221  val btbResp = inLatch.resp.btb
222  val bimResp = inLatch.resp.bim
223  takens    := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B)))
224  targets := btbResp.targets
225  brMask  := VecInit((0 until PredictWidth).map(i => btbResp.types(i) === BTBtype.B && btbResp.hits(i)))
226  jalMask := DontCare
227
228  hasHalfRVI  := btbResp.hits(PredictWidth-1) && !btbResp.isRVC(PredictWidth-1) && HasCExtension.B
229
230  if (BPUDebug) {
231    XSDebug(io.outFire, "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n",
232      btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt)
233  }
234  if (EnableBPUTimeRecord) {
235    io.out.brInfo.map(_.debug_btb_cycle := GTimer())
236  }
237}
238@chiselName
239class BPUStage3 extends BPUStage {
240  class S3IO extends XSBundle {
241
242    val predecode = Input(new Predecode)
243    val realMask = Input(UInt(PredictWidth.W))
244    val prevHalf = Flipped(ValidIO(new PrevHalfInstr))
245    val recover =  Flipped(ValidIO(new CfiUpdateInfo))
246  }
247  val s3IO = IO(new S3IO)
248  // TAGE has its own pipelines and the
249  // response comes directly from s3,
250  // so we do not use those from inLatch
251  val tageResp = io.in.resp.tage
252  val tageTakens = tageResp.takens
253
254  val loopResp = io.in.resp.loop.exit
255
256  // realMask is in it
257  val pdMask     = s3IO.predecode.mask
258  val pdLastHalf = s3IO.predecode.lastHalf
259  val pds        = s3IO.predecode.pd
260
261  val btbResp   = WireInit(inLatch.resp.btb)
262  val btbHits   = WireInit(btbResp.hits.asUInt)
263  val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1)))
264
265  val brs   = pdMask & Reverse(Cat(pds.map(_.isBr)))
266  val jals  = pdMask & Reverse(Cat(pds.map(_.isJal)))
267  val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr)))
268  val calls = pdMask & Reverse(Cat(pds.map(_.isCall)))
269  val rets  = pdMask & Reverse(Cat(pds.map(_.isRet)))
270  val RVCs  = pdMask & Reverse(Cat(pds.map(_.isRVC)))
271
272  val callIdx = PriorityEncoder(calls)
273  val retIdx  = PriorityEncoder(rets)
274
275  val brPred = (if(EnableBPD) tageTakens else bimTakens).asUInt
276  val loopRes = (if (EnableLoop) loopResp else VecInit(Fill(PredictWidth, 0.U(1.W)))).asUInt
277  val prevHalfTaken = s3IO.prevHalf.valid && s3IO.prevHalf.bits.taken && HasCExtension.B
278  val prevHalfTakenMask = prevHalfTaken.asUInt
279  val brTakens = ((brs & brPred | prevHalfTakenMask) & ~loopRes)
280  // we should provide btb resp as well
281  btbHits := btbResp.hits.asUInt | prevHalfTakenMask
282
283  // predict taken only if btb has a target, jal targets will be provided by IFU
284  takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i)))
285
286
287  targets := inLatch.resp.btb.targets
288
289  brMask  := WireInit(brs.asTypeOf(Vec(PredictWidth, Bool())))
290  jalMask := WireInit(jals.asTypeOf(Vec(PredictWidth, Bool())))
291
292  hasHalfRVI  := pdLastHalf && HasCExtension.B
293
294  //RAS
295  if(EnableRAS){
296    val ras = Module(new RAS)
297    ras.io <> DontCare
298    ras.io.pc.bits := packetAligned(inLatch.pc)
299    ras.io.pc.valid := io.outFire//predValid
300    ras.io.is_ret := rets.orR  && (retIdx === io.pred.jmpIdx)
301    ras.io.callIdx.valid := calls.orR && (callIdx === io.pred.jmpIdx)
302    ras.io.callIdx.bits := callIdx
303    ras.io.isRVC := (calls & RVCs).orR   //TODO: this is ugly
304    ras.io.isLastHalfRVI := s3IO.predecode.hasLastHalfRVI
305    ras.io.recover := s3IO.recover
306    ras.fires <> fires
307
308    for(i <- 0 until PredictWidth){
309      io.out.brInfo(i).rasSp :=  ras.io.meta.rasSp
310      io.out.brInfo(i).rasTopCtr := ras.io.meta.rasTopCtr
311      io.out.brInfo(i).rasToqAddr := ras.io.meta.rasToqAddr
312    }
313    takens := VecInit((0 until PredictWidth).map(i => {
314      ((brTakens(i) || jalrs(i)) && btbHits(i)) ||
315          jals(i) ||
316          (ras.io.out.valid && rets(i)) ||
317          (!ras.io.out.valid && rets(i) && btbHits(i))
318      }
319    ))
320
321    for (i <- 0 until PredictWidth) {
322      when(rets(i) && ras.io.out.valid){
323        targets(i) := ras.io.out.bits.target
324      }
325    }
326  }
327
328
329  // we should provide the prediction for the first half RVI of the end of a fetch packet
330  // branch taken information would be lost in the prediction of the next packet,
331  // so we preserve this information here
332  when (hasHalfRVI && btbResp.types(PredictWidth-1) === BTBtype.B && btbHits(PredictWidth-1) && HasCExtension.B) {
333    takens(PredictWidth-1) := brPred(PredictWidth-1) && !loopRes(PredictWidth-1)
334  }
335
336  // targets would be lost as well, since it is from btb
337  // unless it is a ret, which target is from ras
338  when (prevHalfTaken && !rets(0) && HasCExtension.B) {
339    targets(0) := s3IO.prevHalf.bits.target
340  }
341
342  // Wrap tage resp and tage meta in
343  // This is ugly
344  io.out.resp.tage <> io.in.resp.tage
345  io.out.resp.loop <> io.in.resp.loop
346  for (i <- 0 until PredictWidth) {
347    io.out.brInfo(i).tageMeta := io.in.brInfo(i).tageMeta
348    io.out.brInfo(i).specCnt  := io.in.brInfo(i).specCnt
349  }
350
351  if (BPUDebug) {
352    XSDebug(io.inFire, "predecode: pc:%x, mask:%b\n", inLatch.pc, s3IO.predecode.mask)
353    for (i <- 0 until PredictWidth) {
354      val p = s3IO.predecode.pd(i)
355      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",
356        i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType)
357    }
358    XSDebug(p"brs:${Binary(brs)} jals:${Binary(jals)} jalrs:${Binary(jalrs)} calls:${Binary(calls)} rets:${Binary(rets)} rvcs:${Binary(RVCs)}\n")
359    XSDebug(p"callIdx:${callIdx} retIdx:${retIdx}\n")
360    XSDebug(p"brPred:${Binary(brPred)} loopRes:${Binary(loopRes)} prevHalfTaken:${prevHalfTaken} brTakens:${Binary(brTakens)}\n")
361  }
362
363  if (EnbaleCFIPredLog) {
364    val out = io.out
365    XSDebug(io.outFire, p"cfi_pred: fetchpc(${Hexadecimal(out.pc)}) mask(${out.mask}) brmask(${brMask.asUInt}) hist(${Hexadecimal(io.debug_hist)})\n")
366  }
367
368  if (EnableBPUTimeRecord) {
369    io.out.brInfo.map(_.debug_tage_cycle := GTimer())
370  }
371}
372
373trait BranchPredictorComponents extends HasXSParameter {
374  val ubtb = Module(new MicroBTB)
375  val btb = Module(new BTB)
376  val bim = Module(new BIM)
377  val tage = (if(EnableBPD) { Module(new Tage) }
378              else          { Module(new FakeTage) })
379  val loop = Module(new LoopPredictor)
380  val preds = Seq(ubtb, btb, bim, tage, loop)
381  preds.map(_.io := DontCare)
382}
383
384class BPUReq extends XSBundle {
385  val pc = UInt(VAddrBits.W)
386  val hist = UInt(HistoryLength.W)
387  val inMask = UInt(PredictWidth.W)
388}
389
390abstract class BaseBPU extends XSModule with BranchPredictorComponents with HasBPUParameter{
391  val io = IO(new Bundle() {
392    // from backend
393    val cfiUpdateInfo    = Flipped(ValidIO(new CfiUpdateInfo))
394    // from if1
395    val in = Input(new BPUReq)
396    val inFire = Input(Vec(4, Bool()))
397    // to if2/if3/if4
398    val out = Vec(3, Output(new BranchPrediction))
399    // from if4
400    val predecode = Input(new Predecode)
401    val realMask = Input(UInt(PredictWidth.W))
402    val prevHalf = Flipped(ValidIO(new PrevHalfInstr))
403    // to if4, some bpu info used for updating
404    val bpuMeta = Output(Vec(PredictWidth, new BpuMeta))
405  })
406
407  preds.map(p => {
408    p.io.update <> io.cfiUpdateInfo
409    p.fires <> io.inFire
410  })
411
412  val s1 = Module(new BPUStage1)
413  val s2 = Module(new BPUStage2)
414  val s3 = Module(new BPUStage3)
415
416  Seq(s1, s2, s3).foreach(s => s.fires <> io.inFire)
417
418  val s1_fire = io.inFire(0)
419  val s2_fire = io.inFire(1)
420  val s3_fire = io.inFire(2)
421  val s4_fire = io.inFire(3)
422
423  s1.io.in <> DontCare
424  s2.io.in <> s1.io.out
425  s3.io.in <> s2.io.out
426
427  s1.io.inFire := s1_fire
428  s2.io.inFire := s2_fire
429  s3.io.inFire := s3_fire
430
431  s1.io.outFire := s2_fire
432  s2.io.outFire := s3_fire
433  s3.io.outFire := s4_fire
434
435  io.out(0) <> s1.io.pred
436  io.out(1) <> s2.io.pred
437  io.out(2) <> s3.io.pred
438
439  io.bpuMeta := s3.io.out.brInfo
440
441  if (BPUDebug) {
442    XSDebug(io.inFire(3), "bpuMeta sent!\n")
443    for (i <- 0 until PredictWidth) {
444      val b = io.bpuMeta(i)
445      XSDebug(io.inFire(3), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n",
446        i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx)
447      val t = b.tageMeta
448      XSDebug(io.inFire(3), "  tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n",
449        t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits)
450    }
451  }
452  val debug_verbose = false
453}
454
455
456class FakeBPU extends BaseBPU {
457  io.out.foreach(i => {
458    // Provide not takens
459    i <> DontCare
460    i.takens := 0.U
461  })
462  io.bpuMeta <> DontCare
463}
464@chiselName
465class BPU extends BaseBPU {
466
467  //**********************Stage 1****************************//
468
469  val s1_resp_in = Wire(new PredictorResponse)
470  val s1_brInfo_in = Wire(Vec(PredictWidth, new BpuMeta))
471
472  s1_resp_in.tage := DontCare
473  s1_resp_in.loop := DontCare
474  s1_brInfo_in    := DontCare
475  (0 until PredictWidth).foreach(i => s1_brInfo_in(i).fetchIdx := i.U)
476
477  val s1_inLatch = RegEnable(io.in, s1_fire)
478  ubtb.io.pc.valid := s2_fire
479  ubtb.io.pc.bits := s1_inLatch.pc
480  ubtb.io.inMask := s1_inLatch.inMask
481
482
483
484  // Wrap ubtb response into resp_in and brInfo_in
485  s1_resp_in.ubtb <> ubtb.io.out
486  for (i <- 0 until PredictWidth) {
487    s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBMeta.writeWay(i)
488    s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBMeta.hits(i)
489  }
490
491  btb.io.pc.valid := s1_fire
492  btb.io.pc.bits := io.in.pc
493  btb.io.inMask := io.in.inMask
494
495
496
497  // Wrap btb response into resp_in and brInfo_in
498  s1_resp_in.btb <> btb.io.resp
499  for (i <- 0 until PredictWidth) {
500    s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i)
501    s1_brInfo_in(i).btbHitJal   := btb.io.meta.hitJal(i)
502  }
503
504  bim.io.pc.valid := s1_fire
505  bim.io.pc.bits := io.in.pc
506  bim.io.inMask := io.in.inMask
507
508
509  // Wrap bim response into resp_in and brInfo_in
510  s1_resp_in.bim <> bim.io.resp
511  for (i <- 0 until PredictWidth) {
512    s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i)
513  }
514
515
516  s1.io.inFire := s1_fire
517  s1.io.in.pc := io.in.pc
518  s1.io.in.mask := io.in.inMask
519  s1.io.in.resp <> s1_resp_in
520  s1.io.in.brInfo <> s1_brInfo_in
521
522  val s1_hist = RegEnable(io.in.hist, enable=s1_fire)
523  val s2_hist = RegEnable(s1_hist, enable=s2_fire)
524  val s3_hist = RegEnable(s2_hist, enable=s3_fire)
525
526  s1.io.debug_hist := s1_hist
527  s2.io.debug_hist := s2_hist
528  s3.io.debug_hist := s3_hist
529
530  //**********************Stage 2****************************//
531  tage.io.pc.valid := s2_fire
532  tage.io.pc.bits := s2.io.in.pc // PC from s1
533  tage.io.hist := s1_hist // The inst is from s1
534  tage.io.inMask := s2.io.in.mask
535  tage.io.bim <> s1.io.out.resp.bim // Use bim results from s1
536
537  //**********************Stage 3****************************//
538  // Wrap tage response and meta into s3.io.in.bits
539  // This is ugly
540
541  loop.io.pc.valid := s2_fire
542  loop.io.if3_fire := s3_fire
543  loop.io.pc.bits := s2.io.in.pc
544  loop.io.inMask := io.predecode.mask
545  loop.io.respIn.taken := s3.io.pred.taken
546  loop.io.respIn.jmpIdx := s3.io.pred.jmpIdx
547
548
549  s3.io.in.resp.tage <> tage.io.resp
550  s3.io.in.resp.loop <> loop.io.resp
551  for (i <- 0 until PredictWidth) {
552    s3.io.in.brInfo(i).tageMeta := tage.io.meta(i)
553    s3.io.in.brInfo(i).specCnt := loop.io.meta.specCnts(i)
554  }
555
556  s3.s3IO.predecode <> io.predecode
557
558  s3.s3IO.realMask := io.realMask
559
560  s3.s3IO.prevHalf := io.prevHalf
561
562  s3.s3IO.recover.valid <> io.cfiUpdateInfo.valid
563  s3.s3IO.recover.bits <> io.cfiUpdateInfo.bits
564
565  if (BPUDebug) {
566    if (debug_verbose) {
567      val uo = ubtb.io.out
568      XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, ~uo.takens.asUInt & uo.brMask.asUInt)
569      val bio = bim.io.resp
570      XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt)
571      val bo = btb.io.resp
572      XSDebug("debug: btb hits:%b\n", bo.hits.asUInt)
573    }
574  }
575
576
577
578  if (EnableCFICommitLog) {
579    val buValid = io.cfiUpdateInfo.valid && !io.cfiUpdateInfo.bits.isReplay
580    val buinfo  = io.cfiUpdateInfo.bits
581    val pd = buinfo.pd
582    val tage_cycle = buinfo.bpuMeta.debug_tage_cycle
583    XSDebug(buValid, p"cfi_update: isBr(${pd.isBr}) pc(${Hexadecimal(buinfo.pc)}) taken(${buinfo.taken}) mispred(${buinfo.isMisPred}) cycle($tage_cycle) hist(${Hexadecimal(buinfo.bpuMeta.predHist.asUInt)})\n")
584  }
585
586}
587
588object BPU{
589  def apply(enableBPU: Boolean = true) = {
590      if(enableBPU) {
591        val BPU = Module(new BPU)
592        BPU
593      }
594      else {
595        val FakeBPU = Module(new FakeBPU)
596        FakeBPU
597      }
598  }
599}
600