---
title: A RISC-V core in RTL
heading: The decoder, immediate generator, register file and control of a single-cycle RV32I core
eyebrow: S3 · Lesson 41 · A RISC-V core in RTL
xp: 50
---

::: hook
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.
:::

::: play
- 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.
:::
:::

```sim cpu
.data
a: .word 40, 2, 0
.text
    la   t0, a
    lw   t1, 0(t0)        # load: RegWrite, ALUSrc, MemToReg
    lw   t2, 4(t0)
    sub  t3, t1, t2       # R-type: funct7 picks subtract
    slt  t4, t2, t1       # set if less than
    sw   t3, 8(t0)        # store: MemWrite, no RegWrite
    bne  t4, zero, done   # branch: compare, then PC + immediate
    addi t3, t3, 1        # skipped
done:
    ebreak
view: datapath
show: t1, t2, t3, t4
hex: t0
memory: 0x1000..0x100c
speed: 1
```

::: 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?

- [ ] 695 ps, the common case
- [ ] 820 ps, the average
- [x] 945 ps, the slowest instruction
- [ ] 1640 ps, both together

::: reveal
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.
:::
:::

::: nameit
## 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:

```verilog
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:

```verilog
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 $f$ if $f < 2048$, else $f - 4096$.

## Timing and CPI

Execution time = instructions × CPI × clock period.

- **Single cycle:** CPI = 1, period = the slowest instruction's path, $t_{cq} + t_\text{imem} + t_\text{reg} + t_\text{ALU} + t_\text{dmem} + t_\text{mux} + t_\text{setup}$.
- **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.
:::

::: 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.

::: solution
1. **Single-cycle period.** The load path is the longest:
   $$\begin{aligned} T &= 50 + 250 + 150 + 200 \\ &\quad + 250 + 25 + 20 \\ &= 945\ \text{ps} \end{aligned}$$
   which is 1.06 GHz.
2. **Multicycle period.**
   $$250 + 70 = 320\ \text{ps}$$
3. **Multicycle CPI.**
   $$\begin{aligned} \text{CPI} &= 0.25 \times 5 + 0.10 \times 4 \\ &\quad + 0.45 \times 4 + 0.20 \times 3 \\ &= 4.05 \end{aligned}$$
   so $4.05 \times 320 = 1296$ ps per instruction on average, slower than single-cycle.
4. **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.
:::
:::

::: example
## Encode `sw x7, 100(x5)` as eight hex digits.

::: solution
1. **Split the immediate.**
   $$100 = \text{0b0000011\_00100}$$
   so imm[11:5] = 3 and imm[4:0] = 4.
2. **Place the fields.** rs2 is 7, rs1 is 5 and funct3 for sw is 2:
   $$\begin{aligned} &3 \cdot 2^{25} + 7 \cdot 2^{20} \\ &+ 5 \cdot 2^{15} + 2 \cdot 2^{12} \\ &+ 4 \cdot 2^7 + \text{0x23} \\ &= \text{0x0672A223} \end{aligned}$$

**Answer:** 0x0672A223.
:::
:::

::: 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).

::: solution
1. **The immediate field.** −10 as a 12-bit field is
   $$\begin{aligned} &4096 - 10 \\ &= 4086 \\ &= \text{0xFF6} \end{aligned}$$
2. **Shift the fields into place.**
   $$\begin{aligned} &(\text{0xFF6} \ll 20) + (5 \ll 15) \\ &+ (0 \ll 12) + (5 \ll 7) + \text{0x13} \end{aligned}$$
3. **Add.**
   $$\begin{aligned} &\text{0xFF600000} + \text{0x28000} \\ &+ \text{0x280} + \text{0x13} \\ &= \textbf{0xFF628293} \end{aligned}$$
4. **The leading F's.** They are the sign: any negative small immediate starts that way.

**Answer:** 0xFF628293.
:::
:::

::: check
given: rs2 = 6 | 9; rs1 = 2 | 8; imm = 20 | 100 | 1000
answer: floor(imm/32)*2^25 + rs2*2^20 + rs1*2^15 + 2*2^12 + (imm - 32*floor(imm/32))*2^7 + 35
base: 16
width: 8

## Encode `sw x{rs2}, {imm}(x{rs1})` (S-type, funct3 = 2, opcode 0x23).

::: right
Right. imm[11:5] in 31:25, rs2, rs1, funct3, imm[4:0] in 11:7, opcode.
:::

::: misses
- [imm*2^20 + rs1*2^15 + 2*2^12 + rs2*2^7 + 35] That's the I-type layout; stores split the immediate so rs2 can keep its place.
- [any] Not yet. Split the immediate into bits 11:5 and 4:0.
:::
:::

::: check
given: f = 100 | 2047 | 2048 | 3000 | 4095
answer: f - 4096*step(f - 2047.5)
tolerance: 0

## A 12-bit immediate field holds {f} (unsigned). What value does the immediate generator produce after sign extension?

::: right
Right. {answer}.
:::

::: misses
- [any] Not yet. Bit 11 is the sign: subtract 4096 if it's set.
:::
:::

::: check
given: c = 40 | 60; i = 200 | 300; r = 120 | 180; a = 150 | 250; d = 200 | 350; m = 20 | 30; s = 20
answer: c + i + r + a + d + m + s
unit: ps
tolerance: 0.5%

## A single-cycle core: clock-to-Q {c}, instruction memory {i}, register read {r}, ALU {a}, data memory {d}, write-back mux {m}, setup {s} ps. What's the clock period?

::: right
Right. The load's path: {answer} ps.
:::

::: misses
- [c + i + r + a + m + s] The load also passes through the data memory, and it sets the period.
- [any] Not yet. Follow a load from PC to register.
:::
:::

::: check
given: l = 0.2 | 0.3; s = 0.1 | 0.15; b = 0.12 | 0.16
answer: 5*l + 4*s + 3*b + 4*(1 - l - s - b)
tolerance: 0.5%

## A multicycle core takes 5 cycles for loads, 4 for stores and ALU instructions, and 3 for branches. With {l} loads, {s} stores, {b} branches and the rest ALU, what's the CPI?

::: right
Right. The weighted average: {answer}.
:::

::: misses
- [(5 + 4 + 4 + 3)/4] Weight each class by how often it occurs.
- [any] Not yet. Sum fraction × cycles.
:::
:::

::: check
given: N = 1 | 5; C = 1.4 | 2.2 | 4; T = 0.5 | 1
answer: N*1e9*C*T*1e-9
unit: s
tolerance: 0.5%

## A program runs {N} billion instructions at a CPI of {C} with a {T} ns clock. How long does it take?

::: right
Right. $N \cdot \text{CPI} \cdot T$ = {answer} s.
:::

::: misses
- [N*1e9*T*1e-9] Include the CPI.
- [any] Not yet. Instructions × cycles per instruction × period.
:::
:::

::: check
given: R = 16 | 32; W = 32 | 64
answer: (R - 1)*W
tolerance: 0

## A register file has {R} registers of {W} bits, with register 0 hard-wired to zero. How many flip-flops does it need?

::: right
Right. $(R - 1)W$ = {answer}.
:::

::: misses
- [R*W] Register 0 needs no storage.
- [any] Not yet. Every register but x0.
:::
:::

::: check
given: p = 256 | 1024 | 4096; o = -64 | 24 | 200
answer: p + o
base: 16
width: 8

## A taken branch at address 0x{p:hex8} has a B-type offset of {o}. What's the target address?

::: right
Right. The offset is added to the branch's own address.
:::

::: misses
- [p + 4 + o] Branch offsets are relative to the branch itself, not to PC + 4.
- [any] Not yet. Add the signed offset to the branch's address.
:::
:::

::: check
kind: parts
given: a = 0.2 | 0.3; b = 0.1 | 0.15; c = 0.1 | 0.2; p = 300 | 400; n = 10 | 100; f = 2048 | 3000 | 4000 | 4095

## A multicycle core (load 5 cycles, store 4, ALU 4, branch 3) runs a mix of {a} loads, {b} stores and {c} branches, the rest ALU, with a {p} ps clock. Separately, an I-type instruction's 12-bit immediate field reads {f}.

::: part
answer: 5*a + 4*b + 3*c + 4*(1 - a - b - c)
tolerance: 0.5%

### What is its CPI?

::: misses
- [any] Weight each class's cycle count by its share.
:::
:::

::: part
answer: n*1e6*(5*a + 4*b + 3*c + 4*(1 - a - b - c))*p*1e-12*1000
unit: ms
tolerance: 0.5%

### How long do {n} million instructions take?

::: misses
- [any] Instructions × CPI × period.
:::
:::

::: part
answer: f - 4096*step(f - 2048)
tolerance: 0

### What value does the immediate field encode?

::: misses
- [f] Bit 11 is the sign: fields of 2048 or more are negative.
- [any] f if f < 2048, else f − 4096.
:::
:::
:::

::: check
kind: derivation
vars: i = 1e6..1e10, c = 1..5, t = 1e-10..1e-8, d = 1..5, u = 1e-10..1e-8

## 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
answer: i*c*t

### How long does it take on the first core?
:::

::: step
answer: 1/(c*t)

### How many instructions per second does the first core complete?
:::

::: step
answer: (c*t)/(d*u)

### How much faster is the second core?
:::

::: right
Right. Neither CPI nor clock alone decides speed: only their product does.
:::
:::

::: explain
## Why does RISC-V scatter the bits of the store and branch immediates instead of keeping them in one field?

::: model
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

::: check
given: rs2 = 4 | 12; rs1 = 3 | 10; imm = 40 | 500
answer: floor(imm/32)*2^25 + rs2*2^20 + rs1*2^15 + 2*2^12 + (imm - 32*floor(imm/32))*2^7 + 35
base: 16
width: 8

## Encode `sw x{rs2}, {imm}(x{rs1})`.

::: misses
- [any] imm[11:5], rs2, rs1, 010, imm[4:0], 0100011.
:::
:::

::: check
given: f = 50 | 2049 | 3500 | 4094
answer: f - 4096*step(f - 2047.5)
tolerance: 0

## Sign-extend the 12-bit field {f}?

::: misses
- [any] Subtract 4096 if f ≥ 2048.
:::
:::

::: check
given: l = 0.25 | 0.35; s = 0.08 | 0.12; b = 0.12 | 0.18
answer: 5*l + 4*s + 3*b + 4*(1 - l - s - b)
tolerance: 0.5%

## Multicycle CPI with {l} loads, {s} stores, {b} branches?

::: misses
- [any] Weighted average of 5, 4, 3 and 4.
:::
:::
:::
