RTL Design #51 · 2026-11-13 · Branch prediction · Medium · about 25 min

A bimodal branch predictor

Build a bimodal branch predictor: a table of 2^IDX_BITS two-bit saturating counters, indexed by the branch address.

Requirements

  1. Index. Instructions are 4 bytes, so the index is pc[IDX_BITS+1:2]. The low two bits and all higher bits are ignored; branches whose index bits match share a counter.
  2. Counters. Each counter holds 0 to 3. The prediction is the counter's top bit: taken when it is 2 or 3. A counter saturates: it never goes above 3 or below 0.
  3. Predict. pred_taken is combinational from pc. It shows the counter's value as it stands before any update in the current cycle: there is no bypass from the update port.
  4. Update. On a rising edge with upd_valid = 1, the counter selected by upd_pc moves up by one if upd_taken = 1, otherwise down by one. With upd_valid = 0, nothing changes, whatever upd_taken says.
  5. Reset. Synchronous, active low. Every counter becomes 1 (weakly not taken). Reset wins over an update in the same cycle.

Timing example (IDX_BITS = 3; pcs 0x100 and 0x104 use entries 0 and 1):

cycle pc upd_valid upd_pc upd_taken pred_taken counter 0 afterwards
1 0x100 1 0x100 1 0 2
2 0x100 0 - - 1 2
3 0x104 0 - - 0 2
4 0x100 1 0x100 0 1 1
5 0x100 0 - - 0 1

Write the design in bimodal.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.