当前位置:   article > 正文

verilog实现CRC校验_verilog crc4校验

verilog crc4校验

1、模块代码;

2、用于GTKWave的测试代码。

  1. module test(clk,rst_n,data,crc);
  2. input clk;
  3. input rst_n;
  4. input [7:0] data;
  5. output reg [15:0] crc=0;
  6. wire [23:0] stemp;
  7. reg [23:0] temp=0;
  8. parameter polynomial=17'b1_0001_0000_0010_0001;
  9. assign stemp={data,16'b0000_0000_0000_0000};
  10. always@(posedge clk or negedge rst_n) begin
  11. if(!rst_n) begin
  12. crc<=0;
  13. temp<=stemp;
  14. end
  15. else begin
  16. if(temp[23])
  17. temp[23:7]<=temp[23:7]^polynomial;
  18. else if(temp[22])
  19. temp[22:6]<=temp[22:6]^polynomial;
  20. else if(temp[21])
  21. temp[21:5]<=temp[21:5]^polynomial;
  22. else if(temp[20])
  23. temp[20:4]<=temp[20:4]^polynomial;
  24. else if(temp[19])
  25. temp[19:3]<=temp[19:3]^polynomial;
  26. else if(temp[18])
  27. temp[18:2]<=temp[18:2]^polynomial;
  28. else if(temp[17])
  29. temp[17:1]<=temp[17:1]^polynomial;
  30. else if(temp[16])
  31. temp[16:0]<=temp[16:0]^polynomial;
  32. else
  33. crc<=temp[15:0];
  34. end
  35. end
  36. endmodule

 

  1. `timescale 1ns/1ps
  2. module test_tb();
  3. reg clk;
  4. reg [7:0] data;
  5. reg rst_n;
  6. wire [15:0] crc;
  7. test test_ins0(clk,rst_n,data,crc);
  8. initial begin
  9. clk=1'b0;
  10. forever
  11. #5 clk=~clk;
  12. end
  13. initial begin
  14. #10000 $finish;
  15. end
  16. initial begin
  17. $dumpfile("test.vcd"); //这两行主要是给gtkwave这个工具使用的...
  18. $dumpvars(0,test_tb);
  19. end
  20. initial begin
  21. rst_n=1'b0;
  22. #10 rst_n=1'b1;
  23. end
  24. initial begin
  25. data =8'b10110110;
  26. end
  27. endmodule

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/879873
推荐阅读
相关标签
  

闽ICP备14008679号