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
- 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. - 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.
- Predict.
pred_takenis combinational frompc. It shows the counter's value as it stands before any update in the current cycle: there is no bypass from the update port. - Update. On a rising edge with
upd_valid= 1, the counter selected byupd_pcmoves up by one ifupd_taken= 1, otherwise down by one. Withupd_valid= 0, nothing changes, whateverupd_takensays. - 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.