H7.1 SPI Edge Decoder

EasyFixIcarus · browser samples / server checksReview the topic

Fix an SPI decoder that reads modes 0 and 2 correctly but returns a wrong word in modes 1 and 3.

Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Reconstruct a word from the device contract sampling edges.

A bus monitor uses a small helper to rebuild SPI words from a captured trace. Words in modes 0 and 2 come out right. In modes 1 and 3 every word is wrong, yet the sample count looks normal.

decode_spi() gets one record per SCK edge. Bit e of levels is the SCK level just after edge e. Bit e of serial is the data wire at that moment. The function looks at the first 2 x width records. It picks the records where the receiver samples and builds the word from them.

It also counts the picked records. When the count is not exactly width, it sets an error flag.

The tests try all four modes, both bit orders and widths 1, 4, 8 and 16. On the records where the receiver does not sample, the data is the opposite bit. One more trace never toggles SCK at all.

Find the bug and fix it.

Signals in the Timeline

SignalWidthRole
levels32input of each check
serial32input of each check
width8input of each check
cpol1input of each check
cpha1input of each check
lsb_first1input of each check
Full specification
The function (keep its name and arguments)
function automatic logic [79:0] decode_spi(
  input logic [31:0] levels, serial,
  input logic [7:0]  width,
  input logic cpol, cpha, lsb_first);
  // your decoder
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.

RuleMeaning
RecordsLook at records 0 to 2 x width - 1. Ignore every higher bit of levels and serial.
PickA record is a sample when its SCK level is the level the sampling edge leaves behind.
Sampling levelCPHA = 0 samples leading edges, so the level is not CPOL. CPHA = 1 samples trailing edges, so the level equals CPOL.
BuildThe k-th picked record (from 0) is logical bit k. Only the first width picked records go into the word.
CountEvery picked record adds 1 to the count, even past width.
Errorerror = 1 when the count is not exactly width. A trace that never reaches the sampling level gives a count of 0.

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.

Bits of the resultHolds
79:32Always 0.
31:24sample_count: the number of picked records.
23:16error: 1 or 0.
15:0word: the rebuilt word, with 0 above width.

The decoder checks only the count of samples. It does not judge glitches, setup time or hold time.

Examples

Example 1

Mode 0, Eight Bits, MSB First
Input
levels = 0x5555, serial = 0x6699, width 8, CPOL 0, CPHA 0, MSB first
Output
sample_count = 8, error = no, word = 0xa5

SCK is high after every even edge, so records 0, 2, 4 and so on are samples. Their serial bits are 1, 0, 1, 0, 0, 1, 0, 1, which is 0xa5. Eight samples give no error.

Example 2

Mode 3, Four Bits, LSB First
Input
levels = 0xaa, serial = 0x96, width 4, CPOL 1, CPHA 1, LSB first
Output
sample_count = 4, error = no, word = 0x9

CPHA = 1 samples trailing edges. SCK is back at its high idle level after odd edges, so records 1, 3, 5 and 7 are samples. Their bits 1, 0, 0, 1 fill the word from bit 0, giving 9.

Example 3

A Trace That Never Toggles
Input
levels = 0x0000, serial = 0x6aa9, width 8, CPOL 0, CPHA 0, MSB first
Output
sample_count = 0, error = yes, word = 0x0

SCK never leaves its low idle level, so no record is a sample. The count is 0, error is 1 and the word stays 0.

Constraints and Contract

  • width is 1 to 16. Inputs are never x or z.
  • Keep the function name, the argument list and the 80-bit return type.

Hints

Hint 1

Write the sampling level for each of the four modes. Which modes does the current code get right?

Hint 2

Work through example 2 by hand. Mode 3 must pick the odd records, where SCK is back at its high idle level.

Hint 3

Look at the line that sets take_sample. It never reads cpha, so it always picks the records after leading edges.

What the grader checks: The tests cover all four modes, both bit orders, widths 1, 4, 8 and 16 and a trace with no clock. They check the count, error flag and word. Glitches and setup or hold timing are not checked.

163 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

Mode 0, Eight Bits, MSB First

Input
levels = 0x5555, serial = 0x6699, width 8, CPOL 0, CPHA 0, MSB first
Expected output
sample_count = 8, error = no, word = 0xa5

SCK is high after every even edge, so records 0, 2, 4 and so on are samples. Their serial bits are 1, 0, 1, 0, 0, 1, 0, 1, which is 0xa5. Eight samples give no error.

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

Icarus · browser samples / server checks