Practice
Fertigation Frame Guard
Vela Growers runs a fertigation system: a computer doses fertilizer and acid into the greenhouse irrigation water. Recipe frames travel to the dosing engine over 300 m of noisy serial cable, past 200 lamp igniters. Last April a corrupted frame told the acid pump to run 9 times longer than intended. The dosing hardware obeyed, and the acid overdose killed three rows of seedlings. The corrective action lands on your desk.
Two habits of the sending computer matter. Its watchdog restarts transmissions in the middle of anything. And every few seconds it sends a heartbeat: a frame with LEN of 0. The old guard prototype locked up on the first heartbeat of the season. That is why the greenhouse still runs unguarded.
Your task: build a guard block between the line decoder and the dosing engine, so that no frame reaches the engine without the guard's approval. The decoder hands you the framing: sof pulses at the start of a frame, and dv pulses once for each received byte on d. Check each frame's checksum as the bytes stream past. Pulse one verdict per frame: ok when the sum matches, bad when it does not.
Interface
| Port | Direction | Type | Description |
|---|---|---|---|
clk | in | 1 bit | Clock |
rst | in | 1 bit | Synchronous reset (active high) |
sof | in | 1 bit | Start-of-frame marker, 1 cycle wide |
dv | in | 1 bit | Byte strobe, 1 cycle wide per byte |
d | in | 8-bit vector | Decoded byte from the line |
ok | out | 1 bit | Frame verdict good, 1-cycle pulse (registered) |
bad | out | 1 bit | Frame verdict corrupt, 1-cycle pulse (registered) |
Behavior
- After
sof, the firstdvbyte isLEN, the nextLENdvbytes are payload, and the followingdvbyte is the checksum - The checksum is valid when it equals
(LEN + sum of payload bytes) mod 256; the length byte is inside the sum - The cycle after the checksum byte is sampled, exactly one of
okorbadpulses for exactly one cycle; both are 0 at all other times LEN = 0is legal (heartbeat): the checksum byte follows the length byte directly and must equalLEN- An
sofat any point abandons the frame in progress with no verdict and starts a fresh frame - Nothing from the abandoned frame may leak into the new frame's sum
- A
dvcoinciding withsofis ignored: the byte belongs to the marker, and the nextdvbyte is the new frame'sLEN dvbytes outside any frame are ignored; gaps of any length between bytes are legal- A new
sofmay arrive on the very cycle a verdict pulses rst = 1at a rising edge returns the guard to idle, verdict pulses low, frame in progress forgotten
What the bench checks
- The testbench checks
okandbadon every cycle - Good and bad frames verify that the checksum includes
LENand that each verdict lasts one cycle - Valid frames cover arbitrary gaps, zero-length heartbeats, and an eight-byte payload
- Wrapped checksum sums are accepted modulo 256, while an off-by-one checksum after the wrap is rejected
- A new
sofmid-payload or where a checksum was expected must abandon the old frame without a verdict - A byte arriving with
sofand stray bytes outside a frame must be ignored - Reset mid-frame must cancel it, and a new
sofduring a verdict pulse must start the next frame cleanly
Constraints
everything is synchronous to clk; ok and bad are registered one-cycle pulses.
this models the checksum guard between decoder and dosing engine. The line decoder, the noisy cable, and the dosing hardware are out of scope; framing arrives ready-made on sof and dv.
do not buffer whole frames. LEN can be up to 255, and the guard must judge the stream as it flows.
Do not add ports.
Click Run to execute your code. Output will appear here.