Slide 5
Slide 5
Decode
o Addition o Load word
&
Next Instruction
o Read [t1] as opr1 o Read [t1] as opr1 Decode
Operand
o Read [t2] as opr2 o Use 20 as opr2
Fetch
Result Result WB
Result stored in t3 Memory data stored in t3
Write
Steps to design a datapath
? ?
? ? ?
Clk
? ?
? ? ?
Control Logic
❑ Not all instructions need all 5 stages → the control logic selects
“needed” datapath lines based on the instruction.
➢ MUX selector, ALU op selector, write enable, etc.
Fetch Stage: Requirements
❑ Instruction Fetch Stage:
1. Use the Program Counter (PC) to fetch the instruction from memory
▪ PC is implemented as a special register in the processor
2. Increment the PC by 4 to get the address of the next instruction:
▪ How do we know the next instruction is at PC+4?
▪ Note the exception when branch/jump instruction is executed
1. Fetch
2. Decode
3. ALU
4. Memory
5. RegWrite
Fetch Stage: Block diagram
Increment by
4 for next
instruction
Decode Stage
32-bit
register
Zoomed-in element: PC register
❑ Seems that we’re reading and updating PC at the same time!
➢ How can it works properly during a single cycle?
❑ Magic of clock
➢ PC is read during the first half of the clock period and it is updated with PC+4
at the next rising clock edge.
Time
Clk
𝑡𝑠𝑒𝑡𝑢𝑝 𝑡𝑠𝑒𝑡𝑢𝑝
𝑡𝑐𝑙𝑘−2−𝑄
Add
4 PC 100 104
𝑡𝑎𝑑𝑑
PC Read
In address
In 104 108
Instruction
Clk Instruction Flip-flop timing
memory
D Q 𝑡𝑠𝑒𝑡𝑢𝑝 time that D must not change before ↑
𝑡𝑐𝑙𝑘−2−𝑄 Delay after ↑ until D appears at Q
❑ Idealized memory
➢ One input bus: Address Address DataOut
Instruction
➢ One output bus: Data Out 32 32
Memory
❑ Memory word is found by
➢ Address selects the word to put on Data Out
➢ The word must had been written to the memory prior to instruction fetch.
➢ During instruction fetch operation, the memory behaves as a combinational
logic block: Address valid → Data Out valid after “access time”.
❑ Note: in practice, there must be more inputs but they are not
used during instruction fetch.
➢ E.g. Data In, Clock, Write Enable had been used to write the instructions to
the memory (prior to instruction fetch).
Decode Stage: Requirements
❑ Instruction Decode Stage:
➢ Gather data from the instruction fields:
1. Read the opcode to determine instruction type and field lengths
2. Read data from general purpose registers (in the register file)
▪ Can be two (e.g. add), one (e.g. addi) or zero (e.g. auipc)
5 Read 32
ALU Stage
Read
register A data A
5 Read
register B Operands
Inst. 5 Write
register Read 32
data B
Operation
Collection of
registers, known
as register file
Zoomed-in element: Register File
5 RA RA 32
❑ A collection of 32 data BusA
Register
registers: numbers
5 RB
Register
➢ Two 32-bit output busses: 5 RW File Data
busA and busB RB 32
32 data
➢ One 32-bit input bus: Write BusB
Data
data
busW BusW
Inst [Y:X]
= bits X to Y in Instruction
10100
5 AddrB
DataA
32 content of
5
BusA register x19
AddrA
10011 000
Register
5 AddrD File
DataB
32
content of
32
DataD BusB register x20
BusW
10010
Inst [24:20]
5
AddrB DataA
32 content of
5
BusA register x1
00001
AddrA
Register
5 File
AddrD
32
000
DataB
32
DataD BusB
01111
BusW
Problems:
Clk Write Enable RB data is an
Result to be stored
0010011
+4 Reg[]
DataD
ALU
pc IMEM
inst[11:7]
AddrD DataA Reg[rs1] alu
pc+4 inst[19:15] AddrA 0
inst[24:20] AddrB DataB
Reg[rs2] 1
Imm. imm[31:0]
Gen
inst[31:20]
ALUSel=Add
ImmSel=I BSel=1
31 25 24 20 19 15 14 12 11 7 6 0
imm[11:0] rs1 funct3 rd I-opcode
imm[11:5] rs2 rs1 funct3 imm[4:0] S-opcode
5
5
1 6
I S
Memory Stage
Decode Stage
ALU result
Operands ALU
Operation
Logic to perform
arithmetic and
logical operations
Element: Arithmetic Logic Unit
ALUSel
4
❑ ALU (Arithmetic Logic Unit) A
32
➢ Combinational logic to implement
arithmetic and logical operations ALU 32
result
❑ Inputs: B A op B
➢ Two 32-bit numbers 32
❑ Control:
➢ 4-bit to decide the particular operation ALUSel Function
0000 AND
❑ Output: OR
0001
➢ Result of arithmetic/logical operation
0010 add
0110 subtract
0111 slt
1100 NOR
ALU Stage: Branch Instructions
❑ Branch instruction is harder as we need to perform two
calculations
❑ Example: "beq x9, x0, 3"
1. Branch Outcome:
▪ Need a comparator to compare the registers
2. Branch Target Address:
▪ Use ALU to calculate the address
▪ Need PC (from Fetch Stage)
▪ Need Offset (from Decode Stage)
❑ Also need to feed the branch target address back to the fetch
stage!
Branch Comparator
❑ BrEq = 1, if A=B
A Branch
❑ BrLT = 1, if A < B
Comp.
❑ BrUn =1 selects unsigned B
comparison for BrLT, 0=signed
❑ BGE branch: A >= B, if !(A<B)
BrUnBrEq BrLT
B-type Immediate Generator
alu
+4 Reg[] pc
wb 1
DataD Reg[rs1]
1 ALU
0
pc IMEM inst[11:7] AddrD 0
Reg[rs2]
pc+4 inst[19:15] AddrA DataA Branch 0
Comp.
inst[24:20] AddrB DataB 1
inst[31:7]
Imm. imm[31:0]
Gen
BrUn
PCSel=taken/not-taken ImmSel=B RegWEn=0 Bsel=1 ALUSel=Add
Choose to BrEq BrLT ASel=1
Control Signal generate Choose PC as
to select B-type ALU opr 1,
between immediate imm[31:0] as
(PC+4) or ALU opr2, to
Branch Target calculate the
branch target
Memory Stage: Requirements
❑ Instruction Memory Access Stage:
➢ Only the load and store instructions need to perform operation in this stage
▪ Use memory address calculated by ALU Stage
▪ Read from or write to data memory
➢ All other instructions remain idle
▪ Result from ALU Stage will pass through to be used in Register Write
stage (later in this lecture) if applicable
Register Write
ALU Stage
32 Address
Stage
Read 32
Result Data
32 Write
Data Data
Memory
MemRW
Memory which
stores data values
Adding lw to datapath
lw x14, 8(x2)
+4 Reg[] pc alu
wb 1
1 DataD Reg[rs1]
alu pc inst[11:7] 0 ALU DMEM 1
pc+4
0 IMEM AddrD Reg[rs2] Addr wb
inst[19:15] DataA Branch 0 DataR 0
AddrA Comp. DataW mem
inst[24:20] DataB 1
AddrB
inst[31:7]
Imm. imm[31:0]
Gen
RegWEn=1 Bsel=1
Asel=0 ALUSel=Add WBSel=0
PCSel ImmSel=I BrUnBrEq BrLT MemRW=Read
sw x14, 8(x2)
Do we need any modification?
+4 Reg[] pc alu
wb 1
DataD Reg[rs1]
1 ALU
alu pc inst[11:7] AddrD 0 DMEM 1
pc+4
0 IMEM Reg[rs2] Addr wb
inst[19:15] AddrA DataA Branch 0 DataR 0
Comp. mem
inst[24:20] AddrB DataB 1 DataW
inst[31:7]
Imm. imm[31:0]
Gen
RegWEn=0 Bsel=1
Asel=0 ALUSel=Add WBSel=*
PCSel ImmSel=S BrUnBrEq BrLT MemRW=Write
*= “Don’t Care”
Register Write Stage: Requirements
❑ Instruction Register Write Stage:
➢ Most instructions write the result of some computation into a register
▪ Examples: arithmetic, logical, shifts, loads, set-less-than
▪ Need destination register number and computation result
➢ Exceptions are stores, branches, jumps
▪ There are no results to be written
▪ These instructions remain idle in this stage
pc+4
alu +4 Reg[] pc alu
wb 1
DataD Reg[rs1] 2
1 ALU
pc inst[11:7] AddrD 0 DMEM 1
pc+4
0 IMEM Reg[rs2] Addr DataR
wb
inst[19:15] AddrA DataA Branch 0 0
Comp. mem
inst[24:20] AddrB DataB 1 DataW
inst[31:7]
Imm. imm[31:0]
Gen
pc+4
+4 Reg[] pc alu
alu wb 1
DataD Reg[rs1] 2
1 ALU
pc inst[11:7] AddrD 0 DMEM 1
pc+4
0 IMEM Reg[rs2] Addr DataR
wb
inst[19:15] AddrA DataA Branch 0 0
Comp. DataW mem
inst[24:20] AddrB DataB 1
inst[31:7]
Imm. imm[31:0]
Gen
PCSel inst[31:0] ImmSel RegWEn BrUn BrEq BrLT BSel ASel ALUSel MemRW WBSel
Control Unit