Professional Documents
Culture Documents
COMPUTER
ARCHITECTURE
BY
DR. RADWA M. TAWFEEK
MIPS PROCESSOR
MIPS BASICS
• Registers
• 32 4-byte registers in the “register file”
• Denoted “R” (e.g., R[2] is register 2)
• Instructions
• 4 bytes (32 bits)
• 4-byte aligned (i.e., they start at addresses that are a multiple of 4 -- 0x0000, 0x0004, etc.)
• Instructions operate on memory and registers
MIPS REGISTER FILE
• The operands are contained in the datapath’s register file ($t0, $s1, $s2)
MIPS INSTRUCTION FORMATS
MIPS INSTRUCTION SET (1)
shift right arithmetic 0 & 03 sra $s1, $s2, 4 $s1 = $s2 >> 4 (fill with sign bit)
and 0 & 24 and $s1, $s2, $s3 $s1 = $s2 & $s3
or 0 & 25 or $s1, $s2, $s3 $s1 = $s2 | $s3
nor 0 & 27 nor $s1, $s2, $s3 $s1 = not ($s2 | $s3)
and immediate c and $s1, $s2, ff00 $s1 = $s2 & 0xff00
or immediate d or $s1, $s2, ff00 $s1 = $s2 | 0xff00
load upper immediate f lui $s1, 0xffff $s1 = 0xffff0000
MIPS INSTRUCTION SET (2)
Calculate cycle time assuming negligible delays (for muxes, control unit,
sign extend, PC access, shift left 2, wires, setup and hold times) except:
Instruction and Data Memory (4 ns) ALU and adders (2 ns)
Register File access (reads or writes) (1 ns)
• Uses the clock cycle inefficiently – the clock cycle must be timed to
accommodate the slowest instr
• especially problematic for more complex instructions like floating point multiply
Cycle 1 Cycle 2
Clk
lw sw Waste
• May be wasteful of area since some functional units (e.g., adders) must be
duplicated since they can not be shared during a clock cycle
but
• It is simple and easy to understand
MIPS PIPELINE
lw sw Waste
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
Clk
lw sw R-type
IFetch Dec Exec Mem WB IFetch Dec Exec Mem IFetch
Pipeline Implementation:
pipeline clock same
lw IFetch Dec Exec Mem WB
as multi-cycle clock
sw IFetch Dec Exec Mem WB
IF/ID Control
Add
MEM/WB
Add
4 Shift
left 2
Read Addr 1
Instruction Data
Register Read
Memory Read Addr 2Data 1 Memory
Read
PC
Sign
16 Extend 32
WHY PIPELINE? FOR PERFORMANCE!
Time (clock cycles)
Once the
ALU
I Inst 0 IM Reg DM Reg pipeline is full,
n one instruction is
s completed every
ALU
t Inst 1 IM Reg DM Reg
cycle so CPI = 1
r.
ALU
O Inst 2 IM Reg DM Reg
r
d
ALU
e Inst 3 IM Reg DM Reg
r
ALU
Inst 4 IM Reg DM Reg
• Situations that prevent starting the next instruction in the next cycle
• Structure hazards
• A required resource is busy
• Data hazard
• Need to wait for previous instruction to complete its data read/write
• Control hazard
• Deciding on control action depends on previous instruction
ALU
I lw Mem Reg Mem Reg
memory
n
s
ALU
t Inst 1 Mem Reg Mem Reg
r.
ALU
O Inst 2 Mem Reg Mem Reg
r
d
ALU
e Inst 3 Mem Reg Mem Reg
r
ALU
Inst 4 Mem Reg Mem Reg
Reading instruction
from memory
ALU
I add $1, IM Reg DM Reg access hazard by
n doing reads in the
s second half of the
ALU
t Inst 1 IM Reg DM Reg
cycle and writes in
r. the first half
ALU
O Inst 2 IM Reg DM Reg
r
d
ALU
e add $2,$1, IM Reg DM Reg
r
ALU
add $1, IM Reg DM Reg
ALU
sub $4,$1,$5 IM Reg DM Reg
ALU
and $6,$1,$7 IM Reg DM Reg
ALU
or $8,$1,$9 IM Reg DM Reg
ALU
IM Reg DM Reg
xor $4,$1,$5
Read after write data hazard (RAW)
ONE WAY TO “FIX” A DATA HAZARD
ALU
I add $1, IM Reg DM Reg
waiting – stall
n
s
t stall
r.
O stall
r
d
ALU
e sub $4,$1,$5 IM Reg DM Reg
r
ALU
and $6,$1,$7 IM Reg DM Reg
DATA HAZARDS
• An instruction depends on
completion of data access by a
previous instruction
ANOTHER WAY TO “FIX” A DATA HAZARD
ALU
add $1, IM Reg DM Reg
I as soon as they are
n available to where
s they are needed
ALU
t IM Reg DM Reg
sub $4,$1,$5
r.
ALU
IM Reg DM Reg
r and $6,$1,$7
d
e
ALU
r IM Reg DM Reg
or $8,$1,$9
ALU
IM Reg DM Reg
xor $4,$1,$5
FORWARDING (BYPASSING)
ALU
I lw $1,4($2)IM Reg DM Reg
n
s
ALU
sub $4,$1,$5 IM Reg DM Reg
t
r.
ALU
IM Reg DM Reg
O and $6,$1,$7
r
d
ALU
IM Reg DM Reg
e or $8,$1,$9
r
ALU
IM Reg DM Reg
xor $4,$1,$5
• In MIPS pipeline
• Need to compare registers and compute target early in the pipeline
• Add hardware to do it in ID stage
JUMPS INCUR ONE STALL
Fix jump
ALU
I j IM Reg DM Reg
hazard by
n
waiting –
s
flush
ALU
t flush IM Reg DM Reg
r.
ALU
O IM Reg DM Reg
j target
r
d
e
r
ALU
I beq IM Reg DM Reg
n
s
ALU
t lw IM Reg DM Reg
r.
ALU
O Inst 3 IM Reg DM Reg
r
d
ALU
e Inst 4 IM Reg DM Reg
r
ONE WAY TO “FIX” A BRANCH CONTROL HAZARD
Fix branch
ALU
I beq IM Reg DM Reg hazard by
n waiting –
s flush – but
ALU
t flush IM Reg DM Reg
affects CPI
r.
ALU
IM Reg DM Reg
O flush
r
d
ALU
IM Reg DM Reg
e flush
r
ALU
IM Reg DM Reg
beq target
ALU
IM Reg DM
Inst 3
ANOTHER WAY TO “FIX” A BRANCH CONTROL HAZARD
ALU
beq IM Reg DM Reg Fix branch
I
n hazard by
s waiting –
ALU
t flush IM Reg DM Reg flush
r.
ALU
O IM Reg DM Reg
r beq target
d
ALU
e IM Reg DM
r Inst 3
STALL ON BRANCH
• In MIPS pipeline
• Can predict branches not taken
• Fetch instruction after branch, with no delay
YET ANOTHER WAY TO “FIX” A CONTROL HAZARD
Branch decision
ALU
IM Reg DM Reg
I 4 beq $1,$2,2 hardware moved
n to the decode
s
ALU
cycle
8 sub $4,$1,$5 flush
IM Reg DM Reg
t
r.
ALU
16 and $6,$1,$7 IM Reg DM Reg
O
r
d
ALU
IM Reg DM Reg
e 20 or r8,$1,$9
r
MORE-REALISTIC BRANCH PREDICTION