H4.4 Register Side-Effect Checker

HardFixVerilator · serverReview the topic

Fix an APB register checker that stays silent when a faulty peripheral returns a wrong register value.

Hardware context: APB control/status peripheral using the declared teaching subset. Check read-only, write-one-to-clear and read-clear register behavior.

A register checker passed a peripheral that counted every CTRL write twice. After two writes, a read of WRITE_COUNT returned 4, and the checker said nothing.

The class register_checker watches one APB peripheral from the outside. sample() is called once per edge with the bus pins, the two event inputs and irq. It keeps its own byte model of CTRL, STATUS, EVENTS and WRITE_COUNT. It checks PREADY, PSLVERR, read data and irq, and logs each mismatch. report() prints its state after every edge.

The tests run the checker on a correct peripheral and on eleven faulty ones. It must stay silent on the correct one. On a faulty one it must log every mismatch at the edge where it shows.

Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
command_count5input of each check
outside_noise1input of each check
hardware_fault4input of each check
Full specification
The types and the class (keep these names, fields and methods)
typedef struct packed {
  bit sel, enable, write;      // PSEL, PENABLE, PWRITE
  bit [7:0] address;           // PADDR
  bit [31:0] data;             // PWDATA
  bit [3:0] strobe;            // PSTRB
  bit deny;                    // access-control input, not an APB signal
} apb_request;
typedef struct packed {
  bit resetn; apb_request request;
  bit ready, error;            // PREADY, PSLVERR
  bit [31:0] rdata, status_set, event_set;
  bit irq_before, irq_after;
  int configured_waits;
} apb_sample;
class register_checker;
  bit [7:0] control[4], status[4], events[4];   // byte models
  bit [31:0] write_count = 0;
  apb_request pending; bit active = 0;
  int expected_waits, waited, issued = 0, completed = 0, canceled = 0;
  int bad = 0, errors = 0, first_cycle = 255, first_kind = 0;
  function void sample(input int cycle, input apb_sample s);
  function string report();
endclass
EdgeWhat the checker does
ResetClear the register model. A pending transfer is canceled. Counters and error history survive.
SetupCopy the request and the wait count, and add 1 to issued. A setup while another transfer is pending is a PROTOCOL error and cancels it.
AccessThe request must match the setup copy, else PROTOCOL. Write data is compared only for writes. PREADY must equal waited >= expected_waits, else READY.
WaitAn access edge with PREADY = 0 adds 1 to waited.
CompletionPSLVERR must match the register map, else ERROR_FLAG. A good read must return the model value from before the edge, else READ_DATA. An error must return read data 0, else READ_DATA.
Model updateApply the bus effect only when the map allows the transfer, whatever PSLVERR showed. Then OR in status_set and event_set.
Every edgeirq after the edge must equal (STATUS | EVENTS) != 0 in the model, else IRQ.
Stray accessAn access with nothing pending is a PROTOCOL error. So is PSEL = 0 while a transfer is pending, which also cancels it.
KindValue
READY1
ERROR_FLAG2
READ_DATA4
IRQ8
PROTOCOL16

Each failed check adds 1 to errors and ORs its kind into bad. The edge and kind of the first failure are kept. With no failure they stay ff and 00.

The format of report(), printed after every edge
K=<bad>:<first edge>:<first kind>|E=<errors>|N=<issued>,<completed>,<canceled>,<pending>|C=<CTRL>,<WRITE_COUNT>,<STATUS>,<EVENTS>;
AddressRegisterBehaviour
00CTRLRead and write. Only bytes whose strobe bit is 1 change.
04WRITE_COUNTRead only. Adds 1 for each successful write to CTRL, even with all strobes 0.
08STATUSWrite 1 to clear (W1C). Each 1 bit in a strobed byte of the write data clears that bit.
0cEVENTSRead only and read to clear. A successful read returns the value, then clears the whole word.
10IDRead only. Always reads c001c0de.
ErrorAny other address, a write to a read-only word, or deny = 1. PSLVERR = 1, read data 0 and no register change.
RuleMeaning
Event inputsstatus_set and event_set are 32-bit inputs. They are sampled on every edge, also on idle, wait and error edges.
Order on one edgeFirst the bus effect of a completing transfer. Then status_set is ORed into STATUS and event_set into EVENTS.
Same-edge set and clearA hardware set wins over a bus clear on the same edge.
Read to clearA read of EVENTS returns the value from before the edge. The clear and any new event happen on that edge.
Resetresetn is active low and synchronous. It clears all four writable words, drops events and cancels the transfer in flight.
irqirq = 1 when STATUS or EVENTS is not 0 after the edge.
APB ruleMeaning here
SamplingEvery pin is sampled just before the rising edge of the clock.
SetupThe first edge of a transfer has PSEL = 1 and PENABLE = 0.
AccessThe edges after the setup have PSEL = 1 and PENABLE = 1.
CompletionA transfer ends at the one edge where PSEL, PENABLE and PREADY are all 1.
WaitAn access edge with PREADY = 0 is a wait. The request stays the same.
Back to backA new transfer may follow with PSEL still 1. It always starts with its own setup edge.
Ignored pinsPREADY outside an access and PSLVERR outside a completion mean nothing. Some tests drive noise on them.
SourceArm IHI 0024E, sections 3.1 to 3.4 and 4.1. This is a 32-bit subset with one completer and byte strobes.
Wait ruleMeaning
wait_cyclesThe test sets it for each transfer, from 0 to 3. The peripheral captures it at the setup edge.
TimingPREADY stays 0 for exactly wait_cycles access edges. The next access edge completes.
Zero waitsThe transfer still takes two edges: the setup, then one access.
Faulty peripheralWhat is wrong
1Side effects happen on wait edges too.
2Error transfers still change registers.
3A W1C or read-clear loses a hardware set from the same edge.
4Address bits above bit 4 are ignored, so address 20 aliases CTRL.
5Strobes are ignored.
6Reset does not clear the registers.
7PREADY comes one wait early.
8PSLVERR is inverted at completion.
9Bit 0 of read data is flipped.
10WRITE_COUNT adds 2 per CTRL write.
11PREADY never rises.
Part of a transcript rowMeaning
C12Edge number, counting from 0.
N1resetn.
S111PSEL, PENABLE and PWRITE.
00:11223344:f:0Address, write data, strobes and deny, in hex.
W2wait_cycles for this transfer.
R10:a5a50000PREADY, PSLVERR and read data, sampled before the edge.
I01irq before and after the edge.
K=00:ff:00|E=0|...Your report() after this edge.
END=1,3,0,0At the end: all commands done, completed, canceled and timed out.

Each test runs 64 edges and builds a text transcript, one row per edge. Your transcript is compared with the expected one as a whole. The result shows both lengths and exact_equal. Equal lengths alone do not pass.

Examples

Example 1

A Same-Edge Event Survives W1C
Input
Correct peripheral. Clear STATUS bit 0 with 2 waits, read STATUS, try a denied clear of STATUS with 2 waits, read STATUS. STATUS bit 0 is set at edges 0 and 3, bit 8 at edge 6.
Output
actual_length = 7105, expected_length = 7105, exact_equal = yes

The clear completes at edge 3, the edge that sets bit 0 again, so the bit stays set. The read at edge 5 returns 00000001. The denied clear changes nothing, and the last read returns 00000101. No error is logged.

Example 2

Early Read-Clear During Waits
Input
Faulty peripheral 1. Read EVENTS three times with 3 waits, the third with deny = 1, then read EVENTS again. EVENTS gets 3 at edge 0, 4 at edge 4 and 80 at edge 10.
Output
actual_length = 7105, expected_length = 7105, exact_equal = yes

The fault clears EVENTS on the first wait edge, so irq falls at edge 1. The model still holds 3, so the IRQ check fires first: first edge 01, kind 08. At edge 4 the read returns 0 instead of 3, so bad becomes 0c.

Example 3

A Double Count Shows Only in a Read
Input
Faulty peripheral 10. Write 11223344 to CTRL with 2 waits, write aabbccdd with strobes 5 and 1 wait, read CTRL, read WRITE_COUNT, a denied write with 2 waits, read CTRL, read address 20.
Output
actual_length = 7105, expected_length = 7105, exact_equal = yes

CTRL reads 11bb33dd, which is right. WRITE_COUNT reads 4 at edge 10, but the model holds 2. The READ_DATA check fires there: first edge 0a, kind 04. Nothing else fails.

Constraints and Contract

  • The test driver always sends legal APB requests. The PROTOCOL checks are there for drivers that break the rules.
  • The checker sees only the pins and the event inputs, never the registers inside the peripheral.
  • Keep the class name, the fields and the report() format.

Hints

Hint 1

List what each fault changes on the pins. Which faults only show up in the data a read returns?

Hint 2

Follow the double-count example. The model says WRITE_COUNT is 2 and the bus returns 4. Which check should fire, and does it?

Hint 3

Look at the completion branch of sample(). It checks read data for errors, but not for successful reads. Compare a good read with read_model() before the model update.

What the grader checks: The checker watches a real clocked peripheral, correct or with one of eleven faults. Its report after every edge is compared with an independent model. A fault that a test does not trigger must leave the checker silent.

76 checks on Verilator · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 7 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Same-Edge Event Survives W1C

Input
Correct peripheral. Clear STATUS bit 0 with 2 waits, read STATUS, try a denied clear of STATUS with 2 waits, read STATUS. STATUS bit 0 is set at edges 0 and 3, bit 8 at edge 6.
Expected output
actual_length = 7105, expected_length = 7105, exact_equal = yes

The clear completes at edge 3, the edge that sets bit 0 again, so the bit stays set. The read at edge 5 returns 00000001. The denied clear changes nothing, and the last read returns 00000101. No error is logged.

Run samples executes all 3 public checks. Check all tests runs 76 checks on the server.

Verilator · server