Week 08
Week 08
• Lw, Sw, Add, Sub, And, Or, Slt can be performed • Support for j and jr
• For j (jump) we need an additional multiplexor
– For both of them PC value need to come from somewhere else
PCSrc
– For J, PC is created by 4 bits (31:28) from old PC, 26 bits from IR
1
Add M
u
(27:2) and 2 bits are zero (1:0)
x
ALU
4 Add result 0
– For JR, PC value comes from a register
RegWrite Shift
left 2
• Support for JAL
Instruction [25–21] Read
PC Read register 1 Read
data 1
MemWrite – Address is same as for J inst
address Instruction [20–16] Read MemtoReg
ALUSrc
register 2 Zero
Instruction
[31–0] 1 Read
1 ALU ALU
– OLD PC needs to be saved in register 31
Write data 2 result Address Read 1
M register M data
u
Instruction
memory
Instruction [15–11] x Write u
x
M
u • And what about immediate operand instructions
0 data Registers 0
x
Write Data 0
– Second operand from instruction, but without shifting
RegDst data memory
Instruction [15–0] 16 Sign 32
extend ALU MemRead • Support for other instructions like lw and immediate inst write
control
Instruction [5–0]
ALUOp
1 2
MemRead
• All of the logic is combinational • ALU's operation based on instruction type and function code
– e.g., what should the ALU do with any instruction
• We wait for everything to settle down, and the right thing to be done • Example: lw $1, 100($2)
– ALU might not produce “right answer” right away
•
– we use write signals along with clock to determine when to write 35 2 1 100
• Cycle time determined by length of the longest path
op rs rt 16 bit offset
State State
• ALU control input
element Combinational logic element
1 2
000 AND
001 OR
010 add
Clock cycle
110 subtract
111 set-on-less-than
• Must describe hardware to compute 3-bit ALU conrol input • Simple combinational logic to realize the truth tables
– given instruction type Inputs
00 = lw, sw ALUOp Op5
01 = beq, computed from instruction type Op4
ALUOp Op3
10 = arithmetic Op2
ALUcontrol block
11 = Jump ALUOp0
Op1
Op0
– function code for arithmetic ALUOp1
• Control can be described using a truth table: Outputs
A Complete Datapath with Control Datapath with Control and Jump Instruction
9 10
• Calculate cycle time assuming negligible delays except: • Single Cycle Problems:
– memory (2ns), ALU and adders (2ns), register file access (1ns) – what if we had a more complicated instruction like floating point?
PCSrc – wasteful of area
1
M
• One Solution:
Add u
4 ALU
x
0
– use a “smaller” cycle time
Add result
RegWrite Shift – have different instructions take different numbers of cycles
left 2
ALUOp
11 12
Instruction Format Operation for Each Instruction
LW REG 1 REG 2 LOAD ADDRESS OFFSET 1. READ INST 1. READ INST 1. READ INST 1. READ INST 1. READ
31 26 25 21 20 16 15 11 10 6 5 0 INST
SW REG 1 REG 2 STORE ADDRESS OFFSET 2. READ REG 1 2. READ REG 1 2. READ REG 1 2. READ REG 1 2.
31 26 25 21 20 16 15 11 10 6 5 0
5. WRITE REG2 5. 5. WRITE DST 5.
JUMP
5.
JUMP ADDRESS
13 14
• We’ll use a finite state machine for control – We’ll use a Moore machine (output based only on current state)
15 16
• Instruction Fetch
M
U
25-00 X
• Instruction Decode and Register Fetch
PC 25-21 M
M RA1 A
RD1 U
R
U I 20-16
RA2 REG X • Execution, Memory Address Computation, or Branch Completion
X R A
FILE ALU
L
M B M U
U WA WD
RD2
R 4
U
• Memory Access or R-type instruction completion
Add X
X
15-11 BR
MEM Data
D COND • Write-back step
Out 15-00 Sign Shift
R Ext Left 2 ALU BNE
Data In M
CON BEQ
U 05-00
X INSTRUCTIONS TAKE FROM 3 - 5 CYCLES!
ALUOP JUMP
31-26
CONTROL
17 18
Step 1: Instruction Fetch Step 2: Instruction Decode and Register Fetch
• Use PC to get instruction and put it in the Instruction Register. • Read registers rs and rt in case we need them
• Increment the PC by 4 and put the result back in the PC. • Compute the branch address in case the instruction is a branch
• Can be described succinctly using RTL "Register-Transfer Language" • RTL:
IR = Memory[PC]; A = Reg[IR[25-21]];
PC = PC + 4; B = Reg[IR[20-16]];
ALUOut = PC + (sign-extend(IR[15-0]) << 2);
Can we figure out the values of the control signals?
• We aren't setting any control lines based on the instruction type
What is the advantage of updating the PC now? (we are busy "decoding" it in our control logic)
19 20
• ALU is performing one of three functions, based on instruction type • Loads and stores access memory
• R-type:
• R-type instructions finish
ALUOut = A op B;
Reg[IR[15-11]] = ALUOut;
• Branch:
if (A==B) PC = ALUOut; The write actually takes place at the end of the cycle on the edge
21 22
Write
- back step Summary:
• Reg[IR[20-16]]= MDR;
Action for R-type Action for memory-reference Action for Action for
What about all the other instructions? Step name instructions instructions branches jumps
Instruction fetch IR = Memory[PC]
PC = PC + 4
Instruction A = Reg [IR[25-21]]
decode/register fetch B = Reg [IR[20-16]]
ALUOut = PC + (sign-extend (IR[15-0]) << 2)
Execution, address ALUOut = A op B ALUOut = A + sign-extend if (A ==B) then PC = PC [31-28] II
computation, branch/ (IR[15-0]) PC = ALUOut (IR[25-0]<<2)
jump completion
Memory access or R-type Reg [IR[15-11]] = Load: MDR = Memory[ALUOut]
completion ALUOut or
Store: Memory [ALUOut] = B
23 24