Fix an APB phase decoder that mistakes a setup edge for a completed transfer when PREADY happens to be high.
Hardware context: APB control/status peripheral using the declared teaching subset. Classify sampled peripheral-bus cycles and completed reads/writes.
A bus monitor counted one transfer too many, again and again. The extra one came from a setup edge. On that edge the completer happened to drive PREADY high, and the decoder believed it.
The function decode_apb() classifies one rising edge of an APB bus. It gets an apb_sample: the request pins, PREADY, PSLVERR and read data, sampled just before the edge. It returns the phase of the edge, whether a transfer completed there, and the data it carried.
The tests drive a real APB peripheral for 64 edges. They call decode_apb() on every edge and compare each answer. The peripheral drives noise on PREADY, PSLVERR and read data outside the edges where they count.
Find the bug and fix it. The rules in brief: test reset first, then PSEL, then PENABLE. Only an access edge with PREADY = 1 completes a transfer.
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;
function automatic bit [39:0] decode_apb(input apb_sample sampled);
| Bits of the result | Holds |
|---|---|
| 39:38 | Always 0. |
| 37:35 | phase, from the next table. |
| 34 | completed: a transfer ends at this edge. |
| 33 | error: PSLVERR at a completion. 0 on every other edge. |
| 32 | write: PWRITE at a completion, with or without an error. 0 on every other edge. |
| 31:0 | payload: write data for a successful write, read data for a successful read, else 0. |
| Phase | When |
|---|---|
| 0 RESET | resetn = 0. |
| 1 IDLE | PSEL = 0, whatever PENABLE shows. |
| 2 SETUP | PSEL = 1 and PENABLE = 0, whatever PREADY shows. |
| 3 WAIT | PSEL = 1, PENABLE = 1 and PREADY = 0. |
| 4 READ | A read completes without an error. |
| 5 WRITE | A write completes without an error. |
| 6 ERROR | A transfer completes with PSLVERR = 1. |
| 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. |
| 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. |
| 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. |
| 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. |
| T=1000000000 | Your decode_apb() result for this edge, in hex. |
| 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
Two Waits Are Not Two Writes- Input
Noise on. Write 11223344 to CTRL with 2 waits, then read CTRL, read WRITE_COUNT and read address 20, back to back.
- Output
actual_length = 3905, expected_length = 3905, exact_equal = yes
Edge 0 is the setup, even though PREADY = 1 there. Edges 1 and 2 are waits, and edge 3 completes the write. The reads return 11223344 and 1. The read of address 20 ends at edge 9 with an error.
Example 2
Read-Clear Data Belongs to the Completing Edge- Input
Noise on. Read EVENTS with 3 waits, then read it again. Events 10, 20, 40 and 80 arrive at edges 0, 2, 4 and 5.
- Output
actual_length = 3905, expected_length = 3905, exact_equal = yes
EVENTS holds 30 before edge 4, so the first read returns 30. Event 40 arrives on the same edge and survives the clear. Event 80 arrives at the next setup, so the second read returns c0 at edge 6.
Example 3
Reset Cancels an Unfinished Access- Input
Noise on. Write deadbeef to CTRL with 3 waits and a reset after the first wait. Then read CTRL and read ID.
- Output
actual_length = 3905, expected_length = 3905, exact_equal = yes
Edge 1 is a wait and edge 2 is a reset edge, so the write never completes. The CTRL read ends at edge 4 and returns 0. The ID read ends at edge 6 and returns c001c0de.
Constraints and Contract
- The tests drive only 0 and 1, never x or z.
- decode_apb() only classifies edges. It does not check whether the requester broke an APB rule.
- Keep the function name, its argument and its 40-bit result.
Hints
Hint 1
Check the conditions in order: reset, then PSEL, then PENABLE, then PREADY. Each test rules out one phase.
Hint 2
Look at edge 0 of a transfer. PSEL = 1 and PENABLE = 0, and the noise may drive PREADY = 1. Which phase must that edge get?
Hint 3
The SETUP test also looks at PREADY. A setup edge is a setup whatever PREADY shows, so PREADY must not appear in that condition.
What the grader checks: Each test drives a real clocked APB peripheral for 64 edges, with noise on PREADY, PSLVERR and read data. Every decoded edge is compared. The tests do not check request stability; a protocol checker does that.
27 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