H4.1 APB Transfer Decoder

EasyFixVerilator · serverReview the topic

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

SignalWidthRole
command_count5input of each check
outside_noise1input of each check
hardware_fault4input of each check
Full specification
The types and the function (keep its name and types)
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 resultHolds
39:38Always 0.
37:35phase, from the next table.
34completed: a transfer ends at this edge.
33error: PSLVERR at a completion. 0 on every other edge.
32write: PWRITE at a completion, with or without an error. 0 on every other edge.
31:0payload: write data for a successful write, read data for a successful read, else 0.
PhaseWhen
0 RESETresetn = 0.
1 IDLEPSEL = 0, whatever PENABLE shows.
2 SETUPPSEL = 1 and PENABLE = 0, whatever PREADY shows.
3 WAITPSEL = 1, PENABLE = 1 and PREADY = 0.
4 READA read completes without an error.
5 WRITEA write completes without an error.
6 ERRORA transfer completes with PSLVERR = 1.
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.
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.
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.
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.
T=1000000000Your decode_apb() result for this edge, in hex.
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

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

Report a problem with this page

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

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

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

Verilator · server