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.

From the Description to a TestOne set of register files feeds the RTL, the C header and the register-model table. The sweep is made from that table, so it cannot drift from the description.
  • File
  • Tool
  • Design
  • Sequence
  • Manager
  • Data
  • Transaction / TLM
  • Structure
From the Description to a TestOne set of register files feeds the RTL, the C header and the register-model table. The sweep is made from that table, so it cannot drift from the description. file regs/*.json 10 blocks tool regmap one generator design Register RTL and the C header file ap_soc_regmodel.csv 79 registers sequence registerSweep 343 rows manager Seat or processor the same rows readwriteswritesone row a checkrunthe chip under test
  1. file regs/*.json10 blocks
    • read → regmap
  2. tool regmapone generator
    • writes → Register RTL
    • writes → ap_soc_regmodel.csv
  3. design Register RTLand the C header
    • the chip under test → Seat or processor
  4. file ap_soc_regmodel.csv79 registers
    • one row a check → registerSweep
  5. sequence registerSweep343 rows
    • run → Seat or processor
  6. manager Seat or processorthe same rows
  1. One description

    Each block has one JSON file. It names every register, field, access type and reset value.

  2. Everything is generated

    The generator regmap writes the register RTL, the header firmware uses, and a flat table for tests and models.

  3. The test is generated too

    The function registerSweep turns every row of the table into checks: 343 rows for 79 registers.

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

Five questions this page answers
Question
1Why generate a register test instead of typing it?
2What does a reset read check, and what must its mask leave out?
3What does each access type say a write and a read must do?
4Which registers must a sweep leave alone?
5What 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

New words on this page
WordMeaning
Register descriptionThe data files that define every register: regs/*.json here.
Register-model tablegen/ap_soc_regmodel.csv: one line per field, with access, reset value and error rules.
Reset readA read right after reset, checked against the reset values under a mask.
Volatile fieldA field the hardware changes by itself (a status bit, a counter); a reset read must mask it out.
Access checkWrites and reads that prove each field does what its access type says.
Write-error rowA write that must answer ERROR, because the register has no writable field (ap_soc's rule).
SweepThe 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.

Rows from four registers, by hand
RegisterAccessReset readAccess row
PRIORITYread and writeexpect 0write a pattern
PENDINGread onlyexpect 0write must answer ERROR
ENABLEread and writeexpect 0write a pattern
IDread onlyexpect 2A53_10C5write must answer ERROR

The example below runs these 8 rows against a toy block whose PENDING answers OKAY.

The Rules

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

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

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

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

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

  6. Rule 6: Every block gets a hole.

    An offset that holds no register must answer ERROR, read and write.

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

gen/ap_soc_regmodel.csv, the timer's lines
registeroffsetfieldlsbwidthaccessresetvolatileread_errorwrite_error
RUN0x000000run01rw0x0000
DUTY0x000004slots03rw0x0000
CAPTURED0x000008slot02ro—101
DIV_TOP0x00000Ctop08rw0x00000
COMPARE0x000010slot02rw0x0000
IRQ_CLEAR0x000014match01wo—010
IRQ_ENABLE0x000018match01rw0x0000
EVENT0x000020enable01rw0x0000
EVENT0x000020online11ro—100
EVENT0x000020kick21wo—000
EVENT0x000020pending31w1c—100
EVENT0x000020seen41rc—100
EVENT0x000020route51rw0x0100
EVENT_COUNT_LOW0x000024value032ro—101
EVENT_COUNT_HIGH0x000028value032ro—101

Generated by regmap from regs/timer.json. Columns not shown: base, count, stride, held_by, por_only, target, locked_by.

The rows registerSweep made from those lines (they are spread through the sweep; here they are gathered).
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 1

Read 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).

The whole sweep
RowsCount
Registers in the table79
Reset reads46
Reads that must answer ERROR11
Writes that must answer ERROR12
Registers with two patterns43
Blocks with a hole8
Rows in all343

registerSweep on today's table. The 8 holes are the APB blocks; each costs two rows.

The same 343 rows on the seat, and on the processor through the interpreter in ROM.
seat:      verdict=pass cycles=1193 bus_transfers=345
processor: verdict=pass cycles=10254 retired=7269

Both 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has 79 registers in 10 blocks.

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

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

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

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: row 6: write PENDING answered OKAY, expected ERROR 8 rows from 4 registers, 1 failed

Check Yourself

Predict the outputokay_on_write is now 0: the toy's PENDING answers ERROR to a write. What is the last line printed?
// A register description of four registers, turned into rows, run against a toy block.
// The toy's PENDING is read only, so a write must answer ERROR. okay_on_write = 0 makes it answer OKAY.
module tb;
  string     name [4];
  bit [31:0] reset [4], mem [4];
  bit        writable [4];
  int rows = 0, fails = 0;
  // The toy block: a write to a register with no writable field answers ERROR, unless the bug is on.
  function automatic bit write_answers_error(int i);
    return !writable[i] && !(0 && i == 1);
  endfunction
  initial begin
    name[0] = "PRIORITY"; reset[0] = 32'h0;         writable[0] = 1;
    name[1] = "PENDING";  reset[1] = 32'h0;         writable[1] = 0;
    name[2] = "ENABLE";   reset[2] = 32'h0;         writable[2] = 1;
    name[3] = "ID";       reset[3] = 32'h2a53_10c5; writable[3] = 0;
    for (int i = 0; i < 4; i++) mem[i] = reset[i];
    for (int i = 0; i < 4; i++) begin
      rows++;   // reset read
      if (mem[i] != reset[i]) begin fails++; $display("row %0d: %s reads %h, reset %h", rows, name[i], mem[i], reset[i]); end
    end
    for (int i = 0; i < 4; i++) begin
      rows++;   // access: a pattern for a writable register, ERROR for the others
      if (writable[i]) mem[i] = 32'h5a5a5a5a;
      else if (!write_answers_error(i)) begin fails++; $display("row %0d: write %s answered OKAY, expected ERROR", rows, name[i]); end
    end
    $display("%0d rows from 4 registers, %0d failed", rows, fails);
  end
endmodule
ConceptWhy does a generated sweep find what a typed test misses?
ConceptWhich field must a reset read mask out?
ConceptWhat did the sweep of ap_soc find at stage E7?
ConceptWhy does EVENT of the timer get no pattern?

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.

Practice

Report a problem with this page