生成CRC码的基本原理:
任意一个由二进制位串组成的代码都可以和一个系数仅为‘0’和‘1’取值的多项式一一对应。例如:代码1010111对应的多项式为x6+x4+x2+x+1,而多项式为x5+x3+x2+x+1对应的代码101111。
CRC码集选择的原则:若设码字长度为N,信息字段为K位,校验字段为R位(N=K+R),则对于CRC码集中的任一码字,存在且仅存在一个R次多项式。
// 本程序使用的多项式是 x^12 + x^5 + x^0
// C00 = ........X...X... X...X...
// C01 = .........X...X.. .X...X..
// C02 = ..........X...X. ..X...X.
// C03 = ...........X...X ...X...X
// C04 = ............X... ....X...
// C05 = ........X...XX.. X...XX..
// C06 = .........X...XX. .X...XX.
// C07 = ..........X...XX ..X...XX
// C08 = X..........X...X ...X...X
// C09 = .X..........X... ....X...
// C10 = ..X..........X.. .....X..
// C11 = ...X..........X. ......X.
// C12 = ....X...X...X..X X...X..X
// C13 = .....X...X...X.. .X...X..
// C14 = ......X...X...X. ..X...X.
// C15 = .......X...X...X ...X...X
//
module crc16_dat8 (crc_in,dat_in,crc_out);
input [15:0] crc_in;
input [7:0] dat_in;
output [15:0] crc_out;
wire [15:0] crc_out;
generate
crc16_dat8_flat cc (.crc_in(crc_in),.dat_in(dat_in),.crc_out(crc_out));
endgenerate
endmodule
module crc16_dat8_flat (crc_in,dat_in,crc_out);
input [15:0] crc_in;
input [7:0] dat_in;
output [15:0] crc_out;
wire [15:0] crc_out;
wire x7, x6, x5, x4, x3, x2, x1,
x0, x15, x14, x13, x12, x11, x10, x9,
x8;
assign crc_out = {x15,x14,x13,x12,x11,x10,x9,x8,x7,x6,x5,x4,x3,x2,x1,
x0};
wire d0,d1,d2,d3,d4,d5,d6,d7;
assign { d7,d6,d5,d4,d3,d2,d1,d0} = dat_in [7:0];
wire c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,
c15;
assign { c15,c14,c13,c12,c11,c10,c9,c8,c7,c6,c5,c4,c3,c2,c1,
c0} = crc_in [15:0];
xor6 x7i (.out(x7),.a(c10),.b(d6),.c(c14),.d(d2),.e(c15),.f(d7)); // 6 ins 1 outs
xor6 x6i (.out(x6),.a(c9),.b(d5),.c(c13),.d(d1),.e(c14),.f(d6)); // 6 ins 1 outs
xor6 x5i (.out(x5),.a(c8),.b(d4),.c(c12),.d(d0),.e(c13),.f(d5)); // 6 ins 1 outs
xor6 x4i (.out(x4),.a(c12),.b(d4),.c(1'b0),.d(1'b0),.e(1'b0),.f(1'b0)); // 2 ins 1 outs
xor6 x3i (.out(x3),.a(c11),.b(d7),.c(c15),.d(d3),.e(1'b0),.f(1'b0)); // 4 ins 1 outs
xor6 x2i (.out(x2),.a(c10),.b(d6),.c(c14),.d(d2),.e(1'b0),.f(1'b0)); // 4 ins 1 outs
xor6 x1i (.out(x1),.a(c9),.b(d5),.c(c13),.d(d1),.e(1'b0),.f(1'b0)); // 4 ins 1 outs
xor6 x0i (.out(x0),.a(c8),.b(d4),.c(c12),.d(d0),.e(1'b0),.f(1'b0)); // 4 ins 1 outs
xor6 x15i (.out(x15),.a(c11),.b(d7),.c(c15),.d(d3),.e(c7),.f(1'b0)); // 5 ins 1 outs
xor6 x14i (.out(x14),.a(c10),.b(d6),.c(c14),.d(d2),.e(c6),.f(1'b0)); // 5 ins 1 outs
xor6 x13i (.out(x13),.a(c9),.b(d5),.c(c13),.d(d1),.e(c5),.f(1'b0)); // 5 ins 1 outs
assign x12 = c8 ^ d4 ^ c12 ^ d0 ^ c15 ^ d7 ^ c4; // 7 ins 1 outs
xor6 x11i (.out(x11),.a(c14),.b(d6),.c(c3),.d(1'b0),.e(1'b0),.f(1'b0)); // 3 ins 1 outs
xor6 x10i (.out(x10),.a(c13),.b(d5),.c(c2),.d(1'b0),.e(1'b0),.f(1'b0)); // 3 ins 1 outs
xor6 x9i (.out(x9),.a(c12),.b(d4),.c(c1),.d(1'b0),.e(1'b0),.f(1'b0)); // 3 ins 1 outs
xor6 x8i (.out(x8),.a(c11),.b(d7),.c(c15),.d(d3),.e(c0),.f(1'b0)); // 5 ins 1 outs
endmodule
文章评论(0条评论)
登录后参与讨论