Multi-cycle Approach - University of California,...
Transcript of Multi-cycle Approach - University of California,...
![Page 1: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/1.jpg)
1
Multi-cycle Approach• Single cycle CPU
• Multi-cycle CPU– Requires state elements to hold intermediate values
Stateelement
Stateelement
Combinationallogic
clock
one clock cycle or instruction
StateElement
ACombinational
logic
StateElement
B
…
StateElement
N-1Combinational
logic
StateElement
N
clock
one instruction
…
one clock cycle
![Page 2: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/2.jpg)
2
Multi-cycle Approach• Each cycle must
– Store values needed in a later cycle of the current instruction in an internal register. All except IR hold data for one clock cycle.
IR – Instruction Register MDR – Memory Data RegisterA, B – Register File data ALUOut – ALU Result Register
– Store values needed by subsequent instructions in the register file or memory
Address
Read Data(Instr. or Data)
Memory
PC
Write Data
Read Addr 1
Read Addr 2
Write Addr
Register
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
AB A
LU
Ou
t
![Page 3: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/3.jpg)
3
Multi-cycle Control• New control signals needed
– PCWriteCond is set during a beq instruction• Formerly called Branch signal
– PCWrite is set to write PC• Unconditional write signal needed during Fetch cycle
– IorD controls what address is used for the memory• PC holds address for fetch cycle• ALUOut holds address for memory access instructions
– IRWrite controls when the IR is written– ALUSrcA control one input to ALU
• rs register for most operations• PC for branch instructions• Old ALUSrc renamed ALUSrcB and expanded
![Page 4: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/4.jpg)
4
Multi-cycle Control and Datapath
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 5: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/5.jpg)
5
Multi-cycle Steps• Instruction Fetch• Decode and Register Fetch
• Execution• Memory Access• Write Register File
![Page 6: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/6.jpg)
6
(1) Instruction Fetch Cycle• Increment PC using ALU
– PC = PC + 4
• Read instruction from memory– IR = M[PC]
• Control signals must– Select memory address source
– Enable memory reading– Enable PC and IR write– Select PC source– Select ALU input as PC and constant 4
– Select ALU operation (addition)
![Page 7: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/7.jpg)
7
Instruction Fetch
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 8: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/8.jpg)
8
(2) Decode and Register Fetch Cycle• Read register values
– A = R[rs], B = R[rt]
• Compute branch destination– ALUOut = PC + sign extended immediate value
• Prepare for next step based on instruction• Control signals must
– Select ALU inputs as PC and immediate value
– Select ALU operation (addition)
![Page 9: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/9.jpg)
9
Decode and Register Fetch
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 10: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/10.jpg)
10
(3) Execution Cycle• Functionality varies with instructions
– Memory reference• Compute address• ALUOut = A + sign extended immediate
– R-type• Compute operation• ALUOut = A op B
– Branch• Store new PC if needed• PC = ALUOut• ALUOut contains branch destination from previous cycle
• Control signals will depend on instruction type– Mem/R-type: Select ALU input and operation– Branch: Select PC source and set PC write control signal if
needed
![Page 11: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/11.jpg)
11
Execute Branch
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 12: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/12.jpg)
12
(4) Memory Access Cycle• Functionality varies with instructions
– Memory reference• Read memory (lw) or write memory (sw)• MDR = M[ALUOut] or M[ALUOut] = B
– R-type• Write result to register file• R[rd] = ALUOut
• Control signals will depend on instruction type– Memory reference
• Enable memory read or write
• Select memory address
– R-type• Select register file write address and data• Enable register file write
![Page 13: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/13.jpg)
13
R-Type “Memory Access”
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 14: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/14.jpg)
14
(5) Write Register File Cycle• Only used by load instructions• Write memory value to register
– Reg[rt] = MDR
• Control signals must– Enable register file write– Select register file write address and data
![Page 15: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/15.jpg)
15
lw Write Registers
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
UO
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 16: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/16.jpg)
16
Review
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
Uo
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1
![Page 17: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/17.jpg)
17
Defining the CPU Control●Use a finite state machine model to design the control
● Control now has state or memory● Action depends on input and current cycle
●Signal names in each state are asserted● Asserted signals are set or enabled● Unlisted signals are deasserted
●Arcs between states list conditions for transition
●Example State A● Signal Run is asserted● Goes to State B when input Up = 0● Goes to State C when input Up = 1
RunUp=1Up=0
A
B C
![Page 18: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/18.jpg)
18
Instruction Fetch and Decode Cycles
MemReadIorD = 0IRWrite
ALUSrcA = 0ALUSrcB = 01ALUOp = 00
PCSource = 00PCWrite
Instruction Fetch
Start ALUSrcA = 0ALUSrcB = 11ALUOp = 00
Next state dependson instruction
Decode
MemReadIorDIRWrite
Fetch Instruction
ALUSrcAALUSrcBALUOpPCSourcePCWrite
Increment PC
ALUSrcAALUSrcBALUOp
ComputeBranchDestination
![Page 19: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/19.jpg)
19
Write Register File Cycle- Load instruction only
Memory Access Cycle- Read or write memory
Execute Cycle- Compute memory address
Memory Access Instructions
ALUSrcA = 1ALUSrcB = 10ALUOp = 00
MemWriteIorD = 1
RegWriteMemtoReg = 1
RegDst = 0
MemReadIorD = 1
From Decode state if SW or LW
SWLW
Fetch NextInstruction
![Page 20: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/20.jpg)
20
Memory Access Cycle- Write data to register file
Execute Cycle- Compute operation- Control must look at function field
R-type Instructions
ALUSrcA = 1ALUSrcB = 00ALUOp = 10
RegWriteMemtoReg = 0
RegDst = 1
From Decode state if R-type
Fetch NextInstruction
![Page 21: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/21.jpg)
21
Execute Cycle- Compare register values- Write PC if Zero asserted
Branch on Equal Instruction
ALUSrcA = 1ALUSrcB = 00ALUOp = 01
PCSource = 01PCWriteCond
From Decode state if BEQ
Fetch NextInstruction
![Page 22: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/22.jpg)
22
Final State Diagram
![Page 23: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/23.jpg)
23
Implementing jal ●Jump and link (jal)
– J-Type– PC+4 $31– Jump destination PC
●What modifications are required?– 2 Cycle
• Const 31 write destination• Expand PCSource mux with ALUOut as PC+4
– 3 Cycle• Const 31 write destination• PC connected to write data
![Page 24: Multi-cycle Approach - University of California, Davisamerican.cs.ucdavis.edu/.../154bpdf/MIPSmulticycle.pdf · 2011-01-30 · 2 Multi-cycle Approach • Each cycle must – Store](https://reader034.fdocuments.in/reader034/viewer/2022043008/5f98addd109977332b2548e3/html5/thumbnails/24.jpg)
24
Implementing jal – what must be done?
Address
Read Data(Instr. or Data)
MemoryPC
Write Data
Read Addr 1
Read Addr 2
Write AddrRegister
File
Read Data 1
Read Data 2
ALU
Write Data
IRM
DR
A
AL
Uo
ut
SignExtend
Shiftleft 2
ALUcontrol
ALUOp
IRWriteMemtoReg
MemWriteMemRead
IorD
PCWrite
PCWriteCond
RegDstRegWrite
ALUSrcAALUSrcB
zero
PCSource
1
4
Instr[5-0]
Instr[25-0]
PC[31-28]
Instr[15-0]
Instr[31-26]
32
28
Control
Shiftleft 2
0
1
0
0
1
1
0
B 0
1
2
3
2
0
1