SURF  1.0
Crc32.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : Crc32.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2013-05-01
5 -- Last update: 2017-02-23
6 -------------------------------------------------------------------------------
7 -- Description:
8 -- This is an implementation of a generic N-byte input CRC32 calculation.
9 -- The polynomial and CRC register initialization are generic configurable, but
10 -- default to the commonly used 0x04C11DB7 and 0xFFFFFFFF, respectively.
11 -- This implementation is direct, so no bytes need to be appended to the data.
12 -- Bytes are reversed on input before being used for the CRC calculation,
13 -- and the CRC register is reversed on output just before a final XOR with
14 -- 0xFFFFFFFF.
15 --
16 -- With a data input size of 4 bytes, this module is compatible with the
17 -- previous CRC32Rtl.vhdl module in the StdLib.
18 -------------------------------------------------------------------------------
19 -- This file is part of 'SLAC Firmware Standard Library'.
20 -- It is subject to the license terms in the LICENSE.txt file found in the
21 -- top-level directory of this distribution and at:
22 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
23 -- No part of 'SLAC Firmware Standard Library', including this file,
24 -- may be copied, modified, propagated, or distributed except according to
25 -- the terms contained in the LICENSE.txt file.
26 -------------------------------------------------------------------------------
27 
28 library ieee;
29 use ieee.std_logic_1164.all;
30 use ieee.std_logic_arith.all;
31 use ieee.std_logic_unsigned.all;
32 use work.StdRtlPkg.all;
33 use work.CrcPkg.all;
34 
35 --! @see entity
36  --! @ingroup base_general
37 entity Crc32 is
38  generic (
39  BYTE_WIDTH_G : integer := 4;
40  CRC_INIT_G : slv(31 downto 0) := x"FFFFFFFF";
41  CRC_POLY_G : slv(31 downto 0) := x"04C11DB7";
42  TPD_G : time := 0.5 ns);
43  port (
44  crcOut : out slv(31 downto 0); -- CRC output
45  crcClk : in sl; -- system clock
46  crcDataValid : in sl; -- indicate that new data arrived and CRC can be computed
47  crcDataWidth : in slv(2 downto 0); -- indicate width in bytes minus 1, 0 - 1 byte, 1 - 2 bytes ... , 7 - 8 bytes
48  crcIn : in slv((BYTE_WIDTH_G*8-1) downto 0); -- input data for CRC calculation
49  crcReset : in sl); -- initializes CRC logic to CRC_INIT_G
50 end Crc32;
51 
52 architecture rtl of Crc32 is
53 
54  type RegType is record
55  crc : slv(31 downto 0);
56  data : slv((BYTE_WIDTH_G*8-1) downto 0);
57  valid : sl;
58  byteWidth : slv(2 downto 0);
59  end record RegType;
60 
61  constant REG_INIT_C : RegType := (
62  crc => CRC_INIT_G,
63  data => (others => '0'),
64  valid => '0',
65  byteWidth => (others => '0')
66  );
67 
68  signal r : RegType := REG_INIT_C;
69  signal rin : RegType;
70 
71 begin
72 
73  comb : process(crcIn,crcDataWidth,crcReset,crcDataValid,r)
74  variable v : RegType;
75  variable byteXor : slv(7 downto 0);
76  begin
77  v := r;
78 
79  v.byteWidth := crcDataWidth;
80  v.valid := crcDataValid;
81  byteXor := (others => '0');
82 
83  -- Transpose the input data
84  for byte in (BYTE_WIDTH_G-1) downto 0 loop
85  for b in 0 to 7 loop
86  if (crcDataWidth >= BYTE_WIDTH_G-byte-1) then
87  v.data((byte+1)*8-1-b) := crcIn(byte*8+b);
88  else
89  v.data((byte+1)*8-1-b) := '0';
90  end if;
91  end loop;
92  end loop;
93 
94  -- Reset handling
95  if (crcReset = '0') then
96  v.crc := r.crc;
97  else
98  v.crc := CRC_INIT_G;
99  end if;
100 
101  -- Calculate CRC byte-by-byte
102  if (r.valid = '1') then
103  for byte in BYTE_WIDTH_G-1 downto 0 loop
104  if (r.byteWidth >= BYTE_WIDTH_G-byte-1) then
105  byteXor := v.crc(31 downto 24) xor r.data( (byte+1)*8-1 downto byte*8);
106  v.crc := (v.crc(23 downto 0) & x"00") xor crcByteLookup(byteXor,CRC_POLY_G);
107  end if;
108  end loop;
109  end if;
110 
111  rin <= v;
112 
113  -- Transpose each byte in the data out and invert
114  -- This inversion is equivalent to an XOR of the CRC register with xFFFFFFFF
115  for byte in 0 to 3 loop
116  for b in 0 to 7 loop
117  crcOut(byte*8+b) <= not(r.crc((byte+1)*8-1-b));
118  end loop;
119  end loop;
120 
121  end process;
122 
123  seq : process (crcClk) is
124  begin
125  if (rising_edge(crcClk)) then
126  r <= rin after TPD_G;
127  end if;
128  end process seq;
129 
130 end rtl;
std_logic sl
Definition: StdRtlPkg.vhd:28
TPD_Gtime := 0.5 ns
Definition: Crc32.vhd:42
Definition: Crc32.vhd:37
in crcClksl
Definition: Crc32.vhd:45
out crcOutslv( 31 downto 0)
Definition: Crc32.vhd:44
CRC_POLY_Gslv( 31 downto 0) := x"04C11DB7"
Definition: Crc32.vhd:41
_library_ ieeeieee
in crcResetsl
Definition: Crc32.vhd:49
in crcInslv(( BYTE_WIDTH_G* 8- 1) downto 0)
Definition: Crc32.vhd:48
in crcDataValidsl
Definition: Crc32.vhd:46
CRC_INIT_Gslv( 31 downto 0) := x"FFFFFFFF"
Definition: Crc32.vhd:40
in crcDataWidthslv( 2 downto 0)
Definition: Crc32.vhd:47
BYTE_WIDTH_Ginteger := 4
Definition: Crc32.vhd:39
std_logic_vector slv
Definition: StdRtlPkg.vhd:29