xref: /XiangShan/src/main/scala/xiangshan/frontend/Bim.scala (revision 608ba82cb057dd8254e664ceef4be08ae19dbbd7)
1package xiangshan.frontend
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import xiangshan.backend.ALUOpType
7import utils._
8import chisel3.util.experimental.BoringUtils
9import xiangshan.backend.decode.XSTrap
10
11trait BimParams extends HasXSParameter {
12  val BimBanks = PredictWidth
13  val BimSize = 4096
14  val nRows = BimSize / BimBanks
15}
16
17class BIM extends BasePredictor {
18  class BIMResp extends Resp {
19    val ctrs = Vec(PredictWidth, ValidUndirectioned(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 = new BIMResp
28    val meta = new BIMMeta
29  }
30
31  // Update logic
32  // 1 calculate new 2-bit saturated counter value
33  def satUpdate(old: UInt, len: Int, taken: Bool): UInt = {
34    val oldSatTaken = old === ((1 << len)-1).U
35    val oldSatNotTaken = old === 0.U
36    Mux(oldSatTaken && taken, ((1 << len)-1).U,
37      Mux(oldSatNotTaken && !taken, 0.U,
38        Mux(taken, old + 1.U, old - 1.U)))
39  }
40
41  val bimAddr = new TableAddr(log2Up(BimSize), BimBanks)
42
43  val pcLatch = RegEnable(io.pc.bits, io.pc.valid)
44
45  val bim = List.fill(BimBanks) {
46    Module(new SRAMTemplate(UInt(2.W), set = nRows, shouldReset = true, holdRead = true))
47  }
48
49  val baseBank = bimAddr.getBank(io.pc.bits)
50
51  val realMask = circularShiftLeft(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)(log2Up(BimBanks))))
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).valid := RegNext(io.pc.valid) // Does not need the valid bit
78    io.resp.ctrs(b).bits  := ctr
79    io.meta.ctrs(b)       := ctr
80  }
81
82  val u = io.update.bits
83
84  val updateBank = bimAddr.getBank(u.pc)
85  val updateRow = bimAddr.getBankIdx(u.pc)
86
87  val oldCtr = u.brInfo.bimCtr
88  val newTaken = u.taken
89  val oldSaturated = u.taken && oldCtr === 3.U || !u.taken && oldCtr === 0.U
90
91  val needToUpdate = io.update.valid && !oldSaturated && u.pd.isBr
92
93  for (b <- 0 until BimBanks) {
94    bim(b).io.w.req.valid := needToUpdate && b.U == updateBank
95    bim(b).io.w.req.bits.setIdx := updateRow
96    bim(b).io.w.req.bits.data = satUpdate(oldCtr, 2, newTaken)
97  }
98}