Fix an SPI edge model that drives the wrong bit on mode 0 and mode 2 trailing edges.
Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Derive drive/sample edges for each SPI mode, including the first and final bits.
A testbench driver asks a small model what to do on each SCK edge. In modes 1 and 3 the driver works. In modes 0 and 2 the peripheral receives the first bit twice, and every later bit one place late.
edge_rule() describes one SCK edge of a frame. It returns the SCK level after the edge and whether the edge drives or samples. It also returns which bit of the word that action uses. Edge 0 is the first edge away from the idle level. Edge number 255 means the moment before the first edge, when the first bit is preloaded.
The tests check every edge of every mode, for widths 1, 4, 8 and 16 in both bit orders.
Find the bug and fix it.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
edge_no | 8 | 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] edge_rule(
input logic [7:0] edge_no, width,
input logic cpol, cpha, lsb_first);
// your edge model
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.
| Edge | Result |
|---|---|
| 255 (preload) | SCK level = CPOL. sample = 0. drive = 1 only when CPHA = 0. The bit is logical bit 0. |
| Sampling edge | Edge 2k + CPHA samples logical bit k. drive = 0. |
| Drive edge, CPHA = 1 | Leading edge 2k drives logical bit k. |
| Drive edge, CPHA = 0 | Trailing edge 2k + 1 drives logical bit k + 1. Bit k was already on the wire and has just been sampled. |
| Last edge, CPHA = 0 | Edge 2 x width - 1 has no next bit. drive = 0, sample = 0, and the bit is the last logical bit. |
SCK after edge e is not CPOL for even e and CPOL for odd e.
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. The result reports the bit position in the word, not the logical bit number.
| Bits of the result | Holds |
|---|---|
| 79:11 | Always 0. |
| 10 | sck_level: SCK after the edge. |
| 9 | drive: this edge puts a new bit on the wire. |
| 8 | sample: the receiver reads the wire on this edge. |
| 7:0 | bit_index: the bit position in the word. |
Examples
Example 1
Preload the First MSB- Input
edge 255, width 8, CPOL 0, CPHA 0, MSB first
- Output
sck_level = 0, drive = yes, sample = no, bit_index = 7
Before any edge, mode 0 puts logical bit 0 on the wire. MSB first, that is bit 7. SCK is at its low idle level.
Example 2
Mode 3 Leading Edge Drives- Input
edge 0, width 8, CPOL 1, CPHA 1, MSB first
- Output
sck_level = 0, drive = yes, sample = no, bit_index = 7
With CPOL = 1 the first edge falls to 0. CPHA = 1 drives on leading edges, so this edge drives bit 7.
Example 3
Mode 1 Last Edge Samples- Input
edge 15, width 8, CPOL 0, CPHA 1, LSB first
- Output
sck_level = 0, drive = no, sample = yes, bit_index = 7
Edge 15 is the eighth trailing edge, so it samples logical bit 7. LSB first, that is bit 7 of the word.
Example 4
Mode 0 Trailing Edge Drives the Next Bit- Input
edge 1, width 4, CPOL 0, CPHA 0, MSB first
- Output
sck_level = 0, drive = yes, sample = no, bit_index = 2
Edge 0 sampled logical bit 0. The trailing edge 1 falls and drives logical bit 1, which is bit 2 of the word.
Example 5
The Last Mode 0 Edge Does Nothing- Input
edge 1, width 1, CPOL 0, CPHA 0, MSB first
- Output
sck_level = 0, drive = no, sample = no, bit_index = 0
A 1-bit frame has no next bit to drive. The final trailing edge neither drives nor samples, and reports bit 0.
Constraints and Contract
- width is 1 to 16. edge_no is 255 or 0 to 2 x width - 1. Inputs are never x or z.
- The model only maps edges to actions. Chip-select timing is not part of it.
- Keep the function name, the argument list and the 80-bit return type.
Hints
Hint 1
List the actions of a 2-bit mode 0 frame: preload, edge 0, edge 1, edge 2, edge 3. Which bit does each drive edge put on the wire?
Hint 2
In mode 0, bit 0 is preloaded and then sampled on edge 0. The trailing edge after it must drive bit 1, not bit 0 again.
Hint 3
Look at how ordinal is set on a drive edge. When CPHA = 0, the drive edge must move to the next bit before the end-of-frame check.
What the grader checks: The tests check every edge and the preload slot. They cover all four modes, both bit orders and widths 1, 4, 8 and 16. They cover the edge rules only. Real pins and timing come in the clocked problems of this project.
499 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