RTL Design #60 · 2026-11-22 · Branch prediction with history · Medium · about 30 min
A gshare branch predictor
Build a gshare branch predictor: a table of 2^H two-bit saturating counters indexed by the branch address XOR a global history of recent outcomes.
Requirements
- History.
ghris anH-bit shift register of the most recent resolved outcomes. On an update it shifts left by one and the new outcome enters bit 0 (ghr <= {ghr[H-2:0], upd_taken}). It changes only whenupd_valid= 1. - Index. Instructions are 4 bytes, so a pc contributes
pc[H+1:2]. The index is that slice XOR the currentghr. Both the prediction and the update use theghras it stands before the edge. - Counters. Each counter holds 0 to 3 and saturates. The prediction is the counter's top bit: taken when it is 2 or 3.
- Predict.
pred_takenis combinational frompcandghr, and shows the counter before any update in the current cycle (no bypass). - Update. On a rising edge with
upd_valid= 1, the counter selected byupd_pcmoves up ifupd_taken= 1, otherwise down. Withupd_valid= 0 nothing changes. - Reset. Synchronous, active low. Every counter becomes 1 (weakly not taken) and
ghrbecomes 0. Reset wins over an update.
Timing example (H = 4, just after reset):
| cycle | pc | upd_valid | upd_pc | upd_taken | pred_taken |
ghr afterwards |
|---|---|---|---|---|---|---|
| 1 | 0x10 |
1 | 0x10 |
1 | 0 | 0001 |
| 2 | 0x10 |
0 | - | - | 0 | 0001 |
| 3 | 0x14 |
0 | - | - | 1 | 0001 |
| 4 | 0x10 |
1 | 0x10 |
0 | 0 | 0010 |
In cycle 1 the update trained entry 4 up to 2. In cycle 3, 0x14 has slice 5, and 5 XOR 0001 is 4, so it reads that trained entry.
Write the design in gshare.sv. Run uses testbench.sv, a short scenario. Submit runs hidden tests.
Write and run your answer in the browser. The hidden tests check it, and it’s free.