RISC-V Processor Design�Control
CS61C
UC Berkeley�Teaching Professor �Dan Garcia
cs61c.org
Great Ideas
in�Computer Architecture
(a.k.a. Machine Structures)
Garcia, Kao
21-Control (1)
Garcia, FA25
Datapath Control
21-Control (2)
Garcia, FA25
Review
21-Control (3)
Garcia, FA25
Our Single-Core Processor
C
on
t
r
ol
D
a
t
ap
at
h
P
r
o
g
r
am
C
oun
t
er (PC)
R
egi
s
t
e
r
s
A
r
i
t
hm
e
t
i
c
-Logic
U
nit
(
A
L
U)
P
r
o
c
e
s
sor
Memo
r
y
Enabl
e
?
R
ead
/
W
r
i
t
e
A
dd
r
e
s
s
W
r
i
t
e
D
a
t
a
R
ead
D
a
t
a
I
n
put
Ou
t
put
B
y
t
e
s
P
r
o
g
r
am
D
a
t
a
21-Control (4)
Garcia, FA25
Single-Cycle RV32I Datapath and Control
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Control logic
0
1
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
Inst[31:0]
Imm[31:0]
RegWEn
ALUSel
21-Control (5)
Garcia, FA25
Example: sw reg, offset(regbaseptr)
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
=pc+4
=S
=0
=*
=*
=*
=1
=0
=add
=Write
=*
21-Control (6)
Garcia, FA25
Example: beq reg1, reg2, Label
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
=B
=0
=*
=*
=1
=1
=add
=Read
=*
21-Control (7)
Garcia, FA25
Instruction Timing
21-Control (8)
Garcia, FA25
Example: add rd, reg1, reg2
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
=pc+4
=*
=1
=*
=*
=*
=0
=0
=add
=Read
=1
21-Control (9)
Garcia, FA25
add Execution��e.g.,�add x1,x2,x3
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
1000
1004
PC
1004
1008
PC+4
add x1,x2,x3
add x6,x7,x9
inst[31:0]
Clock
Reg[2]
Reg[7]
Reg[rs1]
Reg[2]+Reg[3]
alu
Reg[7]+Reg[9]
Reg[3]
Reg[9]
Reg[rs2]
???
Reg[1]
Reg[2]+Reg[3]
Reg[2]+Reg[3]
wb
Reg[7]+Reg[9]
add control
add control
Control logic
21-Control (10)
Garcia, FA25
Example: add timing
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
=pc+4
=*
=1
=*
=*
=*
=0
=0
=add
=Read
=1
Critical path = tclk-q +max { tAdd+tmux , tIMEM+tReg+tmux+tALU+tmux } + tsetup
= tclk-q + tIMEM+tReg+tmux+tALU+tmux+tsetup
21-Control (11)
Garcia, FA25
Example: lw reg, offset(regbaseptr)
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
=I
=1
=*
=*
=1
=0
=add
=Read
=0
=*
=pc+4
Critical path = tclk-q +max { tAdd+tmux , tIMEM+tImm+tmux+tALU+tDMEM+tmux,
tIMEM+tReg+tmux+tALU+tDMEM+tmux }+tsetup
21-Control (12)
Garcia, FA25
Instruction Timing
IF | ID | EX | MEM | WB | Total |
I-MEM | Reg Read | ALU | D-MEM | Reg W | |
200 ps | 100 ps | 200 ps | 200 ps | 100 ps | 800 ps |
PC
clock
Instr. fetch
Instr. decode
Execute
Memory Access
pc
pc+4
old
old
old
old
old
instruction
register out
ALU result
memory data
tIF
tID
tEX
tMEM
tWB
21-Control (13)
Garcia, FA25
Instruction Timing
Instr | IF = 200ps | ID = 100ps | ALU = 200ps | MEM=200ps | WB = 100ps | Total |
add | X | X | X | | X | 600ps |
beq | X | X | X | | | 500ps |
jal | X | X | X | | | 500ps |
lw | X | X | X | X | X | 800ps |
sw | X | X | X | X | | 700ps |
21-Control (14)
Garcia, FA25
Control Logic Design
21-Control (15)
Garcia, FA25
Control Logic Truth Table
Inst[31:0] | BrEq | BrLT | PCSel | ImmSel | BrUn | ASel | BSel | ALUSel | MemRW | RegWEn | WBSel |
add | * | * | +4 | * | * | Reg | Reg | Add | Read | 1 | ALU |
sub | * | * | +4 | * | * | Reg | Reg | Sub | Read | 1 | ALU |
(R-R Op) | * | * | +4 | * | * | Reg | Reg | (Op) | Read | 1 | ALU |
addi | * | * | +4 | I | * | Reg | Imm | Add | Read | 1 | ALU |
lw | * | * | +4 | I | * | Reg | Imm | Add | Read | 1 | Mem |
sw | * | * | +4 | S | * | Reg | Imm | Add | Write | 0 | * |
beq | 0 | * | +4 | B | * | PC | Imm | Add | Read | 0 | * |
beq | 1 | * | ALU | B | * | PC | Imm | Add | Read | 0 | * |
bne | 0 | * | ALU | B | * | PC | Imm | Add | Read | 0 | * |
bne | 1 | * | +4 | B | * | PC | Imm | Add | Read | 0 | * |
blt | * | 1 | ALU | B | 0 | PC | Imm | Add | Read | 0 | * |
bltu | * | 1 | ALU | B | 1 | PC | Imm | Add | Read | 0 | * |
jalr | * | * | ALU | I | * | Reg | Imm | Add | Read | 1 | PC+4 |
jal | * | * | ALU | J | * | PC | Imm | Add | Read | 1 | PC+4 |
auipc | * | * | +4 | U | * | PC | Imm | Add | Read | 1 | ALU |
21-Control (16)
Garcia, FA25
Control Realization Options
21-Control (17)
Garcia, FA25
RV32I, A Nine-Bit ISA!
inst[6:2]
inst[14:12]
inst[30]
21-Control (18)
Garcia, FA25
Combinational Logic Control
inst[14:12]
inst[6:2]
BrUn = inst[13] ∙ Branch
21-Control (19)
Garcia, FA25
ROM-based Control
ROM
Inst[30,14:12,6:2]
BrEq
9
PCSel
ALUSel[3:0]
4
11-bit address (inputs)
BrLT
ImmSel[2:0]
3
BrUn
ASel
BSel
MemRW
RegWEn
WBSel[1:0]
2
15 data bits (outputs)
21-Control (20)
Garcia, FA25
ROM Controller Implementation
Control Word for add |
Control Word for sub |
Control Word for or |
. |
. |
. |
|
|
|
|
Address Decoder
.
.
.
Inst[]
BrEQ
BrLT
Controller output (PCSel, ImmSel, …)
add
sub
or
jal
11
AND
OR
21-Control (21)
Garcia, FA25
Control Logic to Decode add
add = i[30]•i[14]•i[13]•i[12]•R-type
inst[6:2]
inst[14:12]
inst[30]
R-type = i[6]•i[5]•i[4]•i[3]•i[2]•RV32I
RV32I = i[1]•i[0]
21-Control (22)
Garcia, FA25
“And In Conclusion…”
(drum roll)
21-Control (23)
Garcia, FA25
Call home, we’ve made HW/SW contact!
High Level Language�Program (e.g., C)
Assembly Language �Program (e.g., RISC-V)
Machine Language Program (RISC-V)
Compiler
Assembler
Hardware Architecture Description�(e.g., block diagrams)
Logic Circuit Description�(Circuit Schematic Diagrams)
Architecture Implementation
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
lw x3, 0(x10)
lw x4, 4(x10)
sw x4, 0(x10)
sw x3, 4(x10)
1000 1101 1110 0010 0000 0000 0000 0000
1000 1110 0001 0000 0000 0000 0000 0100
1010 1110 0001 0010 0000 0000 0000 0000
1010 1101 1110 0010 0000 0000 0000 0100
IMEM
ALU
Imm
.
Gen
+4
DMEM
Branch
Comp.
Reg
[]
AddrA
AddrB
DataA
AddrD
DataB
DataD
Addr
DataW
DataR
1
0
0
1
2
1
0
pc
0
1
inst
[11:7]
inst
[19:15]
inst
[24:20]
inst
[31:7]
pc+4
alu
mem
wb
alu
pc+4
Reg
[rs1]
pc
imm
[31:0]
Reg
[rs2]
wb
21-Control (24)
Garcia, FA25
21-Control (25)
Garcia, FA25
“And In conclusion…”
21-Control (26)
Garcia, FA25
Control �and Status Registers
21-Control (27)
Garcia, FA25
Complete Single-Cycle RV32I Datapath!
ALUSel
+4
Add
clk
addr
inst
IMEM
addr
DMEM
PC
pc+4
Inst[24:20]
ALU
clk
Reg [ ]
Inst[19:15]
Inst[11:7]
AddrB
AddrA
DataA
DataB
AddrD
DataD
alu
R[rs1]
R[rs2]
Inst[31:0]
Control logic
RegWEn
0
1
Imm[31:0]
Bsel
Imm.
Gen
Inst�[31:20]
ImmSel�
DataB
WBSel
0
1
MemRW
clk
0
1
Branch
Comp
mem
0
1
pc
PCSel
BrUn
BrEq
BrLT
Asel
2
21-Control (28)
Garcia, FA25
Control and Status Registers
21-Control (29)
Garcia, FA25
CSR Instructions
Register operand | ||||
Instr. | rd | rs | Read CSR? | Write CSR? |
csrrw | x0 | - | no | yes |
csrrw | !x0 | - | yes | yes |
csrrs/c | - | x0 | yes | no |
csrrs/c | - | !x0 | yes | yes |
SYSTEM
rd
instr
source/dest
source�uimm[4:0]
1110011
5
3
7
5
31
20
15
7
12
19
14
11
6
0
rs1
funct3
rd
opcode
csr
12
21-Control (30)
Garcia, FA25
CSR Instructions
Immediate operand | ||||
Instr. | rd | uimm | Read CSR? | Write CSR? |
csrrwi | x0 | - | no | yes |
csrrwi | !x0 | - | yes | yes |
csrrs/ci | - | 0 | yes | no |
csrrs/ci | - | !0 | yes | yes |
SYSTEM
rd
instr
source/dest
source�uimm[4:0]
5
3
7
5
31
20
15
7
12
19
14
11
6
0
rs1
funct3
rd
opcode
csr
12
Zero-extended to 32b
21-Control (31)
Garcia, FA25
CSR Instruction Example
21-Control (32)
Garcia, FA25
System Instructions
SYSTEM
0�0
PRIV�PRIV
ECALL�EBREAK
0�0
5
3
7
5
31
20
15
7
12
19
14
11
6
0
rs1
funct3
rd
opcode
funct12
12
21-Control (33)
Garcia, FA25