This section of the Computer Architecture tutorial is hands-on: you design and build a working RISC-V processor core yourself, one block at a time, and run real compiled C programs on it. The first half of the tutorial explained how processors work in general; here you turn that understanding into hardware. You start with an empty module for each block, write the logic, and a hidden set of tests in ChipVerify Lab tells you whether it is right. Block by block, your own modules come together into a single-cycle core named cvr32, and then into a 3-stage pipeline. This article explains what you will build, how the exercises work, and what you need before you start.
16 min read | Beginner to Intermediate
Prerequisite: Verilog. In this section you write every line of the core yourself, so you need to know Verilog first: modules with ports and parameters, always blocks with blocking and nonblocking assignments, case statements, vectors and bit selects, and how to read a simulation log. If any of that is new, work through the Verilog Tutorial first and come back.
What You'll Learn
- Describe the core you will build in this section and the order in which you build it
- Explain how an exercise works: an empty module, a specification, hidden tests and their messages
- Explain how your own blocks become a complete core in the integration exercises
- Prepare your lab project and know what to read before the first exercise
Before You Start
You need Verilog for this section; it is a firm prerequisite, because no exercise gives you the code. The Verilog Tutorial covers everything used here. The core is written in the small part of SystemVerilog that every simulator understands (logic, always_ff, always_comb), and The Core's Specification explains each of those words the first time it appears.
You do not need to have read all of the first half of this tutorial, but four of its articles describe exactly what you are about to build, and the exercises assume them: The Instruction Cycle (what a processor does for every instruction), The Datapath: ALU, Multiplexers and Control (the blocks and the signals between them), RISC-V Instruction Formats and Encoding (how an instruction's bits are laid out) and Single-Cycle and Multi-Cycle Processors. Each article in this section names the ones it relies on.
What You Will Build
A processor core is the part of a chip that fetches instructions from memory and carries them out. The one you build runs RV32I, the basic 32-bit integer instruction set of RISC-V: arithmetic and logic on 32-bit values, loads and stores of bytes, halfwords and words, branches and jumps. That is everything a C compiler needs to produce working programs, and it is the same instruction set real microcontroller cores implement as their base.
You build it in two forms. The first is a single-cycle core: each instruction is fetched, decoded, executed, given its memory access and written back within one clock cycle. It is the simplest correct design, and it has a useful property for learning: everything an instruction does is visible in one cycle of the waveform. When something goes wrong, you look at one cycle and see the whole instruction.
The second form is a 3-stage pipeline. Pipelining splits the work for one instruction into stages, so that while one instruction is being executed the next one is already being fetched. You split your single-cycle core into three stages, add the registers that sit between them, and then deal with the two problems pipelining creates: an instruction that needs a result its neighbour has not finished writing (a data hazard), and a branch that is decided after the wrong instruction has already been fetched (a control hazard). You fix the first with a forwarding path and the second with a flush. The 5-Stage Pipeline explains the ideas with five stages; three stages show the same problems with less logic.
The core is called cvr32. The name only matters because the hidden tests look for it: every module you write has a fixed name such as cvr32_alu or cvr32_regfile, and a fixed list of ports.
Why Build a Core Yourself
Reading about a datapath and building one are different kinds of knowledge. When you read that the register file has two read ports and one write port, it sounds obvious. When you build it, you have to decide what a read returns in the same cycle as a write to the same register, and later you find out that this one decision determines whether your pipeline needs a forwarding path. Every block has decisions like that, and the exercises are arranged so that you meet them one at a time.
There is a second reason. Processor design is a field where people are judged by what they have built. A core you wrote from empty modules, that runs a compiled matrix multiply and a linked-list walk, and that you then pipelined and measured, is something you can explain line by line in an interview. That is worth far more than having read about one.
The third reason is the one most people discover halfway through: debugging. Most of the time spent on a real core goes into finding out why a program prints the wrong thing. The exercises' tests are written to point at the first thing that went wrong, which is how professional processor verification works too (Verifying a CPU describes it). By the end you will have practised it many times.
How the Articles and Exercises Work
Each article in this section is a design brief. It explains what one block does and why, gives its port list, describes its behavior in a table or a timing diagram, says how it will be tested, and lists the mistakes people usually make. It does not show the answer. The code is yours to write.
Each article then sends you to an exercise in ChipVerify Lab. An exercise opens in the lab's editor with an empty module: the name and the ports are given, the body is empty. The exercise statement next to the editor repeats the specification. You write the logic, press Submit Solution, and the lab compiles your code with a hidden testbench and runs it. A testbench is a piece of Verilog that drives a design's inputs and checks its outputs; "hidden" means you do not see its code, only its verdict.
Next to your design you also get one visible test, testbench.v: for a block, a short list of directed checks; for a core, one of the test programs. You can simulate it as often as you like, look at the waveform and add your own checks. When you submit, it runs together with the hidden tests, and nothing you change in it affects the grading.
The verdict is specific. A test does not just say "fail"; it says what it expected and what it got. Here is a real message from a pipeline exercise, produced by a core whose branch logic used a stale register value:
FAIL: register write 18: expected x16 = 0x00000007, got x13 = 0xf0000000
It says that the eighteenth register write of the test program went wrong: the correct core writes 7 into register x16 at that point, but this one wrote to x13, because it had jumped past the instruction that writes x16. From that one line you know where to look in the waveform. Writing tests like this is a large part of the work behind the exercises: for each one, the tests were run against a correct reference design and against a set of typical wrong solutions, and they had to pass the first and catch every one of the others.
Quick Check
Q: An exercise gives you module cvr32_alu (input logic [3:0] op, input logic [31:0] a, b, output logic [31:0] y); and an empty body. You think the port op would be clearer as alu_op. Should you rename it?
Show Answer
No. The hidden testbench connects to the ports by name, so a renamed port does not connect and every test fails. Module names and port lists are part of the specification; everything inside the module is yours.
The Exercise Ladder
The section has seventeen graded exercises in three chapters after this one. Each builds on the ones before it:
| Chapter | Exercises | What you build |
|---|---|---|
| Building Blocks | E01 to E05 | the ALU (in two parts), the register file, the immediate generator, the instruction decoder |
| The Single-Cycle Core | E06 to E12 | the program counter, the load/store unit and the branch comparator, and the core itself, which first runs ALU instructions, then loads and stores, then branches and compiled C, then every test program |
| Pipelining the Core in Three Stages | E13 to E17 | the pipeline registers, the forwarding logic, the core with forwarding, the core with a flush, and a synthesis check of the finished pipeline |
There are two kinds of exercise. A block exercise tests one module on its own: the ALU is given thousands of operand pairs, the register file is written and read in every pattern that matters, the decoder is shown random instructions of every kind. A core exercise tests your whole core so far: it loads a program into memory, releases reset and watches what your core does.
Building the Core From Your Own Blocks
The core exercises are where your blocks meet. For a core exercise you submit one file that contains all of your own modules, the versions that passed their block exercises, plus the top-level module cvr32_core that connects them. Nothing is supplied by the test except the memory, the program and the testbench. When your core prints "Hello from cvr32!", it is your ALU that computed every address, your decoder that recognised every instruction and your register file that held every value.
That has a consequence worth knowing in advance: a mistake that slipped through a block exercise can show up later. A block test checks the block against its specification, but a real program can use the block in a way you did not think of. When a core test fails, suspect your newest code first, and your blocks second.
Core tests compare your core with a reference core instruction by instruction. Every time your core writes a register or stores to memory, the test checks that write against what the reference core wrote at the same point of the program. That is why one rule applies to every core exercise: the register file inside cvr32_core must be an instance named u_rf, because the test watches its write port. Apart from that and the port lists, the inside of your core is up to you.
Keep all your modules in one project in the lab, one file per module. When you pass an exercise, save that file. When a core exercise asks for "all your modules", you copy them from there.
What Your Finished Core Does
At the end of the single-cycle chapter your core runs seven C programs compiled with gcc: a greeting printed character by character, an array sum, a memory copy, a bubble sort, a matrix multiply, a linked-list walk and a CRC checksum. Each prints or returns a result that the test compares with the same C code run on a PC. A single-cycle core completes exactly one instruction per clock cycle, so its cycle count equals its instruction count: the matrix multiply takes 90,514 cycles for 90,514 instructions.
The pipelined core runs the same programs in more cycles but with a shorter clock. On the same matrix multiply it needs 111,083 cycles, because every taken branch and every jump costs one extra cycle, and two cycles are spent filling the pipeline at the start. The pipeline wins on time anyway, because each cycle is shorter: in a synthesized and placed version of both cores on an open 130 nm cell library, with an assumed memory read time, the single-cycle core needs a clock period of 14.373 ns and the 3-stage core 8.96 ns. The matrix multiply finishes about 1.3 times sooner on the pipeline. The Performance Equation explains why time depends on both numbers, and the last chapter of this section measures them in detail.
Where Is This Used?
Small cores like this one are everywhere: inside microcontrollers, as housekeeping processors next to bigger cores, and inside chips whose main job is something else entirely, such as a storage controller or a sensor hub. Cores for that role are usually short pipelines: the Arm Cortex-M3 has three stages, and lowRISC's open-source RISC-V core Ibex has two, with an optional third. The reason is the same as for the one you build: few stages keep the hazards simple and the area small, and the clock is fast enough for the job.
What the Core Leaves Out
To keep the section focused, the core implements only what compiled C needs. It has no multiply or divide instructions (the compiler calls small library routines instead), no control and status registers, no interrupts or exceptions, no caches and no branch prediction. The first half of this tutorial explains each of those in its own chapter, for example Privilege Levels and CSRs and Why Branches Hurt; once your pipeline works, you will be able to see exactly where each would plug in.
Three RV32I instructions are left out as well: fence, which orders memory accesses between several cores, and ecall and ebreak, which call an operating system or a debugger. None of the test programs uses them. An unknown instruction in your core simply does nothing; if a program depends on it, the test reports that it never finished.
Quick Check
Q: The single-cycle core runs the matrix multiply in 90,514 cycles of 14.373 ns, and the pipeline in 111,083 cycles of 8.96 ns. How long does each take, and why does the pipeline win although it needs more cycles?
Show Answer
Single-cycle: 90,514 x 14.373 ns, about 1,300.96 µs. Pipeline: 111,083 x 8.96 ns, about 995.3 µs. Time is cycles times clock period, and the pipeline's clock period is much shorter than the single-cycle core's, which more than makes up for the extra cycles spent on taken branches, jumps and filling the pipeline.
The Tools
Everything runs in ChipVerify Lab in your browser. The exercises are simulated with Icarus Verilog or Verilator, two open-source simulators, and every test was checked on both. The programs are compiled with the GNU compiler for RISC-V (riscv64-unknown-elf-gcc, version 13.2), the same one the lab's Software panel uses when you compile your own C. Lab Tour: Compiling C for RISC-V shows that panel and what it produces.
All the exercises are free. A free account has a daily limit on submissions that is far above what a normal day of work on this section needs.
Common Mistakes
Mistake #1: Skipping the block exercises and starting with the core
It is tempting to write the whole core at once. But a core test can only tell you that a program went wrong at a certain instruction; a block test tells you that the ALU's SRA gives the wrong answer for one particular input. Pass each block on its own first, and the core exercises become a matter of wiring.
Mistake #2: Changing names and ports
The module names, port names, port widths and the register file's instance name u_rf are part of the specification. A different name compiles in your own project but does not connect to the hidden testbench, and every test fails without a useful message.
Mistake #3: Looking only at the verdict
When a test fails, read the whole message and find that point in your waveform before changing code. The message names the first wrong write; the bug is almost always in the cycle that produced it, not somewhere later.
Your Turn in the Lab
There is nothing to submit for this article. Open the course in ChipVerify Lab, look at the three chapters, and open exercise E01 to see what an exercise looks like: the statement on the left, the empty module in the editor, and the Submit Solution button. Then create one project in the lab for your own modules; you will start filling it in The ALU.
Open the course Computer Architecture: Build a RISC-V Core in ChipVerify LabSummary
- This section is hands-on: you write every module of a RISC-V core yourself, from an empty module, and hidden tests in ChipVerify Lab grade it. Verilog is a prerequisite.
- You build a single-cycle RV32I core that runs compiled C programs, then turn it into a 3-stage pipeline with one forwarding path and one flush.
- Seventeen exercises in three chapters: block exercises test one module, core exercises test your whole core, assembled from your own blocks, against a reference core instruction by instruction.
- Keep the given module names, port lists and the register file instance name
u_rf; keep your passed modules in one lab project. - The finished pipeline runs the same programs as the single-cycle core in more cycles but with a shorter clock, and finishes a matrix multiply about 1.3 times sooner.