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.

An Image Lands in ROM and SRAM in Zero TimeTwo ways into the same memory. The back door writes the flops before the first clock edge; the front door is a real chip's boot path.
  • Tool
  • File
  • Test
  • Memory
  • External
  • Processor
  • Data
An Image Lands in ROM and SRAM in Zero TimeTwo ways into the same memory. The back door writes the flops before the first clock edge; the front door is a real chip's boot path. tool apcc or rv32-asm compile, link, write theimage file Image file one 32-bit word aline, @ addresses test $readmemh at time0 the back door memory ROM 4 KiB u_rom.mem memory SRAM 64 KiB u_sram.memory.mem external SPI flash model 3 bytes a frame processor Core boot code copies hex image+rom= +ram=flops, no busflops, no busSPI padsbus writes
  1. tool apcc or rv32-asmcompile, link, write the image
    • hex image → Image file
  2. file Image fileone 32-bit word a line, @ addresses
    • +rom= +ram= → $readmemh at time 0
  3. test $readmemh at time 0the back door
    • flops, no bus → ROM 4 KiB
    • flops, no bus → SRAM 64 KiB
  4. memory ROM 4 KiBu_rom.mem
  5. memory SRAM 64 KiBu_sram.memory.mem
  6. external SPI flash model3 bytes a frame
    • SPI pads → Core
  7. processor Coreboot code copies
    • bus writes → SRAM 64 KiB
  1. The image is a file

    The toolchain writes the program as words, with @ lines that say where they go.

  2. The back door

    Before the first edge the bench reads the file straight into the memories' flops. No bus, no clock, no time.

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

Five questions this page answers
Question
1Why does a chip bench load its test by the back door?
2What does a line of an image file mean, and what does @ count?
3Which byte of 0x1234_5678 is at the lowest address?
4What does the back door refuse, and why should it say so?
5Why 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

New words on this page
WordMeaning
Back doorWriting or reading a memory's flops directly, with no bus transfer and no simulated time.
Front doorReaching a memory through the chip's own bus, as software does.
Image fileThe program as text: one 32-bit word a line, in the format $readmemh reads.
$readmemhA SystemVerilog system task that reads hex words from a file into a memory array.
Address lineA line "@n" in an image file: the next word goes to element n of the memory.
Little-endianThe byte at the lowest address is the least significant byte of the word; ap_soc is little-endian.
LaneOne 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.

Byte loads of one word, by hand
AddressLaneBitsByte
A07 to 078
A + 1115 to 856
A + 2223 to 1634
A + 3331 to 2412

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

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

  2. Rule 2: Count @ addresses in elements of the memory.

    For ap_soc's word arrays an element is 4 bytes.

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

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

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

The loader of soc_tb, at the start of the initial block that runs the test (soc_tb.sv).
    #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.

The first lines of the SRAM image of the byte-order program (69 lines; the data word is line 69).
// rv32-asm image: base 0x80000000, one 32-bit word per line
@00000000
100042b7
00200313

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

What the program printed on the chip.
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.

Back-door writes of every size and alignment, as tb poke rows (address, data, bytes).
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 1

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

Rule 4 at work: the bench's own errors for bytes that land nowhere (four of its seven lines).
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 SRAM
The front door: ROM code that copies 24 bytes from the SPI flash model into SRAM, three bytes a frame.
CONSOLE 137: copy start
CONSOLE 1720: copy done, first frame=0030373e

The 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has 4 KiB of ROM and 64 KiB of SRAM.

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

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

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

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

Check Yourself

Predict the outputThe front door now takes 3 cycles a word. What is the last line printed?
`timescale 1ns/1ns
module tb;
  bit [31:0] mem [0:7];
  bit [31:0] word;
  initial begin
    // The back door: the bench writes the memory's flops directly, before the first clock edge.
    for (int i = 0; i < 8; i++) mem[i] = 32'h8000_0000 + 4 * i;
    $display("back door: 8 words loaded at %0t ns", $time);
    // The front door: every word is one bus write of 3 cycles of 10 ns.
    for (int i = 0; i < 8; i++) begin #30; mem[i] = 32'h8000_0000 + 4 * i; end
    $display("front door: 8 words loaded at %0t ns", $time);
    // Byte order: ap_soc is little-endian, so byte n of a word is bits 8n+7 to 8n.
    word = 32'h12345678;
    $display("bytes of 0x%08h at +0..+3: %02h %02h %02h %02h", word, word[7:0], word[15:8], word[23:16], word[31:24]);
  end
endmodule
ConceptWhy does soc_tb load firmware by the back door?
ConceptThe word 0xAABB_CCDD is at address A. What does a byte load at A + 1 return?
ConceptAn image for SRAM has the line "@00000200" before a data block. At which byte offset does the block start?
ConceptIn the lab, the front-door copy cost about 66 cycles a byte. What is that number?

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.

Practice

Report a problem with this page