combinational component

Hamming(7,4) Encoder and Corrector

Open in playground

A Hamming(7,4) code turns 4 data bits into a 7-bit codeword that survives any single flipped bit. The encoder places three even-parity bits at positions 1, 2, and 4. Each parity bit covers the positions whose index contains that power of two.

The corrector redoes the three parity checks on the received word. The three results form the syndrome, and the syndrome is the position of the flipped bit, counted from 1. A zero syndrome means the word arrived clean. Anything else points at exactly one bit, which the corrector flips back before extracting the data. err_pos publishes the syndrome, so software can log which wire keeps failing.

One honest limit: plain Hamming(7,4) assumes at most one flip. Two flips produce a nonzero syndrome that points at the wrong position, so the corrector silently miscorrects. Links that expect burst errors add an overall parity bit (the SECDED extension) or a CRC instead.

In the waveform: watch err_mask walk one bit at a time while err_pos counts 1 through 7 in step, and data_out never leaves data_in.

In the netlist: two XOR trees face each other, the encoder's parity generators and the corrector's syndrome checks, joined by the correction stage that flips the accused bit.

Parity, checksums, and error handling on real links are built from first principles in Communication Interfaces.

-- Hamming(7,4) encoder: three even-parity bits at positions 1, 2, and 4
library ieee;
use ieee.std_logic_1164.all;

entity hamming_enc is
  port (
    data : in  std_logic_vector(3 downto 0);
    code : out std_logic_vector(6 downto 0)
  );
end entity hamming_enc;

architecture rtl of hamming_enc is
begin

  -- Positions 3, 5, 6, 7 carry the data bits; positions 1, 2, 4 carry
  -- parity over the positions whose index contains that power of two.
  code(2) <= data(0);
  code(4) <= data(1);
  code(5) <= data(2);
  code(6) <= data(3);
  code(0) <= data(0) xor data(1) xor data(3);  -- covers positions 3, 5, 7
  code(1) <= data(0) xor data(2) xor data(3);  -- covers positions 3, 6, 7
  code(3) <= data(1) xor data(2) xor data(3);  -- covers positions 5, 6, 7

end architecture rtl;
Waveform loading...