5.2.8 · D1Processor Datapath & Pipelining

Foundations — Control hazards and pipeline flushes

1,808 words8 min readBack to topic

This page assumes you have seen the parent note Control hazards and pipeline flushes use words like stage, PC, bubble, branch, EX, saturating counter. Below we build every one of those symbols from nothing, in an order where each idea rests only on the ones before it.


1. What is an "instruction" and where does it live?

Picture a tall locker wall. Each locker has a number painted on it, and inside is one command. In the parent note you saw labels like 100:, 104:, 108:. Those numbers are addresses.

Figure — Control hazards and pipeline flushes

Why the numbers jump by 4. Each instruction is 4 bytes wide (a common RISC choice), and memory is counted in bytes. So the locker after address 100 is 104, not 101. Whenever the parent writes , it means "the very next instruction, right below this one".


2. The Program Counter (PC)

  • Normal step: (slide one locker down).
  • Branch taken: (jump the finger somewhere else).

The arrow just means "gets replaced by". We need the PC because the machine must always have an address ready before it can grab the next command — and that "before" is the root of every control hazard.


3. Stages: chopping one instruction into 5 jobs

Figure — Control hazards and pipeline flushes

Why chop it up? One instruction takes 5 short steps instead of one long step. While instruction A is in EX, instruction B can be in ID, and instruction C in IF — three instructions in flight at once. This overlap is exactly why we start fetching before we know a branch's answer.

Why EX is the villain here. In this classic layout the branch's yes/no answer isn't known until EX — the third stage. By then two younger instructions have already crawled into IF and ID.

Recall Why is overlapping stages what

causes control hazards? Because IF for the next instruction happens two ticks before EX resolves the branch — so we must fetch on a guess. ::: Overlap forces us to fetch before the branch outcome is known.


4. A "clock cycle" and reading the pipeline table

That is why the parent draws a table with cycles as columns and stages as rows (IF/ID/EX/MEM/WB). Reading it: each instruction marches diagonally down-right, one stage per tick.

Figure — Control hazards and pipeline flushes

Trace the amber diagonal: BEQ is in IF at cycle 1, ID at cycle 2, EX at cycle 3 — that's the cycle it finally knows the answer. Meanwhile ADD and SUB entered behind it. If the branch is taken, those two are on the wrong path.


5. Register, register write, and "architectural state"

The collection of all registers + all memory is the architectural state — the official visible result of the program. The golden rule of flushing: a wrong-path instruction must be prevented from touching architectural state.


6. The Bubble (NOP / pipeline flush)

Picture a clear gap sliding down the assembly line where a real product should be. The line stays in step (never desynchronised) but nothing is built at that slot.


7. The branch instruction itself

See Branch instructions for the instruction format itself. Two outcomes give us two words:

The tragedy: we don't know which until EX, but IF already ran on a guess.


8. Counting the damage: the branch penalty

Why subtract? Every stage the branch travels past IF is one extra younger instruction that has been let in behind it. Resolve one stage earlier (in ID) and you flush only .

This number is the whole reason branch prediction exists.


9. Prediction and the saturating counter

We read the two-bit value as: Strongly-Taken, Weakly-Taken, Weakly-Not-Taken, Strongly-Not-Taken. "Saturating" means it can't count below or above — it sticks at the ends. This one idea is deepened in Branch prediction techniques.


How these foundations feed the topic

Instruction and address

Program Counter PC

Pipeline stages IF ID EX MEM WB

Clock cycle and stage table

Overlap fetches before branch resolves

Register and RegWrite

Bubble and flush

Control hazard

Conditional branch taken or not

Branch penalty count

Prediction with saturating counter

This foundation feeds directly into pipelining fundamentals, sits beside data hazards, and points forward to speculative execution. A Hinglish version lives at the Hinglish note.


Equipment checklist

Test yourself — cover the right side.

Why does the next instruction's address usually jump by 4, not 1?
Instructions are 4 bytes wide and memory is counted in bytes.
What does the Program Counter hold?
The address of the instruction about to be fetched.
Name the 5 stages in order.
IF, ID, EX, MEM, WB.
In the classic pipeline, which stage resolves a branch?
EX (the 3rd stage).
Why does overlapping stages cause control hazards?
IF for the next instruction happens before EX resolves the branch, so we fetch on a guess.
What is architectural state?
The visible result of the program — all registers plus memory.
How do you turn a wrong-path instruction harmless without deleting it?
Force its RegWrite and MemWrite control bits to 0, making it a bubble.
Does flushing erase the instruction from memory?
No — only the in-flight pipeline copy is zeroed; the memory copy is untouched.
What does "taken" mean for a branch?
The condition was true, so the PC jumped to the target instead of PC+4.
Give the branch-penalty formula.
Penalty = (stage where branch resolves) − (stage where fetch occurs).
Why use a 2-bit saturating counter instead of 1-bit?
It needs two consecutive wrong guesses to flip, so a single loop-exit costs only one misprediction instead of two.