One Portable C Test, Several Targets
Write a chip test once and run it on the host, on the core, on the virtual platform and on the hybrid. A thin hardware layer makes that possible; a few habits of C make it break.
You Will Be Able To
- Write a test against hal.h and tb.h that builds for the host and for RV32.
- Explain how a host-native test reaches the chip through DPI, one transfer at a time.
- Name what a host-native build cannot do, and which C assumptions differ between the host and RV32.
- Say what portable stimulus is, and what this site does not teach of it.
- Lesson
- V13.4
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A test passes host-native on Monday, with no processor in the way. On Tuesday the firmware team builds the same file for the core, and it prints a different line.
- File
- Test
- Processor
- Manager
- Model
- Data
- Transaction / TLM
- One source
The test calls hal.h and tb.h only.
- Two builds
For the host, hal.h's two functions become DPI calls. For RV32 they are volatile loads and stores.
- Four targets
The host build drives the chip from the core's seat. The RV32 image runs on RTL, the VP and the hybrid.
Read the diagram as text
- main.c: hal.h, tb.h, kit drivers
- Host build: reg_read32 is a DPI transfer
- RV32 build: reg_read32 is a volatile load
- Bus-manager seat: chip RTL, no core
- Chip RTL: the real core
- Virtual platform: models
- Hybrid: model core, RTL devices
- main.c → Host build: cc, AP_HOST_DPI
- main.c → RV32 build: apcc
- Host build → Bus-manager seat: one transfer a turn
- RV32 build → Chip RTL: image
- RV32 build → Virtual platform: same image
- RV32 build → Hybrid: same image
tools/soc/fwkit/hal.h (the AP_HOST_DPI switch); tools/soc/ap_soc/bench/soc_tb/README.md, "A C test on the host" (soc_host.c, soc_host_shim.c).
Before You Start
Read One Bench, Two Modes and Taking the Core Out for the bus-manager seat, and Virtual Platforms. C through DPI is taught in Your First DPI-C Call.
| Question | |
|---|---|
| 1 | What makes a C test portable across targets? |
| 2 | How does a host-native test reach the chip's RTL? |
| 3 | What can a host-native test not do? |
| 4 | Which C assumptions differ between the host and RV32? |
| 5 | What is portable stimulus, and does this site teach it? |
The Idea: A Thin Layer Between the Test and the Target
A chip test says what to do: write this register, wait for that bit, check this value. It should not say how the write reaches the chip. That part changes with the target.
So every access goes through two functions, reg_read32 and reg_write32, in hal.h. On the core they are plain volatile loads and stores. On the host they are calls that ask the bench to make one bus transfer.
The test above the layer stays the same. It runs host-native before a firmware toolchain exists. It runs on the core for the full chip, and on the fast platforms for speed.
New Words
| Word | Meaning |
|---|---|
| Portable test | One source file that builds and gives the same result on every target. |
| HAL | Hardware abstraction layer: here hal.h, the two functions every register access goes through. |
| Host-native | Built for the workstation and run as a program, beside the simulator. |
| Turn | One exchange between the host test and the bench: a request, then its answer. |
| LP64, ilp32 | Size rules of C: long and pointers are 64 bits on the host (LP64) and 32 bits on RV32 (ilp32). |
| Implementation-defined | Left by the C standard for each compiler and machine to choose: the size of long, or the sign of plain char. |
| Portable stimulus | Accellera's standard for describing a test's intent once, for tools to turn into tests for several platforms. |
The Smallest Example: Two Builds of One Line
One line of the test: id = reg_read32(SPI_DATA). Follow it through both builds.
| Build | reg_read32(SPI_DATA) is | Who waits |
|---|---|---|
| RV32 | a volatile load from 0x1000_5000 | the core, on the bus |
| Host (AP_HOST_DPI) | a request over a socket; the bench makes the transfer on the seat | the host program, until the bench answers |
The test cannot tell the two apart, and that is the point. A line that bypasses hal.h, such as a raw pointer to 0x1000_5000, breaks on the host. There, that address is just memory of the host program.
The Rules
Rule 1: Touch the chip only through hal.h and tb.h.
Raw addresses, CSRs, inline assembly and interrupts need the processor.
Rule 2: Name every size and sign.
C leaves some sizes and signs to the platform, and the host and RV32 choose differently. Use fixed-width types and say signed or unsigned.
Rule 3: Never measure time in the test.
Each target takes its own time per access; a count of polls or ticks is not a result.
Rule 4: Run it on more than one target.
A test proved on one target has not proved its portability.
Rule 5: Keep the host test a guest.
It takes turns with the bench, one transfer at a time, so its speed cannot change the run.
Sources for These Rules
hal.h and its host switch AP_HOST_DPI: tools/soc/fwkit/hal.h.
The host test, the socket and the turns: the bench README, "A C test on the host".
Plan §3.6, E7: timing controls inside DPI-exported tasks are not supported, so the host test runs as its own process.
The C standard leaves the sizes and signedness of these types to the implementation. The host build is x86-64 (LP64); apcc uses -mabi=ilp32.
Portable stimulus: the Accellera Portable Test and Stimulus Standard. It is not in this site's reference library, so it is named only.
The Lab: One Test, Four Targets
#include "hal.h"
#include "tb.h"
#include "drivers/spi.h"
/* Only hal.h, tb.h and a kit driver: no pointer to a register, no CSR, no assembly. */
int main(void)
{
unsigned arg = tb_arg(0);
reg_write32(SYSCTL_PINMUX, 0xaaa); /* pads 0 to 5 serial: UART and SPI */
unsigned id = spi_transfer(0x9f); /* the flash's identity */
tb_puts("arg "); tb_put_hex(arg);
tb_puts(" flash id "); tb_put_hex(id);
tb_putc('\n');
return id == 0xc0ffee ? 0 : 1;
}
| Target | Console | End |
|---|---|---|
| Host-native (C through DPI on the seat) | arg 0000002a flash id 00c0ffee | The test ended with the pass word. |
| Chip RTL, on the core | arg 0000002a flash id 00c0ffee | The test ended with the pass word. |
| Virtual platform | arg 0000002a flash id 00c0ffee | The test ended with the pass word. |
| Hybrid | arg 0000002a flash id 00c0ffee | The test ended with the pass word. |
Rules 1 and 4 at work: four targets, one line. Host-native, the test retired no instruction on the chip (retired 0): every access was a transfer of the seat.
Now the habits of C. The same small program printed two facts about the machine it ran on:
#include "hal.h"
#include "tb.h"
/* Two things C leaves to the compiler and the machine: a size and a sign. */
int main(void)
{
volatile unsigned char raw = 0xff;
char c = (char)raw;
tb_puts("sizeof(void *) "); tb_put_hex(sizeof(void *));
tb_puts(" (char)0xff == 0xff: "); tb_put_hex(c == 0xff);
tb_putc('\n');
return 0;
}
| Target | Console |
|---|---|
| Host-native (C through DPI on the seat) | sizeof(void *) 00000008 (char)0xff == 0xff: 00000000 |
| Chip RTL, on the core | sizeof(void *) 00000004 (char)0xff == 0xff: 00000001 |
Rule 2 at work. A pointer is eight bytes on the host and four on the core. A plain char holding 0xFF equals 0xFF on the core, but on the host it is -1 and the test fails. Neither target is wrong; the test is.
The same holds for every type whose size or sign C leaves open. The host is LP64 with a signed char; RV32 with the ilp32 ABI has 32-bit long and pointers and an unsigned char. Code that depends on one of these choices runs on both and gives two answers.
Why It Is Built This Way
Why does the host test run as a separate process, not inside the simulator? Because the simulator's DPI cannot pause inside an exported task for clock edges. The bench therefore owns time, and the test asks it for one transfer per turn.
Why one transfer at a time? So the order of events is fixed by the bench, not by how fast the host thinks. A test that ran on a loaded server gives the same run as on a quiet laptop.
Why two functions and not a full driver layer? Every extra function in the layer is code that must be ported and trusted. Two are enough; the kit drivers are built on them.
Traps
The first trap is a test that passed host-native and is called portable. Host-native proves the test logic against the RTL. It proves nothing about sizes, signs or code that only the core runs.
The second is a test that reads a register through a raw pointer. On RV32 it works. On the host the pointer is an address in the host program, and the run crashes or reads garbage.
The third is a test that needs an interrupt. The host has no processor to take one, so the bench offers polling calls instead. Keep interrupt handlers in tests built for the core.
At Scale
This section looks ahead. Our layer is two functions and one bench.
One test, many tiers
Large teams run the same test on block benches, subsystems, the full chip, emulators and boards.
Generated tests
Portable stimulus tools describe a scenario once and generate C, UVM or both for each platform. This site names the standard and does not teach it.
Silicon
On the lab bench after tape-out, the RV32 build of the same test runs on the real chip.
Try It
Run it: three turns of a host test. The host is slow on one turn and fast on another.
What happens to the bench's cycle numbers when the host is slow?
Make every host turn quick. Which lines change?
turn 0: host took 30 us, asks for 10004010 at bench cycle 0 bench answers at cycle 4 turn 1: host took 5 us, asks for 10005000 at bench cycle 4 bench answers at cycle 8 turn 2: host took 200 us, asks for 10005004 at bench cycle 8 bench answers at cycle 12Check Yourself
Common Mistakes
Wrong Call a test portable because it passed host-native.
Sizes, signs and core-only code differ between the host and RV32.
Fix Run it on the host and on the core, and compare.
Wrong Keep a byte from a register in a plain char and compare it with 0xFF.
Plain char is signed on the host, so the byte becomes -1 and the comparison fails there.
Fix Keep bytes in unsigned char, or in a fixed-width unsigned type.
Wrong Read a register through a raw pointer in a portable test.
On the host the address belongs to the host program.
Fix Use reg_read32 and reg_write32.
Remember
- A portable test touches the chip only through hal.h and tb.h.
- Host-native, each access is one transfer of the bus-manager seat, taken in turns with the bench.
- The host has no processor, and C's sizes and signs differ between the host and RV32.
- Portable stimulus is a standard for generating such tests; it is named here, not taught.