Home › Practice › RTL Design › 2-bit branch predictor
2-bit branch predictor
EasyRTLsequentialarchitecture
A processor meets a branch (an if) long before it knows which way it will go. A guess lets it keep working instead of waiting.
Concept first. A branch predictor guesses whether a branch will be taken. The simplest one remembers only the last outcome. A loop branch that is taken nine times and then not taken once makes that predictor wrong twice per loop: at the exit, and again when the loop is entered next time. A 2-bit saturating counter fixes this by needing two wrong guesses in a row before it changes its mind. It has four states, 0 to 3. States 0 and 1 predict "not taken", states 2 and 3 predict "taken". "Saturating" means it stops at 0 and at 3 instead of wrapping around: a counter at 3 that sees another taken branch stays at 3. Trace a loop through the four states on paper to see why one odd outcome does not flip a strong prediction.
What to build.
- Ports:
clk, rst, update, taken; outputsstate[1:0]andpredict. rstis synchronous, active-high:statebecomes01(weakly not-taken).- On a rising edge with
update = 1:taken = 1moves the state up by one, stopping at 3;taken = 0moves it down by one, stopping at 0. - With
update = 0the state holds. predictis 1 when the state is 2 or 3. It follows the state directly (not registered again).
No install and no sign-up needed. You write your answer in the editor and Nishkam checks it.