1,CRC校验
循环冗余校验(Cyclic Redundancy Check, CRC)是一种根据网络数据包或计算机文件等数据产生简短固定位数校验码的一种信道编码技术,主要用来检测或校验数据传输或者保存后可能出现的错误。它是利用除法及余数的原理来作错误侦测的。
2,CRC 多项式
多项式一般指输入数据异或的对象;其中相关参数还包括:初值,结果异或值,输入翻转,输出翻转。
初始值:计算之前给CRC赋初始值;
结果异或值:计算完CRC输出前;对CRC结果异或的参数值;
输入值反转:输入翻转指对输入输入按字节翻转;
输出值反转:对输出结果在CRC计算之后,异或之前整体翻转
一般常用的CRC多项式如下:
3 CRC逻辑设计实现
设计实现CRC-8 CRC-16 CRC-32位校验计算;下面分别介绍串行,并行的不同实现思路;
3.1 CRC串行实现
实现流程:
a, 输入数据 翻转控制;
b, 翻转后数据与CRC初值异或;8位直接异或;16位与32位先异或高8位;
c, 循环左移异或计算;8位左移8次;16位左移16次;32位左移32次;
16位;在左移8次后需要用异或结果的高8位异或原始数据的低8位;然后在继续左移运算
32位;在左移8,16,24位后用异或结果的高8位异或原始数据的次高8位,次低8位,低8位,然后再继续左移运算;
d, 对计算的CRC结果进行翻转处理;
e, 对输出的翻转数据进行异或数据处理;输出
(1)CRC-8位实现
module crc_8 #(
parameter [7:0] CRC8_PARAM = 8'h07,//多项式
parameter [7:0] CRC_INIT = 8'h00,
parameter [7:0] DATA_XOROUT = 8'h55
)(
input sys_clk,
input sys_rst,
input in_swap_en,
input out_swap_en,
input data_in_en,
input [7:0] data_in,
output data_out_en,
output [7:0] data_out
);
wire [7:0] swap_data;
reg [7:0] crc_reg = CRC_INIT;
reg [7:0] crc_cnt = 0;
reg data_out_en_reg = 0;
reg [7:0] data_out_reg = 0;
assign swap_data = in_swap_en ? {data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]}:data_in;
always @ (posedge sys_clk) begin
if(sys_rst) begin
crc_reg <= CRC_INIT;
crc_cnt <= 0;
end
else if(data_in_en && crc_cnt == 0) begin
crc_reg <= swap_data^crc_reg;
crc_cnt <= 1;
end
else if(crc_cnt == 9) begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
else if(crc_cnt !=0) begin
crc_reg <= (crc_reg[7] == 0 ? crc_reg << 1 : (crc_reg << 1 ^ CRC8_PARAM));
crc_cnt <= crc_cnt + 1;
end
else begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
end
always @ (posedge sys_clk) begin
if(sys_rst) begin
data_out_en_reg <= 0;
data_out_reg <= 0;
end
else if(crc_cnt == 9) begin
data_out_en_reg <= 1;
data_out_reg <= out_swap_en ? {crc_reg[0],crc_reg[1],crc_reg[2],crc_reg[3],crc_reg[4],crc_reg[5],crc_reg[6],crc_reg[7]}:crc_reg;
end
else begin
data_out_en_reg <= 0;
data_out_reg <= data_out_reg;
end
end
assign data_out_en = data_out_en_reg;
assign data_out = data_out_reg ^ DATA_XOROUT;
endmodule
(2)CRC-16位实现
module crc_16 #(
parameter [15:0] CRC16_PARAM = 16'h8005,
parameter [15:0] CRC_INIT = 16'h0000,
parameter [15:0] DATA_XOROUT = 16'hFFFF
)(
input sys_clk,
input sys_rst,
input in_swap_en,
input out_swap_en,
input data_in_en,
input [15:0] data_in,
output data_out_en,
output [15:0] data_out
);
wire [15:0] swap_data;
reg [15:0] crc_reg = CRC_INIT;
reg [7:0] crc_cnt = 0;
reg data_out_en_reg = 0;
reg [15:0] data_out_reg = 0;
assign swap_data = in_swap_en ? {data_in[8],data_in[9],data_in[10],data_in[11],data_in[12],data_in[13],data_in[14],data_in[15],
data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]}:data_in;
always @ (posedge sys_clk) begin
if(sys_rst) begin
crc_reg <= CRC_INIT;
crc_cnt <= 0;
end
else if(data_in_en && crc_cnt == 0) begin
crc_reg <={swap_data[15:8]^crc_reg[15:8],crc_reg[7:0]};
crc_cnt <= 1;
end
else if(crc_cnt == 9) begin
crc_reg <={swap_data[7:0]^crc_reg[15:8],crc_reg[7:0]};
crc_cnt <= crc_cnt + 1;
end
else if(crc_cnt == 18) begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
else if(crc_cnt !=0) begin// && shift_en == 0) begin
crc_reg <= (crc_reg[15] == 0 ? crc_reg << 1 : (crc_reg << 1 ^ CRC16_PARAM));
crc_cnt <= crc_cnt + 1;
end
else begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
end
always @ (posedge sys_clk) begin
if(sys_rst) begin
data_out_en_reg <= 0;
data_out_reg <= 0;
end
else if(crc_cnt == 18) begin
data_out_en_reg <= 1;
data_out_reg <= out_swap_en ? {crc_reg[0],crc_reg[1],crc_reg[2],crc_reg[3],crc_reg[4],crc_reg[5],crc_reg[6],crc_reg[7],crc_reg[8],crc_reg[9],
crc_reg[10],crc_reg[11],crc_reg[12],crc_reg[13],crc_reg[14],crc_reg[15]}:crc_reg;
end
else begin
data_out_en_reg <= 0;
data_out_reg <= data_out_reg;
end
end
assign data_out_en = data_out_en_reg;
assign data_out = data_out_reg ^ DATA_XOROUT;
endmodule
(3)CRC-32位实现
module crc_32 #(
parameter [31:0] CRC32_PARAM = 32'h04C11DB7,
parameter [31:0] CRC_INIT = 32'hFFFFFFFF,
parameter [31:0] DATA_XOROUT = 32'hFFFFFFFF
)(
input sys_clk,
input sys_rst,
input in_swap_en,
input out_swap_en,
input data_in_en,
input [31:0] data_in,
output data_out_en,
output [31:0] data_out
);
wire [31:0] swap_data;
reg [31:0] crc_reg = CRC_INIT;
reg [7:0] crc_cnt = 0;
reg data_out_en_reg = 0;
reg [31:0] data_out_reg = 0;
assign swap_data = in_swap_en ? {data_in[24],data_in[25],data_in[26],data_in[27],data_in[28],data_in[29],data_in[30],data_in[31],
data_in[16],data_in[17],data_in[18],data_in[19],data_in[20],data_in[21],data_in[22],data_in[23],
data_in[8],data_in[9],data_in[10],data_in[11],data_in[12],data_in[13],data_in[14],data_in[15],
data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]}:data_in;
always @ (posedge sys_clk) begin
if(sys_rst) begin
crc_reg <= CRC_INIT;
crc_cnt <= 0;
end
else if(data_in_en && crc_cnt == 0) begin
crc_reg <={swap_data[31:24]^crc_reg[31:24],crc_reg[23:0]};
crc_cnt <= 1;
end
else if(crc_cnt == 9) begin
crc_reg <={swap_data[23:16]^crc_reg[31:24],crc_reg[23:0]};
crc_cnt <= crc_cnt + 1;
end
else if(crc_cnt == 18) begin
crc_reg <={swap_data[15:8]^crc_reg[31:24],crc_reg[23:0]};
crc_cnt <= crc_cnt + 1;
end
else if(crc_cnt == 27) begin
crc_reg <={swap_data[7:0]^crc_reg[31:24],crc_reg[23:0]};
crc_cnt <= crc_cnt + 1;
end
else if(crc_cnt == 36) begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
else if(crc_cnt !=0) begin// && shift_en == 0) begin
crc_reg <= (crc_reg[31] == 0 ? crc_reg << 1 : (crc_reg << 1 ^ CRC32_PARAM));
crc_cnt <= crc_cnt + 1;
end
else begin
crc_reg <= crc_reg;
crc_cnt <= 0;
end
end
always @ (posedge sys_clk) begin
if(sys_rst) begin
data_out_en_reg <= 0;
data_out_reg <= 0;
end
else if(crc_cnt == 36) begin
data_out_en_reg <= 1;
data_out_reg <= out_swap_en ? {crc_reg[0],crc_reg[1],crc_reg[2],crc_reg[3],crc_reg[4],crc_reg[5],crc_reg[6],crc_reg[7],
crc_reg[8],crc_reg[9],crc_reg[10],crc_reg[11],crc_reg[12],crc_reg[13],crc_reg[14],crc_reg[15],
crc_reg[16],crc_reg[17],crc_reg[18],crc_reg[19],crc_reg[20],crc_reg[21],crc_reg[22],crc_reg[23],
crc_reg[24],crc_reg[25],crc_reg[26],crc_reg[27],crc_reg[28],crc_reg[29],crc_reg[30],crc_reg[31]}:crc_reg;
end
else begin
data_out_en_reg <= 0;
data_out_reg <= data_out_reg;
end
end
assign data_out_en = data_out_en_reg;
assign data_out = data_out_reg ^ DATA_XOROUT;
endmodule
3.2 CRC并行实现
并行实现需要在网站生成计算 代码:OutputLogic.com » CRC Generator
计算流程:
a, 输入数据 翻转控制;
b,使用生成逻辑代码进行计算;
c, 对计算的CRC结果进行翻转处理;
d, 对输出的翻转数据进行异或数据处理;输出
说明:生成代码中CRC初值都为FFFF;需要根据具体设计修改;翻转控制,结果异或值等需要根据设计处理。
(1)CRC-8位实现:
实现多项式: 1+x^4+x^5+x^8
CRC初始值: 0x00
输出结果异或值:0x00
输入值使能:使能
输出值使能:使能
module crc_8(
input sys_clk,
input sys_rst,
input [7:0] data_in,
input crc_en,
output [7:0] crc_out
);
wire [7:0] swap_in_data;
wire [7:0] swap_out_data;
reg [7:0] lfsr_q,lfsr_c;
assign swap_in_data = {data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]};
assign swap_out_data = {lfsr_q[0],lfsr_q[1],lfsr_q[2],lfsr_q[3],lfsr_q[4],lfsr_q[5],lfsr_q[6],lfsr_q[7]};
assign crc_out = swap_out_data ^ 8'h00;
//----------------------------------------------------------------------------------------------------------------------------------------
always @(*) begin
lfsr_c[0] = lfsr_q[0] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[6] ^ swap_in_data[0] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[6];
lfsr_c[1] = lfsr_q[1] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[7] ^ swap_in_data[1] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[7];
lfsr_c[2] = lfsr_q[2] ^ lfsr_q[5] ^ lfsr_q[6] ^ swap_in_data[2] ^ swap_in_data[5] ^ swap_in_data[6];
lfsr_c[3] = lfsr_q[3] ^ lfsr_q[6] ^ lfsr_q[7] ^ swap_in_data[3] ^ swap_in_data[6] ^ swap_in_data[7];
lfsr_c[4] = lfsr_q[0] ^ lfsr_q[3] ^ lfsr_q[6] ^ lfsr_q[7] ^ swap_in_data[0] ^ swap_in_data[3] ^ swap_in_data[6] ^ swap_in_data[7];
lfsr_c[5] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[3] ^ lfsr_q[6] ^ lfsr_q[7] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[3] ^ swap_in_data[6] ^ swap_in_data[7];
lfsr_c[6] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[4] ^ lfsr_q[7] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[4] ^ swap_in_data[7];
lfsr_c[7] = lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[5] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[5];
end // always
always @(posedge sys_clk) begin
if(sys_rst)
lfsr_q <= {8{1'b0}};
else if(crc_en)
lfsr_q <= lfsr_c;
else
lfsr_q <= lfsr_q;
end
endmodule
(2)CRC-16位实现:
实现多项式: 1+x^2+x^15+x^16
CRC初始值: 0x0000
输出结果异或值:0xFFFF
输入值使能:使能
输出值使能:使能
module crc_16(
input sys_clk,
input sys_rst,
input [15:0] data_in,
input crc_en,
output [15:0] crc_out
);
wire [15:0] swap_in_data;
wire [15:0] swap_out_data;
reg [15:0] lfsr_q,lfsr_c;
assign swap_in_data = {data_in[8],data_in[9],data_in[10],data_in[11],data_in[12],data_in[13],data_in[14],data_in[15],
data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]};
assign swap_out_data = {lfsr_q[0],lfsr_q[1],lfsr_q[2],lfsr_q[3],lfsr_q[4],lfsr_q[5],lfsr_q[6],lfsr_q[7],
lfsr_q[8],lfsr_q[9],lfsr_q[10],lfsr_q[11],lfsr_q[12],lfsr_q[13],lfsr_q[14],lfsr_q[15]};
assign crc_out = swap_out_data ^ 16'hFFFF;
//----------------------------------------------------------------------------------------------------------------------------------------
always @(*) begin
lfsr_c[0] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[15] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[15];
lfsr_c[1] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[14] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[14];
lfsr_c[2] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[14] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[14];
lfsr_c[3] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[15] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[15];
lfsr_c[4] = lfsr_q[2] ^ lfsr_q[3] ^ swap_in_data[2] ^ swap_in_data[3];
lfsr_c[5] = lfsr_q[3] ^ lfsr_q[4] ^ swap_in_data[3] ^ swap_in_data[4];
lfsr_c[6] = lfsr_q[4] ^ lfsr_q[5] ^ swap_in_data[4] ^ swap_in_data[5];
lfsr_c[7] = lfsr_q[5] ^ lfsr_q[6] ^ swap_in_data[5] ^ swap_in_data[6];
lfsr_c[8] = lfsr_q[6] ^ lfsr_q[7] ^ swap_in_data[6] ^ swap_in_data[7];
lfsr_c[9] = lfsr_q[7] ^ lfsr_q[8] ^ swap_in_data[7] ^ swap_in_data[8];
lfsr_c[10] = lfsr_q[8] ^ lfsr_q[9] ^ swap_in_data[8] ^ swap_in_data[9];
lfsr_c[11] = lfsr_q[9] ^ lfsr_q[10] ^ swap_in_data[9] ^ swap_in_data[10];
lfsr_c[12] = lfsr_q[10] ^ lfsr_q[11] ^ swap_in_data[10] ^ swap_in_data[11];
lfsr_c[13] = lfsr_q[11] ^ lfsr_q[12] ^ swap_in_data[11] ^ swap_in_data[12];
lfsr_c[14] = lfsr_q[12] ^ lfsr_q[13] ^ swap_in_data[12] ^ swap_in_data[13];
lfsr_c[15] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[14] ^ lfsr_q[15] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[14] ^ swap_in_data[15];
end // always
always @(posedge sys_clk) begin
if(sys_rst)
lfsr_q <= {16{1'b0}};
else if(crc_en)
lfsr_q <= lfsr_c;
else
lfsr_q <= lfsr_q;
end
endmodule
(3)CRC-32位实现:
实现多项式: 1+x^1+x^2+x^4+x^5+x^7+x^8+x^10+x^11+x^12+x^16+x^22+x^23+x^26+x^32
CRC初始值: 0xFFFFFFFF
输出结果异或值:0xFFFFFFFF
输入值使能:使能
输出值使能:使能
module crc_32(
input sys_clk,
input sys_rst,
input [31:0] data_in,
input crc_en,
output [31:0] crc_out
);
wire [31:0] swap_in_data;
wire [31:0] swap_out_data;
reg [31:0] lfsr_q,lfsr_c;
assign swap_in_data = {data_in[24],data_in[25],data_in[26],data_in[27],data_in[28],data_in[29],data_in[30],data_in[31],
data_in[16],data_in[17],data_in[18],data_in[19],data_in[20],data_in[21],data_in[22],data_in[23],
data_in[8],data_in[9],data_in[10],data_in[11],data_in[12],data_in[13],data_in[14],data_in[15],
data_in[0],data_in[1],data_in[2],data_in[3],data_in[4],data_in[5],data_in[6],data_in[7]};
assign swap_out_data = {lfsr_q[0],lfsr_q[1],lfsr_q[2],lfsr_q[3],lfsr_q[4],lfsr_q[5],lfsr_q[6],lfsr_q[7],
lfsr_q[8],lfsr_q[9],lfsr_q[10],lfsr_q[11],lfsr_q[12],lfsr_q[13],lfsr_q[14],lfsr_q[15],
lfsr_q[16],lfsr_q[17],lfsr_q[18],lfsr_q[19],lfsr_q[20],lfsr_q[21],lfsr_q[22],lfsr_q[23],
lfsr_q[24],lfsr_q[25],lfsr_q[26],lfsr_q[27],lfsr_q[28],lfsr_q[29],lfsr_q[30],lfsr_q[31]};
assign crc_out = swap_out_data ^ 32'hFFFFFFFF;
//----------------------------------------------------------------------------------------------------------------------------------------
always @(*) begin
lfsr_c[0] = lfsr_q[0] ^ lfsr_q[6] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[12] ^ lfsr_q[16] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[6] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[12] ^ swap_in_data[16] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[30] ^ swap_in_data[31];
lfsr_c[1] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[9] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[24] ^ lfsr_q[27] ^ lfsr_q[28] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[9] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[24] ^ swap_in_data[27] ^ swap_in_data[28];
lfsr_c[2] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[13] ^ lfsr_q[14] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[13] ^ swap_in_data[14] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[30] ^ swap_in_data[31];
lfsr_c[3] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[14] ^ lfsr_q[15] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[25] ^ lfsr_q[27] ^ lfsr_q[31] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[14] ^ swap_in_data[15] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[25] ^ swap_in_data[27] ^ swap_in_data[31];
lfsr_c[4] = lfsr_q[0] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[6] ^ lfsr_q[8] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[15] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[29] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[6] ^ swap_in_data[8] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[15] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[29] ^ swap_in_data[30] ^ swap_in_data[31];
lfsr_c[5] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[10] ^ lfsr_q[13] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[24] ^ lfsr_q[28] ^ lfsr_q[29] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[10] ^ swap_in_data[13] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[24] ^ swap_in_data[28] ^ swap_in_data[29];
lfsr_c[6] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[11] ^ lfsr_q[14] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[25] ^ lfsr_q[29] ^ lfsr_q[30] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[11] ^ swap_in_data[14] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[25] ^ swap_in_data[29] ^ swap_in_data[30];
lfsr_c[7] = lfsr_q[0] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[5] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[10] ^ lfsr_q[15] ^ lfsr_q[16] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[28] ^ lfsr_q[29] ^ swap_in_data[0] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[5] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[10] ^ swap_in_data[15] ^ swap_in_data[16] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[28] ^ swap_in_data[29];
lfsr_c[8] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[8] ^ lfsr_q[10] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[17] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[28] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[8] ^ swap_in_data[10] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[17] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[28] ^ swap_in_data[31];
lfsr_c[9] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[9] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[18] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[29] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[9] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[18] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[29];
lfsr_c[10] = lfsr_q[0] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[5] ^ lfsr_q[9] ^ lfsr_q[13] ^ lfsr_q[14] ^ lfsr_q[16] ^ lfsr_q[19] ^ lfsr_q[26] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[5] ^ swap_in_data[9] ^ swap_in_data[13] ^ swap_in_data[14] ^ swap_in_data[16] ^ swap_in_data[19] ^ swap_in_data[26] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[11] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[9] ^ lfsr_q[12] ^ lfsr_q[14] ^ lfsr_q[15] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[20] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[9] ^ swap_in_data[12] ^ swap_in_data[14] ^ swap_in_data[15] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[20] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[31];
lfsr_c[12] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[9] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[15] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[21] ^ lfsr_q[24] ^ lfsr_q[27] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[9] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[15] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[21] ^ swap_in_data[24] ^ swap_in_data[27] ^ swap_in_data[30] ^ swap_in_data[31];
lfsr_c[13] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[10] ^ lfsr_q[13] ^ lfsr_q[14] ^ lfsr_q[16] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[22] ^ lfsr_q[25] ^ lfsr_q[28] ^ lfsr_q[31] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[10] ^ swap_in_data[13] ^ swap_in_data[14] ^ swap_in_data[16] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[22] ^ swap_in_data[25] ^ swap_in_data[28] ^ swap_in_data[31];
lfsr_c[14] = lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[11] ^ lfsr_q[14] ^ lfsr_q[15] ^ lfsr_q[17] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[23] ^ lfsr_q[26] ^ lfsr_q[29] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[11] ^ swap_in_data[14] ^ swap_in_data[15] ^ swap_in_data[17] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[23] ^ swap_in_data[26] ^ swap_in_data[29];
lfsr_c[15] = lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[12] ^ lfsr_q[15] ^ lfsr_q[16] ^ lfsr_q[18] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[24] ^ lfsr_q[27] ^ lfsr_q[30] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[12] ^ swap_in_data[15] ^ swap_in_data[16] ^ swap_in_data[18] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[24] ^ swap_in_data[27] ^ swap_in_data[30];
lfsr_c[16] = lfsr_q[0] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[8] ^ lfsr_q[12] ^ lfsr_q[13] ^ lfsr_q[17] ^ lfsr_q[19] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[29] ^ lfsr_q[30] ^ swap_in_data[0] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[8] ^ swap_in_data[12] ^ swap_in_data[13] ^ swap_in_data[17] ^ swap_in_data[19] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[29] ^ swap_in_data[30];
lfsr_c[17] = lfsr_q[1] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[9] ^ lfsr_q[13] ^ lfsr_q[14] ^ lfsr_q[18] ^ lfsr_q[20] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[25] ^ lfsr_q[27] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[1] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[9] ^ swap_in_data[13] ^ swap_in_data[14] ^ swap_in_data[18] ^ swap_in_data[20] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[25] ^ swap_in_data[27] ^ swap_in_data[30] ^ swap_in_data[31];
lfsr_c[18] = lfsr_q[2] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[10] ^ lfsr_q[14] ^ lfsr_q[15] ^ lfsr_q[19] ^ lfsr_q[21] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[28] ^ lfsr_q[31] ^ swap_in_data[2] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[10] ^ swap_in_data[14] ^ swap_in_data[15] ^ swap_in_data[19] ^ swap_in_data[21] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[28] ^ swap_in_data[31];
lfsr_c[19] = lfsr_q[3] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[11] ^ lfsr_q[15] ^ lfsr_q[16] ^ lfsr_q[20] ^ lfsr_q[22] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[27] ^ lfsr_q[29] ^ swap_in_data[3] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[11] ^ swap_in_data[15] ^ swap_in_data[16] ^ swap_in_data[20] ^ swap_in_data[22] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[27] ^ swap_in_data[29];
lfsr_c[20] = lfsr_q[4] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[12] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[21] ^ lfsr_q[23] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[28] ^ lfsr_q[30] ^ swap_in_data[4] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[12] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[21] ^ swap_in_data[23] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[28] ^ swap_in_data[30];
lfsr_c[21] = lfsr_q[5] ^ lfsr_q[9] ^ lfsr_q[10] ^ lfsr_q[13] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[22] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[5] ^ swap_in_data[9] ^ swap_in_data[10] ^ swap_in_data[13] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[22] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[22] = lfsr_q[0] ^ lfsr_q[9] ^ lfsr_q[11] ^ lfsr_q[12] ^ lfsr_q[14] ^ lfsr_q[16] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[9] ^ swap_in_data[11] ^ swap_in_data[12] ^ swap_in_data[14] ^ swap_in_data[16] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[23] = lfsr_q[0] ^ lfsr_q[1] ^ lfsr_q[6] ^ lfsr_q[9] ^ lfsr_q[13] ^ lfsr_q[15] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[1] ^ swap_in_data[6] ^ swap_in_data[9] ^ swap_in_data[13] ^ swap_in_data[15] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[24] = lfsr_q[1] ^ lfsr_q[2] ^ lfsr_q[7] ^ lfsr_q[10] ^ lfsr_q[14] ^ lfsr_q[16] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[30] ^ swap_in_data[1] ^ swap_in_data[2] ^ swap_in_data[7] ^ swap_in_data[10] ^ swap_in_data[14] ^ swap_in_data[16] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[30];
lfsr_c[25] = lfsr_q[2] ^ lfsr_q[3] ^ lfsr_q[8] ^ lfsr_q[11] ^ lfsr_q[15] ^ lfsr_q[17] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[2] ^ swap_in_data[3] ^ swap_in_data[8] ^ swap_in_data[11] ^ swap_in_data[15] ^ swap_in_data[17] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[26] = lfsr_q[0] ^ lfsr_q[3] ^ lfsr_q[4] ^ lfsr_q[6] ^ lfsr_q[10] ^ lfsr_q[18] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[28] ^ lfsr_q[31] ^ swap_in_data[0] ^ swap_in_data[3] ^ swap_in_data[4] ^ swap_in_data[6] ^ swap_in_data[10] ^ swap_in_data[18] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[28] ^ swap_in_data[31];
lfsr_c[27] = lfsr_q[1] ^ lfsr_q[4] ^ lfsr_q[5] ^ lfsr_q[7] ^ lfsr_q[11] ^ lfsr_q[19] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[29] ^ swap_in_data[1] ^ swap_in_data[4] ^ swap_in_data[5] ^ swap_in_data[7] ^ swap_in_data[11] ^ swap_in_data[19] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[29];
lfsr_c[28] = lfsr_q[2] ^ lfsr_q[5] ^ lfsr_q[6] ^ lfsr_q[8] ^ lfsr_q[12] ^ lfsr_q[20] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[30] ^ swap_in_data[2] ^ swap_in_data[5] ^ swap_in_data[6] ^ swap_in_data[8] ^ swap_in_data[12] ^ swap_in_data[20] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[30];
lfsr_c[29] = lfsr_q[3] ^ lfsr_q[6] ^ lfsr_q[7] ^ lfsr_q[9] ^ lfsr_q[13] ^ lfsr_q[21] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[25] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[31] ^ swap_in_data[3] ^ swap_in_data[6] ^ swap_in_data[7] ^ swap_in_data[9] ^ swap_in_data[13] ^ swap_in_data[21] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[25] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[31];
lfsr_c[30] = lfsr_q[4] ^ lfsr_q[7] ^ lfsr_q[8] ^ lfsr_q[10] ^ lfsr_q[14] ^ lfsr_q[22] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[26] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[30] ^ swap_in_data[4] ^ swap_in_data[7] ^ swap_in_data[8] ^ swap_in_data[10] ^ swap_in_data[14] ^ swap_in_data[22] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[26] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[30];
lfsr_c[31] = lfsr_q[5] ^ lfsr_q[8] ^ lfsr_q[9] ^ lfsr_q[11] ^ lfsr_q[15] ^ lfsr_q[23] ^ lfsr_q[24] ^ lfsr_q[25] ^ lfsr_q[27] ^ lfsr_q[28] ^ lfsr_q[29] ^ lfsr_q[30] ^ lfsr_q[31] ^ swap_in_data[5] ^ swap_in_data[8] ^ swap_in_data[9] ^ swap_in_data[11] ^ swap_in_data[15] ^ swap_in_data[23] ^ swap_in_data[24] ^ swap_in_data[25] ^ swap_in_data[27] ^ swap_in_data[28] ^ swap_in_data[29] ^ swap_in_data[30] ^ swap_in_data[31];
end // always
always @(posedge sys_clk) begin
if(sys_rst)
lfsr_q <= {32{1'b1}};
else if(crc_en)
lfsr_q <= lfsr_c;
else
lfsr_q <= lfsr_q;
end
endmodule