Skip to content

Commit 8c6608e

Browse files
committed
feat: add branch predictor modules
1 parent f43e4ee commit 8c6608e

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package treecorel2
2+
3+
import chisel3._
4+
import chisel3.util._
5+
6+
import treecorel2.common.ConstVal
7+
8+
class BPU extends Module {
9+
// 2BP 2BC
10+
// Two-level adaptive predictor
11+
val io = IO(new Bundle {
12+
val branchInfo = Flipped(new BRANCHIO)
13+
// predictor interface
14+
val lookupPc = Input(UInt(ConstVal.AddrLen.W))
15+
val predTaken = Output(Bool())
16+
val predTgt = Output(UInt(ConstVal.AddrLen.W))
17+
val predIdx = Output(UInt(ConstVal.GHRLen.W))
18+
})
19+
20+
protected val ghr = Module(new GHR)
21+
protected val pht = Module(new PHT)
22+
protected val btb = Module(new BTB)
23+
24+
ghr.io.branch := io.branchInfo.branch
25+
ghr.io.taken := io.branchInfo.taken
26+
27+
// G-share
28+
protected val idx = io.lookupPc(ConstVal.GHRLen + ConstVal.AddrAlignLen - 1, ConstVal.AddrAlignLen) ^ ghr.io.idx
29+
pht.io.prevBranch := io.branchInfo.branch
30+
pht.io.prevTaken := io.branchInfo.taken
31+
pht.io.prevIdx := io.branchInfo.idx
32+
pht.io.idx := idx
33+
34+
// wire BTB
35+
btb.io.branch := io.branchInfo.branch
36+
btb.io.jump := io.branchInfo.jump
37+
btb.io.pc := io.branchInfo.pc
38+
btb.io.tgt := io.branchInfo.tgt
39+
btb.io.lookupPc := io.lookupPc
40+
41+
// wire output signals
42+
io.predTaken := btb.io.lookupBranch && (pht.io.taken || btb.io.lookupJump)
43+
io.predTgt := btb.io.lookupTgt
44+
io.predIdx := idx
45+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package treecorel2
2+
3+
import chisel3._
4+
import chisel3.util._
5+
6+
import treecorel2.common.ConstVal
7+
8+
class GHR extends Module {
9+
val io = IO(new Bundle {
10+
val branch = Input(Bool())
11+
val taken = Input(Bool())
12+
val idx = Output(UInt(ConstVal.GHRLen.W))
13+
})
14+
15+
protected val ghr = Reg(UInt(ConstVal.GHRLen.W))
16+
17+
when(io.branch) {
18+
ghr := Cat(ghr(ConstVal.GHRLen - 2, 0), io.taken)
19+
}
20+
21+
io.idx := ghr
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package treecorel2
2+
3+
import chisel3._
4+
import chisel3.util._
5+
6+
import treecorel2.common.ConstVal
7+
8+
class PHT extends Module {
9+
val io = IO(new Bundle {
10+
// branch info (from idu)
11+
val prevBranch = Input(Bool())
12+
val prevTaken = Input(Bool())
13+
val prevIdx = Input(UInt(ConstVal.GHRLen.W))
14+
// index for looking up counter table
15+
val idx = Input(UInt(ConstVal.GHRLen.W))
16+
// prediction result
17+
val taken = Output(Bool())
18+
})
19+
20+
protected val init = Seq.fill(ConstVal.PHTSize) { "b10".U(2.W) }
21+
protected val counters = RegInit(VecInit(init))
22+
23+
// update counter
24+
when(io.prevBranch) {
25+
when(counters(io.prevIdx) === "b11".U) {
26+
when(!io.prevTaken) {
27+
counters(io.prevIdx) := counters(io.prevIdx) - 1.U
28+
}
29+
}.elsewhen(counters(io.prevIdx) === "b00".U) {
30+
when(io.prevTaken) {
31+
counters(io.prevIdx) := counters(io.prevIdx) + 1.U
32+
}
33+
}.otherwise {
34+
when(!io.prevTaken) {
35+
counters(io.prevIdx) := counters(io.prevIdx) - 1.U
36+
}.otherwise {
37+
counters(io.prevIdx) := counters(io.prevIdx) + 1.U
38+
}
39+
}
40+
}
41+
42+
io.taken := counters(io.idx)(1)
43+
}

0 commit comments

Comments
 (0)