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.
- External
- Debug
- Memory
- Processor
- Data
- Debug
- A knob is a variable
The test declares it with a first value; the build writes its address and size into the symbol table.
- The bench looks it up
The bench reads the symbol table and finds the address by name. No rebuild is needed.
- A write by the back door
Before the first instruction, or during a hand-shake, the bench writes the word straight into SRAM.
- 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.
| Question | |
|---|---|
| 1 | Why should one image serve many runs? |
| 2 | How does the bench find and write a C variable? |
| 3 | When is a write before the first instruction lost? |
| 4 | When should a knob be written after a hand-shake instead? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Knob | A C variable a test reads so that the bench can change the test without a rebuild. |
| Symbol table | The list apcc writes of each named variable and function, with its address and size. |
| Write before boot | The bench's +set write: one variable, before the first instruction. |
| Write after a hand-shake | A sym row of the bench's table: any variable, at a moment the firmware asked for. |
| .bss | Variables with no first value, or a first value of 0; start-up code clears them. |
| Seed | The 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.
| Declaration | Section | Written before boot | Written after a hand-shake |
|---|---|---|---|
| volatile unsigned rounds = 2; | .data | Kept | Kept |
| volatile unsigned late; | .bss | Cleared by start-up code | Kept |
| volatile unsigned mode = 0; | .bss (a zero first value) | Cleared by start-up code | Kept |
| const unsigned count = 8; | Folded into the code | Never read | Never read |
In the SRAM layout .data is loaded with the program and never copied, so a write there stays.
The Rules
Rule 1: A knob is a volatile global with a name.
The bench finds it by name; volatile makes main load it from memory.
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.
Rule 3: Read knobs after the hand-shake.
A write during the run is seen only by loads that come after it.
Rule 4: Keep a default that is a valid run.
An image with no writes must still be a useful test.
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
#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.
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 0000001fThe 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
This section looks ahead. Three knobs serve one test here.
Test lists
A regression keeps a list of runs: test, image, seed and knobs. One image serves dozens of entries.
Random stimulus
Firmware that makes its own random traffic takes its seed from the bench, so a failing seed can be rerun.
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.
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, keptCheck Yourself
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.