Back-Door Loading
A chip bench puts the test program into ROM and SRAM before the first clock edge, by writing the memories' flops directly. Learn the image file, the byte order and the address rules. Then see why the slow front-door boot is still a test of its own.
You Will Be Able To
- Explain what a back-door load is and why every firmware test uses one.
- Read an image file: one word a line, and @ addresses counted in words of the memory.
- Predict which byte of a word a load of one byte returns on a little-endian chip.
- Say what the back door cannot prove, and which test covers it.
- Lesson
- V3.3
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
Booting a test the way the real chip does means copying it from the SPI flash before main can start. In this lesson's lab that copy costs about 66 cycles for every byte. Multiply that by every test of every night. The bench needs a faster way in.
- Tool
- File
- Test
- Memory
- External
- Processor
- Data
- The image is a file
The toolchain writes the program as words, with @ lines that say where they go.
- The back door
Before the first edge the bench reads the file straight into the memories' flops. No bus, no clock, no time.
- The front door
A real chip boots by copying from flash over the SPI pads and the bus. That path is a test of its own.
Read the diagram as text
- apcc or rv32-asm: compile, link, write the image
- Image file: one 32-bit word a line, @ addresses
- $readmemh at time 0: the back door
- ROM 4 KiB: u_rom.mem
- SRAM 64 KiB: u_sram.memory.mem
- SPI flash model: 3 bytes a frame
- Core: boot code copies
- apcc or rv32-asm → Image file: hex image
- Image file → $readmemh at time 0: +rom= +ram=
- $readmemh at time 0 → ROM 4 KiB: flops, no bus
- $readmemh at time 0 → SRAM 64 KiB: flops, no bus
- SPI flash model → Core: SPI pads
- Core → SRAM 64 KiB: bus writes
soc_tb: +rom and +ram are $readmemh images (README, "Plusargs").
Before You Start
Read Clocks, Resets and Straps From the Outside and From C to an Image first.
| Question | |
|---|---|
| 1 | Why does a chip bench load its test by the back door? |
| 2 | What does a line of an image file mean, and what does @ count? |
| 3 | Which byte of 0x1234_5678 is at the lowest address? |
| 4 | What does the back door refuse, and why should it say so? |
| 5 | Why is a front-door boot still a test of its own? |
The Idea: Skip the Boot, Keep the Truth
A real chip starts from its boot ROM, which copies the program from flash over a slow serial bus. In simulation that copy costs cycles before every test, and it tests the same boot path again and again. So the bench cheats on purpose. Before the first clock edge it writes the program straight into the memories' flops: the back door.
The cheat is safe only if it lands every byte exactly where the chip's own bus would have put it. A wrong lane, a wrong address unit or a wrong memory gives the core a different program than the one you compiled. And the boot path still needs one honest test, through the front door.
New Words
| Word | Meaning |
|---|---|
| Back door | Writing or reading a memory's flops directly, with no bus transfer and no simulated time. |
| Front door | Reaching a memory through the chip's own bus, as software does. |
| Image file | The program as text: one 32-bit word a line, in the format $readmemh reads. |
| $readmemh | A SystemVerilog system task that reads hex words from a file into a memory array. |
| Address line | A line "@n" in an image file: the next word goes to element n of the memory. |
| Little-endian | The byte at the lowest address is the least significant byte of the word; ap_soc is little-endian. |
| Lane | One byte position of a 32-bit word: lane n is bits 8n + 7 to 8n. |
The Smallest Example: One Word, Four Bytes
The image holds the word 0x1234_5678 at an aligned address A. The chip is little-endian. Work out what a load of one byte returns at A to A + 3.
| Address | Lane | Bits | Byte |
|---|---|---|---|
| A | 0 | 7 to 0 | 78 |
| A + 1 | 1 | 15 to 8 | 56 |
| A + 2 | 2 | 23 to 16 | 34 |
| A + 3 | 3 | 31 to 24 | 12 |
Now the address unit. The memory of SRAM is an array of 16,384 words. "@00000080" in its image file means element 0x80 of that array, which is byte offset 0x200. A script that writes a byte offset after @ puts its data four times too far.
The Rules
Rule 1: Load before the first instruction.
soc_tb clears ROM and SRAM and reads the images at 2 ns, long before the core leaves reset.
Rule 2: Count @ addresses in elements of the memory.
For ap_soc's word arrays an element is 4 bytes.
Rule 3: Put bytes in their lanes, little-endian.
A back-door write of a byte, half or word must land where a bus write of the same size would.
Rule 4: Refuse what is no memory.
The bench's back-door writes count a byte outside ROM and SRAM as a bench error, not a silent skip. Such a byte means the data was meant for another place.
Rule 5: Keep one front-door boot.
The back door skips the boot path; a separate test must boot from flash through the pads.
Sources for These Rules
The loader and its plusargs: tools/soc/ap_soc/bench/soc_tb/soc_tb.sv and README.md.
Address lines of $readmemh address elements of the memory: IEEE 1800-2023 clause 21.4.
Little-endian byte order of RV32: the RISC-V Unprivileged specification, 1.5 (memory is little-endian); ap_soc sizes: facts 27.2 and 27.12.
Front-door boot as its own test (V9.3): docs/soc-verification-plan.md §6.2.
The Lab: The Image, the Lanes, the Price of the Front Door
#2;
for (int i = 0; i < 1024; i++) dut.u_rom.mem[i] = 32'h0;
for (int i = 0; i < 16384; i++) dut.u_sram.memory.mem[i] = 32'h0;
if ($value$plusargs("rom=%s", rom_file)) $readmemh(rom_file, dut.u_rom.mem);
if ($value$plusargs("ram=%s", ram_file)) $readmemh(ram_file, dut.u_sram.memory.mem);Rule 1 at work: at 2 ns, both memories are cleared, then the images are read. The core is still in reset until 405 ns, as the last lesson showed.
// rv32-asm image: base 0x80000000, one 32-bit word per line
@00000000
100042b7
00200313The file is a comment, an address line and one word a line. Line 69 holds 12345678. The program reads it as a word and then one byte at a time.
CONSOLE 508: lw=12345678 lbu at +0..+3: 78 56 34 12 Rule 3 at work: the byte at the lowest address is 78, the least significant byte. The table by hand said the same.
tb poke 80000100 11223344 00000004 00000000 0 1
tb poke 80000105 a1b2c3d4 00000004 00000000 0 1
tb poke 8000010e 00005566 00000002 00000000 0 1
tb poke 80000113 00000077 00000001 00000000 0 1
tb poke 00000ffc cafef00d 00000004 00000000 0 1The bench ran these writes and then read the words through the front door, each against the value a little-endian model gives. Verdict: pass. A second table wrote three values that fall partly or wholly outside both memories.
BENCH 10: back door: 00001000 is neither ROM nor SRAM
BENCH 10: back door: 00001001 is neither ROM nor SRAM
BENCH 11: back door: 80010000 is neither ROM nor SRAM
BENCH 12: back door: 10004020 is neither ROM nor SRAMCONSOLE 137: copy start
CONSOLE 1720: copy done, first frame=0030373eThe copy started at cycle 137 and ended at cycle 1720. That is about 66 cycles a byte, through the SPI pads and the bus. The back door loaded the whole image in zero cycles.
What would a whole image cost? This is an estimate, not a measurement. Take 66 cycles a byte from this run and 8,192 bytes, the largest image the site's chip bench accepts. That gives about 540,672 cycles, more than the bench's limit of 400,000 cycles for a whole run. This flash model holds only 256 bytes anyway, so the bench could not even run that boot.
Why It Is Built This Way
A chip regression runs thousands of firmware tests. Boot from flash in each one and most of the night is boot. Load by the back door and each test starts at its own first instruction. The boot path is not forgotten: it gets its own test, and that one runs the slow way on purpose.
The model compare must start from the same bytes. When the core leaves reset, the bench copies ROM and SRAM from the chip into the reference model. A back-door write during a run goes to both. A back door that wrote only the chip would make the next load of that byte a mismatch.
Traps
The classic trap is the unit of @. A script that builds images from sections writes byte offsets, because that is what the linker prints. $readmemh reads them as word indices. The data lands four times too far, the firmware reads zeros, and the failure looks like a firmware bug.
The second trap is byte order in a hand-made data file. Someone types a table of 32-bit constants as bytes in reading order, 12 34 56 78. On a little-endian chip the firmware then sees 0x7856_3412. Generate data files with the same tool as the program, or check one word by a load in the test.
At Scale
This section looks ahead. ap_soc has 4 KiB of ROM and 64 KiB of SRAM.
Big memories
A real SoC has megabytes of SRAM and gigabytes of DRAM behind a controller. Back doors write the DRAM model's arrays, through its address map.
Back doors in UVM
A UVM register model can read and write registers and memories by the back door. On this site's engine that calls a bench task (lesson V3.6).
Boot tests
Every boot device and strap gets one front-door test; everything else loads by the back door.
Try It
Run it: the back door loads eight words at 0 ns; the front door takes 160 ns for the same words.
Read the last line: the byte at the lowest address is 78.
Make the front door 3 cycles a word. When does it finish?
back door: 8 words loaded at 0 ns front door: 8 words loaded at 160 ns bytes of 0x12345678 at +0..+3: 78 56 34 12Check Yourself
Common Mistakes
Wrong Write byte offsets after @ in an image for a word array.
$readmemh counts elements of the memory, so the data lands four times too far.
Fix Divide by 4 for a 32-bit memory, or let the toolchain write the file.
Wrong Skip bytes that fall outside every memory without a word.
An image linked for the wrong address then loads nothing, silently.
Fix Count every refused byte as a bench error.
Wrong Never boot through the front door because the back door is faster.
The boot ROM, the flash interface and the copy loop then go untested.
Fix Keep one front-door boot test for every boot mode.
Remember
- The back door writes ROM and SRAM flops before the first edge: zero cycles, no bus.
- An image file has one word a line; @ addresses count elements of the memory, words here.
- ap_soc is little-endian: the lowest address holds the least significant byte.
- Refuse bytes that land in no memory, and keep one front-door boot as its own test.