xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 3fce4f48e1f4c43253ff1758dbd05b8b71627990)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import xiangshan.backend.ALUOpType
7import utils._
8import xiangshan.backend.decode.XSTrap
9
10trait BimParams extends HasXSParameter {
11  val BimBanks = PredictWidth
12  val BimSize = 4096
13  val nRows = BimSize / BimBanks
14  val bypassEntries = 4
15}
16
17class BIM extends BasePredictor with BimParams{
18  class BIMResp extends Resp {
19    val ctrs = Vec(PredictWidth, UInt(2.W))
20  }
21  class BIMMeta extends Meta {
22    val ctrs = Vec(PredictWidth, UInt(2.W))
23  }
24  class BIMFromOthers extends FromOthers {}
25
26  class BIMIO extends DefaultBasePredictorIO {
27    val resp = Output(new BIMResp)
28    val meta = Output(new BIMMeta)
29  }
30
31  override val io = IO(new BIMIO)
32  // Update logic
33  // 1 calculate new 2-bit saturated counter value
34
35
36  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)
37
38  val pcLatch = RegEnable(io.pc.bits, io.pc.valid)
39
40  val bim = List.fill(BimBanks) {
41    Module(new SRAMTemplate(UInt(2.W), set = nRows, shouldReset = false, holdRead = true))
42  }
43
44  val doing_reset = RegInit(true.B)
45  val resetRow = RegInit(0.U(log2Ceil(nRows).W))
46  resetRow := resetRow + doing_reset
47  when (resetRow === (nRows-1).U) { doing_reset := false.B }
48
49  val baseBank = bimAddr.getBank(io.pc.bits)
50
51  val realMask = circularShiftRight(io.inMask, BimBanks, baseBank)
52
53  // those banks whose indexes are less than baseBank are in the next row
54  val isInNextRow = VecInit((0 until BtbBanks).map(_.U < baseBank))
55
56  val baseRow = bimAddr.getBankIdx(io.pc.bits)
57
58  val realRow = VecInit((0 until BimBanks).map(b => Mux(isInNextRow(b.U), (baseRow+1.U)(log2Up(nRows)-1, 0), baseRow)))
59
60  val realRowLatch = VecInit(realRow.map(RegEnable(_, enable=io.pc.valid)))
61
62  for (b <- 0 until BimBanks) {
63    bim(b).reset                := reset.asBool
64    bim(b).io.r.req.valid       := realMask(b) && io.pc.valid
65    bim(b).io.r.req.bits.setIdx := realRow(b)
66  }
67
68  val bimRead = VecInit(bim.map(_.io.r.resp.data(0)))
69
70  val baseBankLatch = bimAddr.getBank(pcLatch)
71
72  // e.g: baseBank == 5 => (5, 6,..., 15, 0, 1, 2, 3, 4)
73  val bankIdxInOrder = VecInit((0 until BimBanks).map(b => (baseBankLatch +& b.U)(log2Up(BimBanks)-1, 0)))
74
75  for (b <- 0 until BimBanks) {
76    val ctr = bimRead(bankIdxInOrder(b))
77    io.resp.ctrs(b)  := ctr
78    io.meta.ctrs(b)  := ctr
79  }
80
81  val u = io.update.bits.ui
82
83  val updateBank = bimAddr.getBank(u.pc)
84  val updateRow = bimAddr.getBankIdx(u.pc)
85
86
87  val wrbypass_ctrs       = Reg(Vec(bypassEntries, Vec(BimBanks, UInt(2.W))))
88  val wrbypass_ctr_valids = Reg(Vec(bypassEntries, Vec(BimBanks, Bool())))
89  val wrbypass_rows     = Reg(Vec(bypassEntries, UInt(log2Up(nRows).W)))
90  val wrbypass_enq_idx  = RegInit(0.U(log2Up(bypassEntries).W))
91
92  val wrbypass_hits = VecInit((0 until bypassEntries).map( i =>
93    !doing_reset && wrbypass_rows(i) === updateRow))
94  val wrbypass_hit = wrbypass_hits.reduce(_||_)
95  val wrbypass_hit_idx = PriorityEncoder(wrbypass_hits)
96
97  val oldCtr = Mux(wrbypass_hit && wrbypass_ctr_valids(wrbypass_hit_idx)(updateBank), wrbypass_ctrs(wrbypass_hit_idx)(updateBank), u.brInfo.bimCtr)
98  val newTaken = u.taken
99  val newCtr = satUpdate(oldCtr, 2, newTaken)
100  // val oldSaturated = newCtr === oldCtr
101
102  val needToUpdate = io.update.valid && u.pd.isBr
103
104  when (reset.asBool) { wrbypass_ctr_valids.foreach(_.foreach(_ := false.B))}
105
106  when (needToUpdate) {
107    when (wrbypass_hit) {
108      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
109      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
110    } .otherwise {
111      wrbypass_ctrs(wrbypass_hit_idx)(updateBank) := newCtr
112      (0 until BimBanks).foreach(b => wrbypass_ctr_valids(wrbypass_enq_idx)(b) := false.B) // reset valid bits
113      wrbypass_ctr_valids(wrbypass_enq_idx)(updateBank) := true.B
114      wrbypass_rows(wrbypass_enq_idx) := updateRow
115      wrbypass_enq_idx := (wrbypass_enq_idx + 1.U)(log2Up(bypassEntries)-1,0)
116    }
117  }
118
119  for (b <- 0 until BimBanks) {
120    bim(b).io.w.req.valid := needToUpdate && b.U === updateBank || doing_reset
121    bim(b).io.w.req.bits.setIdx := Mux(doing_reset, resetRow, updateRow)
122    bim(b).io.w.req.bits.data := Mux(doing_reset, 2.U(2.W), newCtr)
123  }
124
125  if (BPUDebug && debug) {
126    XSDebug(doing_reset, "Reseting...\n")
127    XSDebug("[update] v=%d pc=%x pnpc=%x tgt=%x brTgt=%x\n", io.update.valid, u.pc, u.pnpc, u.target, u.brTarget)
128    XSDebug("[update] taken=%d isMisPred=%d", u.taken, u.isMisPred)
129    XSDebug(false, true.B, p"brTag=${u.brTag} pd.isBr=${u.pd.isBr} brInfo.bimCtr=${Binary(u.brInfo.bimCtr)}\n")
130    XSDebug("needToUpdate=%d updateBank=%x updateRow=%x newCtr=%b oldCtr=%b\n", needToUpdate, updateBank, updateRow, newCtr, oldCtr)
131    XSDebug("[wrbypass] hit=%d hits=%b\n", wrbypass_hit, wrbypass_hits.asUInt)
132  }
133
134}