Sequences as Virtual Firmware
Write a driver once, in layers, and run it with or without the core. See the same DMA test as firmware, as C on the host through the seat, and as a sequence. Then see how block-level sequences come up to the chip.
You Will Be Able To
- Split a test into a register layer, a driver layer and a scenario.
- Run one C file as firmware and as C on the host, and say what changes.
- Write a driver routine as rows of a sequence.
- Say which block-level sequences can be reused at chip level.
- Lesson
- V6.5
- Time
- 24 min
- Runs on
- Your browser
- Check
- 5 questions
The DMA team tested their block with register sequences. Now the chip team wants the same copy in firmware, and again in a test with no core. Must the routine be written three times?
- Test
- Sequence
- Control / FSM
- Processor
- Manager
- Control
- Data
- Transaction / TLM
- Layers
The test calls the driver; the driver calls the register layer. No layer reaches past the one below it.
- Two targets for one file
On the processor, reg_write32 is a store. On the host, it is one transfer of the seat. The two upper layers do not change.
- The same routine as a sequence
A table row, or a UVM sequence on the register model, does what dma_copy does, with no C at all.
Read the diagram as text
- Layer 3: the test: copy, check, a faulting copy
- Layer 2: dma_copy: start a job, wait with a limit, report
- Layer 1: reg_read32, reg_write32: hal.h
- On the processor: a load or a store
- On the host: one transfer of the seat
- As a sequence: the dma copy row; uvm_reg in UVM
- Layer 3: the test → Layer 2: dma_copy: calls
- Layer 2: dma_copy → Layer 1: reg_read32, reg_write32: calls
- Layer 1: reg_read32, reg_write32 → On the processor: firmware
- Layer 1: reg_read32, reg_write32 → On the host: C on the host
- Layer 2: dma_copy → As a sequence: the same routine, as rows
The lab's DMA driver (v6-labs.mjs, DMA_C), hal.h of the firmware kit, and soc_host_shim.c for the host.
Before You Start
Read Front Door and Back Door at Chip Level and Vertical Reuse of a Block Environment first.
| Question | |
|---|---|
| 1 | What are the three layers of a test, and what may each one know? |
| 2 | What happens to reg_write32 on the processor and on the host? |
| 3 | What does the same routine look like as a sequence? |
| 4 | What did the three forms cost? |
| 5 | Which block sequences can be reused at chip level? |
The Idea: Firmware Without a Core
Most of a peripheral test is a recipe: write these registers, wait for that bit, read the result, check it. Firmware follows the recipe on the core. A sequence follows the same recipe from the bench. Virtual firmware is that sequence: the steps a driver would take, without a processor.
The trick is layering. If only the bottom layer knows how a transfer is made, the recipe above it does not care who makes it. Then one routine serves the processor, the seat and, written as a sequence, a UVM test too.
New Words
| Word | Meaning |
|---|---|
| Layer | A level of a test that calls only the level below it. |
| Register layer | The bottom layer: one read or write of a register (reg_read32 in C, a core row, uvm_reg read in UVM). |
| Driver routine | A layer that knows one block's registers: start a job, wait for its end, report it. |
| Virtual firmware | A sequence that does what a driver would do, run from the bench with no processor. |
| Retarget | Run the same upper layers over another register layer: the processor, the seat, a block bench. |
| Bounded wait | A poll that gives up after a set number of reads and reports it, instead of waiting for ever. |
The Smallest Example: A Flash Read in Three Layers
A toy flash answers each read command with three bytes. Layer 1 sends one command and returns its three bytes. Layer 2 reads n bytes by calling layer 1 as often as it needs. Layer 3 asks for 7 bytes.
| Layer | Calls | Why |
|---|---|---|
| 3: the test | 1 | one request for 7 bytes |
| 2: read n bytes | 1 | called once by the test |
| 1: one frame | 3 | 3 + 3 + 1 bytes: the third frame brings 3 and uses 1 |
The example below prints the same counts.
The Rules
Rule 1: Only the register layer knows how a transfer is made.
In C that is hal.h; in a table, a core row; in UVM, the register model's read and write.
Rule 2: A driver routine knows its block and nothing else.
It starts a job, waits for the end with a limit, and reports how the job ended.
Rule 3: Every wait has a limit.
On a faulty chip a bit may never set. A bounded wait turns that into a failure the bench can see, not a hang.
Rule 4: The scenario checks results, not steps.
It calls drivers and compares what the chip did with what it expected.
Rule 5: A routine written against registers can be retargeted.
One written against a block's pins cannot leave the block bench.
Rule 6: Interrupt-driven drivers need a core.
With the seat there is no handler, so virtual firmware polls, or waits for a pin.
Sources for These Rules
C on the host: tools/soc/ap_soc/bench/soc_tb/README.md ("A C test on the host"), soc_host.h, soc_host_shim.c; facts 27.12.
Register sequences written against the model, so they can be reused or retargeted in another integration: UVM-CB, Registers/SequenceExamples. Layering sequences: UVM-CB, Sequences/Layering. Vertical reuse: UVM-CB, Sequences/Virtual.
A sequence that uses the register model: IEEE 1800.2-2020, 19.4.1 (uvm_reg_sequence).
The Lab: One DMA Test in Three Forms
The driver below has three layers. dma_start and dma_wait know the DMA's registers; dma_copy reports how a job ended; main is the scenario. It copies 12 bytes in SRAM and checks them. Then it asks for a copy from an address in no region, which must end in FAULT.
#include "hal.h"
#include "tb.h"
/* Layer 2, the driver: one job, through hal.h (layer 1) */
static void dma_start(unsigned src, unsigned dst, unsigned len)
{
reg_write32(DMA_SRC, src);
reg_write32(DMA_DST, dst);
reg_write32(DMA_LEN, len);
reg_write32(DMA_STATUS, DMA_STATUS_DONE_MASK | DMA_STATUS_FAULT_MASK);
reg_write32(DMA_START, 1);
}
static unsigned dma_wait(void)
{
for (unsigned tries = 0; tries < 1000; tries++) {
unsigned s = reg_read32(DMA_STATUS);
if (s & (DMA_STATUS_DONE_MASK | DMA_STATUS_FAULT_MASK)) return s;
}
return 0;
}
/* Still layer 2: a copy that reports how it ended */
static int dma_copy(unsigned src, unsigned dst, unsigned len)
{
dma_start(src, dst, len);
return dma_wait() == DMA_STATUS_DONE_MASK;
}
/* Layer 3: the test */
int main(void)
{
const unsigned src = 0x80003000, dst = 0x80003100;
for (unsigned i = 0; i < 3; i++) reg_write32(src + 4 * i, 0x11111111u * (i + 1));
if (!dma_copy(src, dst, 12)) return 1;
for (unsigned i = 0; i < 3; i++)
if (reg_read32(dst + 4 * i) != 0x11111111u * (i + 1)) return 2;
if (dma_copy(0x40000000, dst, 4)) return 3; /* a source in no region must end in FAULT */
tb_puts("dma ok\n");
return 0;
}The same file was built twice. As firmware it was compiled for the processor and loaded by the back door. As C on the host it was compiled for the server and run as a process of its own. There, each reg_read32 and reg_write32 became one transfer of the seat.
firmware (processor): verdict=pass cycles=486 retired=244 bus_transfers=84 dma_bytes=12 console="dma ok"
C on the host (seat): verdict=pass cycles=162 retired=0 bus_transfers=64 dma_bytes=12 console="dma ok"
sequence table (seat): verdict=pass cycles=144 retired=0 bus_transfers=58 dma_bytes=12core w 80003000 11111111 ffffffff 00000000 0 1
core w 80003004 22222222 ffffffff 00000000 0 1
core w 80003008 33333333 ffffffff 00000000 0 1
dma copy 80003000 80003100 0000000c 00000002 0 1
core r 80003100 00000000 ffffffff 11111111 0 1
core r 80003104 00000000 ffffffff 22222222 0 1
core r 80003108 00000000 ffffffff 33333333 0 1
core w 10006014 00000006 ffffffff 00000000 0 1
dma copy 40000000 80003100 00000004 00000004 0 1Rule 5 at work: the C file did not change between the two targets, and both printed dma ok. The DMA moved 12 bytes in both runs. The firmware run took 486 cycles because the core fetched and ran 244 instructions around those transfers. On the host the same C cost no instruction of the chip at all: 162 cycles.
The table is shorter still, 144 cycles, because its dma copy row is a driver routine the bench already has. That is the reuse this lesson is about, in its smallest form.
Why It Is Built This Way
Why does C on the host run as a separate process that takes turns with the bench? Because a C function cannot wait on the simulator's clock here. Stage E7 probed it: Verilator 5.032 does not support timing controls inside a task that C calls through DPI. So the test sends one request, waits for the answer, and sends the next.
Why keep a table front end when C exists? Because a table needs no compiler, and its rows are the checks themselves. C wins when the recipe has loops, arithmetic or data to compute.
Traps
The raw-pointer trap breaks retargeting. A driver that writes *(volatile unsigned *)0x10006000 = src works on the processor. On the host that line is not a bus transfer at all: it touches the host process's own memory at that address. Always go through the register layer.
The timing-luck trap: a driver that waits by counting an empty loop. On the processor its length depends on the compiler, and at -Os an empty loop can vanish. On the host it takes no simulated time at all. Wait by polling a register, with a limit. Even the kit's dma_wait polls with no limit. On a chip whose DMA never ends, it hangs until the bench's cycle limit.
The pin trap: a block sequence that drives the block's own ports cannot be reused at chip level. Inside the chip those ports belong to the fabric. Only sequences written against registers come up, and only their checks against the block's outputs come up as passive monitors.
At Scale
This section looks ahead. ap_soc has one small DMA and a handful of drivers.
Sequence libraries per block
Each IP ships register sequences. The chip team layers them into virtual sequences that run several blocks at once.
One test, many platforms
Teams write portable C tests once. They run them on the seat, on the core, on a virtual platform and on an emulator. Module V13 does that on this chip.
Generated stimulus
Standards such as Accellera's Portable Test and Stimulus describe a scenario once and generate the C and the sequences from it.
Try It
Run it: the test asks for 7 bytes, and layer 1 makes 3 frames.
Ask for 12 bytes (the predict question below). Which layer's count changes?
Where would you add a bounded wait, if a frame could fail to end?
read 7 bytes in 3 frames and 1 call of layer 2 last byte 36Check Yourself
Common Mistakes
Wrong Write registers through raw pointers in a driver meant to run on the host.
On the host a pointer is not a bus transfer.
Fix Use reg_read32 and reg_write32 from hal.h everywhere.
Wrong Wait for a DMA job with an empty counting loop.
Its length is compiler luck on the core and zero on the host.
Fix Poll the status register with a limit.
Wrong Bring a block sequence that drives the block's ports up to the chip.
At chip level the fabric owns those ports.
Fix Reuse register sequences; reuse port monitors as passive checkers.
Remember
- Layer a test: register access, driver routine, scenario.
- The same C runs as firmware and on the host; only the register layer changes.
- A driver routine can also be a sequence: a table row, or a sequence on the register model.
- Only register-level sequences, with bounded waits, come up from the block to the chip.