A Pipelined Datapath: Resisters Are Used To Save Data Between Stages
A Pipelined Datapath: Resisters Are Used To Save Data Between Stages
IF/ID Add 4
ID/EX
EX/MEM
MEM/WB
PC
Instruction
Read register 1
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
1 M u x 0
16
Sign extend
32
Computer
1/14
Corrected Datapath
The Write Register in the IF/ID register is from the wrong instruction. The Write Register is passed through the pipeline and forwarded to the Register File (RF) from the MEM/WB register.
0 M u x 1 IF/ID ID/EX EX/MEM MEM/WB Add 4 Add Add result Shift left 2 PC Address Instruction memory
Instruction
Read register 1
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
1 M u x 0
16
Sign extend
32
Computer
2/14
sub $11,$2,$3
sub $11, $2, $3 Instruction fetch
0 M u x 1
lw $10,20($1)
lw $10, 20($1) Instruction decode
IF/ID
ID/EX
EX/MEM
MEM/WB
PC
Instruction
Read register 1
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
1 M u x 0
16
Sign extend
32
Clock 2
Computer
3/14
16
Sign extend
32
sub $11,$2,$3
0 M u x 1
lw $10,20($1)
lw $10, 20($1) Memory
IF/ID
ID/EX
EX/MEM
MEM/WB
PC
Instruction
Read register 1
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
1 M u x 0
16
Sign extend
32
Clock 4
Computer
4/14
IF/ID
ID/EX
EX/MEM
MEM/WB
PC
Instruction
Read register 1
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
1 M u x 0
16
Sign extend
32
Clock 5
Computer
0
Pipelined Control
Problem: Control lines are defined in the IF stage. Example: In the WB stage MemtoReg and RegWrite are opcode dependent. Solution: Pass the control lines through the pipeline.
WB Instruction Control M EX WB M WB
IF/ID
ID/EX
EX/MEM
MEM/WB
Computer
6/14
IF/ID Add 4
RegWrite
ALUSrc
PC
Read data 1 Read register 2 Registers Read Write data 2 register Write data
0 M u x 1
Read data
Instruction 16 [15 0]
Sign extend
32
ALU control
MemRead
0 M u x 1 RegDst
ALUOp
Computer
MemtoReg
1 M u x 0
Instruction
Read register 1
MemWrite
Shift left 2
Branch
Data Dependencies
Backward lines cause data hazards:
Time (in clock cycles) CC 1 Value of register $2: 10 Program execution order (in instructions) CC 2 10 CC 3 10 CC 4 10 CC 5 10/ 20 CC 6 20 CC 7 20 CC 8 20 CC 9 20
IM
Reg
DM
Reg
IM
Reg
DM
Reg
or $13, $6, $2
IM
Reg
DM
Reg
IM
Reg
DM
Reg
sw $15, 100($2)
IM
Reg
DM
Reg
Computer
8/14
2 Easy Solutions
The RF is written into in the 1st half of the cycle and read from in the 2nd half of the cycle. Thus the data written is the data read. The add instruction isnt a hazard. sub $2,$1,$3 Disallow data hazards by nop adding an nop instruction called and $12,$2,$5 nop (no operation). Now there arent any data hazards.
Computer
9/14
A unit called the Forwarding Unit decides if there is a hazard and solves it by using forwarding. The values are directed from the EX/MEM and MEM/WB registers to the ALU.
Computer Architecture - A Pipelined Datapath
10/14
M u x
Rs Rt Rt Rd
EX/MEM.RegisterRd MEM/WB.RegisterRd
b. With forwarding
Computer
11/14
lw $2, 20($1)
IM
Reg
DM
Reg
IM
Reg
DM
Reg
or $8, $2, $6
IM
Reg
DM
Reg
add $9,$4,$2
slt $1,$6,$7
IM
Reg
DM
Reg
IM
Reg
DM
Reg
Computer
12/14
Inserting a Bubble
The EX,MEM, and WB control fields of the ID/EX register are set to 0. The PC and IF/ID register arent updated. This causes the and & or instructions to repeat cycle 3 during cycle 4.
Program Time (in clock cycles) execution CC 1 CC 2 order (in instructions) lw $2, 20($1) IM Reg CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9 CC 10 DM Reg
IM
Reg
Reg
DM
Reg
or $8, $2, $6
IM
IM
Reg
DM
Reg
bubble
add $9, $4, $2 IM Reg DM Reg
IM
Reg
DM
Reg
Computer
13/14
Control 0
M EX
MEM/WB WB
IF/ID
PCWrite
Instruction
PC
Instruction memory
M u x
The hardware doesnt depend on the compiler to detect and eliminate data hazards. A smart compiler will help in order to achieve better performance.
IF/ID.RegisterRs IF/ID.RegisterRt IF/ID.RegisterRt Rt IF/ID.RegisterRd Rd Rs Rt M u x EX/MEM.RegisterRd
Computer
ID/EX.RegisterRt
Forwarding unit
14/14