verilog实现CRC校验

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



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值