Sub topic 1
Recap: the instruction cycle
Prerequisites
This chapter assumes you know the basic parts of a CPU: registers, memory, and the clock. You should also read simple assembly instructions such as load, store, add, and branch, and be comfortable with ratio and speedup arithmetic.
The instruction cycle
A processor runs a program by repeating the instruction cycle for each instruction. The cycle has three steps: fetch the instruction from memory, decode it to find what it means, and execute it. In a simple processor, one instruction finishes all three steps before the next one starts.
Why pipeline? Overlapping instructions
Each step uses a different part of the chip. In the simple design, most parts sit idle most of the time. A pipeline splits the work into stages, so several instructions occupy different stages at once. Like an assembly line, it completes about one instruction per clock cycle once it is full.
The five-stage pipeline: IF, ID, EX, MEM, WB
The classic pipeline has five stages:
- IF (instruction fetch): reads the instruction at the program counter.
- ID (instruction decode): decodes the instruction and reads its source registers.
- EX (execute): performs the ALU operation or computes an address.
- MEM (memory access): loads from or stores to data memory.
- WB (write back): writes the result to the destination register.
Timing diagrams, pipeline registers, and the clock period
Pipeline registers (latches) sit between stages and hold each instruction's data for the next stage. All stages share one clock. The clock period must cover the slowest stage plus the latch delay. A timing diagram shows, cycle by cycle, which instruction occupies which stage. With k stages and n instructions, the pipeline takes k + n − 1 cycles: k cycles to push the first instruction through, then one cycle for each remaining instruction.
Speedup, throughput, and efficiency
Assume five stages of 1 ns each and n = 100 instructions.
- Non-pipelined time: 100 × 5 × 1 ns = 500 ns.
- Pipelined time: (5 + 100 − 1) × 1 ns = 104 ns.
- Speedup: 500 / 104 ≈ 4.81.
- Throughput: 100 / 104 ns ≈ 0.96 instructions per ns.
- Efficiency: speedup / k = 4.81 / 5 ≈ 0.96, or 96%.
Speedup approaches k as n grows.
What a hazard is and why it forces a stall
A hazard is a situation where the next instruction cannot proceed in its pipeline stage yet. There are three kinds:
- Structural: two instructions need the same hardware in the same cycle.
- Data: an instruction needs a value that an earlier instruction has not produced yet.
- Control: the next fetch address depends on a branch that has not been resolved.
Consider this pair:
add r1, r2, r3 # writes r1 in WB
sub r4, r1, r5 # reads r1 in ID: data hazardThe sub needs r1 in its ID stage, but add does not write r1 until its WB stage, three cycles later. Without help, the pipeline inserts an empty slot called a bubble, which delays sub. Each bubble adds a cycle, so the total cycle count rises and speedup falls.
> Note: the formulas above assume no hazards. Real speedup is lower once stalls are counted.
Exercise
Exercise
A 4-stage pipeline has stages of 2 ns each and runs 50 instructions. How many cycles does it take, and what is the speedup over a non-pipelined design?
Show answer
Cycles = 4 + 50 − 1 = 53. Pipelined time = 53 × 2 ns = 106 ns. Non-pipelined time = 50 × 4 × 2 ns = 400 ns. Speedup = 400 / 106 ≈ 3.77.