RISC Microarchitecture: part 5 of the course
Released — the 20 levels 48–67 are playable in the game, with guides in the library.
After 47 levels you can build a computer that runs a program. The next step is course part 5, the RISC Lab module: build a 32-bit RISC processor on an RV32I teaching subset in Verilog and take it to a five-stage pipeline — with forwarding, load bubbles, and wrong-path flushes that you detect and resolve yourself.
What changes after Part 4
In Parts 1–4 you built the processor around an accumulator: almost every instruction read it, did something with it, and wrote it back. That is convenient for the first steps and inconvenient for speed, because two instructions cannot work at once while both hold the same register.
Part 5 rebuilds the machine around a different model:
- A register file instead of an accumulator. 32 registers
x0–x31;x0always reads as zero, and writes to it are ignored. Every instruction names its operands explicitly: two sourcesrs1,rs2and a destinationrd. - An RV32I teaching subset. 32-bit instructions and data, the standard R/I/S/B/U/J encodings, 26 instructions — from
addandlwtojal. This is not full RV32I: no CSR, exceptions, or byte-wide accesses in the first version. - Memory through load/store only. Arithmetic works on registers;
lwandswmove values between registers and memory. Byte addressing,PC+4, four-byte alignment. - Verilog instead of a canvas. You write the Control, Forwarding, and Hazard Units in Verilog yourself: blocks with ports and tests, not prebuilt schemes. The RTL Viewer shows them as macro blocks you can drill into.
- Five stages. Fetch, decode, execute, memory access, write-back: IF — ID — EX — MEM — WB. One instruction goes through all five while its neighbour sits in the next one.
The difference shows on a simple example. The expression (A + B) − (C + D) on the course machine is a chain through the accumulator: load, add, store to a temporary cell, load again. On a register machine, with the values already in registers, it is three instructions — add, add, sub — and the two adds no longer share one register, so a pipeline can overlap them. Part 5 shows what that takes.
Three principles of the stage
The Part 5 levels work differently from those in Parts 1–2. There, the check compared a circuit against a truth table. Here the mistake is usually not in a single gate but in how the stages agree with each other, and it shows only in behaviour over time.
- Productive failure. First you build a naive pipeline and run a real program on it. The result is wrong. Then you look for the cause in the waveform: which stage, on which cycle, received the wrong value. Before the check you predict the result or the trace — the rule appears as a conclusion from observation, not as a ready answer in the task.
- Open control logic. The Control Unit, Forwarding Unit, and Hazard Unit are Verilog blocks you write yourself. What counts as "the source is ready", "a conflict", or "a bubble" is something you state in terms of signals rather than take on trust.
- Engineering trade-off. A pipeline is not free: it speeds up some programs and complicates the circuit. Every step tracks two metrics — cycles per instruction (CPI) and circuit cost: propagation delay and gate count.
The route: twenty levels
The route has two independent arcs. The first is the single-cycle core: from the register file to a working processor running a program on the LEDs. The second is the pipeline: the same core cut into five stages, with three kinds of hazard, one per level.
Single-cycle core — levels 48–60
Farewell, Accumulator!
From the accumulator to a register machine: 32 registers, x0 hardwired to zero, three addresses per instruction.
Anatomy of an R-type Instruction
The fields of a 32-bit instruction and a 32×32 register file: two reads and one write per cycle.
A 32-bit ALU
Arithmetic, logic, shifts, signed and unsigned comparisons, and the Zero flag for branches.
A Constant Inside the Instruction
The I-format, sign extension, and a mini-assembler that turns mnemonics into machine words.
Memory by the Rules
A load/store architecture: arithmetic on registers only, LW/SW, and four-byte alignment.
A Loop on BNE
A counter, a comparison, and BNE: the first loop on your own machine, with a cycle-count prediction.
Array Sum
A real program: the array base via LUI+ADDI, a walk over elements with LW, and a running sum.
Call and Return
JAL and JALR: the return address in ra, functions, and a nested call that saves ra in memory.
Nested Calls and the Stack
ra and sp, the function prologue and epilogue, nested calls: the frame grows downward through memory.
Assembling RV32I
The complete single-cycle core of the RV32I teaching subset: PC, instruction memory, decoder, registers, ALU, data memory, control.
Knight Rider RISC
A running light on the LEDs through MMIO, plus a project export for the Tang Nano 9K.
The Verification Lab
Scenario verification: inputs, expected states, a trap for a planted bug, and a regression run after the fix.
The pipeline — levels 61–67
Why the Cycle Is Long
The single-cycle critical path, a map of the five stages, and the price of every cycle.
Five Instructions in Flight
Reading the trace: the pipeline diagram, fill and drain, and which instruction is where on cycle k.
The Short Path
RAW forwarding: the ALU result feeds the next instruction directly, with priorities and the x0/lw exceptions.
The Load Bubble
The load-use hazard that cannot be forwarded: the detector inserts exactly one bubble, freezing PC and IF/ID.
Ghosts of the Wrong Path
The branch resolves in EX: a taken branch flushes two ghosts fetched along the old path — a 2-cycle price.
Knight Rider Pipeline
The course finale: Knight Rider on the full pipelined core, hazard prices in cycles, and CPI compared with the single-cycle machine.
Learning outcomes
After the stage a student can:
- read a 32-bit RV32I instruction and name its fields;
- build a single-cycle RISC core: PC, instruction memory, decoder, register file, ALU, data memory, control;
- explain the load/store model and write loops and functions with branches and calls;
- cut the core into five stages and explain what the pipeline registers hold and why control signals and
rdride along with the data; - tell apart the three kinds of hazard — RAW, load-use, and control — and not confuse their causes;
- derive forwarding, stall, and flush rules from observed behaviour instead of memorising a list;
- compute CPI and explain why a pipeline does not speed the machine up fivefold;
- carry the model over to any RISC-like instruction set.
For teachers
Part 5 follows Part 4 and builds on it: by then the student has already built a control unit, a custom instruction set, and a datapath in Verilog. The pipeline introduces no new gates; it introduces a new requirement on how familiar blocks are organised.
The module splits into three lessons: the single-cycle RISC (48–60), the pipeline (61–63), and hazards (64–67). The key check of understanding is that the same program (a counter, Fibonacci, an array sum) gives the same result on the single-cycle and the pipelined core, while the modes without forwarding, stall, or flush fail on it.
Open control logic gives a second point of assessment: the forwarding rule, the stall condition, and the flush point must be written in terms of the circuit's signals. Such a rule can be tested on new programs, not only on the one where it was invented.
The methodological overview of the trajectory is the "RISC Lab: levels 48–67" teacher card.
Status and scope
- Released: levels 48–67 are playable in the level map and are part of the curriculum with guides.
- The guides for levels 48–67 follow the unified library format (Goal / What you should get / Check yourself) and are indexed by search engines.
- Fixed: an RV32I teaching subset — 32 bits, 32 registers, load/store, five stages; of the 26 instructions, 20 are built from scratch and 6 are given as ready-made examples to study.
- Part 5 progress is tracked separately (the risc sync) and grants its own Part 5 certificate; the base-course certificate is counted over levels 1–47.
- Caches, virtual memory, out-of-order execution, superscalarity, and branch prediction (beyond the teaching "not taken" model) are out of scope; the delays in the performance model are teaching values, not synthesis analysis results.
- The target board of the final levels is the Tang Nano 9K; the project sources are exported, and no ready-made board firmware is built.
Next steps
- Course Curriculum — the timeline of all 67 levels.
- The level map in the game — course parts, including the Part 5 tab.
- Verilog and FPGA — how a circuit becomes a chip on a board.
- Level 47 "Create Computer" — the finale of the current course.