Based on the execution steps used by instructions, we could divide
the datapath in to five stages.
Five-stage pipelined datapath shown in Fig. 6.10
in the text book:
1. IF : Instruction fetch
2. ID : Instruction decode and
register file read
3. EX : ALU execution
4. MEM: Data memory access
5. WB : Write back.
Since at most five instructions can be in the datapath at
the same time for five-satge datapath, we need to save information
needed by each instructions. For example, if we did not save one
instruction bits, the following instruction entering the datapath
will re-write the previous instruction. All the information for
the previous one will be lost.
Similar to that PC (program counter) passes the instruction
address from one clock cycle to the next clock cycle, we can
insert registers between two stages, shown in Fig.
6.12 in the text book.
IF/ID registers: PC address, instruction
ID/EX registers: PC address, Read Data 1, Read Data 2, sign-extended offset
EX/MEM registers: branch address, Zero signal, ALU result, Read Data 2
MEM/WB registers: Read Data from Data Memory, ALU result
read
save
PC --> Instruction Mem --> instruction -->
IF/ID register
4
save
PC--> incremented PC --> IF/ID register
Stage 2 (ID) :
read
save
|-> read register 1 --> Read data 1 ----------->|
|
read
save
|
|-> read register 2 --> Read data 2 ----------->|
IF/ID register ->|
|-->ID/EX
|-> write register
|
|
save |
|-> 16-bit offset field ->| sign-extended 32-bits -- >|
Stage 3 (EX) :
read
|---> Read data 2 -------->|
ID/EX register ->| read
|-->ALU -> address -> EX/MEM
|---> sign-extended 32-bits ->|
Stage 4 (MEM) :
read
read
save
EX/MEM --> address --> Data Mem --> data --> MEM/WB
Stage 5 (WB) :
read
write
MEM/WB --> dada ----> Register File
Example 2: sw instruction. See Figs. 6.16, and 6.17
Stage 1 (IF) : instruction is read from Instruction Memory using
the address in the PC, and then saved in the IF/ID pipeline register
read
save
PC --> Instruction Mem --> instruction -->
IF/ID register
4
save
PC--> incremented PC --> IF/ID register
Stage 2 (ID) :
read
save
|-> read register 1 --> Read data 1 ----------->|
|
read
save
|
|-> read register 2 --> Read data 2 ----------->|
IF/ID register ->|
|-->ID/EX
|-> write register
|
|
save |
|-> 16-bit offset field ->| sign-extended 32-bits -- >|
Stage 1 and Stage 2 are executed by all instructions since it is too
early
to know the type of instructions.
Stage 3 (EX) :
read
|---> Read data 1 ------->|
ID/EXE register ->| read
|->ALU -> address ->EX/MEM
|--> sign-extended 32-bits ->|
|
| --->Read data 2------------------------>EX/MEM
Stage 4 (MEM) :
read
EX/MEM -->|---> address
|
write
|---> Read data 2 ---> Data Mem
Stage 5 (WB) : Nothing happens.
An instruction passes through a stage even if there is nothing to do
because later
instructions are already in progress.
What is the correct Write Register needed in the Stage WB?
In Fig. 6.15, the Write Register needed
in the Stage WB is from
pipeline register IF/ID. However, the bits of Write Register
are from an instruction in the Stage ID. In another word,
lw instruction in the Stage WB uses Write register in another instruction
in the Stage ID. It leads to write the data to a wrong destination
register
in Register File. In order to have the correct the Write Register,
we need
to save the information of 5-bit destination register through ID/EX,
EX/MEM, and MEM/WB, see Fig. 6.18.
Fig. 6.20 shows the multiple-clock-cycle pipeline diagram of two instructions.
Figs 6.22, 6.23. 6.24 shows the single-clock-cycle pipeline diagrams of two instructions in the different clocks.