Hamming Decoder
Easy · Combinational · SystemVerilog · VHDL
Design a Hamming(7,4) decoder that corrects a single-bit error.
A Hamming(7,4) codeword carries four data bits and three parity bits. Numbering its bits by position from 1 to 7, the parity bits sit at positions 1, 2 and 4, and the parity bit at position 2^k makes the XOR of every position whose number has bit k set equal to 0. A single flipped bit breaks exactly the checks whose bits spell out its position, which is how the decoder finds it.
codeword_in[i] is position i+1: in order from bit 0 they are p1, p2, d1, p4, d2, d3, d4.
syndrome_out is the syndrome of codeword_in: its bit k is the XOR of the bits at every position whose number has bit k set. A non-zero syndrome is taken as the position of a single flipped bit, and that bit is flipped back; a double-bit error is not told apart from a single one. data_out is d4, d3, d2, d1 of the corrected codeword, with d4 in its top bit. error_detected and error_corrected are both 1 exactly when the syndrome is non-zero.
The module is combinational.