CS61C: Great Ideas in Computer Architecture (aka Machine Structures)
Lecture 13: RISC-V Datapath
Instructors: Caroline Liu, Justin Yokota, Peyrin Kao
CS 61C
Summer 2022
CS 61C Hardware Roadmap
Processor (CPU)
|
Synchronous Digital Systems
|
Transistors |
Today, we’re building a circuit that can run every base RISC-V instruction!
CS 61C
Summer 2022
Review: Subcircuits
CS 61C
Summer 2022
Combinatorial Logic: Multiplexer (MUX)
Out = (A)(!S) + (B)(S)
S | A | B | Out |
0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 |
0 | 0 | 1 | 1 |
1 | 0 | 1 | 1 |
0 | 1 | 0 | 0 |
1 | 1 | 0 | 1 |
0 | 1 | 1 | 0 |
1 | 1 | 1 | 1 |
CS 61C
Summer 2022
Combinatorial Logic: Arithmetic Logic Unit (ALU)
Select
ALU
A
B
R
CS 61C
Summer 2022
Sequential Logic: Registers
D
Q
clk
Input
Output
WEn
WEn
CS 61C
Summer 2022
Sequential Logic: Read-Only Memory
Inputs | MemAddress | 32 bits |
Clock | 1 bit | |
Output | MemReadData | 32 bits |
MEM
MemReadData
MemAddress
CS 61C
Summer 2022
Sequential Logic: Read/Write Memory
Inputs | MemAddress | 32 bits |
MemWriteData | 32 bits | |
MemWEn | 1 bit | |
Clock | 1 bit | |
Output | MemReadData | 32 bits |
MEM
MemWEn
MemReadData
MemWriteData
MemAddress
CS 61C
Summer 2022
Datapath for add
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
add instruction
31 25 24 20 19 15 14 12 11 7 6 0 | ||||||
R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
I | imm[11:0] | rs1 | funct3 | rd | opcode | |
I* | funct7 | imm[4:0] | rs1 | funct3 | rd | opcode |
S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
U | imm[31:12] | rd | opcode | |||
J | imm[20|10:1|11|19:12] | rd | opcode |
CS 61C
Summer 2022
Stage 1: Instruction Fetch (IF)
+4
PC+4
PC
Here’s a circuit that adds 4 to PC on each clock cycle.
PC+4 is written to the register on the next rising edge.
CS 61C
Summer 2022
Stage 1: Instruction Fetch (IF)
IMEM
PC
inst
+4
PC+4
PC
Let’s find out what instruction is at the address in PC.
We can input the address into a memory block, and retrieve a 32-bit instruction.
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID)
inst[11:7]
inst[24:20]
inst[19:15]
IMEM
PC
inst
+4
PC+4
PC
Let’s split up the instruction bits into some useful fields.
Now we have the 5-bit values rs1, rs2, and rd.
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID), Register Read
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Let’s find out what values are in the rs1 and rs2 registers.
We can input the register numbers into the RegFile and retrieve 32-bit values stored in those registers.
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Let’s add the 32-bit values stored in rs1 and rs2 together.
CS 61C
Summer 2022
Stage 4: Memory
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
We don’t need to read from or write to memory, so we can do nothing in this stage.
CS 61C
Summer 2022
Stage 5: Register Write
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Let’s write the computation result back to register rd.
Note that we also set RegWriteIndex to rd.
CS 61C
Summer 2022
Five-Stage Datapath: add
CS 61C
Summer 2022
Why Five-Stage Datapath?
CS 61C
Summer 2022
Datapath for sub
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
sub instruction
31 25 24 20 19 15 14 12 11 7 6 0 | ||||||
R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
I | imm[11:0] | rs1 | funct3 | rd | opcode | |
I* | funct7 | imm[4:0] | rs1 | funct3 | rd | opcode |
S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
U | imm[31:12] | rd | opcode | |||
J | imm[20|10:1|11|19:12] | rd | opcode |
CS 61C
Summer 2022
Stage 1: Instruction Fetch (IF)
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Same as the add instruction. Compute PC+4 for the next instruction.
CS 61C
Summer 2022
Stage 1: Instruction Fetch (IF)
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Same as the add instruction. Fetch the 32-bit instruction from IMEM.
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID)
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Same as the add instruction. Extract fields from the 32-bit instruction.
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID), Register Read
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Same as the add instruction. Read data from registers.
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Different from the add instruction! We want to compute A–B, not A+B.
CS 61C
Summer 2022
Stage 3: Execute
Let’s add an input to select different ALU operations.�
How do we know which operation to select?
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
CS 61C
Summer 2022
Stage 3: Execute
Add a circuit that takes the instruction, checks its opcode/funct3/funct7, and outputs the correct ALUSel value.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
CS 61C
Summer 2022
Stage 4: Memory
Same as the add instruction. Do nothing.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
CS 61C
Summer 2022
Stage 5: Register Write
Same as the add instruction. Write the calculation result to a register.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
CS 61C
Summer 2022
New Subcircuit: Control Logic
ALUSel
inst[31:0]
CS 61C
Summer 2022
Datapath for All R-type Instructions
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
R-Type Instruction Datapath
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
We can run every R-type instruction with this circuit! We just have to set ALUSel depending on the instruction.
ALUSel
inst[31:0]
CS 61C
Summer 2022
Datapath for addi
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
addi instruction
31 25 24 20 19 15 14 12 11 7 6 0 | ||||||
R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
I | imm[11:0] | rs1 | funct3 | rd | opcode | |
I* | funct7 | imm[4:0] | rs1 | funct3 | rd | opcode |
S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
U | imm[31:12] | rd | opcode | |||
J | imm[20|10:1|11|19:12] | rd | opcode |
CS 61C
Summer 2022
Stage 1: Instruction Fetch (IF)
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
+4
PC+4
PC
Same as R-type. Compute PC+4 and fetch instruction from IMEM.
ALUSel
inst[31:0]
PC
inst
IMEM
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID)
ALUSel
inst[31:0]
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Same as the add instruction. Extract fields from the 32-bit instruction.
inst[24:20] don’t correspond to rs2 anymore. Does it matter?
CS 61C
Summer 2022
Stage 2: Instruction Decode (ID), Register Read
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
RegReadData1 has the value in register rs1.
RegReadData2 has garbage! rs2 doesn’t exist in addi.
ALUSel
inst[31:0]
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
ALU
A
B
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
We want to feed the immediate, not RegReadData2, into the B input of the ALU. Where is the immediate?
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
The immediate is in the instruction!
How do we extract the immediate from the instruction?
ALU
A
B
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
Add an immediate generator subcircuit!
We don’t want to break the R-type datapath. How do we support both B inputs?
ALU
A
B
Imm Gen
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
Add a mux to select between the immediate and register value!
How do we know which value to select?
ALU
A
B
0
1
Imm Gen
CS 61C
Summer 2022
Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
Ask the control logic subcircuit to figure out which B input to select!
ALU
A
B
0
1
BSel
Imm Gen
CS 61C
Summer 2022
Stage 4: Memory
BSel
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
addi doesn’t write to memory, so do nothing.
ALU
A
B
Imm Gen
0
1
CS 61C
Summer 2022
Stage 5: Register Write
BSel
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
Same as R-type. Write the result of the computation back to a register.
ALU
A
B
Imm Gen
0
1
CS 61C
Summer 2022
New Subcircuit: Immediate Generator
CS 61C
Summer 2022
New Control Signal: BSel
BSel
0
1
RegReadData2
Immediate
B (ALU input)
CS 61C
Summer 2022
Datapath for I-type Instructions
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
R-Type and I-Type Instruction Datapath
BSel
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
ALU
A
B
Imm Gen
0
1
We can run every non-load I-type instruction with this circuit! We just have to set ALUSel depending on the instruction.
CS 61C
Summer 2022
Datapath for Loads
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
Load instructions
31 25 24 20 19 15 14 12 11 7 6 0 | ||||||
R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
I | imm[11:0] | rs1 | funct3 | rd | opcode | |
I* | funct7 | imm[4:0] | rs1 | funct3 | rd | opcode |
S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
U | imm[31:12] | rd | opcode | |||
J | imm[20|10:1|11|19:12] | rd | opcode |
CS 61C
Summer 2022
Load Stage 1: Instruction Fetch (IF)
BSel
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ALUSel
inst[31:0]
ALU
A
B
Imm Gen
0
1
+4
PC+4
PC
PC
inst
Same as non-load I-types. Compute PC+4 and fetch instruction from IMEM.
IMEM
CS 61C
Summer 2022
Load Stage 2: Instruction Decode (ID), Register Read
BSel
ALUSel
inst[31:0]
ALU
A
B
Imm Gen
0
1
Same as non-load I-types. Read value of rs1 from register.
IMEM
PC
inst
+4
PC+4
PC
inst[11:7]
inst[24:20]
inst[19:15]
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
RegReadData2
RegWEn
CS 61C
Summer 2022
Load Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
What do we need to compute?
Immediate + value in rs1 = memory address to load from
0
1
BSel
Imm Gen
ALU
A
B
CS 61C
Summer 2022
Load Stage 3: Execute
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
BSel=1 to select the immediate, not rs2.
ALUSel set to make ALU perform addition.
0
1
BSel
Imm Gen
ALU
A
B
CS 61C
Summer 2022
Load Stage 4: Memory
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
ALUSel
inst[31:0]
Not adding anything here; just making space for the memory unit.
BSel
ALU
A
B
Imm Gen
0
1
CS 61C
Summer 2022
Load Stage 4: Memory
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
Same as the previous slide, but the ALU output to RegWriteData arrow moved.
CS 61C
Summer 2022
Load Stage 4: Memory
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Here’s a memory block. What’s the address we want to load from?
CS 61C
Summer 2022
Load Stage 4: Memory
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
We computed the address with the ALU!
Where does the data we read go?
CS 61C
Summer 2022
Load Stage 5: Register Write
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
We want to write the data from memory to a register. How do we support writing both data from memory and ALU output to a register?
CS 61C
Summer 2022
Load Stage 5: Register Write
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Add a mux to choose which value to write back to the register!
How do we know which value to write back?
0
1
CS 61C
Summer 2022
Load Stage 5: Register Write
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Add a new control signal: WBSel (write-back select)
WBSel
0
1
CS 61C
Summer 2022
New Control Signal: WBSel
WBSel
1
0
ALU Output
MemReadData
RegWriteData
CS 61C
Summer 2022
Partial Loads
CS 61C
Summer 2022
Datapath for Stores
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
Store instructions
CS 61C
Summer 2022
Store Stage 1: Instruction Fetch (IF)
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
As before, we read the instruction from IMEM.
WBSel
0
1
+4
PC+4
PC
PC
inst
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
IMEM
CS 61C
Summer 2022
Store Stage 2: Instruction Decode (ID)
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Note that we have to read values from both rs1 and rs2!
WBSel
0
1
inst[11:7]
inst[24:20]
inst[19:15]
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
RegReadData2
RegWEn
IMEM
PC
inst
+4
PC+4
PC
CS 61C
Summer 2022
Store Stage 3: Execute
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
What are we computing?�Address = value in rs1 + immediate
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
CS 61C
Summer 2022
Store Stage 3: Execute
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Problem: I-type immediates are in inst[31:20]. S-type immediates are in inst[31:25, 11:7]. How to support both?
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
CS 61C
Summer 2022
Store Stage 3: Execute
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
Add a new control signal, ImmSel (immediate select). Update immediate generator to support I-types and S-types.
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
CS 61C
Summer 2022
Store Stage 4: Memory
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
What are we writing to memory? The value in register rs2!
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
CS 61C
Summer 2022
Store Stage 4: Memory
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
How do we tell when we want to write to memory? Add a MemWEn (memory write-enable) control signal!
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
MemRW
CS 61C
Summer 2022
Store Stage 5: Register Write
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
What are we writing back to the register? Nothing! Store instructions don’t modify register values.
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
MemRW
CS 61C
Summer 2022
Store Stage 5: Register Write
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
How do we tell we don’t want to write to a register? Add a RegWEn (register write-enable) control signal!
WBSel
0
1
IMEM
PC
inst
+4
PC+4
PC
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
MemRW
RegWEn
CS 61C
Summer 2022
New Control Signal: ImmSel
CS 61C
Summer 2022
New Control Signals: MemWEn, RegWEn
CS 61C
Summer 2022
Partial Stores
CS 61C
Summer 2022
Datapath for Branches
CS 61C
Summer 2022
List of RISC-V Instructions
Instruction | Name | Type |
add | ADD | R |
sub | SUBtract | R |
and | bitwise AND | R |
or | bitwise OR | R |
xor | bitwise XOR | R |
sll | Shift Left Logical | R |
srl | Shift Right Logical | R |
sra | Shift Right Arithmetic | R |
slt | Set Less Than (signed) | R |
sltu | Set Less Than (Unsigned) | R |
addi | ADD Immediate | I |
andi | bitwise AND Immediate | I |
ori | bitwise OR Immediate | I |
xori | bitwise XOR Immediate | I |
Instruction | Name | Type |
slli | Shift Left Logical Immediate | I* |
srli | Shift Right Logical Immediate | I* |
srai | Shift Right Arithmetic Immediate | I* |
slti | Set Less Than Immediate (signed) | I |
sltiu | Set Less Than Immediate (Unsigned) | I |
lb | Load Byte | I |
lbu | Load Byte (Unsigned) | I |
lh | Load Half-word | I |
lhu | Load Half-word (Unsigned) | I |
lw | Load Word | I |
Instruction | Name | Type |
sb | Store Byte | S |
sh | Store Half-word | S |
sw | Store Word | S |
beq | Branch if EQual | B |
bge | Branch if Greater or Equal (signed) | B |
bgeu | Branch if Greater or Equal (Unsigned) | B |
blt | Branch if Less Than (signed) | B |
bltu | Branch if Less Than (Unsigned) | B |
bne | Branch if Not Equal | B |
jal | Jump And Link | J |
jalr | Jump And Link Register | I |
auipc | Add Upper Immediate to PC | U |
lui | Load Upper Immediate | U |
CS 61C
Summer 2022
Branch instructions
31 25 24 20 19 15 14 12 11 7 6 0 | ||||||
R | funct7 | rs2 | rs1 | funct3 | rd | opcode |
I | imm[11:0] | rs1 | funct3 | rd | opcode | |
I* | funct7 | imm[4:0] | rs1 | funct3 | rd | opcode |
S | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
B | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
U | imm[31:12] | rd | opcode | |||
J | imm[20|10:1|11|19:12] | rd | opcode |
CS 61C
Summer 2022
Branch Stage 1: Instruction Fetch (IF)
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
As before, we read the instruction from IMEM. We’ll modify the PC logic later, in the write-back stage.
WBSel
0
1
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
ImmSel
MemRW
RegWEn
+4
PC+4
PC
PC
inst
IMEM
CS 61C
Summer 2022
Branch Stage 2: Instruction Decode (ID)
inst[31:0]
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
ImmSel
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
inst[11:7]
inst[24:20]
inst[19:15]
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
RegReadData2
RegWEn
Note that we have to read values from both rs1 and rs2!
CS 61C
Summer 2022
Branch Stage 3: Execute
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Note that we have a new type (B-type), so we need to update the immediate generator and ImmSel.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
ImmSel
CS 61C
Summer 2022
Branch Stage 3: Execute
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
If the branch is taken, we have to compute PC + immediate. How do we send PC to the ALU?
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
ImmSel
CS 61C
Summer 2022
Branch Stage 3: Execute
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Add a new mux and control signal (ASel) so we don’t break any existing instructions.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ALUSel
ImmSel
ASel
1
0
Imm Gen
0
1
ALU
A
B
CS 61C
Summer 2022
Branch Stage 3: Execute
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
We’ve computed PC + immediate, but we didn’t compare the register values, and we ran out of hardware! Let’s add some more.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ALUSel
ALU
A
B
Imm Gen
0
1
ImmSel
ASel
1
0
CS 61C
Summer 2022
Branch Stage 3: Execute
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
The branch comparator takes in register values, and sends the comparison result to the control logic subcircuit.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ASel
Imm Gen
ImmSel
ALUSel
ALU
A
B
1
0
0
1
Branch Comp
BrEq
BrLT
BrUn
CS 61C
Summer 2022
Branch Stage 4: Memory
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Branch instructions don’t write to memory, so nothing to do here. Don’t forget to set MemWEn=0 to disable writing to memory.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ASel
Imm Gen
ImmSel
ALUSel
ALU
A
B
1
0
0
1
Branch Comp
BrEq
BrLT
BrUn
CS 61C
Summer 2022
Branch Stage 5: Register Write
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC+4
PC
Branch instructions don’t write to registers in the RegFile, but they might write PC = PC + immediate.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ASel
Imm Gen
ImmSel
ALUSel
ALU
A
B
1
0
0
1
Branch Comp
BrEq
BrLT
BrUn
CS 61C
Summer 2022
Branch Stage 5: Register Write
inst[31:0]
DMEM
MemWEn
MemReadData
MemWriteData
MemAddress
WBSel
0
1
MemRW
RegWEn
IMEM
PC
inst
+4
PC
Add a mux and control signal (PCSel) to write either PC+4 or PC+imm (ALU output) back to PC.
RegFile
RegWriteData
RegWriteIndex
RegReadIndex1
RegReadIndex2
RegReadData1
inst[11:7]
RegReadData2
inst[24:20]
inst[19:15]
RegWEn
BSel
ASel
Imm Gen
ImmSel
ALUSel
ALU
A
B
1
0
0
1
Branch Comp
BrEq
BrLT
BrUn
PCSel
0
1
PC+4
ALU
CS 61C
Summer 2022
New Control Signal: ASel
ASel
1
0
PC
RegReadData1
A (ALU input)
CS 61C
Summer 2022
New Subcircuit: Branch Comparator
Branch Comp
BrEq
BrLT
BrUn
RegReadData1
RegReadData2
CS 61C
Summer 2022
New Control Signal: PCSel
PCSel
0
1
PC+4
ALU Output
PC (register)
CS 61C
Summer 2022