|
| 1 | +package treecorel2 |
| 2 | + |
| 3 | +import chisel3._ |
| 4 | +import chisel3.util._ |
| 5 | + |
| 6 | +import treecorel2.common.ConstVal |
| 7 | + |
| 8 | +class BTBLine extends Bundle { |
| 9 | + val pc = UInt(ConstVal.BTBPcLen.W) |
| 10 | + val tgt = UInt(ConstVal.BTBTgtLen.W) |
| 11 | + val jump = Bool() |
| 12 | +} |
| 13 | + |
| 14 | +class BTB extends Module { |
| 15 | + val io = IO(new Bundle { |
| 16 | + // branch info (from idu) |
| 17 | + val branch = Input(Bool()) |
| 18 | + val jump = Input(Bool()) |
| 19 | + val pc = Input(UInt(ConstVal.AddrLen.W)) |
| 20 | + val tgt = Input(UInt(ConstVal.AddrLen.W)) |
| 21 | + // BTB lookup interface |
| 22 | + val lookupBranch = Output(Bool()) |
| 23 | + val lookupJump = Output(Bool()) |
| 24 | + val lookupPc = Input(UInt(ConstVal.AddrLen.W)) |
| 25 | + val lookupTgt = Output(UInt(ConstVal.AddrLen.W)) |
| 26 | + }) |
| 27 | + |
| 28 | + // definitions of BTB lines and valid bits |
| 29 | + protected val valids = RegInit(VecInit(Seq.fill(ConstVal.BTBSize) { false.B })) |
| 30 | + protected val lines = Mem(ConstVal.BTBSize, new BTBLine) |
| 31 | + |
| 32 | + // branch info for BTB lines |
| 33 | + protected val idx = io.pc(ConstVal.BTBIdxLen + ConstVal.AddrAlignLen - 1, ConstVal.AddrAlignLen) |
| 34 | + protected val linePc = io.pc(ConstVal.AddrLen - 1, ConstVal.BTBIdxLen + ConstVal.AddrAlignLen) |
| 35 | + |
| 36 | + // write to BTB lines |
| 37 | + when(io.branch) { |
| 38 | + valids(idx) := true.B |
| 39 | + lines(idx).jump := io.jump |
| 40 | + lines(idx).pc := linePc |
| 41 | + lines(idx).tgt := io.tgt(ConstVal.AddrLen - 1, ConstVal.AddrAlignLen) |
| 42 | + } |
| 43 | + |
| 44 | + // signals about BTB lookup |
| 45 | + val lookupIdx = io.lookupPc(ConstVal.BTBIdxLen + ConstVal.AddrAlignLen - 1, ConstVal.AddrAlignLen) |
| 46 | + val lookupPcSel = io.lookupPc(ConstVal.AddrLen - 1, ConstVal.BTBIdxLen + ConstVal.AddrAlignLen) |
| 47 | + val btbHit = valids(lookupIdx) && lines(lookupIdx).pc === lookupPcSel |
| 48 | + |
| 49 | + // BTB lookup |
| 50 | + io.lookupBranch := btbHit |
| 51 | + io.lookupJump := Mux(btbHit, lines(lookupIdx).jump, false.B) |
| 52 | + io.lookupTgt := Cat(Mux(btbHit, lines(lookupIdx).tgt, 0.U), 0.U(ConstVal.AddrAlignLen.W)) |
| 53 | +} |
0 commit comments