Cosmic Ray Shield: Hamming SECDED Codec

Problem Description
The Deep Space Relay Buoy DSR-7 transmits 8-bit telemetry words over an interplanetary link plagued by cosmic-ray single-event upsets. Your task is to implement hamming_secded, a registered encode/decode unit that wraps each 8-bit word in a 13-bit Hamming(12,8) + overall-parity codeword.
On mode=0 (encode), the module samples data_in on every rising edge and presents the 13-bit codeword on tx_out one cycle later. On mode=1 (decode), it samples the received rx_in, computes the syndrome and overall parity, corrects any single-bit error in place (asserting sec_flag), and flags an uncorrectable double-bit error via ded_flag — all with one cycle of latency. valid is de-asserted only during reset.

Interface Definition
Port | Direction | Width | Description |
|---|---|---|---|
| input | 1 | Rising-edge clock |
| input | 1 | Synchronous active-high; clears all outputs and de-asserts |
| input | 1 | 0 = encode |
| input | 8 | Raw data word to encode (sampled each cycle when |
| input | 13 | Received codeword to decode (sampled each cycle when |
| output | 13 | Encoded 13-bit SECDED codeword, registered, valid 1 cycle after input |
| output | 8 | Corrected 8-bit data, registered, valid 1 cycle after input |
| output | 1 | 1 = a single-bit error was detected and corrected this cycle |
| output | 1 | 1 = a double-bit error was detected; |
| output | 1 | De-asserted (0) only while |
Section 5 — Edge Cases
Reset mid-operation:
reset=1on any rising edge must immediately cleartx_out,data_out,sec_flag,ded_flag, andvalid— there is no partial-output race.All-zeros receive word:
rx_in=13'h0000→ syndrome=0, overall_parity=0 → no error,data_out=8'h00, both flags de-asserted.Error in overall parity bit only (
rx_in[0]flipped, syndrome=0, overall=1): data positions are intact — neither flag should fire;data_outis the correct original word.True double-bit error (syndrome≠0, overall=0):
ded_flag=1,sec_flag=0. Do not flip any bit — the syndrome points to a phantom position, not the real error.Single-bit error in a parity position (e.g. syndrome=1): fully correctable just like a data-bit error;
sec_flag=1,data_outunaffected.Encode 8'h00: all parity bits are 0, overall parity is 0 →
tx_out=13'h0000.Encode 8'hFF: expected codeword is
13'h1EEE.
Scenarios Table
Scenario | Syndrome | Overall Parity |
|
| Data OK? |
|---|---|---|---|---|---|
No error | 0 | 0 | ✗ | ✗ | ✓ |
1-bit error in data position | non-zero | 1 | ✓ | ✗ | ✓ (corrected) |
1-bit error in Hamming parity position | non-zero | 1 | ✓ | ✗ | ✓ (corrected) |
1-bit error in overall parity bit only | 0 | 1 | ✗ | ✗ | ✓ |
2-bit error | non-zero | 0 | ✗ | ✓ | ✗ |
3-bit error (looks like 1-bit to SECDED) | non-zero | 1 | ✓ | ✗ | ✗ (miscorrected) |
Worked Example: Cycle Trace (data = 8'hA5)
8'hA5 = d[7:0] = 1,0,1,0,0,1,0,1 (d[7] MSB)
Encoding:
p0 = d[0]^d[1]^d[3]^d[4]^d[6] = 1^0^0^0^0 = 1
p1 = d[0]^d[2]^d[3]^d[5]^d[6] = 1^1^0^1^0 = 1
p2 = d[1]^d[2]^d[3]^d[7] = 0^1^0^1 = 0
p3 = d[4]^d[5]^d[6]^d[7] = 0^1^0^1 = 0
overall = XOR(p0,p1,d[0]..d[7]) = 0
tx_out= 13'h144E =1_0100_0100_1110b
Verilog
Code Editor Access
Log in to write, run and submit your solution to this interview question. It's free.
