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
| Signal | Width | Role |
|---|---|---|
levels | 32 | input of each check |
serial | 32 | input of each check |
width | 8 | input of each check |
cpol | 1 | input of each check |
cpha | 1 | input of each check |
lsb_first | 1 | input of each check |
Full specification
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
| 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.
| Rule | Meaning |
|---|---|
| Records | Look at records 0 to 2 x width - 1. Ignore every higher bit of levels and serial. |
| Pick | A record is a sample when its SCK level is the level the sampling edge leaves behind. |
| Sampling level | CPHA = 0 samples leading edges, so the level is not CPOL. CPHA = 1 samples trailing edges, so the level equals CPOL. |
| Build | The k-th picked record (from 0) is logical bit k. Only the first width picked records go into the word. |
| Count | Every picked record adds 1 to the count, even past width. |
| Error | error = 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 result | Holds |
|---|---|
| 79:32 | Always 0. |
| 31:24 | sample_count: the number of picked records. |
| 23:16 | error: 1 or 0. |
| 15:0 | word: 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