Fix an SPI controller whose clock divider makes every SCK half period one system edge too long.
Hardware context: SPI controller connected to devices with explicit mode and chip-select requirements. Drive and sample all four specified SPI modes with correct chip-select timing.
A new SPI controller talks to a sensor. The bits look right, but every frame runs late. On the scope, each SCK half period lasts one system edge longer than the divisor asks for.
The module spi_master is a full-duplex SPI controller. "Full duplex" means it sends on MOSI and receives on MISO at the same time. A start captures the mode, width, divisor, bit order and target device. Then the controller runs one frame and pulses done.
Each test resets the controller, starts one frame and records the pins after each of 144 clk edges. A model peripheral puts each reply bit on MISO before its sampling edge. The whole record string must match the expected one exactly.
The tests cover all four modes, both bit orders, both devices, widths 1, 4, 8 and 16, and divisors 1 to 4.
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
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
| 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.
| Rule | What a correct controller does |
|---|---|
| Sizes | width is 1 to 16 bits. divisor is 1 to 4 clk edges per half period of SCK. |
| Start | start = 1 while idle captures every setting and begins a frame. A start while busy is ignored. |
| Select | device 0 drives cs_n = 2 (cs_n[0] low). device 1 drives cs_n = 1 (cs_n[1] low). Idle is 3. |
| Receive | Each sampled MISO bit goes straight to its own place in rx_word. Bits not yet sampled read 0. |
| Finish | Both selects go high, busy falls and done is 1 for exactly one edge. MOSI keeps its last value. |
| Reset | Synchronous, active high. Selects high, SCK = cpol, MOSI = 0, busy = 0, rx_word = 0. |
| Abort | abort_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 = 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. |
After every clk edge the tests write one record, such as 20110:003c;. The digits are cs_n (0 to 3), sck, mosi, busy and done. The four hex digits after the colon are rx_word.
The result compares the whole record string. trace_matches is yes only when every record matches. The two lengths help spot a short or long transcript.
Examples
Example 1
Mode 0, Eight Bits- Input
Mode 0, 8 bits, divisor 2, MSB first, device 0. Send 0xa5; the peripheral replies 0x3c.
- Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes
The start edge pulls cs_n to 2 and preloads bit 7 of 0xa5 on MOSI. SCK toggles every 2 edges up to t = 32. Samples land at t = 2, 6, ..., 30. Done pulses at t = 34 with rx_word = 0x003c.
Example 2
Mode 3, Four Bits, LSB First- Input
Mode 3, 4 bits, divisor 1, LSB first, device 1. Send 9; the peripheral replies 6.
- Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes
cs_n goes to 1 and SCK idles high. MOSI is 0 until the first leading edge at t = 1 drives bit 0. Samples land at t = 2, 4, 6 and 8. Done pulses at t = 9 with rx_word = 0x0006.
Example 3
Mode 1, Sixteen Bits- Input
Mode 1, 16 bits, divisor 4, MSB first, device 0. Send 0x8001; the peripheral replies 0xa55a.
- Output
transcript_chars = 1584, expected_chars = 1584, trace_matches = yes
Leading edges at t = 4, 12, ... drive the bits. Trailing edges at t = 8, 16, ..., 128 sample them. Done pulses at t = 132 with rx_word = 0xa55a.
Constraints and Contract
- width is 1 to 16 and divisor is 1 to 4. Inputs are never x or z.
- Only the digital pins are checked. Analog setup and hold timing is not part of the tests.
- Keep the module name and the ports.
Hints
Hint 1
Count the system edges between two SCK toggles in example 1. The divisor there is 2. How many should there be?
Hint 2
elapsed counts up from 0 on each edge. A half period of d edges ends on the edge where elapsed reaches d - 1.
Hint 3
Look at the compare that ends a half period. It tests elapsed == d. It must test elapsed + 1 == d, so the counter wraps after d edges.
What the grader checks: Each test runs the controller on real clock edges for one frame. Every select, SCK, MOSI, busy, done and rx_word value is compared after every edge. Analog timing is not checked.
35 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