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

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

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 17266|回复: 40

[原创] 实用的crc算式生成工具(gencrc)

[复制链接]
发表于 2012-6-4 00:15:14 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 zhangguo1286 于 2012-6-4 22:23 编辑

gencrc.c 为源代码,

gencrc.exe为源代码经VC编译后生成的可执行程序,可在windows ms-dos下的运行

a.out为源代码经linux环境下gcc编译生成


使用格式
windows:   gencrc in.txt
linux:     ./a.out in.txt


--in.txt为数据格式



功能:
将串行CRC计算转换为并行计算,主要用在rtl代码。

为了验证并行计算后的结果是否正确,可以将并行N位计算的结果(花费1个CLK)与连续串行一位N次计算的结果(花费N个CLK)相比较。

另外附一个在线计算CRC算式的网址:
http://www.easics.com/webtools/crctool

gencrc.rar

40.39 KB, 下载次数: 494 , 下载积分: 资产 -2 信元, 下载支出 2 信元

发表于 2012-6-4 10:40:28 | 显示全部楼层
好东西哦。呵呵。用用。呵呵。
发表于 2012-6-4 19:49:26 | 显示全部楼层
不错,看看
发表于 2012-6-9 15:12:07 | 显示全部楼层
好东西
发表于 2012-6-9 15:28:57 | 显示全部楼层
谢谢分享
发表于 2012-10-26 09:55:27 | 显示全部楼层
回复 1# zhangguo1286


    请问您那个在线计算crc的网站生成的代码是可以直接用在工程上计算crc然后进行通行的吗?但是生成的函数是有数据输入,crc输入,然后是新的crc输出,这跟我们传统计算只要数据输入,就会发送数据和计算的crc不一样啊,如果网站上生成的可用,那么函数中生的crc输入是什么啊,谢谢您的指导
 楼主| 发表于 2012-10-26 22:10:12 | 显示全部楼层
回复 6# 574920045


   两者其实是一样的,模块里有 数据输入、CRC输入、CRC输出, CRC输出用寄存器采样,采样的结果送给CRC输入,当前时钟的CRC输出与下一时钟的CRC输入相接就OK了,这样就是CRC一直在累计计算。

  另外在线计算好像只有高比特位先进入做计算的情况。
发表于 2012-10-27 09:21:51 | 显示全部楼层
回复 7# zhangguo1286


    如果我用的多项式是crc32的,数据输入宽度为8,那么我的crc第一次初始设置值是什么,如果我的数据输入是0xc5,那么我的开始的crc是什么,我要得到0XC5的crc校验值需要多少次的计算,这个如何使用哪个网站上生成的函数如何实现,谢谢您!!
 楼主| 发表于 2012-10-27 11:35:57 | 显示全部楼层
回复 8# 574920045


   
    CRC初值是由你的通信协议定的,一般是全0或全1。如果是8位并行计算的话,如果你的数据只有8位,那么只需要计算一次(耗一个时钟)就得到结果,然后再逐拍在把计算好的CRC值输出出去就可以了。

    具体使用是你先要告诉生成器你的CRC多项式是什么样的,你需要多少位并行计算。
    如果你的CRC多项式为X^7+X^3+X^1+1,  在线网址计算的话,需要在X^7 X^3 X^1 的位置打勾,gencrc命令生成的话,gen_ployn=7 3 1 0(注意空格)
发表于 2012-10-27 15:36:52 | 显示全部楼层
回复 9# zhangguo1286


    module crc(
  input [3:0] data_in,
  input crc_en,
  output [4:0] crc_out,
  input rst,
  input clk);
  reg [4:0] lfsr_q,lfsr_c;
  assign crc_out = lfsr_q;
  always @(*) begin
    lfsr_c[0] = lfsr_q[1] ^ lfsr_q[4] ^ data_in[0] ^ data_in[3];
    lfsr_c[1] = lfsr_q[2] ^ data_in[1];
    lfsr_c[2] = lfsr_q[1] ^ lfsr_q[3] ^ lfsr_q[4] ^ data_in[0] ^ data_in[2] ^ data_in[3];
    lfsr_c[3] = lfsr_q[2] ^ lfsr_q[4] ^ data_in[1] ^ data_in[3];
    lfsr_c[4] = lfsr_q[0] ^ lfsr_q[3] ^ data_in[2];
  end // always
  always @(posedge clk, negedge rst) begin
    if(~rst) begin
      lfsr_q <= {5{1'b1}};
    end
    else begin
      lfsr_q <= crc_en ? lfsr_c : lfsr_q;
    end
  end // always
endmodule // crc

CRC module for data[3:0] ,   crc[4:0]=1+x^2+x^5;

这是我在一个网站上生成的模块,我给他输入固定的值a,我通过计算应该输出的crc的值是07,但是他的输出却是很多数,只是07也在这些树当中,这个crc_en是什么控制的,不知道是不是这样。如果用这个模块
--------------------------------------------------------------------------------
-- Copyright (C) 1999-2008 Easics NV.
-- This source file may be used and distributed without restriction
-- provided that this copyright statement is not removed from the file
-- and that any derivative work contains the original copyright notice
-- and the associated disclaimer.
--
-- THIS SOURCE FILE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
-- OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
-- WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
--
-- Purpose : synthesizable CRC function
--   * polynomial: (0 1 2 4 5 7 8 10 11 12 16 22 23 26 32)
--   * data width: 8
--
-- Info : tools@easics.be
--        http://www.easics.com
--------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
package PCK_CRC32_D8 is
  -- polynomial: (0 1 2 4 5 7 8 10 11 12 16 22 23 26 32)
  -- data width: 8
  -- convention: the first serial bit is D[7]
  function nextCRC32_D8
    (Data: std_logic_vector(7 downto 0);
     crc:  std_logic_vector(31 downto 0))
    return std_logic_vector;
end PCK_CRC32_D8;

package body PCK_CRC32_D8 is
  -- polynomial: (0 1 2 4 5 7 8 10 11 12 16 22 23 26 32)
  -- data width: 8
  -- convention: the first serial bit is D[7]
  function nextCRC32_D8
    (Data: std_logic_vector(7 downto 0);
     crc:  std_logic_vector(31 downto 0))
    return std_logic_vector is
    variable d:      std_logic_vector(7 downto 0);
    variable c:      std_logic_vector(31 downto 0);
    variable newcrc: std_logic_vector(31 downto 0);
  begin
    d := Data;
    c := crc;
    newcrc(0) := d(6) xor d(0) xor c(24) xor c(30);
    newcrc(1) := d(7) xor d(6) xor d(1) xor d(0) xor c(24) xor c(25) xor c(30) xor c(31);
    newcrc(2) := d(7) xor d(6) xor d(2) xor d(1) xor d(0) xor c(24) xor c(25) xor c(26) xor c(30) xor c(31);
    newcrc(3) := d(7) xor d(3) xor d(2) xor d(1) xor c(25) xor c(26) xor c(27) xor c(31);
    newcrc(4) := d(6) xor d(4) xor d(3) xor d(2) xor d(0) xor c(24) xor c(26) xor c(27) xor c(28) xor c(30);
    newcrc(5) := d(7) xor d(6) xor d(5) xor d(4) xor d(3) xor d(1) xor d(0) xor c(24) xor c(25) xor c(27) xor c(28) xor c(29) xor c(30) xor c(31);
    newcrc(6) := d(7) xor d(6) xor d(5) xor d(4) xor d(2) xor d(1) xor c(25) xor c(26) xor c(28) xor c(29) xor c(30) xor c(31);
    newcrc(7) := d(7) xor d(5) xor d(3) xor d(2) xor d(0) xor c(24) xor c(26) xor c(27) xor c(29) xor c(31);
    newcrc(8) := d(4) xor d(3) xor d(1) xor d(0) xor c(0) xor c(24) xor c(25) xor c(27) xor c(28);
    newcrc(9) := d(5) xor d(4) xor d(2) xor d(1) xor c(1) xor c(25) xor c(26) xor c(28) xor c(29);
    newcrc(10) := d(5) xor d(3) xor d(2) xor d(0) xor c(2) xor c(24) xor c(26) xor c(27) xor c(29);
    newcrc(11) := d(4) xor d(3) xor d(1) xor d(0) xor c(3) xor c(24) xor c(25) xor c(27) xor c(28);
    newcrc(12) := d(6) xor d(5) xor d(4) xor d(2) xor d(1) xor d(0) xor c(4) xor c(24) xor c(25) xor c(26) xor c(28) xor c(29) xor c(30);
    newcrc(13) := d(7) xor d(6) xor d(5) xor d(3) xor d(2) xor d(1) xor c(5) xor c(25) xor c(26) xor c(27) xor c(29) xor c(30) xor c(31);
    newcrc(14) := d(7) xor d(6) xor d(4) xor d(3) xor d(2) xor c(6) xor c(26) xor c(27) xor c(28) xor c(30) xor c(31);
    newcrc(15) := d(7) xor d(5) xor d(4) xor d(3) xor c(7) xor c(27) xor c(28) xor c(29) xor c(31);
    newcrc(16) := d(5) xor d(4) xor d(0) xor c(8) xor c(24) xor c(28) xor c(29);
    newcrc(17) := d(6) xor d(5) xor d(1) xor c(9) xor c(25) xor c(29) xor c(30);
    newcrc(18) := d(7) xor d(6) xor d(2) xor c(10) xor c(26) xor c(30) xor c(31);
    newcrc(19) := d(7) xor d(3) xor c(11) xor c(27) xor c(31);
    newcrc(20) := d(4) xor c(12) xor c(28);
    newcrc(21) := d(5) xor c(13) xor c(29);
    newcrc(22) := d(0) xor c(14) xor c(24);
    newcrc(23) := d(6) xor d(1) xor d(0) xor c(15) xor c(24) xor c(25) xor c(30);
    newcrc(24) := d(7) xor d(2) xor d(1) xor c(16) xor c(25) xor c(26) xor c(31);
    newcrc(25) := d(3) xor d(2) xor c(17) xor c(26) xor c(27);
    newcrc(26) := d(6) xor d(4) xor d(3) xor d(0) xor c(18) xor c(24) xor c(27) xor c(28) xor c(30);
    newcrc(27) := d(7) xor d(5) xor d(4) xor d(1) xor c(19) xor c(25) xor c(28) xor c(29) xor c(31);
    newcrc(28) := d(6) xor d(5) xor d(2) xor c(20) xor c(26) xor c(29) xor c(30);
    newcrc(29) := d(7) xor d(6) xor d(3) xor c(21) xor c(27) xor c(30) xor c(31);
    newcrc(30) := d(7) xor d(4) xor c(22) xor c(28) xor c(31);
    newcrc(31) := d(5) xor c(23) xor c(29);
    return newcrc;
  end nextCRC32_D8;
end PCK_CRC32_D8;
也是输人固定的的数,我不知道crc的输入是多少,同时输出的crc又是有很多数,按理说一个数据输入应该的到的是同一个crc的值啊,谢谢您的详细解说
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

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

×

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

GMT+8, 2024-3-29 20:12 , Processed in 0.033344 second(s), 10 queries , Gzip On, Redis On.

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