Register Tests from the Description
A chip has too many registers to test by hand, and a hand-typed test drifts from the documents. Generate the reset and access test from the register description, run it on the seat, and read what one generated row found.
You Will Be Able To
- Turn a register description into reset-value, access and hole checks.
- Choose the mask of a reset read from the fields that have a reset and do not move by themselves.
- Say which registers a sweep must leave alone, and why.
- Explain how a generated row found a disagreement nobody had typed a test for.
- Lesson
- V6.2
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
At integration, the generated register sweep ran on ap_soc and found one disagreement. A write to ap_plic's PENDING word answered OKAY. The description said that write must answer ERROR, and no engineer had typed that check. A generator had.
- File
- Tool
- Design
- Sequence
- Manager
- Data
- Transaction / TLM
- Structure
- One description
Each block has one JSON file. It names every register, field, access type and reset value.
- Everything is generated
The generator regmap writes the register RTL, the header firmware uses, and a flat table for tests and models.
- The test is generated too
The function registerSweep turns every row of the table into checks: 343 rows for 79 registers.
- Run and compare
The rows run on the chip. Where the RTL and the description disagree, a row fails.
Read the diagram as text
- regs/*.json: 10 blocks
- regmap: one generator
- Register RTL: and the C header
- ap_soc_regmodel.csv: 79 registers
- registerSweep: 343 rows
- Seat or processor: the same rows
- regs/*.json → regmap: read
- regmap → Register RTL: writes
- regmap → ap_soc_regmodel.csv: writes
- ap_soc_regmodel.csv → registerSweep: one row a check
- registerSweep → Seat or processor: run
- Register RTL → Seat or processor: the chip under test
tools/soc/ap_soc/regs/README.md, tools/soc/regmap.mjs, tools/soc/ap_soc/bench/soc_tb/soc-rows.mjs.
Before You Start
Read Taking the Core Out and The Generated Header first. The UVM register model is taught in Desired values, mirrors and reset and Built-in register tests.
| Question | |
|---|---|
| 1 | Why generate a register test instead of typing it? |
| 2 | What does a reset read check, and what must its mask leave out? |
| 3 | What does each access type say a write and a read must do? |
| 4 | Which registers must a sweep leave alone? |
| 5 | What did the generated sweep of ap_soc find? |
The Idea: The Description Is the Specification
A register file is a promise. At this address, this field resets to this value, and software may write it, or only read it. Firmware relies on every one of those promises, through the generated header.
There are many promises: 79 registers on a small chip like ap_soc. Typed by hand, a test covers what someone remembered, and goes stale when the file changes. Generated from the same description as the RTL, it covers every row, every time.
The test does not trust the RTL generator either. It runs on the chip and compares what the chip answers with what the description says. A wrapper that someone wrote by hand is caught the same way.
New Words
| Word | Meaning |
|---|---|
| Register description | The data files that define every register: regs/*.json here. |
| Register-model table | gen/ap_soc_regmodel.csv: one line per field, with access, reset value and error rules. |
| Reset read | A read right after reset, checked against the reset values under a mask. |
| Volatile field | A field the hardware changes by itself (a status bit, a counter); a reset read must mask it out. |
| Access check | Writes and reads that prove each field does what its access type says. |
| Write-error row | A write that must answer ERROR, because the register has no writable field (ap_soc's rule). |
| Sweep | The whole generated list of reset reads, access checks and holes for every block. |
The Smallest Example: Four Registers, Eight Rows
A toy block has four registers. PRIORITY and ENABLE are read and write, PENDING and ID are read only. Each gets a reset read. Each writable one gets a pattern written; each read-only one gets a write that must answer ERROR.
| Register | Access | Reset read | Access row |
|---|---|---|---|
| PRIORITY | read and write | expect 0 | write a pattern |
| PENDING | read only | expect 0 | write must answer ERROR |
| ENABLE | read and write | expect 0 | write a pattern |
| ID | read only | expect 2A53_10C5 | write must answer ERROR |
The example below runs these 8 rows against a toy block whose PENDING answers OKAY.
The Rules
Rule 1: Every check comes from the description.
No row is typed. A new register, or a changed reset value, changes the test at the next run.
Rule 2: A reset read masks what it cannot know.
The mask holds the fields that have a reset value and do not move by themselves. Status bits and counters are left out.
Rule 3: Each access type has its own check.
A read-and-write field gets two patterns and its reset value back. A write-only register gets a read that must answer ERROR.
Rule 4: A register with no writable field gets a write-error row.
That is ap_soc's rule for a wrong access (facts 27.6). Many chips ignore such a write instead; their test then checks the value did not change.
Rule 5: Mixed and side-effect fields get no pattern.
A read-to-clear or write-1-to-clear field would be changed by the check itself, so the generator only reads its neighbours' reset.
Rule 6: Every block gets a hole.
An offset that holds no register must answer ERROR, read and write.
Rule 7: The sweep leaves the test's own registers alone.
TB_STATUS, TB_PUTC and TB_EVENT mean something to the bench; writing them would end or confuse the test.
Sources for These Rules
The description and the generator: tools/soc/ap_soc/regs/README.md; tools/soc/regmap.mjs; the table gen/ap_soc_regmodel.csv.
The sweep: registerSweep in tools/soc/ap_soc/bench/soc_tb/soc-rows.mjs. The rule for a wrong access: facts 27.6.
Reset values and writes to read-only bits as SoC register checks: MEHTA-DV 16.4. The UVM sequences with the same aims: IEEE 1800.2 Annex E.1 (reset values) and E.2 (bit bash). Annex E.3 adds a test that crosses the front and back doors, the subject of a later lesson.
The PLIC specification says pending bits are read, and cleared by a claim (RV-PLIC chapter 5). It does not say how a write must answer; ERROR is ap_soc's choice.
The Lab: One Block, Then the Whole Chip
Here is the description of the Timer and PWM block, as the register-model table has it. One line per field.
| register | offset | field | lsb | width | access | reset | volatile | read_error | write_error |
|---|---|---|---|---|---|---|---|---|---|
| RUN | 0x000000 | run | 0 | 1 | rw | 0x0 | 0 | 0 | 0 |
| DUTY | 0x000004 | slots | 0 | 3 | rw | 0x0 | 0 | 0 | 0 |
| CAPTURED | 0x000008 | slot | 0 | 2 | ro | — | 1 | 0 | 1 |
| DIV_TOP | 0x00000C | top | 0 | 8 | rw | 0x00 | 0 | 0 | 0 |
| COMPARE | 0x000010 | slot | 0 | 2 | rw | 0x0 | 0 | 0 | 0 |
| IRQ_CLEAR | 0x000014 | match | 0 | 1 | wo | — | 0 | 1 | 0 |
| IRQ_ENABLE | 0x000018 | match | 0 | 1 | rw | 0x0 | 0 | 0 | 0 |
| EVENT | 0x000020 | enable | 0 | 1 | rw | 0x0 | 0 | 0 | 0 |
| EVENT | 0x000020 | online | 1 | 1 | ro | — | 1 | 0 | 0 |
| EVENT | 0x000020 | kick | 2 | 1 | wo | — | 0 | 0 | 0 |
| EVENT | 0x000020 | pending | 3 | 1 | w1c | — | 1 | 0 | 0 |
| EVENT | 0x000020 | seen | 4 | 1 | rc | — | 1 | 0 | 0 |
| EVENT | 0x000020 | route | 5 | 1 | rw | 0x0 | 1 | 0 | 0 |
| EVENT_COUNT_LOW | 0x000024 | value | 0 | 32 | ro | — | 1 | 0 | 1 |
| EVENT_COUNT_HIGH | 0x000028 | value | 0 | 32 | ro | — | 1 | 0 | 1 |
Generated by regmap from regs/timer.json. Columns not shown: base, count, stride, held_by, por_only, target, locked_by.
core r 10002000 00000000 00000001 00000000 0 1
core r 10002004 00000000 00000007 00000000 0 1
core r 1000200c 00000000 000000ff 00000000 0 1
core r 10002010 00000000 00000003 00000000 0 1
core re 10002014 00000000 ffffffff 00000000 0 1
core r 10002018 00000000 00000001 00000000 0 1
core r 10002020 00000000 00000001 00000000 0 1
core w 10002000 00000001 ffffffff 00000000 0 1
core r 10002000 00000000 00000001 00000001 0 1
core w 10002000 00000000 ffffffff 00000000 0 1
core r 10002000 00000000 00000001 00000000 0 1
core w 10002000 00000000 ffffffff 00000000 0 1
core r 10002000 00000000 00000001 00000000 0 1
core w 10002004 00000005 ffffffff 00000000 0 1
core r 10002004 00000000 00000007 00000005 0 1
core w 10002004 00000002 ffffffff 00000000 0 1
core r 10002004 00000000 00000007 00000002 0 1
core w 10002004 00000000 ffffffff 00000000 0 1
core r 10002004 00000000 00000007 00000000 0 1
core we 10002008 00000000 ffffffff 00000000 0 1
core w 1000200c 000000a5 ffffffff 00000000 0 1
core r 1000200c 00000000 000000ff 000000a5 0 1
core w 1000200c 0000005a ffffffff 00000000 0 1
core r 1000200c 00000000 000000ff 0000005a 0 1
core w 1000200c 00000000 ffffffff 00000000 0 1
core r 1000200c 00000000 000000ff 00000000 0 1
core w 10002010 00000001 ffffffff 00000000 0 1
core r 10002010 00000000 00000003 00000001 0 1
core w 10002010 00000002 ffffffff 00000000 0 1
core r 10002010 00000000 00000003 00000002 0 1
core w 10002010 00000000 ffffffff 00000000 0 1
core r 10002010 00000000 00000003 00000000 0 1
core w 10002018 00000001 ffffffff 00000000 0 1
core r 10002018 00000000 00000001 00000001 0 1
core w 10002018 00000000 ffffffff 00000000 0 1
core r 10002018 00000000 00000001 00000000 0 1
core w 10002018 00000000 ffffffff 00000000 0 1
core r 10002018 00000000 00000001 00000000 0 1
core we 10002024 00000000 ffffffff 00000000 0 1
core we 10002028 00000000 ffffffff 00000000 0 1
core re 10002ffc 00000000 ffffffff 00000000 0 1
core we 10002ffc 00000000 ffffffff 00000000 0 1Read the rows against the rules. RUN, DUTY, DIV_TOP, COMPARE and IRQ_ENABLE get a reset read, two patterns and their reset value back (rules 2 and 3). CAPTURED and the two counters are read only, so a write must answer ERROR (rule 4). IRQ_CLEAR is write only, so a read must answer ERROR.
EVENT is the interesting one. Its fields mix read-to-clear, write-1-to-clear, write only and read only. So it gets a reset read of its one plain bit, enable, and no pattern at all (rule 5). The page ends with the hole at offset 0xFFC (rule 6).
| Rows | Count |
|---|---|
| Registers in the table | 79 |
| Reset reads | 46 |
| Reads that must answer ERROR | 11 |
| Writes that must answer ERROR | 12 |
| Registers with two patterns | 43 |
| Blocks with a hole | 8 |
| Rows in all | 343 |
registerSweep on today's table. The 8 holes are the APB blocks; each costs two rows.
seat: verdict=pass cycles=1193 bus_transfers=345
processor: verdict=pass cycles=10254 retired=7269Both runs pass, so ap_soc keeps every promise of its description today. The seat took 1193 cycles; the processor, which fetches and decodes each row, took 10,254.
Why It Is Built This Way
Why a flat table, and not the JSON itself? Because a test, a UVM model and a document all want the same thing: one line per field with its rules. The table is that, generated, and checked for freshness by the regmap gate.
Why not generate the RTL and trust it? Because not every block is generated. The blocks ap_plic and ap_clint keep hand-written RTL, and a wrapper is code a person wrote. The sweep does not care who wrote a block. It asks the chip.
The UVM register layer has the same idea built in. It keeps a model of every register, with ready sequences that read reset values and test access (IEEE 1800.2 Annex E). This bench's UVM environment has a test of its own that reads every reset value from its model. The table here is that idea without UVM, so it runs on the seat with no UVM build.
Traps
The story of the hook is a real one. The sweep was written at stage E7, and one row disagreed with the chip: a write of ap_plic PENDING answered OKAY. The block ap_plic is hand-written; its wrapper answered ERROR only for an offset with no register. The description, regs/plic.json, said ERROR for a register with no writable field.
Either side could have been wrong. The team decided the description was right, changed the wrapper, and the row now passes. Facts 27.9 records the fix. Nobody would have typed that row: who tests a write to a read-only interrupt word?
The second trap is a reset read with too wide a mask. RESET_CAUSE says why the chip last reset, so it is volatile. A mask that includes it fails on a chip that is right, and the team learns to ignore the sweep. Mask exactly the fields with a reset and no motion of their own.
The third trap hides in the patterns. Two repeated patterns, A5 and 5A in every byte, cannot tell two crossed fields apart. The next lesson shows why, and what to write instead.
At Scale
This section looks ahead. ap_soc has 79 registers in 10 blocks.
Thousands of registers
A large SoC has tens of thousands. Its register files are written in a format such as IP-XACT or SystemRDL, and one generator feeds RTL, headers, documents and models.
The sweep as an entry gate
The reset and access sweep runs at every drop of the RTL, on the seat, before any firmware test.
Quirky registers
Locks, shadows, indirect windows and read side effects need rules the generator cannot guess. Teams mark them in the description, as LOCK and EVENT are marked here.
Try It
Run it: four registers become eight rows, and the toy's PENDING answers OKAY to a write.
Set okay_on_write to 0 (the predict question below). What does the last line say?
Why does the reset read of PENDING pass even with the bug on?
row 6: write PENDING answered OKAY, expected ERROR 8 rows from 4 registers, 1 failedCheck Yourself
Common Mistakes
Wrong Type the register test by hand from the document.
It covers what someone remembered and goes stale when the file changes.
Fix Generate every row from the register-model table.
Wrong Read RESET_CAUSE in a reset check with a full mask.
A volatile field fails on a correct chip.
Fix Mask only fields with a reset and no motion of their own.
Wrong Write patterns to every register, including TB_STATUS.
The bench reads a meaning into the mailbox; the test ends itself.
Fix Leave the test's own registers out of the sweep.
Remember
- Generate the register test from the same description as the RTL and the header.
- Reset reads mask volatile fields; access checks follow each access type.
- ap_soc answers ERROR to a write of a register with no writable field; that is its choice.
- A generated row found a hand-written wrapper that disagreed with the description.