S3 · Lesson 41 · A RISC-V core in RTL
The decoder, immediate generator, register file and control of a single-cycle RV32I core
C8 drew the single-cycle processor as a datapath. This lesson writes it as hardware: a register file of flip-flops, an immediate generator that untangles RISC-V's scrambled immediate fields, a decoder that turns an opcode into control signals, and a next-PC multiplexer. The whole RV32I core fits in a few hundred lines of Verilog. Its weakness is timing: every instruction must finish in one clock, so the slowest one, a load, sets the period for all of them. Splitting the work over several shorter cycles (multicycle) or overlapping instructions (pipelining, the next lesson) are the two ways out.
- A short program on the single-cycle datapath. Step it and watch which units each instruction lights: a load uses every one, an R-type instruction skips the data memory, a store writes no register, and a branch uses the ALU to compare and an adder for the target.
- Each instruction's opcode alone decides the control signals (RegWrite, ALUSrc, MemWrite, MemToReg, Branch); funct3 and funct7 pick the ALU operation.
- The store writes 38 to memory at 0x1008, and the branch is taken because 2 < 40.
Predict
A single-cycle core's load takes 945 ps from PC to register write, and an add takes 695 ps. What's the shortest clock period?
945 ps. The clock can't know which instruction is coming, so every cycle must be long enough for the slowest, the load, which passes through both the instruction and the data memory. An add then wastes 250 ps of every cycle doing nothing. That waste is what multicycle and pipelined designs recover.
Name it
The parts in RTL
- Register file: 32 registers of 32 bits with two read ports (combinational) and one write port (on the clock edge). x0 always reads 0, so only 31 registers need flip-flops.
- Immediate generator: RISC-V keeps rs1, rs2 and rd in the same bit positions in every format, and pays for it by scattering the immediate bits. The sign is always bit 31:
wire [31:0] imm_i = {{20{ins[31]}}, ins[31:20]};
wire [31:0] imm_s = {{20{ins[31]}}, ins[31:25], ins[11:7]};
wire [31:0] imm_b = {{19{ins[31]}}, ins[31], ins[7], ins[30:25], ins[11:8], 1'b0};
wire [31:0] imm_u = {ins[31:12], 12'b0};
- Decoder: a combinational case on the opcode:
always @(*) begin
{reg_write, alu_src, mem_write, mem_to_reg, branch} = 5'b0;
case (opcode)
7'b0110011: reg_write = 1; // R-type
7'b0010011: begin reg_write = 1; alu_src = 1; end // I-type ALU
7'b0000011: begin reg_write = 1; alu_src = 1; mem_to_reg = 1; end // load
7'b0100011: begin alu_src = 1; mem_write = 1; end // store
7'b1100011: branch = 1; // branch
endcase
end
- Next PC: PC + 4, or PC + imm_b for a taken branch, PC + imm_j for jal, (rs1 + imm_i) with bit 0 cleared for jalr.
Encodings to know
S-type (stores): imm[11:5] in bits 31:25, rs2 in 24:20, rs1 in 19:15, funct3 in 14:12, imm[4:0] in 11:7, opcode 0x23. A 12-bit immediate field f means if , else .
Timing and CPI
Execution time = instructions × CPI × clock period.
- Single cycle: CPI = 1, period = the slowest instruction's path, .
- Multicycle: each instruction takes one cycle per step it needs (load 5, store 4, ALU 4, branch 3), and the period is the slowest step plus register overhead. CPI is the mix's weighted average.
Worked example
A core has these delays: PC clock-to-Q 50 ps, instruction memory 250, register read 150, ALU 200, data memory 250, write-back mux 25, setup 20. Find the single-cycle period, and compare a multicycle version with steps limited by the 250 ps memories and 70 ps of register overhead, on a mix of 25% loads, 10% stores, 45% ALU and 20% branches.
Show the solution
- Single-cycle period. The load path is the longest: which is 1.06 GHz.
- Multicycle period.
- Multicycle CPI. so ps per instruction on average, slower than single-cycle.
- What multicycle was for. Its real advantage was reusing one memory and one adder; pipelining keeps the short period and brings CPI back near 1.
Answer 945 ps single-cycle (1.06 GHz), against 1296 ps per instruction for the multicycle version.
Worked example
Encode sw x7, 100(x5) as eight hex digits.
Show the solution
- Split the immediate. so imm[11:5] = 3 and imm[4:0] = 4.
- Place the fields. rs2 is 7, rs1 is 5 and funct3 for sw is 2:
Answer 0x0672A223.
Worked example
Encode addi x5, x5, -10 (I-type: imm[11:0] in bits 31:20, rs1 in 19:15, funct3 = 0 in 14:12, rd in 11:7, opcode 0x13).
Show the solution
- The immediate field. −10 as a 12-bit field is
- Shift the fields into place.
- Add.
- The leading F's. They are the sign: any negative small immediate starts that way.
Answer 0xFF628293.
Check
Encode sw x6, 1000(x2) (S-type, funct3 = 2, opcode 0x23).
Check
A 12-bit immediate field holds 2047 (unsigned). What value does the immediate generator produce after sign extension?
Check
A single-cycle core: clock-to-Q 40, instruction memory 200, register read 180, ALU 250, data memory 200, write-back mux 20, setup 20 ps. What's the clock period?
Write units the way you normally would: 1.5 ps and 0.0015 ns are the same answer.
Check
A multicycle core takes 5 cycles for loads, 4 for stores and ALU instructions, and 3 for branches. With 0.2 loads, 0.1 stores, 0.16 branches and the rest ALU, what's the CPI?
Check
A program runs 1 billion instructions at a CPI of 1.4 with a 1 ns clock. How long does it take?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A register file has 16 registers of 32 bits, with register 0 hard-wired to zero. How many flip-flops does it need?
Check
A taken branch at address 0x00000400 has a B-type offset of −64. What's the target address?
Check
A multicycle core (load 5 cycles, store 4, ALU 4, branch 3) runs a mix of 0.2 loads, 0.1 stores and 0.2 branches, the rest ALU, with a 400 ps clock. Separately, an I-type instruction's 12-bit immediate field reads 3000.
(a)
What is its CPI?
(b)
How long do 10 million instructions take?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(c)
What value does the immediate field encode?
Check
A program of i instructions runs at CPI c with clock period t on one core, and at CPI d with period u on another.
Step 1
How long does it take on the first core?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How many instructions per second does the first core complete?
Step 3
How much faster is the second core?
Explain why
Why does RISC-V scatter the bits of the store and branch immediates instead of keeping them in one field?
In every RISC-V format the register fields rs1, rs2 and rd sit in the same bit positions, so the register file can start reading its two source registers before the decoder has even worked out what kind of instruction it is. Stores and branches have no rd but need rs2, so their immediate bits have to go around the fixed register fields, filling the spaces where rd and funct7 would be. The sign bit is always bit 31, so sign extension can begin at once too. The cost is a little wiring in the immediate generator, which is cheap, while keeping the register reads off the critical path saves time on every instruction.
Practice
Keep practicing
11 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.