Fix an SPI pin checker that stays silent when both chip selects go low together.
Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Schedule different SPI device modes without overlapping chip selects.
Two SPI peripherals share one controller. In the lab, both chip selects went low together, and both devices drove MISO. The pin checker in the testbench saw it and said nothing.
check_spi() is that checker. After each clk edge it gets the frame settings, the edge number t and the observed pins. It computes what each pin should be from the settings and the words alone. It returns one error bit per pin.
The tests reset once and run three frames, 144 clk edges each, with no reset between them. Frame 2 flips CPOL, CPHA, the bit order and the device. It sends 4 bits: 9 out, 6 back. Frame 3 goes back to the first settings and sends 0x55aa, receiving 0x8001. Frame 1 may be aborted.
A correct controller drives the pins. On one edge per frame the tests may corrupt one value that the checker sees. The checker must flag exactly that pin, on exactly that edge.
Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
mode | 2 | input of each check |
word_bits | 5 | input of each check |
half_period | 3 | input of each check |
Full specification
function automatic logic [7:0] check_spi(
input int t, w, d, p, h, l, dev, abort_at,
input logic [15:0] sent, received,
input logic [1:0] cs,
input logic ck, mo, active, complete,
input logic [15:0] got);
// your checker
endfunction
| Argument | Meaning |
|---|---|
| t | The clk edge number since this frame's start edge, from 0. |
| w, d | The frame width and divisor. |
| p, h, l | CPOL, CPHA and lsb_first of this frame. |
| dev | The device this frame selects, 0 or 1. |
| abort_at | The edge where this frame is aborted, or -1 for no abort. |
| sent, received | The word the controller sends and the word the peripheral replies. |
| cs, ck, mo, active, complete, got | The observed cs_n, sck, mosi, busy, done and rx_word after edge t. |
| Mode | CPOL | CPHA | Idle SCK | Samples on | Drives on |
|---|---|---|---|---|---|
| 0 | 0 | 0 | low | leading edge (rising) | trailing edge (falling) |
| 1 | 0 | 1 | low | trailing edge (falling) | leading edge (rising) |
| 2 | 1 | 0 | high | leading edge (falling) | trailing edge (rising) |
| 3 | 1 | 1 | high | trailing edge (rising) | leading edge (falling) |
CPOL is the idle level of SCK. A leading edge leaves the idle level, and a trailing edge returns to it. With CPHA = 0, bit 0 is placed on the data wire before the first edge. This is called the preload.
Logical bit 0 is the first bit on the wire. It is the most significant bit (MSB) of the word, unless lsb_first = 1. Only the low width bits of a word take part.
| Edge t (from the start edge) | What the pins show after the edge |
|---|---|
| t = 0 | The start edge. One select goes low, SCK is idle and busy = 1. MOSI holds bit 0 when CPHA = 0, else 0. |
| t = divisor, 2 x divisor, ... | SCK toggles once per divisor edges, 2 x width times in all. The last toggle is at t = 2 x width x divisor. |
| t = (2k + 1 + CPHA) x divisor | Bit k (counting from 0) of the reply is sampled and appears in rx_word. |
| t = (2 x width + 1) x divisor | Both selects rise, busy = 0 and done = 1 for this edge only. |
| Bit of the result | Set when the observed value differs from |
|---|---|
| 0 | cs: 3 once finished, else 2 for device 0 and 1 for device 1. |
| 1 | ck: CPOL XOR (e mod 2), where e = t / d, at most 2 x w. |
| 2 | mo: logical bit e / 2 when CPHA = 0, or (e - 1) / 2 when CPHA = 1, capped at w - 1. With CPHA = 1 and e = 0, MOSI is 0. |
| 3 | active: 1 until the frame is finished. |
| 4 | complete: 1 only at t = (2 x w + 1) x d. |
| 5 | got: logical bit k is present once t is at least (2k + 1 + h) x d. All other bits are 0. |
| 7:6 | Always 0. |
A frame is finished from t = (2 x w + 1) x d. From abort_at on, an aborted frame expects cs = 3, ck = CPOL, mo = 0, active = 0, complete = 0 and got = 0.
| Fault | What the checker sees at t = d + 1 of every frame |
|---|---|
| 0 | Nothing changed. |
| 1 | cs forced to 0, both selects low. |
| 2 | ck inverted. |
| 3 | mo inverted. |
| 4 | active inverted. |
| 5 | complete inverted. |
| 6 | Bit 15 of got flipped. |
After every clk edge the tests write the real pins and the checker result, such as 20110:0000;00;. The digits are cs_n (0 to 3), sck, mosi, busy and done. Then come rx_word in hex and the 8-bit result in hex.
The result compares the whole record string. trace_matches is yes only when every record matches.
Examples
Example 1
Three Clean Frames- Input
Frame 1: mode 0, 8 bits, divisor 2, device 0, send 0xa5, reply 0x3c. Frame 2: mode 3, 4 bits, LSB first, device 1. Frame 3: mode 0, 8 bits, send 0x55aa, reply 0x8001. No fault.
- Output
transcript_chars = 6048, expected_chars = 6048, trace_matches = yes
Frame 3 uses 8 bits, so only 0xaa goes out and 0x01 comes back. Every pin is right, so the result is 00 after all 432 edges.
Example 2
Both Selects Low- Input
Frame 1: mode 3, 4 bits, divisor 1, LSB first, device 1. Frame 2: mode 0, device 0. Frame 3: like frame 1. Fault 1.
- Output
transcript_chars = 6048, expected_chars = 6048, trace_matches = yes
At t = 2 of each frame the checker sees cs = 0. Both devices look selected at once. The result must be 01 on those three edges and 00 everywhere else.
Example 3
Abort, Restart and a Flipped Bit- Input
Frame 1: mode 1, 16 bits, divisor 4, device 0, aborted at t = 5. Frame 2: mode 2, 4 bits, LSB first, device 1. Frame 3: mode 1, 16 bits. Fault 6.
- Output
transcript_chars = 6048, expected_chars = 6048, trace_matches = yes
Frame 1 is idle from t = 5, so got must be 0. The checker sees 0x8000 there and reports 20. Frames 2 and 3 start cleanly without a reset and report 20 at t = 5 too.
Constraints and Contract
- Build every expected value from the arguments only. Do not read the controller's counters.
- width is 1 to 16 and divisor is 1 to 4. Inputs are never x or z.
- Keep the function name, the argument list and the 8-bit return type.
Hints
Hint 1
Six pins are checked, one bit each. Check that every bit really compares something.
Hint 2
Example 2 forces cs to 0. Which expected value should the checker compare it with on edge 2?
Hint 3
Look at the line that sets bad[0]. It only asks whether some select is low. It must compare both select pins.
What the grader checks: Each test runs three frames on a trusted controller without a reset between them. One observed value may be corrupted per frame. The checker must stay quiet on clean edges and flag the corrupted pin. Analog timing is not checked.
87 checks on Icarus · browser samples / server checks. 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