H4.3 Back-to-Back Access Bug

MediumFixVerilator · serverReview the topic

Fix an APB transfer controller that misses the second of two back-to-back transfers when PSEL stays high.

Hardware context: APB control/status peripheral using the declared teaching subset. Repair skipped or duplicated updates during consecutive APB transfers.

Two writes to CTRL went out back to back, with PSEL held high between them. The second write never landed. The bug is in how the controller spots a new transfer.

The module apb_gate is the transfer controller inside an APB peripheral. At each setup edge it copies the request into held and loads a wait counter. It counts down the wait edges and raises access_ready when the transfer may complete.

The tests drive back-to-back transfers with 0 to 3 wait states. They mix reads and writes, errors and resets. Every pin on every edge is compared with a reference model.

Find the bug and fix it. A new transfer starts at every setup edge, even when PSEL never fell.

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 ports (keep them)
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;
module apb_gate(input bit clk, resetn,
                input apb_request request,
                input int wait_cycles,
                output bit active,
                output apb_request held,
                output bit access_ready);
  // your controller
endmodule
Controller ruleMeaning
Resetresetn = 0 clears active, held and remaining.
Setup edgePSEL = 1 and PENABLE = 0. Set active, copy the whole request into held and load remaining with wait_cycles.
Access edge while activeIf remaining > 0, subtract 1. Otherwise this edge completes, so clear active.
PSEL = 0Clear active.
access_readyactive && remaining == 0. The peripheral drives PREADY from it during an access.
heldKeeps every request field, strobes and deny included. The register bank acts on held.
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.
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.
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

Two Writes While PSEL Stays High
Input
Noise on. Write 11223344 to CTRL, write aabbccdd to CTRL with strobes 5, read CTRL, read WRITE_COUNT, all back to back.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

PSEL stays 1 from edge 0 to edge 7. Edge 2 is the second setup. Strobes 5 change bytes 0 and 2, so CTRL reads 11bb33dd. WRITE_COUNT reads 2.

Example 2

An Error Does Not Block the Next Transfer
Input
Noise on. Write 01234567 with 2 waits, read ID, write ffffffff with deny = 1 and 3 waits, write 89abcdef with strobes a and 1 wait, read CTRL, read WRITE_COUNT.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

The denied write ends at edge 10 with an error and changes nothing. The next setup follows at edge 11. Strobes a change bytes 1 and 3, so CTRL reads 8923cd67. WRITE_COUNT reads 2.

Example 3

Reset Ends the Earlier Writes Too
Input
Noise on. Write ffffffff, write 12345678 with 3 waits and a reset after 2 waits, write 55aa55aa with 1 wait, read CTRL, read WRITE_COUNT.
Output
actual_length = 3073, expected_length = 3073, exact_equal = yes

Reset at edge 5 cancels the second write. It also clears CTRL and WRITE_COUNT, so the first write is gone too. The 55aa55aa write completes at edge 8. CTRL reads 55aa55aa and WRITE_COUNT reads 1.

Constraints and Contract

  • Keep the module name and its ports. All state changes on the rising edge of clk.
  • The rest of the peripheral is given and trusted: address decode, errors, read data and the register bank.
  • The test driver plays a list of transfers. It can reset the bus in the middle of a transfer on purpose. It also resets after more than wait_cycles + 2 access edges without PREADY, so a broken design cannot hang the test.

Hints

Hint 1

Look at the first example. PSEL is 1 from edge 0 to edge 7. How many setup edges are there?

Hint 2

A setup edge is PSEL = 1 with PENABLE = 0. Nothing else, such as a rising PSEL, marks a new transfer.

Hint 3

The setup branch also requires was_selected to be 0. Remove that condition and the register that feeds it.

What the grader checks: The controller runs inside a real clocked APB peripheral. Back-to-back reads and writes, errors and resets are driven for 64 edges, and every pin is compared with an independent model. Later reads show each commit.

51 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 Writes While PSEL Stays High

Input
Noise on. Write 11223344 to CTRL, write aabbccdd to CTRL with strobes 5, read CTRL, read WRITE_COUNT, all back to back.
Expected output
actual_length = 3073, expected_length = 3073, exact_equal = yes

PSEL stays 1 from edge 0 to edge 7. Edge 2 is the second setup. Strobes 5 change bytes 0 and 2, so CTRL reads 11bb33dd. WRITE_COUNT reads 2.

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

Verilator · server