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

  1. History. ghr is an H-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 when upd_valid = 1.
  2. Index. Instructions are 4 bytes, so a pc contributes pc[H+1:2]. The index is that slice XOR the current ghr. Both the prediction and the update use the ghr as it stands before the edge.
  3. Counters. Each counter holds 0 to 3 and saturates. The prediction is the counter's top bit: taken when it is 2 or 3.
  4. Predict. pred_taken is combinational from pc and ghr, and shows the counter before any update in the current cycle (no bypass).
  5. Update. On a rising edge with upd_valid = 1, the counter selected by upd_pc moves up if upd_taken = 1, otherwise down. With upd_valid = 0 nothing changes.
  6. Reset. Synchronous, active low. Every counter becomes 1 (weakly not taken) and ghr becomes 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.