The Generated Header
Chip tests never type register addresses by hand. Learn to use the generated header ap_soc_regs.h and why it gives offsets rather than struct overlays. See how a hand-written overlay drifts away from the hardware.
You Will Be Able To
- Compute a register address and a field value from header names.
- Explain why one description feeds RTL, header and document.
- Explain why ap_soc uses offsets, and how a struct overlay can drift.
- Lesson
- V0.3
- Time
- 24 min
- Runs on
- Verilator with C/C++
- Check
- 5 questions
A test reads GPIO STATUS and gets the wrong bits, though the address in the document is right. The test used a struct that someone typed by hand, and it left out two registers. Every member after the gap now sits eight bytes too low.
- File
- Tool
- Design
- Report
- Data
- One source
Every register is described once: name, offset, fields, reset value, access type.
- Generated, never typed
A generator writes the decoder, the header and the document from that one file.
- Three views agree
Hardware, firmware and readers see the same offsets, because nobody copied them by hand.
Read the diagram as text
- Register description: regs/uart.json: names, offsets, fields, access
- Generator: regmap
- RTL decoder: what the hardware does
- ap_soc_regs.h: what the firmware uses
- Document: what people read
- Register description → Generator: one source
- Generator → RTL decoder: decode
- Generator → ap_soc_regs.h: names, masks
- Generator → Document: tables
The flow planned for ap_soc v1.5 (regmap is to be built). The header used here is written in the generated style from the facts file.
Before You Start
Read Bits, Masks and Fields first. The UART registers come from Serial Peripherals; the map from The Memory Map.
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 | Why is the header generated, not typed? |
| 2 | What does ap_soc_regs.h give you? |
| 3 | How do you form an address and a field value from its names? |
| 4 | What is a struct overlay, and how can it drift? |
| 5 | Why does ap_soc choose offsets? |
The Idea: Copied Numbers Drift
Every register offset exists in at least three places: the RTL decoder, the header the firmware uses, and the document people read. If each is typed by hand, they drift. A late change moves a register in the RTL, the document is updated, and a header in some test folder is not.
Chip teams solve this by describing each register once, in a data file, and generating the rest. The plan for ap_soc v1.5 does exactly that with a generator called regmap. A test then uses only names from the generated header. If a register moves, the header moves with it, and every test follows.
New Words
| Word | Meaning |
|---|---|
| Register description | A data file that lists every register once: name, offset, fields, reset value and access type. |
| Generated header | A C header written by a tool from the register description, never edited by hand. |
| Offset | The distance in bytes from a block's base address to one of its registers. |
| Struct overlay | A C struct whose members are laid out like a block's registers, placed at the block's base address. |
| Access type | How a register behaves: read only, write only, read and write, or write-1-to-clear. The header states it in a comment. |
Reading ap_soc_regs.h
/* ap_soc_regs.h: GENERATED from the register descriptions. Do not edit by hand.
* ap_soc map v1.0 subset: UART0, GPIO, ap_sysctl PINMUX. Access types are in the comments. */
#ifndef AP_SOC_REGS_H
#define AP_SOC_REGS_H
/* ---- UART0 ---- */
#define AP_UART0_BASE 0x10000000u
#define AP_UART_TXDATA 0x00u /* WO low byte = next byte to send */
#define AP_UART_STATUS 0x04u /* RO */
#define AP_UART_STATUS_ROOM (1u << 0) /* transmitter can take a byte */
#define AP_UART_STATUS_RX_READY (1u << 1) /* at least one received byte waits */
#define AP_UART_STATUS_RX_FULL (1u << 2) /* the receive queue is full */
#define AP_UART_IRQ_ENABLE 0x08u /* WO bit 0: ROOM may raise the request */
#define AP_UART_RXDATA 0x0Cu /* RO oldest received byte; THE READ REMOVES IT */
/* ---- GPIO (bit n = pin n, 16 pins) ---- */
/* ---- ap_sysctl ---- */Each register has a base and an offset: AP_UART0_BASE plus AP_UART_STATUS is the STATUS address. Each bit has a mask: AP_UART_STATUS_RX_READY is 1 shifted left by 1. The comment carries the access type, and one comment is in capitals for a reason: a read of RXDATA removes the byte.
Offsets or a Struct Overlay
There are two common styles. With offsets, an access is a base plus a named offset, passed to a helper. With a struct overlay, you declare a struct with one member per register, point it at the base, and write gpio->status. Overlays read nicely, and many vendor headers use them.
An overlay has one weakness: a member's offset is the sum of every member before it. Leave out a register you do not use, or reserved space, and every later member moves. The compiler cannot know, because the struct is still valid C. Offsets name each address directly, so a missing register changes nothing else.
ap_soc chooses offsets, for two reasons. The host-native build of this module and of V13 reaches registers through hal.h calls, not pointers. Offsets work there, and overlays would not. And generated offsets cannot drift by omission. This is our choice, not a rule of the industry.
The Rules
Rule 1: Use names, never numbers.
Addresses and masks come from the generated header.
Rule 2: Read the access type in the comment.
Read only, write only, write-1-to-clear and reads with side effects change how you write the test.
Rule 3: Never edit a generated file.
Change the register description and generate again.
Rule 4: If you write an overlay, declare every word.
Reserved space needs members too, or later registers move.
Sources for These Rules
The generated header and the regmap flow: docs/soc-verification-plan.md section 3.3, the firmware kit.
Data files are never kept by hand in two places: the same plan, section 3.2.
Register offsets: docs/soc/ap_soc-facts.md sections 5, 6 and 7. Struct layout and offsetof follow the C standard (ISO C11, sections 6.7.2.1 and 7.19); not in the library, common practice.
Now Run It: The Header Against a Hand-Written Overlay
The lab asks the C two things. First, where is GPIO STATUS? It asks the header, and a struct overlay someone typed with nine members, skipping two unused registers. Second, it reads UART0 STATUS by name and decodes the three bits with the header's masks.
GPIO STATUS offset: header 24, struct 1c
bus read 10000004 -> 00000003
UART0 STATUS: ROOM=1 RX_READY=1 RX_FULL=0The first line is the opening scene, measured: the header says 24, the overlay says 1c. Eight bytes are missing, the two skipped registers. The decode reads 10000004, gets 3, and reports ROOM and RX_READY set, RX_FULL clear.
The Trap: A Header in a Test Folder
Copying a header into a test folder feels harmless: it compiles, and the tests pass. Months later the register description changes, the real header is generated again, and the copy is not. The tests now pass against the old layout, on hardware that no longer has it.
Include the generated header from its one location, and treat any hand-typed address in a test as a bug to fix.
Try It
Run the lab. Explain the difference between 24 and 1c in registers.
Make the bench return STATUS = 6. Predict the last line.
Read describe.c: add the two missing members to the overlay in your head, and say where status lands.
GPIO STATUS offset: header 24, struct 1c bus read 10000004 -> 00000003 UART0 STATUS: ROOM=1 RX_READY=1 RX_FULL=0Read 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: ap_soc_regs.h
/* ap_soc_regs.h: GENERATED from the register descriptions. Do not edit by hand.
* ap_soc map v1.0 subset: UART0, GPIO, ap_sysctl PINMUX. Access types are in the comments. */
#ifndef AP_SOC_REGS_H
#define AP_SOC_REGS_H
/* ---- UART0 ---- */
#define AP_UART0_BASE 0x10000000u
#define AP_UART_TXDATA 0x00u /* WO low byte = next byte to send */
#define AP_UART_STATUS 0x04u /* RO */
#define AP_UART_STATUS_ROOM (1u << 0) /* transmitter can take a byte */
#define AP_UART_STATUS_RX_READY (1u << 1) /* at least one received byte waits */
#define AP_UART_STATUS_RX_FULL (1u << 2) /* the receive queue is full */
#define AP_UART_IRQ_ENABLE 0x08u /* WO bit 0: ROOM may raise the request */
#define AP_UART_RXDATA 0x0Cu /* RO oldest received byte; THE READ REMOVES IT */
/* ---- GPIO (bit n = pin n, 16 pins) ---- */
#define AP_GPIO_BASE 0x10001000u
#define AP_GPIO_DIR 0x00u /* RW 1 = output driver on; reset 0 */
#define AP_GPIO_IN 0x04u /* RO pin levels */
#define AP_GPIO_OUT 0x08u /* RW saved output value */
#define AP_GPIO_SET 0x0Cu /* WO 1 sets that OUT bit */
#define AP_GPIO_CLR 0x10u /* WO 1 clears that OUT bit */
#define AP_GPIO_TGL 0x14u /* WO 1 inverts that OUT bit */
#define AP_GPIO_IRQ_ENABLE 0x18u /* RW reset 0 */
#define AP_GPIO_LEVEL_MODE 0x1Cu /* RW 1 = level cause, 0 = edge cause; reset 0 */
#define AP_GPIO_ACTIVE_HIGH 0x20u /* RW 1 = high level or rising edge; reset all ones */
#define AP_GPIO_STATUS 0x24u /* R/W1C read: raw status; write 1: clear that edge flag */
#define AP_GPIO_MASKED_STATUS 0x28u /* RO STATUS & IRQ_ENABLE */
/* ---- ap_sysctl ---- */
#define AP_SYSCTL_BASE 0x10004000u
#define AP_SYSCTL_PINMUX 0x10u /* RW two bits per pad, pads 0 to 3 */
#define AP_PINMUX_PAD_SHIFT(pad) (2u * (pad))
#define AP_PINMUX_PAD_MASK(pad) (3u << AP_PINMUX_PAD_SHIFT(pad))
#define AP_PINMUX_RELEASED 0u
#define AP_PINMUX_GPIO 1u
#define AP_PINMUX_SERIAL 2u
#define AP_PINMUX_AUX 3u
#endif
Read included file: describe.h
/* describe.h: the function you write. */
#ifndef DESCRIBE_H
#define DESCRIBE_H
#include "hal.h"
#ifdef __cplusplus
extern "C" {
#endif
int status_offset_from_header(void);
int status_offset_from_struct(void);
int uart_status_decoded(void);
#ifdef __cplusplus
}
#endif
#endif
Read included file: describe.c
/* describe.c: use the generated header, and compare it with a hand-written struct overlay. */
#include <stddef.h>
#include "describe.h"
#include "ap_soc_regs.h"
/* Someone wrote this overlay of GPIO by hand and left out two registers they did not use. */
struct gpio_overlay {
uint32_t dir, in, out, set, clr, tgl, irq_enable, status, masked_status;
};
int status_offset_from_header(void) { return (int)AP_GPIO_STATUS; }
int status_offset_from_struct(void) { return (int)offsetof(struct gpio_overlay, status); }
/* Read UART0 STATUS by name and decode it with the header's masks. */
int uart_status_decoded(void) {
uint32_t s = reg_read32(AP_UART0_BASE + AP_UART_STATUS);
return ((s & AP_UART_STATUS_ROOM) ? 100 : 0) + ((s & AP_UART_STATUS_RX_READY) ? 10 : 0) + ((s & AP_UART_STATUS_RX_FULL) ? 1 : 0);
}
Check Yourself
Common Mistakes
Wrong Type register addresses into a test.
They drift when the register description changes.
Fix Use names from the generated header.
Wrong Leave unused registers out of a struct overlay.
Every later member moves to a lower offset.
Fix Declare every word, with reserved members for gaps; or use offsets.
Wrong Ignore the access type in the header comment.
A read of RXDATA removes a byte; a second read loses it.
Fix Read the comment before you write the access.
Remember
- Register addresses are described once and generated into RTL, header and document.
- A test uses base plus offset and mask names from ap_soc_regs.h, never typed numbers.
- A struct overlay's members depend on every member before them, so omissions move registers.
- ap_soc chooses offsets: they work host-native through hal.h and cannot drift by omission.