Lockstep with an Instruction-Level Model
A firmware test checks what it thought of. A model of the core checks everything the core does, on every instruction. This page runs ap_soc with its model in lockstep. It shows what is compared and what must come from the chip. Then it catches a core fault no test asked about.
You Will Be Able To
- Explain how a lockstep compare checks every instruction without expected values.
- Say which values the model must take from the chip, and why.
- Read a mismatch line and find the first record that differs.
- State the limits of our bench's compare.
- Lesson
- V5.2
- Time
- 28 min
- Runs on
- Your browser
- Check
- 5 questions
A test of arithmetic reaches its pass word. Every check it made held. One of its results is wrong, and nothing in the test knew what the right value was. Only something that computes every instruction itself could have noticed.
- Processor
- Bus
- Peripheral
- Debug
- Data
- The chip retires
The core finishes an instruction and the trace port gives its record.
- Take what only the chip knows
A device's answer, a bus error, the interrupt lines it sampled: the model cannot know them, so they come from the record.
- The model steps
The model executes the same instruction from its own copy of ROM and SRAM.
- Compare every field
All 24 fields must agree. In the lab the sub at 80000024 wrote edcbaa88; the model wrote 12345578.
Read the diagram as text
- ap_rv32 in the chip: retires a record
- The record: 24 fields
- ap_iss, the model: one step
- What only the chip knows: device data, errors, mip, mcycle
- Compare: first mismatch: pc 80000024, rd_wdata
- ap_rv32 in the chip → The record: trace port
- The record → What only the chip knows: taken from it
- What only the chip knows → ap_iss, the model: given to the model
- ap_iss, the model → Compare: model's record
- The record → Compare: chip's record
soc_tb on ap_soc v1.5 runs this once per record, inside the simulation, through DPI (soc_iss_dpi.c).
Before You Start
Read The Retire Trace as a Monitor first. Your First DPI-C Call explains how C code runs inside a simulation.
| Question | |
|---|---|
| 1 | Why does a chip need a model of its core when its tests pass? |
| 2 | What is compared, and how often? |
| 3 | Which values must the model take from the chip? |
| 4 | Why does only the first mismatch count? |
| 5 | What can our bench's compare not see? |
The Idea: A Second Core That Never Gets Tired
A test is a list of questions someone thought of. It cannot check a value it has no expected answer for. An instruction-level model is a program that executes RISC-V instructions one at a time. Run it beside the core, and every instruction has an expected answer.
In lockstep, the model takes one step for every record the core retires, and the two records must agree in every field. No test has to predict anything. A random program, a boot loader or a driver all become checked for free.
The model cannot know everything. A device answers what it answers; an interrupt arrives when the chip sends it. Those values come from the chip's own record, and the model is checked on what it does with them.
New Words
| Word | Meaning |
|---|---|
| Instruction-level model | A program that executes instructions one at a time, with no clock: here ap_iss, written in C. Also called an ISS. |
| Lockstep | Running the model one step for each record the core retires, and comparing the two records. |
| Mismatch | A record where the chip and the model differ in a field. |
| Device read | A load from a register block. Its value depends on the device and the moment, so the model takes it from the record. |
| Echo | A mismatch that follows from an earlier one, because the two machines no longer hold the same state. |
| False pass | A test that reports pass on a chip that is wrong. |
The Smallest Example: Be the Model
Do one step of the model by hand. The record of the chip says: pc 0x8000_0024, sub a2, a0, a1, with a0 = 0x1234_5678 and a1 = 0x0000_0100. It wrote a2 = 0xEDCB_AA88.
The model computes 0x1234_5678 - 0x0000_0100 = 0x1234_5578. The two differ in field 14, rd_wdata. Look at the chip's value: 0x0000_0100 - 0x1234_5678 is 0xEDCB_AA88. The chip subtracted in the reverse order.
The Rules
Rule 1: Compare every field of every record.
A field left out is a class of bugs left out: mip, the masks and the CSR fields matter.
Rule 2: Take from the chip only what the model cannot know.
A device's data, its bus errors and the sampled interrupt lines come from the record. The bench adds the cycle counter.
Rule 3: Keep the model's memory equal to the chip's.
Copy it at every reset; give it the writes of other managers in bus order.
Rule 4: Only the first mismatch is a finding.
After it, the two machines hold different state; later lines are echoes.
Rule 5: A mismatch ends the run.
It is a checker error: the bench stops the test, whatever its pass word says.
Sources for These Rules
The compare: tools/soc/ap_soc/bench/soc_tb/soc_iss_dpi.c and soc_tb.sv ("the retire trace and the model"). The model: tools/soc/ap_iss, proved against the JavaScript model on random programs (check-ap-iss.mjs, 5,217,879 records).
What comes from the record, the model's memory and the ordering of other writes: docs/soc/ap_soc-facts.md 27.12, "The model compare". The lockstep rules of the core: docs/soc/ap_rv32-blueprint.md 9.2.
The end of a test: a checker error outranks every status word (facts 27.12, order of authority).
Now Run It: A Device Program, Then a Fault
First the device program: it reads CHIP_ID, TB_ARG, UART0 STATUS and DIV, and MTIME, then takes three access faults. The model has none of those devices. It took each value from the record and agreed on all 64 records: 0 mismatches.
8000001c lw a0, 0(s0) device at 10004000 answered 2a5310c5
80000020 lw a1, 44(s0) device at 1000402c answered 5eed1234
80000028 lw a2, 4(t0) device at 10000004 answered 00000001
8000002c lhu a3, 16(t0) device at 10000010 answered 00000010
80000038 lw a4, 0(t0) device at 0200bff8 answered 0000001dThen the arithmetic program, with no expected values in it. On the good chip all 24 records agree. On a chip whose SUB subtracts in the reverse order, the bench stops it:
ISS mismatch cycle=19 pc=80000024 field=rd_wdata rtl=edcbaa88 model=12345578
ISS mismatch cycle=20 pc=80000028 field=rs2_rdata rtl=edcbaa88 model=12345578
verdict=fail reason=checker compares=15 mismatches=2The first line is the finding: rule 4. The second is its echo: the next store reads a2, which already differs. Now the same chip with the model switched off:
verdict=pass reason=pass status=0000900d cycles=49
the store of a2 wrote edcbaa88 to 80000058The test passed, and memory holds a wrong word. The fault table of the chip says the same at scale. Six ALU faults reach their pass word, and only the model compare catches them. So do two faults of the trace port itself, which no program reads.
li a0, 0x12345678
li a1, 0x00000100
sub a2, a0, a1 # 0x12345578
sw a2, 0(s1)Why It Is Built This Way
The compare runs inside the simulation, record by record, through DPI. A trace file would do the same work afterwards, but a long run makes millions of records. The plan measured 165 characters a record, and a JavaScript replay of 2.5 million records ran out of memory. In-line, the cost was about 1 percent of run time at stage E7.
The model owns ROM and SRAM and nothing else. Every other address is a device, answered from the record. That is the line between predictable and unpredictable. Memory holds what the program put there; a register block holds what the hardware decided.
Taking device answers from the record is safe for the core, because the model still checks every use of the value. It also hides things. A device that returns a wrong word looks right. So does one that answers OKAY where it should answer ERROR, because the model takes the error from the chip's trap too.
Know our bench's stated limits. A reset can cut off a store halfway, and that store leaves no record. So the model's memory is copied from the chip whenever the core leaves reset. A wrong store just before a reset is therefore not seen. Writes to SRAM by the DMA or the debug module reach the model in bus order. That order holds against the core's data accesses, not its fetches. And Icarus has no model compare at all: it lacks the DPI imports.
The Trap: The Model That Agreed with a Broken Device
A UART returns a wrong byte. The core loads it, adds one, stores it. The model takes the byte from the record, adds one, and agrees with every field. The lockstep passes, because it was never asked to check the UART.
So the model compare is a core checker, not a chip checker. In the chip's fault campaign it caught 8 of the 130 faults. Six were in the core's ALU, and two in the trace port that reports its work. The other 122 needed firmware tests, the bus-manager seat or passive checkers. The practice problems build the loop itself and read a mismatch of a faulty core.
At Scale
This section looks ahead. ap_soc has one simple core and one model.
Out-of-order cores
The core retires in order, so lockstep still works on the retire stream. The hard part is the exact moment of every interrupt and memory value.
Shared memory
With caches and other cores, a load's value depends on timing. Benches take it from the record, like a device, and check memory by other means.
Open models
Spike and the Sail model are open instruction-level models of RISC-V; lockstep benches wrap one through DPI as ours wraps ap_iss.
Try It
Run the slice: it lists the chip's value and the model's for every record of the faulty run that wrote a register.
Find the first mismatch. Which instruction is it, and what would the right value be?
The predict question removes that record.
pc 80000000 chip 10004000 model 10004000 pc 80000004 chip 00000002 model 00000002 pc 8000000c chip 10004000 model 10004000 pc 80000010 chip 80000010 model 80000010 pc 80000014 chip 80000058 model 80000058 pc 80000018 chip 12345000 model 12345000 pc 8000001c chip 12345678 model 12345678 pc 80000020 chip 00000100 model 00000100 pc 80000024 chip edcbaa88 model 12345578 FIRST MISMATCH pc 8000002c chip 00000014 model 00000014 pc 80000030 chip 10000000 model 10000000 pc 80000038 chip 00000001 model 00000001 pc 80000040 chip 12345778 model 12345778 pc 80000048 chip 00009000 model 00009000 pc 8000004c chip 0000900d model 0000900d first mismatch at record 8, pc 80000024Check Yourself
Common Mistakes
Wrong Let the model compute what a device returns.
The model has no devices; any guess makes a good chip fail.
Fix Take a device's data and errors from the chip's record.
Wrong Report every mismatch line as a bug.
After the first, the two machines hold different state.
Fix Debug the first mismatch; read the rest as echoes.
Wrong Trust a pass word when the model is off.
A core fault can reach the pass word.
Fix Run with the model compare on, and treat a mismatch as a failure.
Remember
- The model takes one step per retired record; all 24 fields must agree.
- Device data, bus errors, interrupt lines and the cycle counter come from the chip with each record; memory is the model's own.
- Only the first mismatch is a finding, and a mismatch ends the run.
- The compare checks the core, not the devices. Of 130 chip faults it caught 8, all in the core or its trace.