r00r10r20r30PREDICT01IFIDEXMEMWBRAM · 256 Bcycle1234567895
White cards: instructions, one stage per clock cycleRound pucks: bubbles, empty slots that do nothingLit wire with moving dashes: a value is flowingChart: one row per instruction, one column per cycleInstructions of the course CPU TINY-8 move one stage per clock cycle through fetch, decode, execute, memory and write back. A data hazard (OUT r0 needs the 5 that ADD r0, r1 has not written back yet) is fixed by stalling with bubbles or by forwarding the result. A control hazard (JNZ decided in execute) is fixed by flushing or by predicting; a structural hazard (one memory) by separate memories. TINY-8 numbers come from the course simulator: ADD r0, r1 with r0 = 2 and r1 = 3 gives 5; JNZ 12 sits at address 14 and runs 3 times, jumping back twice; memory past the program holds 0, which decodes as HALT.

The pipeline

  1. A pipeline splits each instruction into five stages: fetch, decode, execute, memory and write back. At every clock tick each instruction moves one stage on, so up to five overlap. These four LDIs never need each other's results.

Data hazards

Control hazards

Structural hazards

The pipeline, cycle by cycle

;; step 1/10 · five-stages
;; about: Fetch, Decode, Execute, Memory, Write back
stage  does
IF     read the instruction at PC
ID     decode it, read its registers
EX     ALU work; JNZ checks the flag
MEM    LD reads / ST writes memory
WB     write the result register

cycle      1  2  3  4
LDI r0, 2  IF ID EX MEM
LDI r1, 3     IF ID EX
LDI r2, 3        IF ID
LDI r3, 1           IF

The register file is written in the first half of a clock cycle and read in the second half, so without forwarding an instruction that needs the result of the one just before it waits two cycles (Patterson and Hennessy, section 4.8). In this lesson's pipeline JNZ checks the zero flag in execute, so a taken jump flushes the two instructions fetched behind it. Real pipelines often decide branches earlier and predict them; backward-taken, forward-not-taken is one simple static rule (Hennessy and Patterson, Computer Architecture: A Quantitative Approach, appendix C).