0
0
VhdlHow-ToBeginner · 4 min read

VHDL Code for Hamming Code Decoder: Syntax and Example

A Hamming code decoder in VHDL detects and corrects single-bit errors in encoded data by calculating syndrome bits and correcting the error position. The decoder uses XOR logic to find error location and outputs the corrected data. This can be implemented using combinational logic in a VHDL entity and architecture.
📐

Syntax

The basic structure of a Hamming code decoder in VHDL includes an entity defining inputs and outputs, and an architecture implementing the decoding logic. Inputs are the received encoded bits, and outputs are the corrected data bits and an error flag.

  • entity: Declares input/output ports.
  • architecture: Contains the logic to calculate syndrome bits and correct errors.
  • signal: Used internally to hold syndrome and corrected data.
vhdl
entity HammingDecoder is
    Port (
        encoded : in std_logic_vector(6 downto 0); -- 7-bit encoded input
        data_out : out std_logic_vector(3 downto 0); -- 4-bit corrected data output
        error_detected : out std_logic -- error flag
    );
end HammingDecoder;

architecture Behavioral of HammingDecoder is
    signal syndrome : std_logic_vector(2 downto 0);
    signal corrected : std_logic_vector(6 downto 0);
begin
    -- decoding logic here
end Behavioral;
💻

Example

This example shows a complete VHDL code for a (7,4) Hamming code decoder. It calculates syndrome bits by XORing specific encoded bits, detects error position, corrects the bit if needed, and outputs the corrected 4-bit data and an error flag.

vhdl
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity HammingDecoder is
    Port (
        encoded : in std_logic_vector(6 downto 0); -- 7-bit encoded input
        data_out : out std_logic_vector(3 downto 0); -- 4-bit corrected data output
        error_detected : out std_logic -- error flag
    );
end HammingDecoder;

architecture Behavioral of HammingDecoder is
    signal syndrome : std_logic_vector(2 downto 0);
    signal corrected : std_logic_vector(6 downto 0);
    signal error_pos : integer range 0 to 7;
begin
    -- Calculate syndrome bits
    syndrome(0) <= encoded(6) xor encoded(4) xor encoded(2) xor encoded(0); -- parity bit 1
    syndrome(1) <= encoded(5) xor encoded(4) xor encoded(1) xor encoded(0); -- parity bit 2
    syndrome(2) <= encoded(3) xor encoded(2) xor encoded(1) xor encoded(0); -- parity bit 3

    -- Convert syndrome to error position (1-based)
    error_pos <= to_integer(unsigned(syndrome));

    process(encoded, error_pos)
    begin
        corrected <= encoded;
        if error_pos /= 0 then
            -- Correct the bit at error_pos (1 to 7)
            corrected(7 - error_pos) <= not encoded(7 - error_pos);
            error_detected <= '1';
        else
            error_detected <= '0';
        end if;

        -- Extract corrected data bits (positions 6,5,3,2)
        data_out(3) <= corrected(6);
        data_out(2) <= corrected(5);
        data_out(1) <= corrected(3);
        data_out(0) <= corrected(2);
    end process;
end Behavioral;
Output
When given a 7-bit encoded input with at most one bit error, the decoder outputs the corrected 4-bit data and sets error_detected to '1' if an error was found and corrected, otherwise '0'.
⚠️

Common Pitfalls

  • Incorrect syndrome bit calculation can cause wrong error detection.
  • Forgetting to invert the bit at the error position leads to no correction.
  • Mixing bit positions (1-based vs 0-based) causes wrong bit flips.
  • Not handling the case when syndrome is zero (no error) can cause false error flags.
vhdl
wrong syndrome calculation example:
-- syndrome(0) <= encoded(6) xor encoded(5) xor encoded(2) xor encoded(0); -- wrong parity bits

correct syndrome calculation:
-- syndrome(0) <= encoded(6) xor encoded(4) xor encoded(2) xor encoded(0);
📊

Quick Reference

Remember these key points when writing a Hamming code decoder in VHDL:

  • Use XOR to calculate syndrome bits from parity and data bits.
  • Syndrome value zero means no error; non-zero indicates error position.
  • Flip the bit at the error position to correct single-bit errors.
  • Extract corrected data bits from fixed positions after correction.

Key Takeaways

Calculate syndrome bits using XOR of specific encoded bits to detect errors.
Use syndrome value to find and correct the single-bit error position.
Output corrected data bits and an error flag indicating correction.
Be careful with bit indexing and syndrome calculation to avoid mistakes.
Test the decoder with error and no-error inputs to verify correctness.