在verilog中的8位输入数据上使用8408 polunomial生成16位CRC

时间:2015-11-03 08:43:15

标签: system-verilog

我有8位输入数据,并且我已经计算了输入输入字节的累积CRC-16。请帮我用多项式0x8408生成CRC-16生成的verilog代码。

function bit [15:0] ecc::cal_crc(input bit [31:0] data[$]);
bit [15:0] rCRC = 16'hFFFF;
bit [31:0] payload;
begin
foreach(data[i]) 
begin
   payload = data[i];
   rCRC[0] =  rCRC[0] ^ rCRC[4] ^ rCRC[5] ^ rCRC[6] ^ rCRC[10] ^ rCRC[12]
             ^  rCRC[13] ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[2] 
            ^ payload[2] ^ payload[4] ^ payload[5] ^ payload[6] ^ payload[8] 
           ^ payload[8] ^ payload[9] ^ payload[9] ^ payload[10] ^ payload[12]
       ^ payload[13] ^ payload[16] ^ payload[16] ^ payload[17] ^ payload[17] 
           ^ payload[20] ^ payload[21] ^ payload[24] ^ payload[28];
   rCRC[1] =  rCRC[0] ^ rCRC[1] ^ rCRC[5] ^ rCRC[6] ^ rCRC[7]  ^ rCRC[11] ^   rCRC[13] ^ rCRC[14]                                  ^ payload[0] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[5] ^ payload[6] ^ payload[7] ^ payload[9] ^ payload[9] ^ payload[10] ^ payload[10] ^ payload[11] ^ payload[13] ^ payload[14] ^ payload[17] ^ payload[17] ^ payload[18] ^ payload[18] ^ payload[21] ^ payload[22] ^ payload[25] ^ payload[29];
   rCRC[2] =  rCRC[1] ^ rCRC[2] ^ rCRC[6] ^ rCRC[7] ^ rCRC[8]  ^ rCRC[12] ^ rCRC[14] ^ rCRC[15]                                  ^ payload[1] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[6] ^ payload[7] ^ payload[8] ^ payload[10] ^ payload[10] ^ payload[11] ^ payload[11] ^ payload[12] ^ payload[14] ^ payload[15] ^ payload[18] ^ payload[18] ^ payload[19] ^ payload[19] ^ payload[22] ^ payload[23] ^ payload[26] ^ payload[30];
   rCRC[3] =  rCRC[0] ^ rCRC[2] ^ rCRC[3] ^ rCRC[7] ^ rCRC[8]  ^ rCRC[9]  ^ rCRC[13] ^ rCRC[15]                                  ^ payload[0] ^ payload[2] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[7] ^ payload[8] ^ payload[9] ^ payload[11] ^ payload[11] ^ payload[12] ^ payload[12] ^ payload[13] ^ payload[15] ^ payload[16] ^ payload[19] ^ payload[19] ^ payload[20] ^ payload[20] ^ payload[23] ^ payload[24] ^ payload[27] ^ payload[31];
   rCRC[4] =  rCRC[0] ^ rCRC[1] ^ rCRC[3] ^ rCRC[5] ^ rCRC[6]  ^ rCRC[8]  ^ rCRC[9]  ^ rCRC[12] ^ rCRC[13] ^ rCRC[14]            ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[8] ^ payload[9] ^ payload[9] ^ payload[9] ^ payload[10] ^ payload[10] ^ payload[12] ^ payload[13] ^ payload[14] ^ payload[16] ^ payload[17] ^ payload[20] ^ payload[21] ^ payload[25];
   rCRC[5] =  rCRC[0] ^ rCRC[1] ^ rCRC[2] ^ rCRC[4] ^ rCRC[6]  ^ rCRC[7]  ^ rCRC[9]  ^ rCRC[10] ^ rCRC[13] ^ rCRC[14] ^ rCRC[15] ^ payload[0] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[9] ^ payload[10] ^ payload[10] ^ payload[10] ^ payload[11] ^ payload[11] ^ payload[13] ^ payload[14] ^ payload[15] ^ payload[17] ^ payload[18] ^ payload[21] ^ payload[22] ^ payload[26];
   rCRC[6] =  rCRC[1] ^ rCRC[2] ^ rCRC[3] ^ rCRC[5] ^ rCRC[7]  ^ rCRC[8]  ^ rCRC[10] ^ rCRC[11] ^ rCRC[14] ^ rCRC[15]            ^ payload[0] ^ payload[0] ^ payload[1] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[10] ^ payload[11] ^ payload[11] ^ payload[11] ^ payload[12] ^ payload[12] ^ payload[14] ^ payload[15] ^ payload[16] ^ payload[18] ^ payload[19] ^ payload[22] ^ payload[23] ^ payload[27];
   rCRC[7] =  rCRC[0] ^ rCRC[2] ^ rCRC[3] ^ rCRC[4] ^ rCRC[6]  ^ rCRC[8]  ^ rCRC[9]  ^ rCRC[11] ^ rCRC[12] ^ rCRC[15]            ^ payload[0] ^ payload[0] ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[2] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[8] ^ payload[8] ^ payload[9] ^ payload[11] ^ payload[12] ^ payload[12] ^ payload[12] ^ payload[13] ^ payload[13] ^ payload[15] ^ payload[16] ^ payload[17] ^ payload[19] ^ payload[20] ^ payload[23] ^ payload[24] ^ payload[28];
   rCRC[8] =  rCRC[1] ^ rCRC[3] ^ rCRC[4] ^ rCRC[5] ^ rCRC[7]  ^ rCRC[9]  ^ rCRC[10] ^ rCRC[12] ^ rCRC[13]                       ^ payload[0] ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[8] ^ payload[8] ^ payload[9] ^ payload[9] ^ payload[9] ^ payload[10] ^ payload[12] ^ payload[13] ^ payload[13] ^ payload[13] ^ payload[14] ^ payload[14] ^ payload[16] ^ payload[17] ^ payload[18] ^ payload[20] ^ payload[21] ^ payload[24] ^ payload[25] ^ payload[29];
   rCRC[9] =  rCRC[2] ^ rCRC[4] ^ rCRC[5] ^ rCRC[6] ^ rCRC[8]  ^ rCRC[10] ^ rCRC[11] ^ rCRC[13] ^ rCRC[14]                       ^ payload[0] ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[5] ^ payload[6] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[9] ^ payload[9] ^ payload[10] ^ payload[10] ^ payload[10] ^ payload[11] ^ payload[13] ^ payload[14] ^ payload[14] ^ payload[14] ^ payload[15] ^ payload[15] ^ payload[17] ^ payload[18] ^ payload[19] ^ payload[21] ^ payload[22] ^ payload[25] ^ payload[26] ^ payload[30];
   rCRC[10] = rCRC[0] ^ rCRC[3] ^ rCRC[5] ^ rCRC[6] ^ rCRC[7]  ^ rCRC[9]  ^ rCRC[11] ^ rCRC[12] ^ rCRC[14] ^ rCRC[15]            ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[2] ^ payload[2] ^ payload[3] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[6] ^ payload[7] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[8] ^ payload[9] ^ payload[10] ^ payload[10] ^ payload[11] ^ payload[11] ^ payload[11] ^ payload[12] ^ payload[14] ^ payload[15] ^ payload[15] ^ payload[15] ^ payload[16] ^ payload[16] ^ payload[18] ^ payload[19] ^ payload[20] ^ payload[22] ^ payload[23] ^ payload[26] ^ payload[27] ^ payload[31];
   rCRC[11] = rCRC[0] ^ rCRC[1] ^ rCRC[5] ^ rCRC[7] ^ rCRC[8]  ^ rCRC[15]                                                        ^ payload[0] ^ payload[1] ^ payload[3] ^ payload[3] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[7] ^ payload[8] ^ payload[11] ^ payload[11] ^ payload[12] ^ payload[12] ^ payload[15] ^ payload[16] ^ payload[19] ^ payload[23] ^ payload[27];
   rCRC[12] = rCRC[0] ^ rCRC[1] ^ rCRC[2] ^ rCRC[6] ^ rCRC[8]  ^ rCRC[9]                                                         ^ payload[0] ^ payload[1] ^ payload[2] ^ payload[4] ^ payload[4] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[8] ^ payload[9] ^ payload[12] ^ payload[12] ^ payload[13] ^ payload[13] ^ payload[16] ^ payload[17] ^ payload[20] ^ payload[24] ^ payload[28];
   rCRC[13] = rCRC[1] ^ rCRC[2] ^ rCRC[3] ^ rCRC[7] ^ rCRC[9]  ^ rCRC[10]                                                        ^ payload[1] ^ payload[2] ^ payload[3] ^ payload[5] ^ payload[5] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[9] ^ payload[10] ^ payload[13] ^ payload[13] ^ payload[14] ^ payload[14] ^ payload[17] ^ payload[18] ^ payload[21] ^ payload[25] ^ payload[29];
   rCRC[14] = rCRC[2] ^ rCRC[3] ^ rCRC[4] ^ rCRC[8] ^ rCRC[10] ^ rCRC[11]                                                        ^ payload[0] ^ payload[0] ^ payload[2] ^ payload[3] ^ payload[4] ^ payload[6] ^ payload[6] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[10] ^ payload[11] ^ payload[14] ^ payload[14] ^ payload[15] ^ payload[15] ^ payload[18] ^ payload[19] ^ payload[22] ^ payload[26] ^ payload[30];
   rCRC[15] = rCRC[3] ^ rCRC[4] ^ rCRC[5] ^ rCRC[9] ^ rCRC[11] ^ rCRC[12]                                                        ^ payload[0] ^ payload[0] ^ payload[1] ^ payload[1] ^ payload[3] ^ payload[4] ^ payload[5] ^ payload[7] ^ payload[7] ^ payload[8] ^ payload[8] ^ payload[9] ^ payload[11] ^ payload[12] ^ payload[15] ^ payload[15] ^ payload[16] ^ payload[16] ^ payload[19] ^ payload[20] ^ payload[23] ^ payload[27] ^ payload[31];
end
cal_crc = rCRC;
end
endfunction

这是我的系统verilog设计。我正在驱动4个字节的数据; 32'h000000B0。预期结果为62BD,但我得到了56F1

有人建议我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

生成CRC计算器代码是(几乎)微不足道的工作,用于跟踪输入的哪些位和先前的CRC值进入所得到的CRC值的每一位,并且通过对输入数据字的每个位迭代CRC位添加来完成。您可以使用您知道的任何编程语言来创建一个脚本,该脚本将为您生成所需CRC生成器的verilog代码。

但有一些陷阱:

  1. CRC寄存器移位的方向 - 可以是右或左
  2. 数据字位进入CRC的方式:首先是最低位或最重要位。
  3. 数据字的字节进入CRC的方式(如果数据字中的字节数超过8位) - 首先是最重要或最不重要的字节。
  4. CRC寄存器的初始值(通常为全1)
  5. 计算CRC后CRC值的变化:可能是某种方式的反转或位反转,例如,以太网CRC都有这两种情况。
  6. 对于宽数据字的情况,拥有2个步骤的流水线可能是有利可图的,首先是预先对数据字进行预处理,然后将xor全部放在CRC寄存器中。这样,CRC寄存器的每一位都只能从单个预先存储的数据位输入。

答案 1 :(得分:0)

我已尝试使用以下代码,它对我来说很好用;

    function bit [15:0] cal_16crc(input bit [7:0] pkt_bytes[$]);   
      reg [7:0] d;
      bit int_lsb;
      reg [15:0] c = 16'hFFFF;
      reg [15:0] newcrc;
    begin
     foreach(pkt_bytes[i])
     begin      
           d = pkt_bytes[i]; //Data;
           $display("DATA:%0h",d); 

        c = c ^ d;
        for(int i=0;i<8;i++)
        begin
           int_lsb = c & 16'h0001;
           c = c >> 1;
           c = c & 16'h7FFF;
           if(int_lsb == 1)
             c = c ^ 16'h8408;
        end

     end
     newcrc[7:0]  = c & 16'h00FF;
     newcrc[15:8] = (c >> 8) & 16'h00FF;
    cal_16crc = newcrc;
    end

   endfunction

There is certain condition that the data will be driven from LSB to MSB.
Hope this may helpful to others.

thanks,
mahesh