H4.7 Side Effects During Wait States

MediumFixVerilator · serverReview the topic

Fix an APB register bank whose write-one-to-clear and read-to-clear effects fire on wait edges, before the transfer completes.

Hardware context: APB control/status peripheral using the declared teaching subset. Expose register side effects incorrectly repeated during peripheral wait cycles.

A peripheral cleared a STATUS bit while the write that clears it was still waiting. The final read looked right, but irq fell three edges early. Software that polls irq saw a missing interrupt.

The function next_registers() is the next-state logic of the peripheral's register bank. It gets the old register values and one sampled edge. It returns CTRL, WRITE_COUNT, STATUS and EVENTS after that edge. The bank stores the result on every rising edge.

The tests drive 64 edges of APB traffic with waits, errors, resets and hardware events. They compare every pin and irq on every edge with a reference model.

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 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;
typedef struct packed { bit [31:0] control, write_count, status, events; } apb_state;
function automatic apb_state next_registers(input apb_state old_state, input apb_sample sampled);
StepWhat next_registers() does
1. Resetresetn = 0 returns all four words as 0. Events on that edge are dropped.
2. CompletionA bus effect needs PSEL, PENABLE and PREADY all 1, and PSLVERR = 0.
3. CTRL writeCopy each strobed byte. Add 1 to WRITE_COUNT, even when all strobes are 0.
4. STATUS writeClear each bit that is 1 in a strobed byte of the write data.
5. EVENTS readClear the whole word. The read data already came from the old value.
6. Hardware setsOR status_set into STATUS and event_set into EVENTS. This happens on every edge except a reset.

In sampled, error is the decoded error of the transfer: 1 when the register map refuses it.

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

The Final Read Cannot See an Early W1C
Input
Noise on. Clear STATUS bit 0 with 3 waits, then read STATUS. STATUS bit 0 is set at edge 0.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

Edges 1 to 3 are waits, so irq must stay 1. The clear happens at edge 4, and irq falls after it. The read returns 0. A bank that clears on a wait edge gives the same read but drops irq three edges early.

Example 2

Read-Clear Keeps the Value Until Completion
Input
Noise on. Read EVENTS with 3 waits, then read it again. EVENTS gets 81 at edge 0 and bit 1 at edge 2.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

EVENTS holds 83 when the read completes at edge 4, so the read returns 83. The read then clears the word, and the next read returns 0.

Example 3

A Denied Clear Leaves STATUS Alone
Input
Noise on. Clear all of STATUS with deny = 1 and 2 waits, then read STATUS. STATUS gets 80000001 at edge 0.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

The denied clear ends at edge 3 with an error. An error has no bus effect, so STATUS stays 80000001 and irq stays 1. The read returns 80000001.

Constraints and Contract

  • The transfer controller, the address decode and the test driver are given and trusted.
  • next_registers() is combinational. The register bank stores its result on each rising edge.
  • Keep the function name, its arguments and its result type.

Hints

Hint 1

A wait edge has PSEL = 1 and PENABLE = 1, but PREADY = 0. Should a register change on that edge?

Hint 2

In the first example, irq must stay 1 on edges 1 to 3. Trace which edge clears STATUS in this code.

Hint 3

Look at how complete is computed. It must include sampled.ready.

What the grader checks: The function is clocked into a real APB peripheral. Read data and irq are compared on every edge, waits included, so early effects show even when the final read agrees. The event and error rules are this peripheral's choices, not APB rules.

87 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

The Final Read Cannot See an Early W1C

Input
Noise on. Clear STATUS bit 0 with 3 waits, then read STATUS. STATUS bit 0 is set at edge 0.
Expected output
actual_length = 3073, expected_length = 3073, exact_equal = yes

Edges 1 to 3 are waits, so irq must stay 1. The clear happens at edge 4, and irq falls after it. The read returns 0. A bank that clears on a wait edge gives the same read but drops irq three edges early.

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

Verilator · server