集成电路技术分享

 找回密码
 我要注册

QQ登录

只需一步,快速开始

搜索
查看: 918|回复: 0

crc3

[复制链接]
fpga_feixiang 发表于 2021-10-14 20:16:53 | 显示全部楼层 |阅读模式
`timescale 1ns/1ns
module crc3;
reg clk,rst_n;

initial begin
clk=0;
rst_n=0;
#53 rst_n=1;
end
always #2 clk=~clk;


reg [7:0]data_fm;
reg [7:0] cnt;
always @(posedge clk or negedge rst_n)
           if(~rst_n)
                        cnt<=8'd0;
           else
                        cnt<=cnt+1'b1;


always @(posedge clk or negedge rst_n)
           if(~rst_n)
                        data_fm<={$random}%256;
           else if(cnt==8'hFF)
                        data_fm<={$random}%256;
                       




reg [0:0]data;
reg en_crc;

always @(posedge clk or negedge rst_n)
           if(~rst_n)begin
                        en_crc<=1'b0;
                        data<=1'b0;end
           else case(cnt)
                        8'd2 :begin en_crc<=1'b1;data<=data_fm[7];end
                        8'd3 :begin en_crc<=1'b1;data<=data_fm[6];end
                        8'd4 :begin en_crc<=1'b1;data<=data_fm[5];end
                        8'd5 :begin en_crc<=1'b1;data<=data_fm[4];end
                        8'd6 :begin en_crc<=1'b1;data<=data_fm[3];end
                        8'd7 :begin en_crc<=1'b1;data<=data_fm[2];end
                        8'd8 :begin en_crc<=1'b1;data<=data_fm[1];end
                        8'd9 :begin en_crc<=1'b1;data<=data_fm[0];end
                        default:en_crc<=1'b0;
                endcase
               



wire [2:0]crc_tb;

reg [7:0]num_tb;       
always @(posedge clk or negedge rst_n)
           if(~rst_n)
                        num_tb<=8'd0;
           else if(cnt==8'hFF)
                        num_tb<=num_tb+1'b1;
integer fid;
initial fid=$fopen("CRC_DATA.DAT");
always @(posedge clk)
           if(cnt==8'd11)
                        $fdisplay(fid,"SDATA=%0h \tDb=%0b \tDh=%0h \tDd=%0d",data_fm ,crc_tb,crc_tb,crc_tb);

always @(posedge clk)
           if(num_tb>8'd99) begin
                $fclose(fid);
                $stop;
          
           end
               
//**************CRC计算*****************************************************
parameter CRC_INIT=3'b101;
reg [2:0]crc_current;
wire [2:0] crc_next;//G(X)=1101
assign crc_tb=crc_current;
always @(posedge clk or negedge rst_n)
           if(~rst_n)
                        crc_current<=CRC_INIT;
           else if(en_crc)
                        crc_current<=crc_next;
           else
                        crc_current<=CRC_INIT;

wire   feedback;
assign feedback=data[0]^crc_current[2];
assign crc_next[0]=(feedback*1'b1)  ^1'b0;
assign crc_next[1]=(feedback*1'b0)  ^crc_current[0];
assign crc_next[2]=(feedback*1'b1)  ^crc_current[1];
endmodule
您需要登录后才可以回帖 登录 | 我要注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|小黑屋|手机版|Archiver|集成电路技术分享 ( 京ICP备20003123号-1 )

GMT+8, 2024-4-26 08:56 , Processed in 0.073197 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表