CRC 循环冗余码
表示形式:多项式G(x):G(x) = X4+X3+1
假设:输入数据 Data,选定的多项式G(x)是x4 + x3 + 1.
所以G(M) = 11001.
CRC = Data mod G
注:CRC的位数要始终比G少1位,因为余数肯定比除数小且只小1位,高位为0不能省略。
CRC校验码的产生方式:模2除法
示例:
发送端通过CRC校验码将Data转换成NewData发送;
接收端接收到NewData对同样的G(M)进行模2运算,若没有余数,则数据正确传输,否则传输出现错误。
CRC代码生成链接
代码示例
// Copyright (C) 1999-2008 Easics NV.
// This source file may be used and distributed without restriction
// provided that this copyright statement is not removed from the file
// and that any derivative work contains the original copyright notice
// and the associated disclaimer.
//
// THIS SOURCE FILE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Purpose : synthesizable CRC function
// * polynomial: x^8 + x^5 + x^3 + x^2 + x^1 + 1
// * data width: 24
//
// Info : tools@easics.be
// http://www.easics.com
module CRC8_D24;
// polynomial: x^8 + x^5 + x^3 + x^2 + x^1 + 1
// data width: 24
// convention: the first serial bit is D[23]
function [7:0] nextCRC8_D24;
input [23:0] Data;
input [7:0] crc;
reg [23:0] d;
reg [7:0] c;
reg [7:0] newcrc;
begin
d = Data;
c = crc;
newcrc[0] = d[23] ^ d[22] ^ d[21] ^ d[15] ^ d[12] ^ d[11] ^ d[10] ^ d[9] ^ d[8] ^ d[7] ^ d[5] ^ d[3] ^ d[0] ^ c[5] ^ c[6] ^ c[7];
newcrc[1] = d[21] ^ d[16] ^ d[15] ^ d[13] ^ d[7] ^ d[6] ^ d[5] ^ d[4] ^ d[3] ^ d[1] ^ d[0] ^ c[0] ^ c[5];
newcrc[2] = d[23] ^ d[21] ^ d[17] ^ d[16] ^ d[15] ^ d[14] ^ d[12] ^ d[11] ^ d[10] ^ d[9] ^ d[6] ^ d[4] ^ d[3] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[5] ^ c[7];
newcrc[3] = d[23] ^ d[21] ^ d[18] ^ d[17] ^ d[16] ^ d[13] ^ d[9] ^ d[8] ^ d[4] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[2] ^ c[5] ^ c[7];
newcrc[4] = d[22] ^ d[19] ^ d[18] ^ d[17] ^ d[14] ^ d[10] ^ d[9] ^ d[5] ^ d[3] ^ d[2] ^ d[1] ^ c[1] ^ c[2] ^ c[3] ^ c[6];
newcrc[5] = d[22] ^ d[21] ^ d[20] ^ d[19] ^ d[18] ^ d[12] ^ d[9] ^ d[8] ^ d[7] ^ d[6] ^ d[5] ^ d[4] ^ d[2] ^ d[0] ^ c[2] ^ c[3] ^ c[4] ^ c[5] ^ c[6];
newcrc[6] = d[23] ^ d[22] ^ d[21] ^ d[20] ^ d[19] ^ d[13] ^ d[10] ^ d[9] ^ d[8] ^ d[7] ^ d[6] ^ d[5] ^ d[3] ^ d[1] ^ c[3] ^ c[4] ^ c[5] ^ c[6] ^ c[7];
newcrc[7] = d[23] ^ d[22] ^ d[21] ^ d[20] ^ d[14] ^ d[11] ^ d[10] ^ d[9] ^ d[8] ^ d[7] ^ d[6] ^ d[4] ^ d[2] ^ c[4] ^ c[5] ^ c[6] ^ c[7];
nextCRC8_D24 = newcrc;
end
endfunction
endmodule