Email: bknpk@hotmail.com Phone: +972-54-7649119


V

 

ASIC/FPGA Design and Verification Out Source Services

VHDL SD CRC16 function.



  1. For a test-bench in a SD slave project I needed to calculate CRC16 in VHDL. For that I wrote a function and will describe it in this page.

  2. The CRC function receives data and number of bits to work on. The CRC is calculated using a simple for loop. For debug I only print a few results in the start and end of the calculation.

    1.   function f_CRC16( data : std_logic_vector; nbits : in std_logic_vector
    2.   ) return std_logic_vector is
    3.   variable v_crc_reg : std_logic_vector(15 downto 0);
    4.   variable v_crc_temp : std_logic_vector(15 downto 0);
    5.   variable v_this_bit : integer;
    6.   variable v_tmp : integer;
    7.   variable my_line : line;
    8.   variable result : std_logic_vector(15 downto 0);
    9.   variable data_d : std_logic_vector(data'length-1 downto 0) := data;
    10.   begin
    11.     v_crc_reg := (others => '0');
    12.     for v_this_bit in 1 to f_dbg_conv_i(nbits) loop
    13.       v_crc_temp := v_crc_reg;
    14.       v_crc_reg(15 downto 1) := v_crc_temp(14 downto 0);
    15.       v_crc_reg(0) := (data(f_dbg_conv_i(nbits)-v_this_bit) xor v_crc_temp(15));
    16.       v_crc_reg(5) := (v_crc_temp( 4) xor v_crc_reg(0));
    17.       v_crc_reg(12) := (v_crc_temp(11) xor v_crc_reg(0));
    18.       --dbg pini
    19.       if(v_this_bit <= 2 or v_this_bit >= f_dbg_conv_i(nbits)-2) then
    20.         write(my_line, string'("CRC16 dbg reg= "));
    21.         hwrite(my_line, v_crc_reg);
    22.         write(my_line, string'(" N= "));
    23.         v_tmp := f_dbg_conv_i(nbits)-v_this_bit;
    24.         write(my_line, v_tmp);
    25.         write(my_line, string'(" di= "));
    26.         write(my_line, data(v_tmp downto v_tmp));
    27.         writeline(output, my_line);
    28.       end if;
    29.       --dbg pini
    30.     end loop;

    31.     result := v_crc_reg;
    32.     return result;
    33.   end f_CRC16;


  3. For more details, please e mail me to bknpk@hotmail.com . Put SD flash in the e mail subject.



Search This Site


Feedback This Site




new pages on this site