64 lines
1.8 KiB
VHDL
64 lines
1.8 KiB
VHDL
library ieee;
|
|
use ieee.std_logic_1164.all;
|
|
use ieee.numeric_std.all;
|
|
|
|
entity crc is
|
|
generic (
|
|
CRC_WIDTH : positive;
|
|
DWIDTH : positive
|
|
);
|
|
port (
|
|
clk : in std_logic;
|
|
-- Kontrollsignale
|
|
reset : in std_logic;
|
|
enable : in std_logic;
|
|
initial_value : in std_logic_vector(CRC_WIDTH-1 downto 0);
|
|
polynomial : in std_logic_vector(CRC_WIDTH-1 downto 0);
|
|
-- Datensignale
|
|
data : in std_logic_vector(DWIDTH-1 downto 0);
|
|
checksum : out std_logic_vector(CRC_WIDTH-1 downto 0)
|
|
);
|
|
end crc;
|
|
|
|
architecture rtl of crc is
|
|
|
|
-- Interne Signale fuer CRC Pruefsumme
|
|
signal checksum_i : std_logic_vector(CRC_WIDTH-1 downto 0);
|
|
signal nextChecksum : std_logic_vector(CRC_WIDTH-1 downto 0);
|
|
|
|
begin
|
|
-- Kombinatorik fuer CRC-Berechnung
|
|
ProcNextCRC: process (data, checksum_i)
|
|
variable mix: std_logic_vector(CRC_WIDTH-1 downto 0);
|
|
variable MSB : std_logic;
|
|
begin
|
|
mix := checksum_i;
|
|
for i in data'range loop
|
|
-- Pruefen ob MSB gesetzt ist
|
|
MSB := mix(mix'length-1);
|
|
-- neues Bit reinschieben
|
|
mix := mix(mix'length-2 downto 0) & data(i);
|
|
-- XOR Verknuepfung
|
|
if MSB = '1' then
|
|
mix := mix XOR polynomial;
|
|
end if;
|
|
end loop;
|
|
nextChecksum <= mix;
|
|
end process;
|
|
|
|
-- Register zum Speichern der CRC-Pruefsumme
|
|
Reg: process (clk)
|
|
begin
|
|
if rising_edge(clk) then
|
|
if reset = '1' then
|
|
checksum_i <= initial_value;
|
|
elsif enable = '1' then
|
|
checksum_i <= nextChecksum;
|
|
end if;
|
|
end if;
|
|
end process;
|
|
|
|
checksum <= checksum_i;
|
|
|
|
end architecture;
|