Sub topic 4
Timing diagrams, pipeline registers, and the clock period
Assume you know the CPU clock, registers, memory, and basic assembly instructions such as add and load. You should also be comfortable with simple arithmetic and speedup ratios.
What a pipeline timing diagram shows
A pipeline divides instruction execution into stages. A common five-stage design uses:
- IF: instruction fetch
- ID: instruction decode and register read
- EX: execute or calculate an address
- MEM: access data memory
- WB: write the result to a register
Instructions enter the pipeline on successive clock cycles. They overlap, but each instruction still passes through every required stage.
For three instructions, the timing can be shown as follows:
| Instruction | Cycle 1 | Cycle 2 | Cycle 3 | Cycle 4 | Cycle 5 | Cycle 6 | Cycle 7 |
|---|---|---|---|---|---|---|---|
| `I1` | IF | ID | EX | MEM | WB | ||
| `I2` | IF | ID | EX | MEM | WB | ||
| `I3` | IF | ID | EX | MEM | WB |
The first instruction completes after five cycles. After the pipeline fills, one instruction completes during each later cycle. This is why pipelining mainly improves throughput, the number of completed instructions per unit of time. It does not usually reduce the latency of one instruction.
Pipeline registers
A pipeline register is a small storage element between two stages. At the clock edge, it captures the output of one stage and holds it stable for the next stage.
For example, the register between ID and EX may store:
- The decoded operation
- Source operand values
- The destination register number
- Immediate values
- Control signals
Each instruction carries its own data and control signals through these registers. Without them, the output of one instruction could be overwritten while a later instruction is being processed.
A pipeline register adds delay. Let the largest stage logic delay be Dmax and the register overhead be L. The clock period must satisfy:
Tclock >= Dmax + L
The slowest stage determines the clock period. Making other stages faster does not help unless the slowest stage is also improved.
Counting cycles
For an ideal k-stage pipeline processing n instructions:
Total cycles = k + n - 1
The first instruction needs k cycles to travel through the stages. The remaining n - 1 instructions need one additional cycle each.
def pipelined_time(stage_delays, latch_delay, n):
clock_period = max(stage_delays) + latch_delay
stages = len(stage_delays)
cycles = stages + n - 1
return cycles * clock_period
delays = [60, 80, 70, 90, 50]
print(pipelined_time(delays, 10, 100)) # 10400Here, the slowest stage takes 90 ns and the pipeline register takes 10 ns, so the clock period is 100 ns. Five stages and 100 instructions require 5 + 100 - 1 = 104 cycles.
Exercise
A four-stage pipeline processes 20 instructions. Ignoring stalls, how many clock cycles are required?
Show answer
The total is k + n - 1 = 4 + 20 - 1 = 23 clock cycles.