A Register Is an Address

In a chip test, writing a register is a store to an address, and reading one is a load. Learn why the compiler may drop or merge those accesses, and how volatile stops it.

You Will Be Able To

  • Write a register access in C through a pointer to a fixed address.
  • Say what volatile forbids the compiler to do, and why registers need it.
  • Count the bus accesses a small C fragment really makes.
Lesson
V0.1
Time
24 min
Runs on
Verilator with C/C++
Check
5 questions

Your first C test waits for a button, and the simulation hangs forever. The button was pressed, the pin went high, and the loop never noticed. The compiler read the pin once and decided the answer could never change.

One Line of C Becomes One Bus WriteStep through the journey of one register store, from the C source to the register in the device.
  • File
  • Tool
  • Processor
  • Bus
  • Peripheral
  • Data
One Line of C Becomes One Bus WriteStep through the journey of one register store, from the C source to the register in the device. file C source *OUT = 1; tool Compiler turns it into sw processor Core executes sw bus Bus one write transfer peripheral GPIO OUT at 0x1000_1008 sourceone sw instructionaddress, datadecoded to GPIO
  1. file C source*OUT = 1;
    • source → Compiler
  2. tool Compilerturns it into sw
    • one sw instruction → Core
  3. processor Coreexecutes sw
    • address, data → Bus
  4. bus Busone write transfer
    • decoded to GPIO → GPIO OUT
  5. peripheral GPIO OUTat 0x1000_1008
  1. A pointer to a fixed address

    The C line stores through a pointer whose value is the register address.

  2. The compiler decides

    The compiler may keep, merge or delete the store unless volatile forbids it.

  3. One instruction, one transfer

    The core executes sw. The bus carries one write to 0x1000_1008.

  4. The device acts

    The decoder picks GPIO, and OUT takes the value. A pin changes.

Read the diagram as text
  • C source: *OUT = 1;
  • Compiler: turns it into sw
  • Core: executes sw
  • Bus: one write transfer
  • GPIO OUT: at 0x1000_1008
  • C source → Compiler: source
  • Compiler → Core: one sw instruction
  • Core → Bus: address, data
  • Bus → GPIO OUT: decoded to GPIO

On the chip, the store is an instruction. On the host-native lab below, hal.h turns it into one call to the bench.

Before You Start

If SoC verification is new to you, start with An SoC as a Verifier Sees It.

Basic C helps: variables, loops, functions and pointers. You should know memory-mapped registers from Addresses and Memory-Mapped I/O and the map of The Memory Map.

The lab runs your C on the host, through DPI, as in Your First DPI-C Call. The labs of this module are small slices. They use the real addresses and interrupt numbers of ap_soc v1.5, and each page says so. The whole chip runs from module V2 on, and in the guided run of the fundamentals module.

Five questions this page answers
Question
1How does C name a register?
2Why may the compiler drop, merge or move a register access?
3What does volatile promise?
4How many bus accesses does a fragment of C really make?
5How does the host-native lab stand in for the chip?

The Idea: The Compiler Thinks It Is Memory

To the core, a register is just an address. A store to 0x1000_1008 goes out on the bus like any store, and GPIO OUT takes it. So C reaches a register through a pointer whose value is that address.

The trouble is the compiler. It assumes memory only changes when the program writes it. Under that rule, reading the same address twice is waste, and writing it twice in a row keeps only the last value. For memory, that is a fine optimisation. For a register, it is wrong, because the hardware changes the value and acts on every write.

The keyword volatile tells the compiler the truth: every access to this object must happen, in order, as written. Every register pointer in a chip test is volatile.

New Words

New words on this page
WordMeaning
RegisterA storage place inside a device, reached at a fixed address on the bus.
Load and storeThe instructions that read and write memory: lw and sw on RISC-V. A register access is one of them.
volatileA C keyword: every access to this object must happen, in order, exactly as the source says.
hal.hThe small header of register helpers, reg_read32 and reg_write32, used by every V0 test.
Host-nativeRunning the test's C on the computer itself, with the chip played by a simulation over DPI.

The Smallest Example: What the Compiler Builds

Two loops wait for GPIO pin 3. They differ in one word. The first reads IN through a plain pointer, the second through a volatile one. Here is what the RISC-V compiler made of each.

IN_P and IN_V both point at GPIO IN, 0x1000_1004; only IN_V is volatile.
void wait_plain(void) { while ((IN_P & 8u) == 0) { } }
void wait_vol(void) { while ((IN_V & 8u) == 0) { } }
Recorded with riscv64-unknown-elf-gcc 14.2.0, -march=rv32i_zicsr -mabi=ilp32 -O2 -ffreestanding -nostdlib.
wait_plain:
 li a5,268439552
 addi a5,a5,4
 lw a5,0(a5)
 andi a5,a5,8
 beq a5,zero,.L3
 ret
.L3:
 j .L3

wait_vol:
 li a4,268439552
 addi a4,a4,4
.L6:
 lw a5,0(a4)
 andi a5,a5,8
 beq a5,zero,.L6
 ret

Read wait_plain first. It loads IN once, tests bit 3, and if the bit is 0 it jumps to .L3, which jumps to itself forever. The compiler proved, under its rule, that a second load would give the same answer. wait_vol puts the lw inside the loop, so each pass asks the hardware again.

The Rules

  1. Rule 1: A register is an address.

    Reach it through a pointer to a fixed address, or through hal.h.

  2. Rule 2: Every register access is volatile.

    Then each read asks the hardware, and each write reaches it, in order.

  3. Rule 3: Count executions.

    A loop with one volatile load makes one bus read on every pass.

  4. Rule 4: Never cache a register in a variable you keep.

    A copy is a snapshot; the hardware moves on.

Sources for These Rules

The C standard defines volatile access as part of the program's observable behaviour (ISO C11, section 5.1.2.3). Not in the reference library; stated as common practice.

The listings are recorded with riscv64-unknown-elf-gcc 14.2.0, -march=rv32i_zicsr -mabi=ilp32 -O2 -ffreestanding -nostdlib.

Now Run It: Every Call Is One Transfer

The lab runs on the host. Your C calls reg_read32 and reg_write32 from hal.h. On the host, each call crosses DPI into the bench, which plays GPIO and prints the transfer. On the chip, the same helpers are volatile loads and stores, so the order and the count are the same.

The C waits for the button on pin 3, then lights the LED on pin 0. The bench holds pin 3 low for three reads.

Printed by the lab (Verilator with the C over DPI).
  bus read  10001004 -> 00000000
  bus read  10001004 -> 00000000
  bus read  10001004 -> 00000000
  bus read  10001004 -> 00000008
  bus write 10001008 <- 00000001
button seen on read 4; OUT = 0001; bus reads 4, writes 1

Walk the rows. Three reads of 10001004 return 0, the fourth returns 8, which is bit 3. Then one write puts 1 in OUT at 10001008. The last line counts four reads and one write, exactly the calls in the C. Nothing was merged or dropped.

The Trap: Reading Once, Testing Forever

The hang in the opening scene is the most common first bug in firmware tests. It has two forms. The compiler can keep the register in a processor register, as wait_plain shows. Or the programmer does it by hand: read STATUS into a variable before the loop, then test the variable.

Both fail the same way, and both pass on a fast bench where the bit is already set at the first read. Make every register pointer volatile, and put the read inside the loop. The first practice problem catches the hand-made version.

Try It

Run the lab and count the bus reads and writes in the trace.

Set low_reads to 0 in main.sv. Predict the last line before you run it.

Read button.c: find the line that makes each pass ask the hardware again.

RUNVerilator + C/C++ over DPI · simulator service
Edit the code, then run it
Expected: bus read 10001004 -> 00000000 bus read 10001004 -> 00000000 bus read 10001004 -> 00000000 bus read 10001004 -> 00000008 bus write 10001008 <- 00000001 button seen on read 4; OUT = 0001; bus reads 4, writes 1
Read included file: hal.h
/* hal.h: register access for chip tests (ASIC Point, SoC Verification V0).
 * Host-native build: every call is one bus transfer, carried to the bench over DPI.
 * On the chip, the same two functions are volatile pointer accesses (lesson V0.1). */
#ifndef HAL_H
#define HAL_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
int bus_read(int addr);              /* provided by the bench */
void bus_write(int addr, int data);  /* provided by the bench */
#ifdef __cplusplus
}
#endif
static inline uint32_t reg_read32(uint32_t addr) { return (uint32_t)bus_read((int)addr); }
static inline void reg_write32(uint32_t addr, uint32_t value) { bus_write((int)addr, (int)value); }
#endif
Read included file: button.h
/* button.h: the function you write. */
#ifndef BUTTON_H
#define BUTTON_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int wait_button_then_light(void);
#ifdef __cplusplus
}
#endif
#endif
Read included file: button.c
/* button.c: wait for the button on GPIO pin 3, then light the LED on pin 0. */
#include "button.h"
#define GPIO_IN  0x10001004u
#define GPIO_OUT 0x10001008u

int wait_button_then_light(void) {
  int reads = 0;
  uint32_t pins;
  do {
    pins = reg_read32(GPIO_IN);   /* one bus read each time round */
    reads++;
  } while ((pins & (1u << 3)) == 0);
  reg_write32(GPIO_OUT, 1u << 0);  /* one bus write */
  return reads;
}

Check Yourself

Predict the outputThe bench now holds pin 3 high from the start (low_reads = 0). What is the last line the lab prints?
// The bench plays GPIO. Pin 3 (the button) reads 0 for the first ${pressAfter} reads of IN, then 1.
module tb;
  export "DPI-C" function bus_read;
  export "DPI-C" function bus_write;
  int unsigned n_reads /*verilator public_flat_rw*/, n_writes /*verilator public_flat_rw*/;
  bit trace_on;
  function int bus_read(input int addr);
    int data;
    data = block_read(addr);
    n_reads++;
    if (trace_on) $display("  bus read  %08h -> %08h", addr, data);
    return data;
  endfunction
  function void bus_write(input int addr, input int data);
    n_writes++;
    if (trace_on) $display("  bus write %08h <- %08h", addr, data);
    block_write(addr, data);
  endfunction
  import "DPI-C" context function int wait_button_then_light();
  int unsigned low_reads /*verilator public_flat_rw*/;
  bit [15:0] out_reg /*verilator public_flat_rw*/;
  function int block_read(input int addr);
    if (addr == 32'h1000_1004) begin
      if (low_reads > 0) begin low_reads--; return 0; end
      return 32'h0000_0008;
    end
    return 0;
  endfunction
  function void block_write(input int addr, input int data);
    if (addr == 32'h1000_1008) out_reg = data[15:0];
  endfunction
  initial begin
    int n;
    trace_on = 1; low_reads = 0;
    n = wait_button_then_light();
    $display("button seen on read %0d; OUT = %04h; bus reads %0d, writes %0d", n, out_reg, n_reads, n_writes);
    $finish;
  end
endmodule
Read included file: hal.h
/* hal.h: register access for chip tests (ASIC Point, SoC Verification V0).
 * Host-native build: every call is one bus transfer, carried to the bench over DPI.
 * On the chip, the same two functions are volatile pointer accesses (lesson V0.1). */
#ifndef HAL_H
#define HAL_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
int bus_read(int addr);              /* provided by the bench */
void bus_write(int addr, int data);  /* provided by the bench */
#ifdef __cplusplus
}
#endif
static inline uint32_t reg_read32(uint32_t addr) { return (uint32_t)bus_read((int)addr); }
static inline void reg_write32(uint32_t addr, uint32_t value) { bus_write((int)addr, (int)value); }
#endif
Read included file: button.h
/* button.h: the function you write. */
#ifndef BUTTON_H
#define BUTTON_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int wait_button_then_light(void);
#ifdef __cplusplus
}
#endif
#endif
Read included file: button.c
/* button.c: wait for the button on GPIO pin 3, then light the LED on pin 0. */
#include "button.h"
#define GPIO_IN  0x10001004u
#define GPIO_OUT 0x10001008u

int wait_button_then_light(void) {
  int reads = 0;
  uint32_t pins;
  do {
    pins = reg_read32(GPIO_IN);   /* one bus read each time round */
    reads++;
  } while ((pins & (1u << 3)) == 0);
  reg_write32(GPIO_OUT, 1u << 0);  /* one bus write */
  return reads;
}
ConceptWhat does volatile tell the compiler?
ConceptIn the recorded wait_plain, how often does the loop read GPIO IN once bit 3 was 0 at the first read?
ConceptOn the host-native lab, what does one call of reg_read32 become?
ConceptA loop body holds one volatile load and runs 6 times. How many bus reads?

Common Mistakes

Wrong Read a register through a plain pointer.

The compiler may read it once and keep the value.

Fix Use a volatile pointer, or hal.h.

Before
#define IN (*(uint32_t *)0x10001004u)
After
#define IN (*(volatile uint32_t *)0x10001004u)

Wrong Read STATUS into a variable before a wait loop.

The loop tests a snapshot that never changes.

Fix Read the register inside the loop, every pass.

Wrong Count bus accesses by counting lw lines in the listing.

A loop executes its one lw many times.

Fix Count how many times each access executes.

Remember

  • A register is an address; C reaches it through a pointer to that address.
  • The compiler treats plain pointers as memory and may drop, merge or move accesses.
  • volatile makes every access happen, in order; every register access in a test is volatile.
  • Count bus accesses by executions, not by lines of assembly.

Practice

Report a problem with this page