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.
- File
- Tool
- Processor
- Bus
- Peripheral
- Data
- A pointer to a fixed address
The C line stores through a pointer whose value is the register address.
- The compiler decides
The compiler may keep, merge or delete the store unless volatile forbids it.
- One instruction, one transfer
The core executes sw. The bus carries one write to 0x1000_1008.
- 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.
| Question | |
|---|---|
| 1 | How does C name a register? |
| 2 | Why may the compiler drop, merge or move a register access? |
| 3 | What does volatile promise? |
| 4 | How many bus accesses does a fragment of C really make? |
| 5 | How 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
| Word | Meaning |
|---|---|
| Register | A storage place inside a device, reached at a fixed address on the bus. |
| Load and store | The instructions that read and write memory: lw and sw on RISC-V. A register access is one of them. |
| volatile | A C keyword: every access to this object must happen, in order, exactly as the source says. |
| hal.h | The small header of register helpers, reg_read32 and reg_write32, used by every V0 test. |
| Host-native | Running 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.
void wait_plain(void) { while ((IN_P & 8u) == 0) { } }
void wait_vol(void) { while ((IN_V & 8u) == 0) { } }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
retRead 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
Rule 1: A register is an address.
Reach it through a pointer to a fixed address, or through hal.h.
Rule 2: Every register access is volatile.
Then each read asks the hardware, and each write reaches it, in order.
Rule 3: Count executions.
A loop with one volatile load makes one bus read on every pass.
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.
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 1Walk 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.
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 1Read 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
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.
#define IN (*(uint32_t *)0x10001004u)#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.