xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision 7b2bea82c44dd9822bc8bcc9a697a03e1d97779f)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import utils._
6import xiangshan._
7import xiangshan.backend.ALUOpType
8import xiangshan.backend.JumpOpType
9
10trait HasBPUParameter extends HasXSParameter {
11  val BPUDebug = true
12}
13
14class TableAddr(val idxBits: Int, val banks: Int) extends XSBundle {
15  def tagBits = VAddrBits - idxBits - 1
16
17  val tag = UInt(tagBits.W)
18  val idx = UInt(idxBits.W)
19  val offset = UInt(1.W)
20
21  def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this)
22  def getTag(x: UInt) = fromUInt(x).tag
23  def getIdx(x: UInt) = fromUInt(x).idx
24  def getBank(x: UInt) = getIdx(x)(log2Up(banks) - 1, 0)
25  def getBankIdx(x: UInt) = getIdx(x)(idxBits - 1, log2Up(banks))
26}
27
28class PredictorResponse extends XSBundle {
29  class UbtbResp extends XSBundle {
30  // the valid bits indicates whether a target is hit
31    val targets = Vec(PredictWidth, UInt(VAddrBits.W))
32    val hits = Vec(PredictWidth, Bool())
33    val takens = Vec(PredictWidth, Bool())
34    val notTakens = Vec(PredictWidth, Bool())
35    val is_RVC = Vec(PredictWidth, Bool())
36  }
37  class BtbResp extends XSBundle {
38  // the valid bits indicates whether a target is hit
39    val targets = Vec(PredictWidth, UInt(VAddrBits.W))
40    val hits = Vec(PredictWidth, Bool())
41    val types = Vec(PredictWidth, UInt(2.W))
42    val isRVC = Vec(PredictWidth, Bool())
43  }
44  class BimResp extends XSBundle {
45    val ctrs = Vec(PredictWidth, UInt(2.W))
46  }
47  class TageResp extends XSBundle {
48  // the valid bits indicates whether a prediction is hit
49    val takens = Vec(PredictWidth, Bool())
50    val hits = Vec(PredictWidth, Bool())
51  }
52
53  val ubtb = new UbtbResp
54  val btb = new BtbResp
55  val bim = new BimResp
56  val tage = new TageResp
57}
58
59abstract class BasePredictor extends XSModule with HasBPUParameter{
60  val metaLen = 0
61
62  // An implementation MUST extend the IO bundle with a response
63  // and the special input from other predictors, as well as
64  // the metas to store in BRQ
65  abstract class Resp extends XSBundle {}
66  abstract class FromOthers extends XSBundle {}
67  abstract class Meta extends XSBundle {}
68
69  class DefaultBasePredictorIO extends XSBundle {
70    val flush = Input(Bool())
71    val pc = Flipped(ValidIO(UInt(VAddrBits.W)))
72    val hist = Input(UInt(HistoryLength.W))
73    val inMask = Input(UInt(PredictWidth.W))
74    val update = Flipped(ValidIO(new BranchUpdateInfoWithHist))
75  }
76
77  val io = new DefaultBasePredictorIO
78
79  val debug = true
80
81  // circular shifting
82  def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = {
83    val res = Wire(UInt(len.W))
84    val higher = source << shamt
85    val lower = source >> (len.U - shamt)
86    res := higher | lower
87    res
88  }
89
90  def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = {
91    val res = Wire(UInt(len.W))
92    val higher = source << (len.U - shamt)
93    val lower = source >> shamt
94    res := higher | lower
95    res
96  }
97}
98
99class BPUStageIO extends XSBundle {
100  val pc = UInt(VAddrBits.W)
101  val mask = UInt(PredictWidth.W)
102  val resp = new PredictorResponse
103  val target = UInt(VAddrBits.W)
104  val brInfo = Vec(PredictWidth, new BranchInfo)
105}
106
107
108abstract class BPUStage extends XSModule with HasBPUParameter{
109  class DefaultIO extends XSBundle {
110    val flush = Input(Bool())
111    val in = Flipped(Decoupled(new BPUStageIO))
112    val pred = Decoupled(new BranchPrediction)
113    val out = Decoupled(new BPUStageIO)
114    val predecode = Flipped(ValidIO(new Predecode))
115    val recover =  Flipped(ValidIO(new BranchUpdateInfo))
116    val cacheValid = Input(Bool())
117  }
118  val io = IO(new DefaultIO)
119
120  val predValid = RegInit(false.B)
121
122  io.in.ready := !predValid || io.out.fire() && io.pred.fire() || io.flush
123
124  def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U)
125
126  val inFire = io.in.fire()
127  val inLatch = RegEnable(io.in.bits, inFire)
128
129  val outFire = io.out.fire()
130
131  // Each stage has its own logic to decide
132  // takens, notTakens and target
133
134  val takens = Wire(Vec(PredictWidth, Bool()))
135  val notTakens = Wire(Vec(PredictWidth, Bool()))
136  val jmpIdx = PriorityEncoder(takens)
137  val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i)).reduce(_||_)
138  val taken = takens.reduce(_||_)
139  // get the last valid inst
140  val lastValidPos = WireInit(PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U)))
141  val lastHit   = Wire(Bool())
142  val lastIsRVC = Wire(Bool())
143
144  val targetSrc = Wire(Vec(PredictWidth, UInt(VAddrBits.W)))
145  val target = Mux(taken, targetSrc(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask)))
146
147  io.pred.bits <> DontCare
148  io.pred.bits.redirect := target =/= inLatch.target
149  io.pred.bits.taken := taken
150  io.pred.bits.jmpIdx := jmpIdx
151  io.pred.bits.hasNotTakenBrs := hasNTBr
152  io.pred.bits.target := target
153  io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken && !(jmpIdx === 0.U && !io.predecode.bits.isFetchpcEqualFirstpc)) || !taken ) && !lastIsRVC && lastHit
154
155  io.out.bits <> DontCare
156  io.out.bits.pc := inLatch.pc
157  io.out.bits.mask := inLatch.mask
158  io.out.bits.target := target
159  io.out.bits.resp <> inLatch.resp
160  io.out.bits.brInfo := inLatch.brInfo
161
162  // Default logic
163  //  pred.ready not taken into consideration
164  //  could be broken
165  when (io.flush)     { predValid := false.B }
166  .elsewhen (inFire)  { predValid := true.B }
167  .elsewhen (outFire) { predValid := false.B }
168  .otherwise          { predValid := predValid }
169
170  io.out.valid  := predValid && !io.flush
171  io.pred.valid := predValid && !io.flush
172
173  if (BPUDebug) {
174    XSDebug(io.in.fire(), "in:(%d %d) pc=%x, mask=%b, target=%x\n",
175      io.in.valid, io.in.ready, io.in.bits.pc, io.in.bits.mask, io.in.bits.target)
176    XSDebug(io.out.fire(), "out:(%d %d) pc=%x, mask=%b, target=%x\n",
177      io.out.valid, io.out.ready, io.out.bits.pc, io.out.bits.mask, io.out.bits.target)
178    XSDebug("flush=%d\n", io.flush)
179    XSDebug("taken=%d, takens=%b, notTakens=%b, jmpIdx=%d, hasNTBr=%d, lastValidPos=%d, target=%x\n",
180      taken, takens.asUInt, notTakens.asUInt, jmpIdx, hasNTBr, lastValidPos, target)
181    val p = io.pred.bits
182    XSDebug(io.pred.fire(), "outPred: redirect=%d, taken=%d, jmpIdx=%d, hasNTBrs=%d, target=%x, saveHalfRVI=%d\n",
183      p.redirect, p.taken, p.jmpIdx, p.hasNotTakenBrs, p.target, p.saveHalfRVI)
184    XSDebug(io.pred.fire() && p.taken, "outPredTaken: fetchPC:%x, jmpPC:%x\n",
185      inLatch.pc, inLatch.pc + (jmpIdx << 1.U))
186    XSDebug(io.pred.fire() && p.redirect, "outPred: previous target:%x redirected to %x \n",
187      inLatch.target, p.target)
188    XSDebug(io.pred.fire(), "outPred targetSrc: ")
189    for (i <- 0 until PredictWidth) {
190      XSDebug(false, io.pred.fire(), "(%d):%x ", i.U, targetSrc(i))
191    }
192    XSDebug(false, io.pred.fire(), "\n")
193  }
194}
195
196class BPUStage1 extends BPUStage {
197
198  // 'overrides' default logic
199  // when flush, the prediction should also starts
200  when (inFire)        { predValid := true.B }
201  .elsewhen (io.flush) { predValid := false.B }
202  .elsewhen (outFire)  { predValid := false.B }
203  .otherwise           { predValid := predValid }
204  // io.out.valid := predValid
205
206  // ubtb is accessed with inLatch pc in s1,
207  // so we use io.in instead of inLatch
208  val ubtbResp = io.in.bits.resp.ubtb
209  // the read operation is already masked, so we do not need to mask here
210  takens    := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.takens(i)))
211  notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.notTakens(i)))
212  targetSrc := ubtbResp.targets
213
214  lastIsRVC := ubtbResp.is_RVC(lastValidPos)
215  lastHit   := ubtbResp.hits(lastValidPos)
216
217  // resp and brInfo are from the components,
218  // so it does not need to be latched
219  io.out.bits.resp <> io.in.bits.resp
220  io.out.bits.brInfo := io.in.bits.brInfo
221
222  if (BPUDebug) {
223    io.out.bits.brInfo.map(_.debug_ubtb_cycle := GTimer())
224    XSDebug(io.pred.fire(), "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n",
225      ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ubtbResp.notTakens.asUInt, ubtbResp.is_RVC.asUInt)
226  }
227}
228
229class BPUStage2 extends BPUStage {
230
231  io.out.valid := predValid && !io.flush && io.cacheValid
232  // Use latched response from s1
233  val btbResp = inLatch.resp.btb
234  val bimResp = inLatch.resp.bim
235  takens    := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B)))
236  notTakens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && btbResp.types(i) === BTBtype.B && !bimResp.ctrs(i)(1)))
237  targetSrc := btbResp.targets
238
239  lastIsRVC := btbResp.isRVC(lastValidPos)
240  lastHit   := btbResp.hits(lastValidPos)
241
242
243  if (BPUDebug) {
244    io.out.bits.brInfo.map(_.debug_btb_cycle := GTimer())
245    XSDebug(io.pred.fire(), "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n",
246      btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt)
247  }
248}
249
250class BPUStage3 extends BPUStage {
251
252
253  io.out.valid := predValid && io.predecode.valid && !io.flush
254  // TAGE has its own pipelines and the
255  // response comes directly from s3,
256  // so we do not use those from inLatch
257  val tageResp = io.in.bits.resp.tage
258  val tageTakens = tageResp.takens
259
260  val pdMask = io.predecode.bits.mask
261  val pds    = io.predecode.bits.pd
262
263  val btbHits   = inLatch.resp.btb.hits.asUInt
264  val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1)))
265
266  val brs   = pdMask & Reverse(Cat(pds.map(_.isBr)))
267  val jals  = pdMask & Reverse(Cat(pds.map(_.isJal)))
268  val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr)))
269  val calls = pdMask & Reverse(Cat(pds.map(_.isCall)))
270  val rets  = pdMask & Reverse(Cat(pds.map(_.isRet)))
271  val RVCs = pdMask & Reverse(Cat(pds.map(_.isRVC)))
272
273   val callIdx = PriorityEncoder(calls)
274   val retIdx  = PriorityEncoder(rets)
275
276  val brTakens =
277    if (EnableBPD) {
278      brs & Reverse(Cat((0 until PredictWidth).map(i => tageTakens(i))))
279    } else {
280      brs & Reverse(Cat((0 until PredictWidth).map(i => bimTakens(i))))
281    }
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  // Whether should we count in branches that are not recorded in btb?
286  // PS: Currently counted in. Whenever tage does not provide a valid
287  //     taken prediction, the branch is counted as a not taken branch
288  notTakens := (if (EnableBPD) { VecInit((0 until PredictWidth).map(i => brs(i) && !tageTakens(i)))}
289                else           { VecInit((0 until PredictWidth).map(i => brs(i) && !bimTakens(i)))})
290  targetSrc := inLatch.resp.btb.targets
291
292  //RAS
293  if(EnableRAS){
294    val ras = Module(new RAS)
295    ras.io <> DontCare
296    ras.io.pc.bits := inLatch.pc
297    ras.io.pc.valid := io.out.fire()//predValid
298    ras.io.is_ret := rets.orR  && (retIdx === jmpIdx) && io.predecode.valid
299    ras.io.callIdx.valid := calls.orR && (callIdx === jmpIdx) && io.predecode.valid
300    ras.io.callIdx.bits := callIdx
301    ras.io.isRVC := (calls & RVCs).orR   //TODO: this is ugly
302    ras.io.recover := io.recover
303
304    for(i <- 0 until PredictWidth){
305      io.out.bits.brInfo(i).rasSp :=  ras.io.branchInfo.rasSp
306      io.out.bits.brInfo(i).rasTopCtr := ras.io.branchInfo.rasTopCtr
307      io.out.bits.brInfo(i).rasToqAddr := ras.io.branchInfo.rasToqAddr
308    }
309    takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i)|| rets(i)))
310    when(ras.io.is_ret && ras.io.out.valid){targetSrc(retIdx) :=  ras.io.out.bits.target}
311  }
312
313
314  // when (!io.predecode.bits.isFetchpcEqualFirstpc) {
315  //   lastValidPos := PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U)) + 1.U
316  // }
317
318  lastIsRVC := pds(lastValidPos).isRVC
319  when (lastValidPos === 1.U) {
320    lastHit := pdMask(1) |
321      !pdMask(0) & !pdMask(1) |
322      pdMask(0) & !pdMask(1) & (pds(0).isRVC | !io.predecode.bits.isFetchpcEqualFirstpc)
323  }.elsewhen (lastValidPos > 0.U) {
324    lastHit := pdMask(lastValidPos) |
325      !pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) |
326      pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) & pds(lastValidPos - 1.U).isRVC
327  }.otherwise {
328    lastHit := pdMask(0) | !pdMask(0) & !pds(0).isRVC
329  }
330
331
332  // Wrap tage resp and tage meta in
333  // This is ugly
334  io.out.bits.resp.tage <> io.in.bits.resp.tage
335  for (i <- 0 until PredictWidth) {
336    io.out.bits.brInfo(i).tageMeta := io.in.bits.brInfo(i).tageMeta
337  }
338
339  if (BPUDebug) {
340    io.out.bits.brInfo.map(_.debug_tage_cycle := GTimer())
341    XSDebug(io.predecode.valid, "predecode: pc:%x, mask:%b\n", inLatch.pc, io.predecode.bits.mask)
342    for (i <- 0 until PredictWidth) {
343      val p = io.predecode.bits.pd(i)
344      XSDebug(io.predecode.valid && io.predecode.bits.mask(i), "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n",
345        i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType)
346    }
347  }
348}
349
350trait BranchPredictorComponents extends HasXSParameter {
351  val ubtb = Module(new MicroBTB)
352  val btb = Module(new BTB)
353  val bim = Module(new BIM)
354  val tage = (if(EnableBPD) { Module(new Tage) }
355              else          { Module(new FakeTage) })
356  val preds = Seq(ubtb, btb, bim, tage)
357  preds.map(_.io := DontCare)
358}
359
360class BPUReq extends XSBundle {
361  val pc = UInt(VAddrBits.W)
362  val hist = UInt(HistoryLength.W)
363  val inMask = UInt(PredictWidth.W)
364}
365
366class BranchUpdateInfoWithHist extends XSBundle {
367  val ui = new BranchUpdateInfo
368  val hist = UInt(HistoryLength.W)
369}
370
371object BranchUpdateInfoWithHist {
372  def apply (brInfo: BranchUpdateInfo, hist: UInt) = {
373    val b = Wire(new BranchUpdateInfoWithHist)
374    b.ui <> brInfo
375    b.hist := hist
376    b
377  }
378}
379
380abstract class BaseBPU extends XSModule with BranchPredictorComponents with HasBPUParameter{
381  val io = IO(new Bundle() {
382    // from backend
383    val inOrderBrInfo    = Flipped(ValidIO(new BranchUpdateInfoWithHist))
384    val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist))
385    // from ifu, frontend redirect
386    val flush = Input(Vec(3, Bool()))
387    val cacheValid = Input(Bool())
388    // from if1
389    val in = Flipped(ValidIO(new BPUReq))
390    // to if2/if3/if4
391    val out = Vec(3, Decoupled(new BranchPrediction))
392    // from if4
393    val predecode = Flipped(ValidIO(new Predecode))
394    // to if4, some bpu info used for updating
395    val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo))
396  })
397
398  def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U)
399
400  preds.map(_.io.update <> io.outOfOrderBrInfo)
401  tage.io.update <> io.inOrderBrInfo
402
403  val s1 = Module(new BPUStage1)
404  val s2 = Module(new BPUStage2)
405  val s3 = Module(new BPUStage3)
406
407  s1.io.flush := io.flush(0)
408  s2.io.flush := io.flush(1)
409  s3.io.flush := io.flush(2)
410
411  s1.io.in <> DontCare
412  s2.io.in <> s1.io.out
413  s3.io.in <> s2.io.out
414
415  io.out(0) <> s1.io.pred
416  io.out(1) <> s2.io.pred
417  io.out(2) <> s3.io.pred
418
419  s1.io.predecode <> DontCare
420  s2.io.predecode <> DontCare
421  s3.io.predecode <> io.predecode
422
423  io.branchInfo.valid := s3.io.out.valid
424  io.branchInfo.bits := s3.io.out.bits.brInfo
425  s3.io.out.ready := io.branchInfo.ready
426
427  s1.io.recover <> DontCare
428  s2.io.recover <> DontCare
429  s3.io.recover.valid <> io.inOrderBrInfo.valid
430  s3.io.recover.bits <> io.inOrderBrInfo.bits.ui
431
432  s1.io.cacheValid := DontCare
433  s2.io.cacheValid := io.cacheValid
434  s3.io.cacheValid := io.cacheValid
435
436  if (BPUDebug) {
437    XSDebug(io.branchInfo.fire(), "branchInfo sent!\n")
438    for (i <- 0 until PredictWidth) {
439      val b = io.branchInfo.bits(i)
440      XSDebug(io.branchInfo.fire(), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n",
441        i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx)
442      val t = b.tageMeta
443      XSDebug(io.branchInfo.fire(), "  tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n",
444        t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits)
445    }
446  }
447  val debug_verbose = false
448}
449
450
451class FakeBPU extends BaseBPU {
452  io.out.foreach(i => {
453    // Provide not takens
454    i.valid := true.B
455    i.bits <> DontCare
456    i.bits.redirect := false.B
457  })
458  io.branchInfo <> DontCare
459}
460
461class BPU extends BaseBPU {
462
463  //**********************Stage 1****************************//
464  val s1_fire = s1.io.in.fire()
465  val s1_resp_in = Wire(new PredictorResponse)
466  val s1_brInfo_in = Wire(Vec(PredictWidth, new BranchInfo))
467
468  s1_resp_in.tage := DontCare
469  s1_brInfo_in    := DontCare
470  (0 until PredictWidth).foreach(i => s1_brInfo_in(i).fetchIdx := i.U)
471
472  val s1_inLatch = RegEnable(io.in, s1_fire)
473  ubtb.io.flush := io.flush(0) // TODO: fix this
474  ubtb.io.pc.valid := s1_inLatch.valid
475  ubtb.io.pc.bits := s1_inLatch.bits.pc
476  ubtb.io.inMask := s1_inLatch.bits.inMask
477
478
479
480  // Wrap ubtb response into resp_in and brInfo_in
481  s1_resp_in.ubtb <> ubtb.io.out
482  for (i <- 0 until PredictWidth) {
483    s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBBranchInfo.writeWay(i)
484    s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBBranchInfo.hits(i)
485  }
486
487  btb.io.flush := io.flush(0) // TODO: fix this
488  btb.io.pc.valid := io.in.valid
489  btb.io.pc.bits := io.in.bits.pc
490  btb.io.inMask := io.in.bits.inMask
491
492
493
494  // Wrap btb response into resp_in and brInfo_in
495  s1_resp_in.btb <> btb.io.resp
496  for (i <- 0 until PredictWidth) {
497    s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i)
498    s1_brInfo_in(i).btbHitJal   := btb.io.meta.hitJal(i)
499  }
500
501  bim.io.flush := io.flush(0) // TODO: fix this
502  bim.io.pc.valid := io.in.valid
503  bim.io.pc.bits := io.in.bits.pc
504  bim.io.inMask := io.in.bits.inMask
505
506
507  // Wrap bim response into resp_in and brInfo_in
508  s1_resp_in.bim <> bim.io.resp
509  for (i <- 0 until PredictWidth) {
510    s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i)
511  }
512
513
514  s1.io.in.valid := io.in.valid
515  s1.io.in.bits.pc := io.in.bits.pc
516  s1.io.in.bits.mask := io.in.bits.inMask
517  s1.io.in.bits.target := npc(io.in.bits.pc, PopCount(io.in.bits.inMask)) // Deault target npc
518  s1.io.in.bits.resp <> s1_resp_in
519  s1.io.in.bits.brInfo <> s1_brInfo_in
520
521  val s1_hist = RegEnable(io.in.bits.hist, enable=io.in.valid)
522
523  //**********************Stage 2****************************//
524  tage.io.flush := io.flush(1) // TODO: fix this
525  tage.io.pc.valid := s1.io.out.fire()
526  tage.io.pc.bits := s1.io.out.bits.pc // PC from s1
527  tage.io.hist := s1_hist // The inst is from s1
528  tage.io.inMask := s1.io.out.bits.mask
529  tage.io.s3Fire := s3.io.in.fire() // Tell tage to march 1 stage
530  tage.io.bim <> s1.io.out.bits.resp.bim // Use bim results from s1
531
532  //**********************Stage 3****************************//
533  // Wrap tage response and meta into s3.io.in.bits
534  // This is ugly
535
536  s3.io.in.bits.resp.tage <> tage.io.resp
537  for (i <- 0 until PredictWidth) {
538    s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i)
539  }
540
541  if (BPUDebug) {
542    if (debug_verbose) {
543      val uo = ubtb.io.out
544      XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, uo.notTakens.asUInt)
545      val bio = bim.io.resp
546      XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt)
547      val bo = btb.io.resp
548      XSDebug("debug: btb hits:%b\n", bo.hits.asUInt)
549    }
550  }
551
552}
553
554object BPU{
555  def apply(enableBPU: Boolean = true) = {
556      if(enableBPU) {
557        val BPU = Module(new BPU)
558        BPU
559      }
560      else {
561        val FakeBPU = Module(new FakeBPU)
562        FakeBPU
563      }
564  }
565}