The hidden tests tell you whether your core is right, but you cannot see or change them, and the next chapter changes your core more than any exercise so far. To change a working design without breaking it, you need tests of your own. This article explains the three main ways to test a processor, from a single assembly program that checks itself to a comparison of every register write with a reference, shows how to write and run your own directed tests on your core in the lab, describes how the hidden tests of this section were themselves tested, and introduces the test suites and reference simulators the RISC-V industry uses.
11 min read | Beginner to Intermediate
What You'll Learn
- Compare directed tests, self-checking programs and trace comparison, and say what each one finds
- Write a directed assembly test that reports its result through EXIT, and run it on your own core
- Choose the corner cases a processor test must cover
- Explain how mutation testing shows that a test suite catches real mistakes
Before You Start
You need Verilog to read and extend a testbench; the Verilog Tutorial covers it. This article assumes a core that passes E12, from Running Real Programs, and a project set up as in The Core's Specification. Verifying a CPU in the first half of the tutorial describes how processor teams verify their designs; this article is the hands-on version for your core.
Why Your Own Tests
A hidden test is a verdict: pass, or a message about the first thing that went wrong. That is enough to finish an exercise, but not to develop a design. When you change your core, you want to rerun a set of checks in seconds, in your own project, with the waveform open. When a hidden test fails in a way you do not understand, you want a smaller program that fails the same way. And in the next chapter you will restructure the whole core into a pipeline: everything that worked must keep working, and the only way to know is to have a test for it.
There are three common kinds of processor test, and they find different things:
Directed Tests in Assembly
A directed test is a short program written by hand to check specific behavior. In assembly you control every instruction, so you can test exactly the case you mean: the unsigned comparison of a negative number, a load at byte offset 3, a jump to an odd address. The program checks its own results with branches and reports through EXIT. Here is the pattern used by branch.S, one of the hidden test programs:
addi x31, x0, 0 # bit mask of passed checks
beq x2, x2, 1f
addi x31, x31, 1000 # skipped
1: ori x31, x31, 1
beq x1, x2, bad
bne x1, x2, 2f
j bad
2: ori x31, x31, 2
...
bad:
lui x8, 0xf0000
sw x0, 0(x8)
Each check that passes sets one bit of x31, and the program ends by storing x31 to EXIT, so the result tells you which checks passed: 0x1FF means all nine. Any check that fails jumps to bad, which ends the program with result 0. Testing each behavior both ways matters: beq must branch when the registers are equal and must not branch when they are not, and a core that always branches passes the first half.
You can run tests like this on your own core with the lab's Software panel, which compiles assembly files as well as C. The generated start-up code calls a function named main and stores its return value to EXIT, so write your test as a function: a global label main, the checks, the mask in a0, and ret:
.globl main
main:
li a0, 0 # mask of passed checks
li t0, -5
li t1, 5
sltu t2, t0, t1 # 0xFFFFFFFB < 5 unsigned? no
bnez t2, 1f
ori a0, a0, 1
1: slt t2, t0, t1 # -5 < 5 signed? yes
beqz t2, 2f
ori a0, a0, 2
2: ret # main returns the mask; crt0 stores it to EXIT
Save it as main.S in your project instead of main.c (a project may not have both, because they would compile to the same object) and run the simulation. The boot testbench prints the result; this test should end with 3.
Quick Check
Q: A directed test for loads stores 0x89AB_CDEF and checks lb at offsets 0 to 3, expecting 0xFFFF_FFEF, 0xFFFF_FFCD, 0xFFFF_FFAB and 0xFFFF_FF89. Why is this word a better choice than 0x1234_5678?
Show Answer
Every byte of 0x89AB_CDEF has its top bit set, so every lb result must be sign-extended with ones. With 0x1234_5678 every byte is below 0x80, and a core that zero-extends would pass. The bytes are also all different, so a load from the wrong lane gives a wrong value.
Self-Checking Programs
A directed test checks one behavior. A self-checking program checks that a whole computation is right: it computes something nontrivial and its result is compared with the answer worked out somewhere else. The seven C programs of E12 are self-checking in this sense: the expected results came from compiling the same C for a PC and running it there. A program like the CRC uses hundreds of thousands of instructions in combinations nobody would think to write by hand, which is how it found the sign-extension bug described in Running Real Programs.
The weakness is the other side of the strength: a wrong result says only that something went wrong somewhere in 71,695 instructions. Self-checking programs find bugs; directed tests and traces locate them.
Trace Comparison
The most precise method compares your core's behavior with a reference at every step. The core tests of this section do this: for each register write and each store your core makes, they check it against the write the reference core made at the same point of the program, and the first difference is the message. That turns "the result is wrong" into "the 28th register write is wrong", which leads straight to one instruction in the listing and one cycle in the waveform.
You can build part of this into your own testbench. A few lines that print every register write (watch u_rf's write enable, address and data at each clock edge) give you a trace of your core. For a short directed test you can check that trace by hand against the listing. For long programs, industry uses an instruction set simulator as the reference: a program that executes RISC-V instructions in software, one at a time, exactly as the specification defines them. Spike is the standard one for RISC-V, and Sail, a formal model written from the specification, is used for compliance checking. The lab does not provide them yet; the reference traces behind the hidden tests come from the reference core of this section, checked against compiled C on a PC.
What to Cover
Whatever the method, a test is only as good as the cases it exercises. For a core like yours, these are the cases most likely to expose a bug:
- x0 as destination (writes must vanish) and as source (it must read 0).
- Immediates at their extremes: the largest and smallest 12-bit values, negative values with bit 30 set, and branches and jumps both forward and backward.
- Shifts by 0, by 31, and by amounts above 31 for the register forms.
- Comparisons and branches with negative operands in both signed and unsigned forms, and with equal operands for
bgeandbgeu. - Loads and stores of every size at every allowed offset, with sign bits set, and a load right after a store to the same address.
- Calls and returns, including
jalrto an odd address and ajalrwhose rd is its own rs1.
The hidden test programs and the seven C programs together use 37 instructions, every one the core implements. Using an instruction is not the same as covering its corner cases, which is why the assembly tests were written on purpose for the list above.
Testing the Tests
How do you know a test suite would catch a real mistake? By making the mistake on purpose. For every exercise in this section, the tests were run against the reference solution, which must pass, against the empty starter, which must fail, and against a set of deliberately wrong solutions, each one a small, typical mistake in the reference: SRA computed as SRL, x0 writable, byte enables not shifted, a branch target computed from PC + 4, a missing flush. There are 57 of them across the 17 exercises, and each fails at least one test: on both simulators for the simulated exercises, and in the lab's synthesis grader for the synthesis exercise. Where one did not at first, the test was extended.
This technique is called mutation testing, and each wrong solution is a mutant. It is the honest way to measure a test suite: not by how many tests it has, but by how many plausible bugs it catches. You can use it on your own tests too. Break your core in one small way, run your directed tests, and see whether any of them notices.
Where Is This Used?
The RISC-V community maintains open test suites for exactly this purpose. riscv-tests is a collection of small assembly tests, one or more per instruction, each reporting pass or fail through a memory location the testbench watches, the same idea as EXIT. riscv-arch-test, the official architecture test suite, runs tests on a core and compares the memory region each test writes, its signature, with the signature from the reference model; a core whose signatures all match can claim compatibility. Processor teams add random instruction generators that produce millions of instructions for trace comparison against Spike.
Quick Check
Q: You write a directed test for jalr and it passes on your core. To check the test, you change your core so that jalr no longer clears bit 0 of the target, and the test still passes. What does that tell you?
Show Answer
Your test does not cover bit 0: it probably only jumps to even addresses, where clearing bit 0 changes nothing. Add a jump through an odd address and check a value that depends on where the jump landed, such as the link register of the next call or a mask bit set only at the correct target.
Common Mistakes
Mistake #1: Testing only the case that should happen
A branch test that checks only that beq branches on equal values passes on a core that always branches. Test every behavior in both directions.
Mistake #2: Test values that cannot tell right from wrong
Values below 0x80 make sign extension invisible, equal bytes make a wrong lane invisible, and a shift by 0 makes a wrong shifter invisible. Choose values for which the likely mistakes give different answers.
Mistake #3: Tests that only run once
A test you ran once while writing a block does not protect the next change. Keep your tests in the project and rerun them every time you change the core, especially through the next chapter.
Your Turn in the Lab
There is no graded exercise for this article. Build a small test set for your core instead:
- Write the
sltu/slttest above asmain.Sin your project and run it on your core; it should end with 3. - Write a second directed test for loads: store 0x89AB_CDEF, load it back with
lb,lbu,lhandlhuat every allowed offset, and set one mask bit per correct value. - Check your tests: break your load/store unit on purpose (sign-extend
lbu, for example) and make sure one of them fails. Then repair it.
Summary
- Your own tests let you change the core safely; the hidden tests only give verdicts.
- Directed assembly tests check specific behavior and report a pass mask through EXIT; in the lab, write them as a main function in a .S file.
- Self-checking programs find bugs in large computations; trace comparison against a reference locates the first wrong instruction.
- Good tests cover x0, immediate extremes, shifts, signed and unsigned comparisons, every load size and offset, and calls and returns, each in both directions.
- Mutation testing measures a test suite by the deliberate mistakes it catches: 57 across this section's exercises, every one caught.