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
| Signal | Width | Role |
|---|---|---|
command_count | 5 | input of each check |
outside_noise | 1 | input of each check |
hardware_fault | 4 | input of each check |
Full specification
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
| Edge | What the checker does |
|---|---|
| Reset | Clear the register model. A pending transfer is canceled. Counters and error history survive. |
| Setup | Copy 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. |
| Access | The request must match the setup copy, else PROTOCOL. Write data is compared only for writes. PREADY must equal waited >= expected_waits, else READY. |
| Wait | An access edge with PREADY = 0 adds 1 to waited. |
| Completion | PSLVERR 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 update | Apply the bus effect only when the map allows the transfer, whatever PSLVERR showed. Then OR in status_set and event_set. |
| Every edge | irq after the edge must equal (STATUS | EVENTS) != 0 in the model, else IRQ. |
| Stray access | An access with nothing pending is a PROTOCOL error. So is PSEL = 0 while a transfer is pending, which also cancels it. |
| Kind | Value |
|---|---|
| READY | 1 |
| ERROR_FLAG | 2 |
| READ_DATA | 4 |
| IRQ | 8 |
| PROTOCOL | 16 |
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.
K=<bad>:<first edge>:<first kind>|E=<errors>|N=<issued>,<completed>,<canceled>,<pending>|C=<CTRL>,<WRITE_COUNT>,<STATUS>,<EVENTS>;
| Address | Register | Behaviour |
|---|---|---|
| 00 | CTRL | Read and write. Only bytes whose strobe bit is 1 change. |
| 04 | WRITE_COUNT | Read only. Adds 1 for each successful write to CTRL, even with all strobes 0. |
| 08 | STATUS | Write 1 to clear (W1C). Each 1 bit in a strobed byte of the write data clears that bit. |
| 0c | EVENTS | Read only and read to clear. A successful read returns the value, then clears the whole word. |
| 10 | ID | Read only. Always reads c001c0de. |
| Error | Any other address, a write to a read-only word, or deny = 1. PSLVERR = 1, read data 0 and no register change. |
| Rule | Meaning |
|---|---|
| Event inputs | status_set and event_set are 32-bit inputs. They are sampled on every edge, also on idle, wait and error edges. |
| Order on one edge | First the bus effect of a completing transfer. Then status_set is ORed into STATUS and event_set into EVENTS. |
| Same-edge set and clear | A hardware set wins over a bus clear on the same edge. |
| Read to clear | A read of EVENTS returns the value from before the edge. The clear and any new event happen on that edge. |
| Reset | resetn is active low and synchronous. It clears all four writable words, drops events and cancels the transfer in flight. |
| irq | irq = 1 when STATUS or EVENTS is not 0 after the edge. |
| APB rule | Meaning here |
|---|---|
| Sampling | Every pin is sampled just before the rising edge of the clock. |
| Setup | The first edge of a transfer has PSEL = 1 and PENABLE = 0. |
| Access | The edges after the setup have PSEL = 1 and PENABLE = 1. |
| Completion | A transfer ends at the one edge where PSEL, PENABLE and PREADY are all 1. |
| Wait | An access edge with PREADY = 0 is a wait. The request stays the same. |
| Back to back | A new transfer may follow with PSEL still 1. It always starts with its own setup edge. |
| Ignored pins | PREADY outside an access and PSLVERR outside a completion mean nothing. Some tests drive noise on them. |
| Source | Arm IHI 0024E, sections 3.1 to 3.4 and 4.1. This is a 32-bit subset with one completer and byte strobes. |
| Wait rule | Meaning |
|---|---|
| wait_cycles | The test sets it for each transfer, from 0 to 3. The peripheral captures it at the setup edge. |
| Timing | PREADY stays 0 for exactly wait_cycles access edges. The next access edge completes. |
| Zero waits | The transfer still takes two edges: the setup, then one access. |
| Faulty peripheral | What is wrong |
|---|---|
| 1 | Side effects happen on wait edges too. |
| 2 | Error transfers still change registers. |
| 3 | A W1C or read-clear loses a hardware set from the same edge. |
| 4 | Address bits above bit 4 are ignored, so address 20 aliases CTRL. |
| 5 | Strobes are ignored. |
| 6 | Reset does not clear the registers. |
| 7 | PREADY comes one wait early. |
| 8 | PSLVERR is inverted at completion. |
| 9 | Bit 0 of read data is flipped. |
| 10 | WRITE_COUNT adds 2 per CTRL write. |
| 11 | PREADY never rises. |
| Part of a transcript row | Meaning |
|---|---|
| C12 | Edge number, counting from 0. |
| N1 | resetn. |
| S111 | PSEL, PENABLE and PWRITE. |
| 00:11223344:f:0 | Address, write data, strobes and deny, in hex. |
| W2 | wait_cycles for this transfer. |
| R10:a5a50000 | PREADY, PSLVERR and read data, sampled before the edge. |
| I01 | irq before and after the edge. |
| K=00:ff:00|E=0|... | Your report() after this edge. |
| END=1,3,0,0 | At 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