D10 · Lesson 13 · Capstone
A single-cycle RISC-V core described as hardware, the datapath of program counter, register file, ALU and memory, the decoder that pulls fields and immediates out of each instruction, the controller that sets the multiplexers, and a cycle-accurate model in Python that runs a real program
C8 taught RISC-V from the programmer's side: instructions, registers and memory. This capstone builds the other side, the hardware that executes those instructions. A single-cycle core does an entire instruction every clock: fetch it from memory, decode its fields, read two registers, compute with the ALU, touch data memory if needed, and write the result back, all before the next edge. It's the datapath-and-controller design of lesson 3 at full size, and its Verilog fits on two pages. Here you'll model it cycle by cycle in Python and run a real program on it, checked against the reference simulator below.
- The program the core will run, on the course's RISC-V simulator: it points a0 at the data area (0x1000), sums 10 + 9 + ... + 1 into t1, stores and reloads it, and then exercises slt, sub, and and or before halting with a jump to itself.
- Step through it and watch the registers. Your Python core must end in exactly the same state, after the same 39 instructions.
- Each instruction here takes one clock on a single-cycle core.
Predict
A single-cycle core's slowest instruction (lw) needs 0.3 ns to fetch, 0.2 ns to decode, 1.0 ns to read registers and compute the address, 1.2 ns of data memory, 0.3 ns of multiplexing and 0.2 ns to write back. What's its fastest clock?
Everything for the slowest instruction must fit in one cycle: 0.3 + 0.2 + 1.0 + 1.2 + 0.3 + 0.2 = 3.2 ns, so 312.5 MHz, even though an add needs much less. That waste is why real cores are pipelined (lesson 9, C8): split into five stages, the clock could approach the slowest stage, 1.2 ns plus overhead.
Name it
The datapath
- A program counter register, incremented by 4 or loaded with a branch or jump target.
- Instruction memory read at the PC.
- A register file of 32 × 32 bits with two read ports and one write port; x0 always reads 0.
- An immediate generator that assembles and sign-extends the immediate for each format: I (12 bits from 31:20), S (31:25 and 11:7), B (a 13-bit offset with bit 0 zero), J (a 21-bit offset).
- An ALU selected by funct3 and funct7: add or subtract, and, or, set-less-than.
- Data memory for lw and sw, and a write-back multiplexer choosing the ALU result, the loaded word or PC + 4.
The controller
A decoder maps the opcode to control signals: whether to write a register, use the immediate as the ALU's second operand, read or write memory, branch, or jump. In Verilog it's a combinational always @(*) with a case on the opcode (lesson 5).
The Verilog
always @(posedge clk) begin
pc <= next_pc;
if (reg_write && rd != 0) regs[rd] <= wb_value;
if (mem_write) dmem[alu_out[31:2]] <= regs[rs2];
end
assign next_pc = jump ? pc + imm_j
: (branch && taken) ? pc + imm_b
: pc + 4;
Running it
A single-cycle core has a CPI of exactly 1, so a program's time is its instruction count times the clock period: 39 instructions at 50 MHz take 780 ns.
Worked example
Pull the fields out of add t1, t1, t0, encoded 0x00530333.
Show the solution
- Opcode. Bits 6:0 are 0x33, an R-type instruction.
- Destination. rd = bits 11:7 = 6 (t1).
- Sources. rs1 = bits 19:15 = 6 (t1) and rs2 = bits 24:20 = 5 (t0).
- Function codes. funct3 = 0 and funct7 = 0, so the instruction is add (a funct7 of 0x20 would be sub).
Answer opcode 0x33 (R-type), rd = 6, funct3 = 0, rs1 = 6, rs2 = 5 and funct7 = 0: an add.
Worked example
Why does the loop run 30 instructions, and how many does the whole program run?
Show the solution
- The loop. The body (add, addi, bne) runs once for each value of t0 from 10 down to 1:
- The whole program. Add 3 instructions to set up and 6 after the loop: before it reaches the self-jump that halts it.
Answer the loop runs 30 instructions, and the whole program 39.
Worked example
The single-cycle core runs at 50 MHz. How long does the 39-instruction program take?
Show the solution
- One instruction per cycle. At 50 MHz each cycle lasts 20 ns.
- Run time.
- A pipeline. A five-stage pipeline could clock faster, but taken branches and the loop's dependent instructions would add stalls.
Answer 0.78 µs.
Check
Build the core: decode instructions, execute them, and run the program.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Which register does the instruction 00530333 write? (rd is bits 11:7.)
Check
A program spends 2 instructions setting up, runs a 3-instruction loop 10 times and then 6 more instructions. How many instructions does it execute?
Check
A single-cycle core runs 1000 instructions at 25 MHz. How long does it take?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
A single-cycle lw takes 0.2 ns to fetch, 0.2 ns to decode, 0.8 ns for registers and the ALU, 2 ns of data memory, 0.3 ns of multiplexing and 0.2 ns to write back. What's the fastest clock?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
A single-cycle core runs 100 instructions at 25 MHz.
(a)
How long does it take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
(b)
A pipelined version clocks 1.5 times faster but needs a CPI of 1.3. How long does it take?
Check
A loop body of 3 instructions runs 4 times, after 3 setup instructions and before 6 more.
(a)
How many instructions run in all?
(b)
What share is the loop?
Explain why
Why is a single-cycle CPU simple to design but slow to run?
A single-cycle core finishes every instruction within one clock period, so it needs no pipeline registers, no hazard detection and no forwarding: each instruction simply flows through fetch, decode, register read, ALU, memory and write-back before the next edge, and the controller is one combinational decoder. But the clock must be long enough for the slowest instruction, usually a load, which passes through both instruction and data memory, so simple instructions like add waste most of every cycle waiting. Pipelining splits that path into stages so different instructions overlap, letting the clock run several times faster at the cost of the hazards that C8 and S4 deal with.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.