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.

One Source File, Four ArrowsOne C test, written against hal.h and tb.h, is built four ways. Only the two functions of hal.h change.
  • File
  • Test
  • Processor
  • Manager
  • Model
  • Data
  • Transaction / TLM
One Source File, Four ArrowsOne C test, written against hal.h and tb.h, is built four ways. Only the two functions of hal.h change. file main.c hal.h, tb.h, kit drivers test Host build reg_read32 is a DPItransfer processor RV32 build reg_read32 is avolatile load manager Bus-manager seat chip RTL, no core processor Chip RTL the real core model Virtual platform models model Hybrid model core, RTLdevices cc, AP_HOST_DPIapccone transfer a turnimagesame imagesame image
  1. file main.chal.h, tb.h, kit drivers
    • cc, AP_HOST_DPI → Host build
    • apcc → RV32 build
  2. test Host buildreg_read32 is a DPI transfer
    • one transfer a turn → Bus-manager seat
  3. processor RV32 buildreg_read32 is a volatile load
    • image → Chip RTL
    • same image → Virtual platform
    • same image → Hybrid
  4. manager Bus-manager seatchip RTL, no core
  5. processor Chip RTLthe real core
  6. model Virtual platformmodels
  7. model Hybridmodel core, RTL devices
  1. One source

    The test calls hal.h and tb.h only.

  2. Two builds

    For the host, hal.h's two functions become DPI calls. For RV32 they are volatile loads and stores.

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

Five questions this page answers
Question
1What makes a C test portable across targets?
2How does a host-native test reach the chip's RTL?
3What can a host-native test not do?
4Which C assumptions differ between the host and RV32?
5What 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

New words on this page
WordMeaning
Portable testOne source file that builds and gives the same result on every target.
HALHardware abstraction layer: here hal.h, the two functions every register access goes through.
Host-nativeBuilt for the workstation and run as a program, beside the simulator.
TurnOne exchange between the host test and the bench: a request, then its answer.
LP64, ilp32Size rules of C: long and pointers are 64 bits on the host (LP64) and 32 bits on RV32 (ilp32).
Implementation-definedLeft by the C standard for each compiler and machine to choose: the size of long, or the sign of plain char.
Portable stimulusAccellera'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.

What one call becomes
Buildreg_read32(SPI_DATA) isWho waits
RV32a volatile load from 0x1000_5000the core, on the bus
Host (AP_HOST_DPI)a request over a socket; the bench makes the transfer on the seatthe 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

  1. Rule 1: Touch the chip only through hal.h and tb.h.

    Raw addresses, CSRs, inline assembly and interrupts need the processor.

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

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

  4. Rule 4: Run it on more than one target.

    A test proved on one target has not proved its portability.

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

The lab test (TB_ARG = 0x2A)
#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;
}
TargetConsoleEnd
Host-native (C through DPI on the seat)arg 0000002a flash id 00c0ffeeThe test ended with the pass word.
Chip RTL, on the corearg 0000002a flash id 00c0ffeeThe test ended with the pass word.
Virtual platformarg 0000002a flash id 00c0ffeeThe test ended with the pass word.
Hybridarg 0000002a flash id 00c0ffeeThe 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:

Sizes and signs
#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;
}
TargetConsole
Host-native (C through DPI on the seat)sizeof(void *) 00000008 (char)0xff == 0xff: 00000000
Chip RTL, on the coresizeof(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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. Our layer is two functions and one bench.

  1. One test, many tiers

    Large teams run the same test on block benches, subsystems, the full chip, emulators and boards.

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

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

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

Check Yourself

Predict the outputEvery host turn now takes 1 us. What is the last line printed?
// One host-native C test: it sends a request and waits; the bench runs the transfer on the seat and answers.
// host_delay is how long the host process took to think, in real time; bus cycles are the bench's own.
module tb;
  int cycle = 0;
  int host_delay [0:2];
  logic [31:0] addr [0:2];
  initial begin
    host_delay[0] = 1;
    host_delay[1] = 1;
    host_delay[2] = 1;
    addr[0] = 32'h1000_4010; addr[1] = 32'h1000_5000; addr[2] = 32'h1000_5004;
    for (int k = 0; k < 3; k++) begin
      // the host thinks; the bench waits and its clock does not move
      $display("turn %0d: host took %0d us, asks for %h at bench cycle %0d", k, host_delay[k], addr[k], cycle);
      cycle += 4;                            // the transfer on the seat
      $display("        bench answers at cycle %0d", cycle);
    end
    $finish;
  end
endmodule
ConceptWhat makes a C test portable here?
ConceptHow does a host-native test reach the RTL?
ConceptWhat does the host build not have?
ConceptRecorded: what was sizeof(void *) on the core?

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.

Practice

Report a problem with this page