SURF  1.0
CRC32Rtl.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : CRC32Rtl.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2013-05-01
5 -- Last update: 2017-02-23
6 -------------------------------------------------------------------------------
7 -- Description:
8 -- VHDL source file for CRC32 calculation to replace macro of Virtex5 in Virtex6 and Spartan6.
9 -- assuming clock positive edge, reset positive edge, LSB first, data width is 32,
10 -- polynomial CRC32 IEEE802.3 type X32+X26+X23+x22+x16+X12+X11+X10+X8+X7+X5+X4+X2+X1+1
11 -- with CRCRESETial value of 0xffffffff
12 -- similar equation can be derived from
13 -- http://www.xilinx.com/support/documentation/application_notes/xapp209.pdf
14 -- and related app notes
15 -- http://www.xilinx.com/support/documentation/application_notes/xapp562.pdf
16 -------------------------------------------------------------------------------
17 -- This file is part of 'SLAC Firmware Standard Library'.
18 -- It is subject to the license terms in the LICENSE.txt file found in the
19 -- top-level directory of this distribution and at:
20 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
21 -- No part of 'SLAC Firmware Standard Library', including this file,
22 -- may be copied, modified, propagated, or distributed except according to
23 -- the terms contained in the LICENSE.txt file.
24 -------------------------------------------------------------------------------
25 
26 library ieee;
27 use ieee.std_logic_1164.all;
28 use ieee.std_logic_arith.all;
29 use ieee.std_logic_unsigned.all;
30 
31 --! @see entity
32  --! @ingroup base_general
33 entity CRC32Rtl is
34  generic (
35  CRC_INIT : bit_vector := x"FFFFFFFF");
36  port (
37  CRCOUT : out std_logic_vector(31 downto 0); -- CRC output
38  CRCCLK : in std_logic; -- system clock
39  CRCCLKEN : in std_logic := '1';-- system clock enable
40  CRCDATAVALID : in std_logic; -- indicate that new data arrived and CRC can be computed
41  CRCDATAWIDTH : in std_logic_vector(2 downto 0); -- indicate width in bytes minus 1, 0 - 1 byte, 1 - 2 bytes
42  CRCIN : in std_logic_vector(31 downto 0); -- input data for CRC calculation
43  CRCRESET : in std_logic); -- to set CRC logic to value in crc_cNIT
44 end CRC32Rtl;
45 
46 architecture rtl of CRC32Rtl is
47 
48  -- Local Signals
49  signal data : std_logic_vector(31 downto 0);
50  signal crc : std_logic_vector(31 downto 0);
51  signal CRCDATAVALID_d : std_logic;
52  signal CRCDATAWIDTH_d : std_logic_vector(2 downto 0);
53  constant Polyval : std_logic_vector(31 downto 0) := X"04C11DB7";
54  type fb_array is array (32 downto 0) of std_logic_vector(31 downto 0);
55  signal MSBVect, TempXOR : fb_array;
56 
57  -- Register delay for simulation
58  constant tpd : time := 0.5 ns;
59 
60 begin
61  TempXOR(0) <= crc xor data;
62 
63  MS0 : for i in 0 to 31 generate
64  MS1 : for j in 0 to 31 generate
65  MSBVect(i)(j) <= TempXOR(i)(31);
66  end generate MS1;
67  end generate MS0;
68 
69  MS2 : for i in 0 to 31 generate
70  TempXOR(i+1) <= ((TempXOR(i)(30 downto 0) & '0') xor (Polyval and MSBVect(i)));
71  end generate MS2;
72 
73  process(CRCCLK,CRCCLKEN)
74  begin
75  if rising_edge(CRCCLK) and (CRCCLKEN = '1') then
76  for i in 24 to 31 loop
77  data(31 - (i - 24)) <= (CRCIN(i));
78  end loop;
79  if (CRCDATAWIDTH = "001" or CRCDATAWIDTH = "010" or CRCDATAWIDTH = "011") then
80  for i in 16 to 23 loop
81  data(23 - (i - 16)) <= (CRCIN(i));
82  end loop;
83  end if;
84  if (CRCDATAWIDTH = "010" or CRCDATAWIDTH = "011") then
85  for i in 8 to 15 loop
86  data(15 - (i - 8)) <= (CRCIN(i));
87  end loop;
88  end if;
89  if (CRCDATAWIDTH = "011") then
90  for i in 0 to 7 loop
91  data(7 - (i)) <= (CRCIN(i));
92  end loop;
93  end if;
94 
95  if (CRCDATAWIDTH = "000") then
96  data(23 downto 0) <= (others => '0');
97  elsif (CRCDATAWIDTH = "001") then
98  data(15 downto 0) <= (others => '0');
99  elsif (CRCDATAWIDTH = "010") then
100  data(7 downto 0) <= (others => '0');
101  end if;
102  CRCDATAVALID_d <= CRCDATAVALID;
103  CRCDATAWIDTH_d <= CRCDATAWIDTH;
104  end if;
105  end process;
106 
107  CRCP : process (CRCCLK)
108  begin
109  if rising_edge(CRCCLK) then
110  if (CRCRESET = '1') then
111  crc <= To_StdLogicVector(CRC_INIT);
112  elsif (CRCDATAVALID_d = '1') and (CRCCLKEN = '1') then
113  if (CRCDATAWIDTH_d = "000") then
114  crc <= TempXOR(8);
115  elsif (CRCDATAWIDTH_d = "001") then
116  crc <= TempXOR(16);
117  elsif (CRCDATAWIDTH_d = "010") then
118  crc <= TempXOR(24);
119  elsif (CRCDATAWIDTH_d = "011") then
120  crc <= TempXOR(32);
121  end if;
122  end if;
123  end if;
124  end process;
125 
126  -- Trasposing CRC bytes
127  CRCOUT <= not(crc(24) & crc(25) & crc(26) & crc(27) & crc(28) & crc(29) & crc(30) & crc(31)
128  & crc(16) & crc(17) & crc(18) & crc(19) & crc(20) & crc(21) & crc(22) & crc(23)
129  & crc(8) & crc(9) & crc(10) & crc(11) & crc(12) & crc(13) & crc(14) & crc(15)
130  & crc(0) & crc(1) & crc(2) & crc(3) & crc(4) & crc(5) & crc(6) & crc(7));
131 end rtl;
132 
in CRCRESETstd_logic
Definition: CRC32Rtl.vhd:43
out CRCOUTstd_logic_vector( 31 downto 0)
Definition: CRC32Rtl.vhd:37
in CRCINstd_logic_vector( 31 downto 0)
Definition: CRC32Rtl.vhd:42
in CRCCLKENstd_logic := '1'
Definition: CRC32Rtl.vhd:39
_library_ ieeeieee
in CRCDATAVALIDstd_logic
Definition: CRC32Rtl.vhd:40
CRC_INITbit_vector := x"FFFFFFFF"
Definition: CRC32Rtl.vhd:35
in CRCDATAWIDTHstd_logic_vector( 2 downto 0)
Definition: CRC32Rtl.vhd:41
in CRCCLKstd_logic
Definition: CRC32Rtl.vhd:38