Sub topic 4
Timing diagrams, pipeline registers, and the clock period
This chapter assumes you know the five stages (IF, ID, EX, MEM, WB) from the previous chapter. It also assumes basic ratio arithmetic and that you know a clock cycle is the fixed time between two clock ticks. The chapter covers three connected things: how to draw a pipeline timing diagram, what the registers between stages do, and how the slowest stage and the register delay set the clock period.
The timing diagram
A pipeline timing diagram has one row per instruction and one column per clock cycle. Each cell holds the stage that instruction occupies in that cycle. Instruction i+1 starts one cycle after instruction i, so each row is the row above shifted right by one column.
| Instr | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| I1 | IF | ID | EX | MEM | WB | |||
| I2 | IF | ID | EX | MEM | WB | |||
| I3 | IF | ID | EX | MEM | WB | |||
| I4 | IF | ID | EX | MEM | WB |
Three facts follow from this table:
- The first instruction needs k cycles, where k is the number of stages. Here k = 5.
- After that, one instruction finishes per cycle. I1 finishes in cycle 5, I2 in cycle 6, and so on.
- The last of n instructions finishes in cycle k + n - 1. Here that is 5 + 4 - 1 = 8.
The first k - 1 cycles are the fill time, when later stages are idle. The last k - 1 cycles are the drain time, when early stages are idle. Only the cycles in between keep every stage busy. This is why the speedup of a pipeline depends on n.
You can generate such a diagram with a short script:
def timing_diagram(n, stages=("IF", "ID", "EX", "MEM", "WB")):
k = len(stages)
cycles = k + n - 1
print(" " + "".join(f"{c:>5}" for c in range(1, cycles + 1)))
for i in range(n):
row = [""] * cycles
for s, name in enumerate(stages):
row[i + s] = name # instruction i is in stage s during cycle i + s + 1
print(f"I{i + 1:<4}" + "".join(f"{cell:>5}" for cell in row))
timing_diagram(4)Pipeline registers
The stages work on different instructions at the same time. Each stage therefore needs its own private copy of whatever its instruction produced so far. A pipeline register (also called a latch) sits between each pair of adjacent stages and holds that data for one cycle. The classic names are IF/ID, ID/EX, EX/MEM, and MEM/WB.
What each register holds:
- IF/ID holds the fetched instruction and its program counter value.
- ID/EX holds the two register operands, the sign-extended immediate, and control signals for the later stages.
- EX/MEM holds the ALU result, the value to store (for a store instruction), and the destination register number.
- MEM/WB holds the value loaded from memory or the ALU result, plus the destination register number.
All registers capture their inputs on the same clock edge. Between two edges, each stage computes from the values its input register captured at the previous edge. Without these registers, a fast stage would overwrite data that a slower stage still needed, and the instructions would mix.
The registers also cost time. Each one has a latch delay: the time to set up the input before the edge and to get the stored value out after it. A stage's result cannot be used by the next stage until this delay has passed.
The clock period
All stages share one clock, so the clock period must be long enough for the slowest stage to finish and for its result to be latched:
T = max(stage delays) + latch delay
Faster stages sit idle for part of every cycle. The slowest stage is the bottleneck.
The time for n instructions in a k-stage pipeline with no stalls is:
time = (k + n - 1) x T
A non-pipelined processor does one instruction at a time with no pipeline registers, so its time is n x (sum of stage delays). The functions below compute both, using stage delays of 60, 80, 70, 90 and 50 ns and a latch delay of 10 ns:
def pipelined_time(stage_delays, latch_delay, n, stall_cycles=0):
# The clock period is set by the slowest stage plus the pipeline-register delay.
clock = max(stage_delays) + latch_delay
k = len(stage_delays)
return (k + n - 1 + stall_cycles) * clock
def non_pipelined_time(stage_delays, n):
return n * sum(stage_delays)
delays = [60, 80, 70, 90, 50] # ns per stage
n = 100
tp = pipelined_time(delays, 10, n) # (5 + 99) * 100 = 10400 ns
tn = non_pipelined_time(delays, n) # 100 * 350 = 35000 ns
print(tp, tn, round(tn / tp, 2)) # 10400 35000 3.37Here T = 90 + 10 = 100 ns. The first instruction finishes after 5 x 100 = 500 ns, which is longer than the 350 ns it would take unpipelined. Pipelining does not shorten the latency of one instruction. It raises throughput, the number of instructions finished per unit time. After the pipeline fills, one instruction completes every 100 ns.
As n grows, the ratio approaches 350 / 100 = 3.5. The speedup stays below the ideal value of 5 because the stages are unequal and the latch delay is added to every cycle.
Reading the numbers: balance and the cost of extra stages
The slowest stage controls the period, so improving any other stage changes nothing. Suppose the 90 ns stage is split into two 45 ns stages. The new delays are [60, 80, 70, 45, 45, 50], and k becomes 6. The slowest stage is now 80 ns, so T = 90 ns. For n = 100:
time = (6 + 99) x 90 = 9450 ns
This is better than 10400 ns. Splitting further gains little, because the 80 ns stage now limits the clock. Each added stage also adds a latch delay to the period and increases the cost of fill, drain, and later hazard penalties. Deeper pipelines give diminishing returns for this reason.
Edge cases and common errors
- Stall cycles. Each stall or bubble adds one full cycle of length T. The total becomes (k + n - 1 + stalls) x T, which is what the
stall_cyclesargument in the code above does. - Latch delay not given. Some GATE questions give no latch delay. Treat it as 0 in that case, and do not invent one.
- Memory stages that take multiple cycles. If a stage is slower than the others by design, the question usually either states a larger cycle count for it or asks you to set T by that stage.
- Counting from zero. For one instruction, the formula gives k + 1 - 1 = k cycles. If your answer for n = 1 is not k, you have an off-by-one error.
- Time versus cycles. Cycle counts do not depend on stage delays. Time does. State which one the question asks for before you multiply by T.
- Unequal pipelines. If an instruction type skips a stage (for example, an ALU instruction does no MEM work), the standard model still passes it through every stage so that results complete in order.
Method for timing questions
- List the stage delays and find the maximum.
- Add the latch delay to get T.
- Count cycles as k + n - 1 plus any stalls.
- Multiply cycles by T.
- For speedup, divide the non-pipelined time, n x (sum of delays), by this result.
Exercise
A 4-stage pipeline has stage delays of 100 ns, 150 ns, 120 ns and 130 ns, and a latch delay of 20 ns. (a) What is the clock period? (b) How long does it take to run 50 instructions with no stalls? (c) What is the speedup over a non-pipelined processor for those 50 instructions?
Show answer
(a) T = max(100, 150, 120, 130) + 20 = 170 ns. (b) Cycles = 4 + 50 - 1 = 53, so time = 53 x 170 = 9010 ns. (c) The non-pipelined time is 50 x (100 + 150 + 120 + 130) = 50 x 500 = 25000 ns. Speedup = 25000 / 9010, which is about 2.77.
Exercise
In the 5-stage example with delays 60, 80, 70, 90 and 50 ns and a 10 ns latch, the 90 ns stage is split into two 45 ns stages with no other change. Why does the speedup for large n not rise to 350 / 80 = 4.375, and what does it approach instead?
Show answer
The period is the slowest stage plus the latch delay, so it becomes 80 + 10 = 90 ns, not 80 ns. The sum of the stage delays is still 350 ns. For large n the speedup therefore approaches 350 / 90, which is about 3.89.