A core is finished when it runs real programs correctly, not when it passes a list of instructions. In exercise E12 your single-cycle core runs seven C programs compiled with gcc: a greeting, an array sum, a memory copy, a bubble sort, a matrix multiply, a linked-list walk and a CRC checksum. You add one output, retire, so that the testbench can count the instructions your core completes. This article describes the programs and what each one stresses, explains what the test checks, shows why seven programs catch bugs that one does not, and gives a method for the hardest failure of all: a program that never ends.
10 min read | Beginner to Intermediate
What You'll Learn
- Add a retire output and explain what "an instruction completes" means in a single-cycle core
- Describe what the seven test programs do and what part of the core each one stresses
- Compute the execution time of a program from its cycles and the clock period
- Debug a wrong result or a hang using the listing, the test message and the waveform
Before You Start
This exercise needs no new Verilog beyond what you have written; the Verilog Tutorial is the reference if you need it. Pass E11 first: E12 uses the same core plus one output. Workloads and Benchmarks explains why processors are measured on a set of programs rather than one, and The Performance Equation how cycles, instructions and clock period combine.
The Retire Output
An instruction retires, or completes, when its effects are final: its register is written, its store has happened, its next PC is set. From E12 on, the core has a port that says when that happens:
output logic retire // 1 in a cycle where an instruction completes
It comes after dmem_ready in the port list (the E12 starter shows it). In a single-cycle core, an instruction completes in every cycle after reset, so retire is 1 whenever the core is out of reset. It seems pointless now, but it is how the testbench counts instructions without knowing anything about your design, and in the 3-stage pipeline it will be 1 only in some cycles. Counting it there is how you will measure exactly what the pipeline costs.
The Seven Programs
All seven are small C programs with no C library, compiled for rv32i at -O2 with the same start-up code as the template in Lab Tour: Compiling C for RISC-V. Each computes a 32-bit result and returns it from main; the expected result is the same C code compiled for and run on a PC.
| Program | What it does | What it stresses | Expected result |
|---|---|---|---|
| hello | prints "Hello from cvr32!" and counts the vowels | byte loads, stores to CONSOLE, a short loop | 0x0000_0003 |
| sum | fills an array of 1,024 words with 3i + 1 and adds it up | word stores and loads, a counting loop | 0x0017_FE00 |
| memcpy | fills 4 KB with pseudo-random words, copies it, checksums the copy | loads and stores, shifts | 0x2ABA_7023 |
| bubble | sorts 64 values with 2,016 comparisons | data-dependent branches, swaps in memory | 0x001A_7F84 |
| matmul | multiplies two 12 x 12 matrices; with no multiply instruction, each of the 1,728 multiplications calls a library routine | calls and returns, shifts and adds | 0x01C3_9D6E |
| list | builds a linked list of 512 nodes and walks it 8 times | pointer loads, function calls, the stack | 0x0085_4540 |
| crc | computes a bit-by-bit CRC-32 over 1 KB of pseudo-random bytes | unsigned byte loads, shifts, XOR, a branch per bit | 0x8BDD_9129 |
Together they use 25 different RV32I instructions. They are small enough to simulate in seconds and different enough that a mistake in one part of the core usually shows up in at least one of them. These are their sizes on the reference core:
Not every instruction writes a register. In the matrix multiply, for example, 62,289 of the 90,514 instructions write a register; the others are the 444 stores and the branches and jumps that write no register. The core tests in earlier exercises compared every write; for programs this long the test checks the end result and the counts instead.
What the Test Checks
For each program, the testbench loads the image into memory, releases reset and counts two things each clock cycle until the program stores its result to EXIT: the cycles, and the cycles in which retire is 1. Then it checks three things:
- The result matches the PC's answer.
- The retire count equals the number of instructions the program executes, counting the final store to EXIT. For the matrix multiply that is 90,514.
- The cycle count equals the instruction count. That is what makes a core single-cycle: one instruction per cycle, a CPI (cycles per instruction) of exactly 1.
The instruction count is a property of the program, not of the core: any correct RV32I core executes exactly the same instructions. Only the number of cycles they take depends on the design, which is why the pipeline exercises check the same instruction count with a different cycle count.
From Cycles to Time
A cycle count becomes a time when you multiply it by the clock period. A single-cycle core's period must fit its slowest instruction, the load, which Loads and Stores measured at 14.373 ns in a placed version of the reference core, with an assumed memory read time of 2 ns. At that period the matrix multiply takes 90,514 x 14.373 ns, about 1,300.96 µs, and the linked-list walk about 2,194.38 µs. Every instruction pays for the load's long path, including the many that never touch memory.
Quick Check
Q: The sum program executes 8,205 instructions. How many cycles does it take on the single-cycle core, and how long at 14.373 ns per cycle?
Show Answer
8,205 cycles, one per instruction. 8,205 x 14.373 ns is about 117.93 µs.
Why Seven Programs
When this test was checked against typical mistakes, one of them showed exactly why a single test program is not enough. A load/store unit that treats lbu like lb, sign-extending an unsigned byte, passed six of the seven programs. Only one failed:
FAIL: the program ended with result 0x19b1c917, expected 0x8bdd9129
It was the CRC. The greeting loads bytes with lbu too, but they are ASCII letters, all below 0x80, so sign extension changes nothing. The CRC loads pseudo-random bytes, about half of them 0x80 or above, and every one of those became a negative number. Six correct results said nothing about the bug; the seventh found it. The block test for the load/store unit in E08 catches this mistake directly, but a core that passed E08 and then lost the fix, for example by pasting an older file, would only be caught by the CRC. This is the reason real processors are tested on large and varied sets of programs.
Where Does This Matter?
A processor team's regression is the same idea at scale: thousands of programs, from tiny directed tests to operating system boots, run every night on the latest design. A bug that only one program exposes is common, and finding which program, and which instruction in it, is daily work. Verifying a CPU describes how such regressions are built.
When a Program Never Ends
A wrong result is easy to report. A program that never reaches EXIT is harder: the core is running somewhere, but not where it should. The test waits several times the expected number of cycles and then reports where the PC was:
FAIL: no end after 815 cycles: the core is stuck or running the wrong code (PC 0x00000044) FAIL: no end after 24815 cycles: the PC is unknown (X); an X reached it, often from a register that was never written or an undriven signal
To debug a hang in your own project:
- Find the PC in the listing. The address in the message is where the core was when the test gave up. If it is inside a loop, the loop never exits: look at its branch condition and at the register it tests.
- Go back to the first wrong step. In the waveform, follow the PC from reset and compare it with the listing until the first instruction after which the PC goes somewhere the listing does not. That instruction, or a value it used, is the bug.
- For an X on the PC, follow the X back: the PC took it from the target or from PC + 4; the target came from the immediate or a register; a register holds X if it was never written, or if it was written with an X. Usually the source is an output you left undriven or a multiplexer with an unhandled select value.
Quick Check
Q: Your core returns the right result for every program but the retire count is wrong by exactly one for each. What is the likely cause?
Show Answer
retire is not 1 in exactly the cycles where an instruction completes. The test counts the cycles from the first clock edge after reset up to and including the one with the EXIT store, so a retire that is 0 in the first cycle after reset, or that only rises one cycle late, misses one instruction. In the single-cycle core, retire should be 1 in every cycle in which reset is released.
Common Mistakes
Mistake #1: Adding retire in the wrong place in the port list
Ports are connected by name, but the port must exist with exactly the name retire and be an output. The E12 starter has it after dmem_ready; copy that header.
Mistake #2: Trusting one passing program
A program that passes exercises only the instructions and values it happens to use. Run all seven in your own project when you change anything, not just the one that failed.
Mistake #3: Debugging from the C source
The core runs the listing, not the C. Compare the waveform with the listing instruction by instruction; the compiler's reordering and inlining make the C a poor map.
Your Turn in the Lab
The exercise opens with a visible test, testbench.v, next to your design: the C program hello, checked for its result, its output and its cycle count, run on your core and compared write by write with the reference core, naming the first write that differs. Simulate it as you work and look at the waveform where it fails. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.
Add retire to your core and submit it to E12. When all seven programs pass, your single-cycle core is complete: it runs every RV32I instruction a C compiler generates, at one instruction per cycle.
Summary
- The retire output is 1 in every cycle in which an instruction completes; in a single-cycle core that is every cycle after reset.
- Seven C programs, from 205 to 152,674 instructions, test the finished single-cycle core; each returns a result checked against the same C run on a PC.
- The test checks the result, that retire counts every instruction, and that cycles equal instructions: a CPI of exactly 1.
- Time is cycles times the clock period, which the load's long path sets at 14.373 ns in the placed reference core: about 1,300.96 µs for the matrix multiply.
- Some bugs show in only one program, such as an lbu that sign-extends, found only by the CRC; hangs are debugged from the PC in the message, the listing and the waveform.