5.2.3 · D4Processor Datapath & Pipelining

Exercises — Classic 5-stage pipeline (IF - ID - EX - MEM - WB)

3,612 words16 min readBack to topic

This page is a graded workout for the 5-stage pipeline. Every problem has a fully worked solution you can hide and re-derive. Levels climb from "can you name it?" to "can you build the reasoning yourself?". Do a level, then read its trap.

Figure s01 (below) is the mental picture behind every timing problem: rows are instructions, columns are cycles. The diagonal of coloured cells is the pipeline "filling." Count columns touched = total cycles.

Figure — Classic 5-stage pipeline (IF - ID - EX - MEM - WB)

Level 1 — Recognition

Recall Solution L1.1
  1. IF — Instruction Fetch: read the instruction from instruction memory at the address in the PC; then PC ← PC + 4.
  2. ID — Instruction Decode / register read: decode the opcode, read source registers from the register file, sign-extend the immediate.
  3. EX — Execute: the ALU does arithmetic/logic, or computes a memory address, or checks a branch condition.
  4. MEM — Memory access: loads read data memory, stores write it; everyone else idles here.
  5. WB — Write Back: write the result into the register file.
Recall Solution L1.2

Registers are read in ID and results are written in WB. Mnemonic: Decode reads, Write writes.

Recall Solution L1.3

The four are IF/ID, ID/EX, EX/MEM, MEM/WB. Each carries data (operand values, the immediate, the ALU result, the loaded value) and control signals (RegWrite, MemRead, MemWrite, …). Control decided in ID must survive all the way to WB, four cycles later — the registers ferry it there.


Level 2 — Application

Recall Solution L2.1

Step (what): apply . Why: cycles to fill the pipe (first result at cycle 5), then more results, one each subsequent cycle. The first instruction finishes at cycle (it must pass all five stages). Look at Figure s01: the last coloured cell of the top row sits in column 5.

Recall Solution L2.2

Step (what): take the max delay. Why: all stages share one clock; the cycle must be long enough for the slowest stage to finish, or that stage's result would not be ready to latch. Step: add the latch overhead, because every cycle also spends writing into the pipeline register.

Recall Solution L2.3

Step: cycles . Step: time ps ns. Why multiply cycles by ? Each clock cycle lasts exactly , and the cycles happen one after another; so total wall-clock time is simply "how many cycles" times "how long each cycle is."


Level 3 — Analysis

Recall Solution L3.1

Single-cycle time per instruction: one giant cycle must fit all combinational work plus one latch: Pipelined time per instruction (large ): one instruction finishes every ps. Speedup: Why this ratio and not ? Because the two machines have different cycle times here (915 vs. 265), so we compare wall-clock time per instruction directly, exactly as the intro formula box warned. Why not 5×? The stages are unbalanced: WB (100 ps) and ID (120 ps) waste much of their 265-ps slot, while IF/MEM (250 ps) set the pace. Ideal 5× requires every stage to be equal and . See Clocking & Latch Overhead for why can never be zero.

Recall Solution L3.2
  • lw $t0, 8($s1) : IF · ID(read s1+8) · MEM(read) · WB(write $t0). Uses all five.
  • add $t2,$t3,$t4 : IF · ID(read t4) · EX(add) · MEM(idle) · WB(write $t2).
  • sw $t5, 0($s2) : IF · ID(read t5) · EX(addr = $s2+0) · MEM(write) · no WB. Why keep idle slots? So every instruction has the identical 5-stage shape. Uniform shape ⇒ simple control ⇒ one instruction still finishes every cycle. Skipping stages would break the assembly line's steady rhythm.
Recall Solution L3.3
  • 5-stage: ps.
  • 10-stage: ps. Step (what): form the throughput ratio . Why is that the throughput ratio? For a long stream, one instruction finishes every , so throughput = 1 / (instructions per second). Comparing two throughputs is therefore — throughput scales as the inverse of the cycle time, so a smaller is proportionally faster. The result is less than the naive 2× you'd hope for by doubling stages. Why? The latch overhead did not shrink; it is now of the cycle instead of . As stages get thinner, eats a bigger fraction. Push far enough and speedup saturates or reverses.

Level 4 — Synthesis

Recall Solution L4.1

Both IF and MEM are 250 ps. Splitting either drops its halves to 125 ps, but the other 250-ps stage still remains and re-sets the max. So one split alone does not lower below 250 — the new bottleneck is still the un-split 250-ps stage. Insight: you must split both 250-ps stages to see any gain. Splitting only one wastes an extra latch for nothing. This is the core lesson: the second-slowest stage caps how much helping the slowest can achieve. Balance is global, not local.

Recall Solution L4.2

Set pipelined time < single-cycle time: So from onward the pipeline already wins. Why so early? The single-cycle machine's per-instruction cost (915) is enormous next to 265; the fill penalty of only 4 extra cycles is paid off almost immediately.


Level 5 — Mastery

Recall Solution L5.1

(a) ; ps. (b) Cycles . (c) Time ps ns. (d) Total work ps; single-cycle ps/instr. Large- pipelined ps/instr. Speedup . (e) Balanced ideal: each stage ps, so ps, giving — still not 5×. The theoretical 5× needs too, but latch overhead can never be zero (real flip-flops have setup + propagation time — see Clocking & Latch Overhead). Hazards would erode it further.

Recall Solution L5.2

Recall a stall cycle = an inserted empty cycle that delays an instruction until its needed value is reachable. Lay both instructions on a timeline; rows are instructions, columns are clock cycles (c1…c7), each cell is the stage that instruction occupies that cycle.

**Step 1 — find when t0 in its WB, which is cycle 5:

instruction c1 c2 c3 c4 c5
add IF ID EX MEM WB

**Step 2 — find when sub needs 6 - 3 = 3$ cycles → 3 stall cycles. The corrected timeline:

instruction c1 c2 c3 c4 c5 c6 c7 c8 c9
add IF ID EX MEM WB
sub IF ID EX MEM WB

(the three "–" cells are the inserted bubbles). Step 5 — total cycles. Without stalls two instructions would take cycles; the 3 bubbles push sub back by 3, giving cycles total (last cell WB in c9). Why forwarding fixes it: the ALU result exists right after add's EX (c3) — feed it straight into sub's EX and all 3 stalls vanish, back to 6 cycles. This is the entire motivation for Data Forwarding / Bypassing.

Recall Solution L5.3

Goal: show . Step 1 — clear the fraction. Since , multiply both sides by it without flipping the inequality: Step 2 — expand the right side. Distribute : Step 3 — cancel the common term. Both sides carry (left is ), so subtract it: Step 4 — factor and interpret. . For any real pipeline , both and are positive, so is always true — but notice this step used only , independent of . Wait: we cancelled assuming the strict form; the strictness in fact requires so that the pipeline is genuinely overlapping (at both sides equal ). Hence for every finite the strict inequality holds. Step 5 — the limit. Equality would need , impossible for ; but as the "" in becomes negligible and the ratio approaches . Meaning: the fill/drain overhead of cycles can be amortized toward nothing but never actually removed for a finite stream.


Active recall

What is in terms of stage delays and latch overhead?
— the slowest stage plus the latch cost every cycle pays.
Cycles to run instructions through a -stage pipeline, no stalls?
— fill with , then one result per cycle.
Why does splitting only the single slowest stage often not help?
The second-slowest stage becomes the new bottleneck; may not drop, so is unchanged.
Why can real speedup never reach exactly ?
Latch overhead , unbalanced stages waste slots, and hazards add stalls; for all finite .
What assumption lets us write speedup as ?
Both machines share the same ; when cycle times differ (single-cycle vs. pipelined), compare wall-clock times directly instead.
What is a stall cycle?
An inserted empty (bubble) cycle where a stage does no useful work because it is waiting for a not-yet-reachable value or resource; it adds one to the cycle count.