The Life of a C Test
A firmware test is a C program that the chip itself runs. Follow one from the compiler to the pass word, stage by stage and cycle by cycle. Learn what each stage costs and what can go wrong in it.
You Will Be Able To
- Name the seven stages of a C test on the chip and who owns each one.
- Read the milestones of a run from its console, trace and result block.
- Say what a test must do in each stage for its pass to mean something.
- Estimate what a test costs on the chip and on the server.
- Lesson
- V4.1
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
A colleague hands you a C test and says it passes. You ask which stage passed. The compiler built it, the bench loaded it, the core ran start-up code, main checked something and a word reached the bench. Any of those can fail, and some can fail silently.
- External
- Memory
- Processor
- Peripheral
- Bus
- Data
- Reset
- Compile
apcc builds main.c with the kit into an image, a listing and a symbol table. Nothing has run yet.
- Load
The bench writes the image straight into SRAM before time starts. A real boot would read it from flash; that is its own test.
- Reset: cycle 9
Clocks and resets come up, and the core fetches its first instruction.
- Boot: 174 cycles
Start-up code sets sp, gp and mtvec and clears 72 bytes of .bss. Then it writes 2 to TB_STATUS: main entered.
- Check: cycle 351
The test prints a line and reads the registers it checks.
- Report: cycle 499
main returns 0; start-up code writes the pass word, and the bench ends the run.
Read the diagram as text
- Compile: apcc, riscv64-unknown-elf-gcc 14.2.0: 706 bytes of code
- Load: back door into SRAM: 0 cycles
- Reset: first fetch at cycle 9
- Boot: start-up code: 174 cycles
- main: TB_STATUS = 2 at cycle 183
- Check: reads CHIP_ID at cycle 351
- Report: TB_STATUS = 0x900D at cycle 499
- Compile → Load: image, symbols
- Load → Reset: memories filled
- Reset → Boot: first fetch
- Boot → main: call main
- main → Check: loads
- Check → Report: return 0
Cycle numbers recorded on ap_soc v1.5 (soc_tb, Verilator 5.052 2026-09-05), firmware built with riscv64-unknown-elf-gcc 14.2.0.
Before You Start
Read Before main: Start-Up Code and The First Run first. This module starts the tests that run on the chip's own processor.
| Question | |
|---|---|
| 1 | Why does a chip need tests that its own processor runs? |
| 2 | What are the stages of a C test, from compile to report? |
| 3 | Which stages cost cycles, and which cost time on a computer? |
| 4 | How does the bench know the test really ran? |
| 5 | What changes when a regression runs thousands of such tests? |
The Idea: The Chip Tests Itself
A block bench drives a block from outside. A chip has a processor, and the software that will ship on it talks to every block through it. So the most direct test of the whole chip is a program on that processor. It programs the blocks, waits for them, checks what they did and reports.
Such a test has a life with stages, and each stage has an owner. The compiler and the firmware kit make the image. The bench loads it, starts the clocks and watches. Start-up code prepares C. Your main does the work. If any stage is wrong, the pass word at the end proves nothing.
This is the method few engineers outside chip teams ever use. The rest of the module builds on this one test. It covers how a test reports, talks to the bench and fails.
New Words
| Word | Meaning |
|---|---|
| C test | A test written in C that runs on the chip's own processor and reports its result to the bench. |
| Image | The bytes of a compiled program, laid out for the chip's memories. |
| Back-door load | The bench writes the image into memory directly, before the first clock, with no bus transfer. |
| Milestone | A cycle at which a known event of the test happened, such as main entered. |
| Fail code | A number that says which check failed: the low 16 bits of 0xBAD0_xxxx in TB_STATUS. |
| Result block | The lines the bench prints at the end of a run: verdict, reason, status, cycles and counts. |
The Smallest Example: One Test, Seven Stages
Here is a whole test. It prints a line, checks two registers against the chip's data sheet and returns 0.
#include "hal.h"
#include "tb.h"
int main(void)
{
tb_puts("life: start\n");
if (reg_read32(SYSCTL_CHIP_ID) != 0x2a5310c5u) return 1; /* check 1: the chip's identity */
if (reg_read32(UART0_DIV) != 16) return 2; /* check 2: a reset value */
tb_puts("life: ok\n");
return 0; /* the kit writes the pass word */
}| Stage | Owner | What must be true |
|---|---|---|
| Compile | apcc and the kit | The image fits the memory; the symbol table is written |
| Load | The bench | Every byte lands where the linker put it |
| Reset | The chip | Clocks run, resets release in order |
| Boot | Start-up code (crt0.S) | sp, gp, mtvec set; .data in place; .bss cleared; TB_STATUS = 2 |
| main | You | Every check really runs |
| Check | You | Each check compares against the specification, not against the chip |
| Report | main and the kit | Return 0 for pass, or a code that names the failed check |
The test above passes through all seven.
The Rules
Rule 1: A pass needs proof that main ran.
The bench accepts the pass word only after TB_STATUS = 2, written by start-up code before main.
Rule 2: Every check compares with the specification.
Expected values come from the data sheet or the generated header, never from reading the chip first.
Rule 3: Every failure has its own code.
The code is what the regression report shows; it must point at one check.
Rule 4: Count the cycles of each stage.
Boot costs the same in every test; a short test is mostly boot.
Rule 5: Name the compiler.
Another compiler builds another program: other addresses, other cycle counts, the same behaviour.
Sources for These Rules
The kit's start-up code and its status words: tools/soc/fwkit/README.md and crt0.S. The bench's end of test: its manual, README.md.
Cycle counts and costs: recorded on this page; server costs measured at stage E8 (docs/soc/ap_soc-facts.md 27.13).
Self-checking C tests on the processor of an SoC are common practice in chip verification. The status-word and log-channel pattern also appears in open chip projects (plan §7). This chip's mailbox is our choice of design.
Now Run It: One Life, Measured
The test ran on ap_soc v1.5 with the processor and its reference model, built with riscv64-unknown-elf-gcc 14.2.0. The bench printed two console lines and its result block.
CONSOLE 337: life: start
CONSOLE 472: life: ok
verdict=pass
reason=pass
status=0000900d
main_entered=1
cycles=507
retired=243
visited=0098
compares=243
mismatches=0Rule 1 at work: main_entered=1, so the pass counts. The retire trace gives the milestones. The first fetch came at cycle 9 and main entered at 183. The CHIP_ID check came at 351, the pass word at 499.
9 | 80000000 | auipc gp, 0x1 | x3 = 80001000
183 | 8000007c | sw t1, 0(t0) | mem[10004020] = 00000002
351 | 80000250 | lw a4, 0(a5) | x14 = 2a5310c5, read mem[10004000] = 2a5310c5
358 | 80000268 | lw a4, 16(a5) | x14 = 00000010, read mem[10000010] = 00000010
499 | 800001f4 | sw a5, 32(a4) | mem[10004020] = 0000900dStart-up code took 174 cycles, about a third of this short run: rule 4. Most of it clears .bss, 72 bytes, which the kit's interrupt tables need. The two checks took a few cycles each. The printing between them cost more.
Then the same test was written against an older register file, which said that DIV resets to 8. Check 2 failed, the run ended at cycle 390, and TB_STATUS read 0xbad00002: rule 3. The fail code names the check without a single extra print.
Why It Is Built This Way
The bench could load the test in other ways. It could use the debug port, or the SPI flash as the real boot does. Those are slower and are tested on their own (module V9). For every other test, a back-door load costs no cycles, so the run spends its time on what the test is about.
The cost moves elsewhere. Compiling and linking took 121 ms on the laptop and 515 ms on the live server, measured at stage E8. A test this short simulates in a few milliseconds. That is why one image is reused for many runs, the subject of a later lesson.
The Trap: The Pass That Skipped a Stage
Picture a test that has always passed in the regression. Then a bring-up engineer finds that its main never ran: a broken image wrote the pass word from start-up code. The bench of this chip refuses such a pass, because "main entered" never came first.
The quieter version is a check that cannot fail. A test reads DIV and compares it with the value it read a moment earlier. Or it compares with a constant copied from the chip in simulation. It passes on every chip, broken or not. Rule 2 exists for that trap, and the practice problem plants errors that only a real comparison catches.
At Scale
This section looks ahead. One test of 500 cycles is easy to watch.
Thousands of tests
A chip regression runs thousands of C tests a night. Boot and build are paid by every one, so teams keep them short and reuse images.
Real boot
Large chips boot through ROM code, a flash and often a second stage. A few tests do the real boot; the rest are loaded by the back door.
Two compilers
Firmware teams and verification teams often use different compiler versions. Tests that grade cycles exactly break; tests that grade behaviour do not.
Try It
Run the slice: it turns the recorded milestones into the stages of the test.
Change the pass word's cycle to 10,499 (a test whose main takes ten thousand cycles more). What happens to the start-up share?
The predict question below uses the run that failed check 2.
reset to first fetch: 9 cycles first fetch to main entered: 174 cycles main entered to first check: 168 cycles first check to pass word: 148 cycles pass word to last cycle: 8 cycles start-up share of the run: 34%Check Yourself
Common Mistakes
Wrong Read a value from the chip and use it as the expected value.
The check then agrees with any chip, broken or not.
Fix Take expected values from the specification or the generated header.
Wrong Return 1 from every failed check.
The regression report cannot tell which check failed.
Fix Give each check its own fail code.
Wrong Grade a test by its cycle count.
Another compiler makes another program with other counts.
Fix Grade behaviour; treat cycles as a budget with room.
Remember
- A C test lives through compile, load, reset, boot, main, check and report.
- The pass counts only after "main entered"; each check needs an expected value from the specification and its own fail code.
- This short test spent 174 of its 507 cycles in start-up code.
- Grade behaviour, never exact cycle counts: the compiler changes them.