Parameters from the Bench

One firmware image can serve a hundred runs if the bench can change its knobs: a seed, a loop count, a variant. Learn the two ways the bench writes a C variable, when each write survives, and the zero that quietly undoes one.

You Will Be Able To

  • Write a knob the bench can set through the symbol table, and read it at the right time.
  • Say when a write before the first instruction survives start-up code, and when it does not.
  • Choose between TB_ARG, a write before boot and a write after a hand-shake.
  • Work out what reusing one image saves against a rebuild per run.
Lesson
V4.5
Time
24 min
Runs on
Your browser
Check
5 questions

A regression needs the same test with many seeds. The first plan is one build for each seed. The second plan is one image and many runs, each with its own seed written in by the bench. The second plan works, except for the knob whose default is zero.

The Bench Writes a C VariableStep through one knob. It starts as a name in C and an address in the symbol table, then a back-door write, then a load.
  • External
  • Debug
  • Memory
  • Processor
  • Data
  • Debug
The Bench Writes a C VariableStep through one knob. It starts as a name in C and an address in the symbol table, then a back-door write, then a load. external volatile unsignedrounds = 2; a knob in the firmware external Symbol table rounds at0x80000394, 4bytes debug Bench +set=rounds, or a symrow memory SRAM the word at thataddress processor main reads rounds afterthe hand-shake apcc, nmname to addressback-door writeload
  1. external volatile unsigned rounds = 2;a knob in the firmware
    • apcc, nm → Symbol table
  2. external Symbol tablerounds at 0x80000394, 4 bytes
    • name to address → Bench
  3. debug Bench+set=rounds, or a sym row
    • back-door write → SRAM
  4. memory SRAMthe word at that address
    • load → main
  5. processor mainreads rounds after the hand-shake
  1. A knob is a variable

    The test declares it with a first value; the build writes its address and size into the symbol table.

  2. The bench looks it up

    The bench reads the symbol table and finds the address by name. No rebuild is needed.

  3. A write by the back door

    Before the first instruction, or during a hand-shake, the bench writes the word straight into SRAM.

  4. main reads it

    A volatile load after the hand-shake sees the bench's value.

Read the diagram as text
  • volatile unsigned rounds = 2;: a knob in the firmware
  • Symbol table: rounds at 0x80000394, 4 bytes
  • Bench: +set=rounds, or a sym row
  • SRAM: the word at that address
  • main: reads rounds after the hand-shake
  • volatile unsigned rounds = 2; → Symbol table: apcc, nm
  • Symbol table → Bench: name to address
  • Bench → SRAM: back-door write
  • SRAM → main: load

Addresses from the lab image (riscv64-unknown-elf-gcc 14.2.0).

Before You Start

Read From C to an Image and Hand-Shakes first.

Five questions this page answers
Question
1Why should one image serve many runs?
2How does the bench find and write a C variable?
3When is a write before the first instruction lost?
4When should a knob be written after a hand-shake instead?
5What does a rebuild per run cost a regression?

The Idea: Knobs Instead of Builds

A test usually has a few numbers that change between runs: a seed for its random values, a count, a mode. If each value meant a new build, a regression would spend most of its time compiling. Instead, the test keeps those numbers in C variables, and the bench writes them before or during the run.

The bench finds a variable by name in the symbol table that the build wrote. It writes the word at that address by the back door. It costs no bus cycle and no rebuild. TB_ARG is the other way in: one word, read with a bus load.

The catch is timing. Start-up code runs between the bench's first chance to write and the first line of main, and it clears .bss. A write that lands there before boot is gone.

New Words

New words on this page
WordMeaning
KnobA C variable a test reads so that the bench can change the test without a rebuild.
Symbol tableThe list apcc writes of each named variable and function, with its address and size.
Write before bootThe bench's +set write: one variable, before the first instruction.
Write after a hand-shakeA sym row of the bench's table: any variable, at a moment the firmware asked for.
.bssVariables with no first value, or a first value of 0; start-up code clears them.
SeedThe start value of a test's random sequence; the same seed repeats the same run.

The Smallest Example: Which Writes Survive

Sort four knobs of this SRAM image by where they live and when the bench writes them.

Four knobs
DeclarationSectionWritten before bootWritten after a hand-shake
volatile unsigned rounds = 2;.dataKeptKept
volatile unsigned late;.bssCleared by start-up codeKept
volatile unsigned mode = 0;.bss (a zero first value)Cleared by start-up codeKept
const unsigned count = 8;Folded into the codeNever readNever read

In the SRAM layout .data is loaded with the program and never copied, so a write there stays.

The Rules

  1. Rule 1: A knob is a volatile global with a name.

    The bench finds it by name; volatile makes main load it from memory.

  2. Rule 2: A knob written before boot needs a home in .data.

    Give it a nonzero first value, or place it in .data by hand.

  3. Rule 3: Read knobs after the hand-shake.

    A write during the run is seen only by loads that come after it.

  4. Rule 4: Keep a default that is a valid run.

    An image with no writes must still be a useful test.

  5. Rule 5: Print the knobs.

    A failing run must say which seed and mode it had, or nobody can repeat it.

Sources for These Rules

The symbol table and the bench's +set and sym writes: tools/soc/fwkit/README.md, tools/soc/ap_soc/bench/soc_tb/README.md.

Start-up code clears .bss and, in the SRAM layout, copies no .data: tools/soc/fwkit/crt0.S and README.md.

A zero first value puts a variable in .bss, as this image's symbol table shows. GCC does so unless it is given -fno-zero-initialized-in-bss. apcc does not pass that option (tools/soc/fwkit/apcc.mjs).

Writing firmware variables from the bench by symbol is a pattern open chip projects use (plan §7).

Now Run It: One Image, Four Runs

KNOB_C: three knobs, a hand-shake, one line of output.
#include "tb.h"

volatile unsigned rounds = 2;           /* .data: a first value, so start-up code leaves it alone */
volatile unsigned seed = 1;
volatile unsigned late;                 /* .bss: start-up code clears it */

int main(void)
{
    tb_event(0x5e01);                   /* bench: set the knobs now */
    tb_wait_event(0x5e02);              /* bench: done */
    unsigned x = seed;
    for (unsigned i = 0; i < rounds; i++) x = x * 5 + 1;
    tb_puts("rounds ");
    tb_put_hex(rounds);
    tb_puts(" seed ");
    tb_put_hex(seed);
    tb_puts(" late ");
    tb_put_hex(late);
    tb_puts(" x ");
    tb_put_hex(x);
    tb_putc('\n');
    return 0;
}

The image was built once (riscv64-unknown-elf-gcc 14.2.0) and run four times. The symbol table put seed at 0x80000390 and rounds at 0x80000394, both in .data, and late at 0x800003e0, in .bss.

Measured on our bench (soc_tb, Verilator 5.052 2026-09-05): SYMBOL lines are the bench's writes; each run's console line follows.
no writes:              rounds 00000002 seed 00000001 late 00000000 x 0000001f
SYMBOL 0: seed at 80000390 = 00000007
seed before boot:       rounds 00000002 seed 00000007 late 00000000 x 000000b5
SYMBOL 216: rounds at 80000394 = 00000005
SYMBOL 216: late at 800003e0 = 00000033
rounds, late after:     rounds 00000005 seed 00000001 late 00000033 x 00000f42
SYMBOL 0: late at 800003e0 = 00000033
late before boot:       rounds 00000002 seed 00000001 late 00000000 x 0000001f

The first run is the default, rule 4. The second wrote seed at cycle 0, before the first instruction: it stayed, because seed lives in .data, rule 2. The third wrote rounds and late at cycle 216, between the event and the reply. Both stayed, and main read them after the hand-shake: rule 3.

The fourth wrote late at cycle 0. The bench logged the write, and main read 0: start-up code cleared .bss after the bench wrote it. Nothing warned anyone. Only rule 5 shows it: the line prints late 00000000.

Why It Is Built This Way

The bench could pass every knob through TB_ARG. That is one word, read with a bus load, and it needs no symbol table. It also gives one test one number, and every extra number needs its own hand-shake. Writes by symbol scale to many knobs and cost no cycles.

The price is that the knobs become part of the test's interface. Renaming a variable breaks the bench's command line. Keep knob names stable, and print them so a run can be repeated exactly.

The Trap: The Zero That Undid a Write

A test takes a mode from the bench: 0 is the normal run, 1 the stress run. Its author writes volatile unsigned mode = 0;, which reads as "default 0". The compiler reads it as "zero-initialised", and places it in .bss. Every stress run the bench asks for, written before boot, is cleared by start-up code. Night after night the regression runs the normal case and calls it stress.

The fix is one attribute, or a writer that waits for the hand-shake. The habit that catches it is rule 5: print the mode you ran. The practice problem builds a test with exactly such a knob.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. Three knobs serve one test here.

  1. Test lists

    A regression keeps a list of runs: test, image, seed and knobs. One image serves dozens of entries.

  2. Random stimulus

    Firmware that makes its own random traffic takes its seed from the bench, so a failing seed can be rerun.

  3. Other platforms

    Emulators and the virtual platform write the same symbols. A run can move to a faster platform with its knobs.

Try It

Run the slice: it resolves the bench's writes through the image's symbols.

Mark the rounds write as before boot. Does the answer change?

The predict question below feeds it the fourth run.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: seed (+set) at 80000390: .data, the write is kept rounds (sym row) at 80000394: .data, the write is kept late (sym row) at 800003e0: .bss, written after start-up, kept

Check Yourself

Predict the outputThe slice now reads the fourth run: late written before the first instruction. What is the last line it prints?
// Symbols of one image (from apcc's symbol table) and the bench's back-door writes of one run.
// A write before start-up code ran is lost if it lands in .bss, which start-up code clears.
module tb;
  localparam logic [31:0] DATA_START = 32'h80000390, DATA_END = 32'h80000398;
  localparam logic [31:0] BSS_START  = 32'h80000398, BSS_END  = 32'h800003e4;
  localparam int N = 1;
  string       what [N];
  logic [31:0] addr [N];
  bit          before_boot [N];
  initial begin
    what[0] = "late (+set)"; addr[0] = 32'h800003e0; before_boot[0] = 1;
    for (int i = 0; i < N; i++)
      if (addr[i] >= DATA_START && addr[i] < DATA_END) $display("%s at %h: .data, the write is kept", what[i], addr[i]);
      else if (addr[i] >= BSS_START && addr[i] < BSS_END && before_boot[i])
        $display("%s at %h: .bss, start-up code clears the write", what[i], addr[i]);
      else if (addr[i] >= BSS_START && addr[i] < BSS_END)
        $display("%s at %h: .bss, written after start-up, kept", what[i], addr[i]);
      else $display("%s at %h: outside the variables", what[i], addr[i]);
  end
endmodule
ConceptWhy is one image reused for many runs?
ConceptWhen must a knob live in .data?
ConceptA knob is written during a hand-shake. When must main read it?
ConceptA failing run must be repeated tomorrow. What must it print?

Common Mistakes

Wrong Declare a knob const.

The compiler builds the value into the code and never reads the variable.

Fix Make it a volatile global.

Wrong Give a knob a default of 0 and write it before boot.

Start-up code clears .bss after the bench writes.

Fix Place it in .data, or write it after a hand-shake.

Wrong Copy the knobs into locals at the top of main.

Writes made during the hand-shake come later.

Fix Read the knobs after the hand-shake.

Remember

  • The bench writes knobs by name: before the first instruction, or after a hand-shake.
  • A write before boot survives only in .data; a zero first value means .bss.
  • Read knobs after the hand-shake; print them in every run.
  • One image, many runs: the build is paid once.

Practice

Report a problem with this page