H7.5 SPI Controller Regression

HardFixIcarus · browser samples / server checksReview the topic

Fix an SPI controller that passes one frame after reset but breaks the next frame.

Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Qualify word lengths, divisors, aborted transfers and edge/bit-order mutations.

An SPI controller passed every single-frame test. In the system test, a frame that follows a finished frame ends early, often with no clock edges at all. A frame that follows an abort works.

You get an SPI controller, spi_master, and the pin checker check_spi() from Multi-Device SPI Sequencing. The checker is correct. The controller has one bug.

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.

After each clk edge the tests record the real pins and the checker result. On one edge per frame the tests may corrupt one value the checker sees. The checker must flag it.

Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
mode2input of each check
word_bits5input of each check
half_period3input of each check
Full specification
The controller ports (keep them)
module spi_master(
  input  logic clk, reset, abort_frame, start,
  input  logic [15:0] tx_word,
  input  logic [7:0]  width, divisor,
  input  logic cpol, cpha, lsb_first, device,
  input  logic miso,
  output logic [1:0]  cs_n,
  output logic sck, mosi, busy, done,
  output logic [15:0] rx_word);
  // your controller
endmodule
The checker (keep its name and arguments)
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
ModeCPOLCPHAIdle SCKSamples onDrives on
000lowleading edge (rising)trailing edge (falling)
101lowtrailing edge (falling)leading edge (rising)
210highleading edge (falling)trailing edge (rising)
311hightrailing 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.

RuleWhat a correct controller does
Sizeswidth is 1 to 16 bits. divisor is 1 to 4 clk edges per half period of SCK.
Startstart = 1 while idle captures every setting and begins a frame. A start while busy is ignored.
Selectdevice 0 drives cs_n = 2 (cs_n[0] low). device 1 drives cs_n = 1 (cs_n[1] low). Idle is 3.
ReceiveEach sampled MISO bit goes straight to its own place in rx_word. Bits not yet sampled read 0.
FinishBoth selects go high, busy falls and done is 1 for exactly one edge. MOSI keeps its last value.
ResetSynchronous, active high. Selects high, SCK = cpol, MOSI = 0, busy = 0, rx_word = 0.
Abortabort_frame = 1 cancels the frame. Selects high, SCK = the captured idle level, MOSI = 0, rx_word = 0, no done.
Edge t (from the start edge)What the pins show after the edge
t = 0The 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 divisorBit k (counting from 0) of the reply is sampled and appears in rx_word.
t = (2 x width + 1) x divisorBoth selects rise, busy = 0 and done = 1 for this edge only.
ArgumentMeaning
tThe clk edge number since this frame's start edge, from 0.
w, dThe frame width and divisor.
p, h, lCPOL, CPHA and lsb_first of this frame.
devThe device this frame selects, 0 or 1.
abort_atThe edge where this frame is aborted, or -1 for no abort.
sent, receivedThe word the controller sends and the word the peripheral replies.
cs, ck, mo, active, complete, gotThe observed cs_n, sck, mosi, busy, done and rx_word after edge t.
Bit of the resultSet when the observed value differs from
0cs: 3 once finished, else 2 for device 0 and 1 for device 1.
1ck: CPOL XOR (e mod 2), where e = t / d, at most 2 x w.
2mo: 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.
3active: 1 until the frame is finished.
4complete: 1 only at t = (2 x w + 1) x d.
5got: logical bit k is present once t is at least (2k + 1 + h) x d. All other bits are 0.
7:6Always 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.

FaultWhat the checker sees at t = d + 1 of every frame
0Nothing changed.
1cs forced to 0, both selects low.
2ck inverted.
3mo inverted.
4active inverted.
5complete inverted.
6Bit 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 1 ends with 16 SCK edges. Frame 2 must start its count from 0 and run 8 edges. The checker result is 00 on every edge.

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

The abort in frame 1 clears the controller, so frame 2 starts cleanly. Frame 3 follows a finished frame and must also start its count from 0. The checker reports 20 at t = 5 of each frame.

Constraints and Contract

  • Submit both spi_master and check_spi. The checker builds its expected values from its arguments only.
  • width is 1 to 16 and divisor is 1 to 4. Inputs are never x or z.
  • Keep the module, the function, their ports and their arguments.

Hints

Hint 1

The first frame after reset works. Ask what a finished frame leaves behind that a reset or an abort would clear.

Hint 2

Trace example 1. Frame 1 ends after 16 SCK edges. What is edge_count when frame 2 starts?

Hint 3

Compare the start branch with the reset and abort branches. Which counter does start forget to clear?

What the grader checks: Each test runs three frames on one controller without a reset between them. It covers all modes, both devices, both bit orders, aborts and corrupted observations. Both the real pins and the checker result are compared on every edge.

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

Report a problem with this page

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

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.
Expected output
transcript_chars = 6048, expected_chars = 6048, trace_matches = yes

Frame 1 ends with 16 SCK edges. Frame 2 must start its count from 0 and run 8 edges. The checker result is 00 on every edge.

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

Icarus · browser samples / server checks