在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 2567|回复: 2

[求助] 以太网CRC校验代码问题

[复制链接]
发表于 2016-8-25 10:24:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x




  1. //////////////////////////////////////////////////////////////////////
  2. ////                                                              ////
  3. ////  CRC_chk.v                                                   ////
  4. ////                                                              ////
  5. ////  This file is part of the Ethernet IP core project           ////
  6. ////  http://www.opencores.org/projects.cgi/web/ethernet_tri_mode/////
  7. ////                                                              ////
  8. ////  Author(s):                                                  ////
  9. ////      - Jon Gao (gaojon@yahoo.com)                            ////
  10. ////                                                              ////
  11. ////                                                              ////
  12. //////////////////////////////////////////////////////////////////////
  13. ////                                                              ////
  14. //// Copyright (C) 2001 Authors                                   ////
  15. ////                                                              ////
  16. //// This source file may be used and distributed without         ////
  17. //// restriction provided that this copyright statement is not    ////
  18. //// removed from the file and that any derivative work contains  ////
  19. //// the original copyright notice and the associated disclaimer. ////
  20. ////                                                              ////
  21. //// This source file is free software; you can redistribute it   ////
  22. //// and/or modify it under the terms of the GNU Lesser General   ////
  23. //// Public License as published by the Free Software Foundation; ////
  24. //// either version 2.1 of the License, or (at your option) any   ////
  25. //// later version.                                               ////
  26. ////                                                              ////
  27. //// This source is distributed in the hope that it will be       ////
  28. //// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
  29. //// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
  30. //// PURPOSE.  See the GNU Lesser General Public License for more ////
  31. //// details.                                                     ////
  32. ////                                                              ////
  33. //// You should have received a copy of the GNU Lesser General    ////
  34. //// Public License along with this source; if not, download it   ////
  35. //// from http://www.opencores.org/lgpl.shtml                     ////
  36. ////                                                              ////
  37. //////////////////////////////////////////////////////////////////////
  38. //                                                                    
  39. // CVS Revision History                                               
  40. //                                                                    
  41. // $Log: not supported by cvs2svn $
  42. // Revision 1.2  2005/12/16 06:44:16  Administrator
  43. // replaced tab with space.
  44. // passed 9.6k length frame test.
  45. //
  46. // Revision 1.1.1.1  2005/12/13 01:51:45  Administrator
  47. // no message
  48. //                                          

  49. module CRC_chk(
  50. Reset       ,
  51. Clk         ,
  52. CRC_data    ,
  53. CRC_init    ,
  54. CRC_en      ,
  55. //From cpu  
  56. CRC_chk_en  ,
  57. CRC_err     
  58. );
  59. input       Reset       ;
  60. input       Clk         ;
  61. input[7:0]  CRC_data    ;
  62. input       CRC_init    ;
  63. input       CRC_en      ;
  64.             //From CPU
  65. input       CRC_chk_en  ;
  66. output      CRC_err     ;
  67. //******************************************************************************   
  68. //internal signals                                                              
  69. //******************************************************************************
  70. reg [31:0]  CRC_reg;
  71. wire[31:0]  Next_CRC;
  72. //******************************************************************************
  73. //input data width is 8bit, and the first bit is bit[0]
  74. function[31:0]  NextCRC;
  75.     input[7:0]      D;
  76.     input[31:0]     C;
  77.     reg[31:0]       NewCRC;
  78.     begin
  79.     NewCRC[0]=C[24]^C[30]^D[1]^D[7];
  80.     NewCRC[1]=C[25]^C[31]^D[0]^D[6]^C[24]^C[30]^D[1]^D[7];
  81.     NewCRC[2]=C[26]^D[5]^C[25]^C[31]^D[0]^D[6]^C[24]^C[30]^D[1]^D[7];
  82.     NewCRC[3]=C[27]^D[4]^C[26]^D[5]^C[25]^C[31]^D[0]^D[6];
  83.     NewCRC[4]=C[28]^D[3]^C[27]^D[4]^C[26]^D[5]^C[24]^C[30]^D[1]^D[7];
  84.     NewCRC[5]=C[29]^D[2]^C[28]^D[3]^C[27]^D[4]^C[25]^C[31]^D[0]^D[6]^C[24]^C[30]^D[1]^D[7];
  85.     NewCRC[6]=C[30]^D[1]^C[29]^D[2]^C[28]^D[3]^C[26]^D[5]^C[25]^C[31]^D[0]^D[6];
  86.     NewCRC[7]=C[31]^D[0]^C[29]^D[2]^C[27]^D[4]^C[26]^D[5]^C[24]^D[7];
  87.     NewCRC[8]=C[0]^C[28]^D[3]^C[27]^D[4]^C[25]^D[6]^C[24]^D[7];
  88.     NewCRC[9]=C[1]^C[29]^D[2]^C[28]^D[3]^C[26]^D[5]^C[25]^D[6];
  89.     NewCRC[10]=C[2]^C[29]^D[2]^C[27]^D[4]^C[26]^D[5]^C[24]^D[7];
  90.     NewCRC[11]=C[3]^C[28]^D[3]^C[27]^D[4]^C[25]^D[6]^C[24]^D[7];
  91.     NewCRC[12]=C[4]^C[29]^D[2]^C[28]^D[3]^C[26]^D[5]^C[25]^D[6]^C[24]^C[30]^D[1]^D[7];
  92.     NewCRC[13]=C[5]^C[30]^D[1]^C[29]^D[2]^C[27]^D[4]^C[26]^D[5]^C[25]^C[31]^D[0]^D[6];
  93.     NewCRC[14]=C[6]^C[31]^D[0]^C[30]^D[1]^C[28]^D[3]^C[27]^D[4]^C[26]^D[5];
  94.     NewCRC[15]=C[7]^C[31]^D[0]^C[29]^D[2]^C[28]^D[3]^C[27]^D[4];
  95.     NewCRC[16]=C[8]^C[29]^D[2]^C[28]^D[3]^C[24]^D[7];
  96.     NewCRC[17]=C[9]^C[30]^D[1]^C[29]^D[2]^C[25]^D[6];
  97.     NewCRC[18]=C[10]^C[31]^D[0]^C[30]^D[1]^C[26]^D[5];
  98.     NewCRC[19]=C[11]^C[31]^D[0]^C[27]^D[4];
  99.     NewCRC[20]=C[12]^C[28]^D[3];
  100.     NewCRC[21]=C[13]^C[29]^D[2];
  101.     NewCRC[22]=C[14]^C[24]^D[7];
  102.     NewCRC[23]=C[15]^C[25]^D[6]^C[24]^C[30]^D[1]^D[7];
  103.     NewCRC[24]=C[16]^C[26]^D[5]^C[25]^C[31]^D[0]^D[6];
  104.     NewCRC[25]=C[17]^C[27]^D[4]^C[26]^D[5];
  105.     NewCRC[26]=C[18]^C[28]^D[3]^C[27]^D[4]^C[24]^C[30]^D[1]^D[7];
  106.     NewCRC[27]=C[19]^C[29]^D[2]^C[28]^D[3]^C[25]^C[31]^D[0]^D[6];
  107.     NewCRC[28]=C[20]^C[30]^D[1]^C[29]^D[2]^C[26]^D[5];
  108.     NewCRC[29]=C[21]^C[31]^D[0]^C[30]^D[1]^C[27]^D[4];
  109.     NewCRC[30]=C[22]^C[31]^D[0]^C[28]^D[3];
  110.     NewCRC[31]=C[23]^C[29]^D[2];
  111.     NextCRC=NewCRC;
  112.     end
  113.         endfunction

  114. always @ (posedge Clk or posedge Reset)
  115.     if (Reset)
  116.         CRC_reg     <=32'hffffffff;
  117.     else if (CRC_init)
  118.         CRC_reg     <=32'hffffffff;
  119.     else if (CRC_en)
  120.         CRC_reg     <=NextCRC(CRC_data,CRC_reg);

  121. assign  CRC_err = CRC_chk_en&(CRC_reg[31:0] != 32'hc704dd7b);

  122. endmodule


复制代码


最后一行代码是怎么回事?他是怎么实现CRC校验的
发表于 2016-8-26 07:46:09 | 显示全部楼层
这是CRC parallel algorithm。最后一行的意思就是如果CRC的parity不同于这数,就报错,用于固定存储数据。
 楼主| 发表于 2016-8-27 10:37:48 | 显示全部楼层
回复 2# masaka_xlw


   看了,这是所谓的magic number
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

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

×

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-26 20:49 , Processed in 0.019764 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表