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.

The Life of One C TestStep through the seven stages. The first two happen before the chip has a clock; the rest are cycles of clk_sys, from the run below.
  • External
  • Memory
  • Processor
  • Peripheral
  • Bus
  • Data
  • Reset
The Life of One C TestStep through the seven stages. The first two happen before the chip has a clock; the rest are cycles of clk_sys, from the run below. external Compile apcc,riscv64-unknown-elf-gcc14.2.0: 706 bytes ofcode memory Load back door into SRAM: 0cycles external Reset first fetch at cycle 9 processor Boot start-up code: 174cycles processor main TB_STATUS = 2 at cycle183 peripheral Check reads CHIP_ID at cycle351 bus Report TB_STATUS = 0x900Dat cycle 499 image, symbolsmemories filledfirst fetchcall mainloadsreturn 0
  1. external Compileapcc, riscv64-unknown-elf-gcc 14.2.0: 706 bytes of code
    • image, symbols → Load
  2. memory Loadback door into SRAM: 0 cycles
    • memories filled → Reset
  3. external Resetfirst fetch at cycle 9
    • first fetch → Boot
  4. processor Bootstart-up code: 174 cycles
    • call main → main
  5. processor mainTB_STATUS = 2 at cycle 183
    • loads → Check
  6. peripheral Checkreads CHIP_ID at cycle 351
    • return 0 → Report
  7. bus ReportTB_STATUS = 0x900D at cycle 499
  1. Compile

    apcc builds main.c with the kit into an image, a listing and a symbol table. Nothing has run yet.

  2. 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.

  3. Reset: cycle 9

    Clocks and resets come up, and the core fetches its first instruction.

  4. 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.

  5. Check: cycle 351

    The test prints a line and reads the registers it checks.

  6. 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.

Five questions this page answers
Question
1Why does a chip need tests that its own processor runs?
2What are the stages of a C test, from compile to report?
3Which stages cost cycles, and which cost time on a computer?
4How does the bench know the test really ran?
5What 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

New words on this page
WordMeaning
C testA test written in C that runs on the chip's own processor and reports its result to the bench.
ImageThe bytes of a compiled program, laid out for the chip's memories.
Back-door loadThe bench writes the image into memory directly, before the first clock, with no bus transfer.
MilestoneA cycle at which a known event of the test happened, such as main entered.
Fail codeA number that says which check failed: the low 16 bits of 0xBAD0_xxxx in TB_STATUS.
Result blockThe 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.

LIFE_C: the lab program of this page.
#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 */
}
Who does what
StageOwnerWhat must be true
Compileapcc and the kitThe image fits the memory; the symbol table is written
LoadThe benchEvery byte lands where the linker put it
ResetThe chipClocks run, resets release in order
BootStart-up code (crt0.S)sp, gp, mtvec set; .data in place; .bss cleared; TB_STATUS = 2
mainYouEvery check really runs
CheckYouEach check compares against the specification, not against the chip
Reportmain and the kitReturn 0 for pass, or a code that names the failed check

The test above passes through all seven.

The Rules

  1. 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.

  2. 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.

  3. Rule 3: Every failure has its own code.

    The code is what the regression report shows; it must point at one check.

  4. Rule 4: Count the cycles of each stage.

    Boot costs the same in every test; a short test is mostly boot.

  5. 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.

Measured on our bench (soc_tb, Verilator 5.052 2026-09-05); the result block cut to the keys this page uses.
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=0

Rule 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.

Five records of the retire trace: cycle, pc, instruction, effect.
    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] = 0000900d

Start-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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. One test of 500 cycles is easy to watch.

  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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

Predict the outputThe slice now reads the milestones of the run that failed check 2. What is the last line it prints?
// Milestones of one C test on ap_soc v1.5, from its retire trace (cycles of clk_sys after reset).
// Each phase of the test's life is the distance between two milestones.
module tb;
  localparam int N = 5;
  string name [N];
  int    at   [N];
  initial begin
    name[0] = "first fetch"; at[0] = 9;
    name[1] = "main entered"; at[1] = 183;
    name[2] = "first check"; at[2] = 351;
    name[3] = "fail word"; at[3] = 382;
    name[4] = "last cycle"; at[4] = 390;
    $display("reset to %s: %0d cycles", name[0], at[0]);
    for (int i = 1; i < N; i++)
      $display("%s to %s: %0d cycles", name[i - 1], name[i], at[i] - at[i - 1]);
    $display("start-up share of the run: %0d%%", 100 * (at[1] - at[0]) / at[N - 1]);
  end
endmodule
ConceptWhy does a chip need tests that its own processor runs?
ConceptHow does this bench know that a test really ran?
ConceptIn the run above, which stage took about a third of the cycles?
ConceptA regression of short tests is slow. What usually costs the most per test?

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.

Practice

Report a problem with this page