1、模块代码;
2、用于GTKWave的测试代码。
module test(clk,rst_n,data,crc);
input clk;
input rst_n;
input [7:0] data;
output reg [15:0] crc=0;
wire [23:0] stemp;
reg [23:0] temp=0;
parameter polynomial=17'b1_0001_0000_0010_0001;
assign stemp={data,16'b0000_0000_0000_0000};
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
crc<=0;
temp<=stemp;
end
else begin
if(temp[23])
temp[23:7]<=temp[23:7]^polynomial;
else if(temp[22])
temp[22:6]<=temp[22:6]^polynomial;
else if(temp[21])
temp[21:5]<=temp[21:5]^polynomial;
else if(temp[20])
temp[20:4]<=temp[20:4]^polynomial;
else if(temp[19])
temp[19:3]<=temp[19:3]^polynomial;
else if(temp[18])
temp[18:2]<=temp[18:2]^polynomial;
else if(temp[17])
temp[17:1]<=temp[17:1]^polynomial;
else if(temp[16])
temp[16:0]<=temp[16:0]^polynomial;
else
crc<=temp[15:0];
end
end
endmodule
`timescale 1ns/1ps
module test_tb();
reg clk;
reg [7:0] data;
reg rst_n;
wire [15:0] crc;
test test_ins0(clk,rst_n,data,crc);
initial begin
clk=1'b0;
forever
#5 clk=~clk;
end
initial begin
#10000 $finish;
end
initial begin
$dumpfile("test.vcd"); //这两行主要是给gtkwave这个工具使用的...
$dumpvars(0,test_tb);
end
initial begin
rst_n=1'b0;
#10 rst_n=1'b1;
end
initial begin
data =8'b10110110;
end
endmodule