在Systemverilog中寻找CRC实现

时间:2013-07-15 06:15:00

标签: crc system-verilog

全部, 我正在寻找一个实现CRC生成器(或检查器)的类或模块。 我可以从零开始创建一个,但是如果那里有一个现成的那个 可能是一个真正的节省时间: - )

谢谢! 然

2 个答案:

答案 0 :(得分:0)

function byte calc_crc(byte unsigned cmd[]);
    bit [7:0] crc, d, c;
    int i;
    crc = 0;

    for (i=0; i<cmd.size(); i++) begin
            d = cmd[i];
            c = crc;
            crc[0] = d[7] ^ d[6] ^ d[0] ^ c[0] ^ c[6] ^ c[7];
            crc[1] = d[6] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[6];
            crc[2] = d[6] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[2] ^ c[6];
            crc[3] = d[7] ^ d[3] ^ d[2] ^ d[1] ^ c[1] ^ c[2] ^ c[3] ^ c[7];
            crc[4] = d[4] ^ d[3] ^ d[2] ^ c[2] ^ c[3] ^ c[4];
            crc[5] = d[5] ^ d[4] ^ d[3] ^ c[3] ^ c[4] ^ c[5];
            crc[6] = d[6] ^ d[5] ^ d[4] ^ c[4] ^ c[5] ^ c[6];
            crc[7] = d[7] ^ d[6] ^ d[5] ^ c[5] ^ c[6] ^ c[7];
    //$display("crc result: %h",crc);
    end
    return crc;
endfunction 

答案 1 :(得分:0)

Example of Ethercat POLYNOM

这里是CRC计算的另一个例子。以下示例计算CRC顺序。因此,与使用XOR并行计算每个CRC位的其他解决方案相比,它们更慢。但是,我发现此解决方案更多&#34; 可自定义&#34;如果你需要一个不同的POLYNOM。

class GreetingActorFactory
  include UntypedActorFactory

  def create
    actor = GreetingActor.new
    actor.connect
    actor
  end
end

您可以从以下网站自动生成CRC 的 http://www.easics.com/services/freesics/crctool.html

类似的实现在C语言使用TOPBIT而不是LSB位。来自Altera的例子: https://www.altera.com/content/dam/altera-www/global/en_US/others/support/examples/download/crc_accelerator.zip

在德语中,在维基百科中。有关如何计算这个的例子。 https://de.wikipedia.org/wiki/Zyklische_Redundanzprüfung

来自:https://www.altera.com/support/support-resources/design-examples/intellectual-property/embedded/nios-ii/exm-crc-acceleration.html

localparam CRC32POL = 32'hEDB88320; /* Ethernet CRC-32 Polynom, reverse Bits */

function automatic bit[31:0] genCRC32(input bit [7:0] databyte_stream[]);
    int unsigned i, j;
    bit [31:0] crc32_val = 32'hffffffff; // shiftregister,startvalue 
    bit [7:0]  data;

    //The result of the loop generate 32-Bit-mirrowed CRC
    for (i = 0; i < databyte_stream.size; i++)  // Byte-Stream
    begin
        data = databyte_stream[i];
        for (j=0; j < 8; j++) // Bitwise from LSB to MSB
        begin
            if ((crc32_val[0]) != (data[0])) begin
                crc32_val = (crc32_val >> 1) ^ CRC32POL;
            end else begin
                crc32_val >>= 1;
            end
            data >>= 1;
        end
    end
    crc32_val ^= 32'hffffffff; //invert results
    return crc32_val;
endfunction : genCRC32

电话

#define WIDTH    (8 * sizeof(crc))
#define TOPBIT   (1 << (WIDTH - 1))
#define POLYNOMIAL          0x04C11DB7
#define INITIAL_REMAINDER   0xFFFFFFFF
#define FINAL_XOR_VALUE     0xFFFFFFFF

功能

crc   crcSlow(unsigned char const message[], int nBytes);