Fix an APB transfer controller that completes every transfer on its first access edge, whatever wait count it was given.
Hardware context: APB control/status peripheral using the declared teaching subset. Implement exactly-once peripheral completion through inserted wait states.
A slow APB peripheral must hold PREADY low for a few edges before it answers. This one never did. Every transfer completed on its first access edge.
The module apb_gate is the transfer controller inside the 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 64 edges into the peripheral with 0 to 3 wait states per transfer. They compare every pin on every edge with a reference model. Later reads show whether each write landed exactly once.
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;
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 rule | Meaning |
|---|---|
| Reset | resetn = 0 clears active, held and remaining. |
| Setup edge | PSEL = 1 and PENABLE = 0. Set active, copy the whole request into held and load remaining with wait_cycles. |
| Access edge while active | If remaining > 0, subtract 1. Otherwise this edge completes, so clear active. |
| PSEL = 0 | Clear active. |
| access_ready | active && remaining == 0. The peripheral drives PREADY from it during an access. |
| held | Keeps every request field, strobes and deny included. The register bank acts on held. |
| 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. |
| 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. |
| 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. |
| 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 Partial Write Completes Once After Three Waits- Input
No noise. Write aabbccdd to CTRL with strobes 5 and 3 waits, read CTRL with 2 waits, then read WRITE_COUNT.
- Output
actual_length = 3073, expected_length = 3073, exact_equal = yes
The setup is edge 0. Edges 1 to 3 wait and edge 4 completes. Strobes 5 select bytes 0 and 2, so the CTRL read at edge 9 returns 00bb00dd. WRITE_COUNT reads 1.
Example 2
Zero Strobes and a Denied Write- Input
Noise on. Write ffffffff to CTRL with strobes 0, read WRITE_COUNT, write 12345678 to CTRL with deny = 1 and 2 waits, read CTRL.
- Output
actual_length = 3073, expected_length = 3073, exact_equal = yes
The zero-strobe write changes no byte but still counts, so WRITE_COUNT reads 1. The denied write ends at edge 8 with PSLVERR = 1. It changes nothing, so CTRL reads 0.
Example 3
Reset After the Setup Drops the Request- Input
Noise on. Write 01234567 to CTRL with 3 waits and a reset right after the setup. Then read CTRL and read WRITE_COUNT.
- Output
actual_length = 3073, expected_length = 3073, exact_equal = yes
Reset comes at edge 1, before any access edge. The write never completes, so both reads return 0.
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
Trace a transfer with wait_cycles = 2 by hand. Which edges must have PREADY = 0, and which edge completes?
Hint 2
access_ready depends on remaining. What value does remaining hold on the first access edge after the setup?
Hint 3
Look at the setup branch. It must load remaining with wait_cycles, not with 0.
What the grader checks: The controller runs inside a real clocked APB peripheral. Every pin on 64 edges is compared with an independent model, and later reads show each commit. The exact wait count is this peripheral's choice, not an APB rule.
67 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