OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_bfm/] [ether/] [crc_ether.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
//////////////////////////////////////////////////////////////////////////////////
2
#include <stdlib.h>
3
#include <stdio.h>
4
 
5
//////////////////////////////////////////////////////////////////////////////////
6
 
7
// AUTODIN II, Ethernet, & FDDI
8
#define CRC32_POLY 0x04c11db7
9
 
10
// Generating polynomial:
11
typedef unsigned int uint32_t;
12
typedef unsigned char u8_t;
13
const uint32_t ethernet_polynomial_le = 0xedb88320U;
14
//////////////////////////////////////////////////////////////////////////////////
15
 
16
long crc_ether(char *buf, int len) { // Ethernet FCS, 32bit
17
    int length; u8_t *data; int foxes;
18
    length = len;
19
    data = buf;
20
    foxes = 1;
21
 
22
    unsigned int crc = (foxes) ? 0xffffffff: 0;  /* Initial value. */
23
    while(--length >= 0)
24
      {
25
        unsigned char current_octet = *data++;
26
        int bit;
27
        // printf("%02X, %08X,  inv %08X\n", current_octet, crc, ~crc);
28
 
29
        for (bit = 8; --bit >= 0; current_octet >>= 1) {
30
          if ((crc ^ current_octet) & 1) {
31
            crc >>= 1;
32
            crc ^= ethernet_polynomial_le;
33
          } else
34
            crc >>= 1;
35
        }
36
      }
37
    return ~crc;
38
}
39
 
40
/*
41
The CRC algorithm implementation by C language is as follows: This algorithm is often used as the Ethernet FCS(Frame Check Sequence).
42
 
43
#define CRCPOLY2 0xEDB88320UL  // left-right reversal
44
 
45
static unsigned long crc2(int n, unsigned char c[])
46
{
47
        int i, j;
48
        unsigned long r;
49
 
50
        r = 0xFFFFFFFFUL;
51
        for (i = 0; i < n; i++) {
52
                r ^= c[i];
53
                for (j = 0; j < CHAR_BIT; j++)
54
                        if (r & 1) r = (r >> 1) ^ CRCPOLY2;
55
                        else       r >>= 1;
56
        }
57
        return r ^ 0xFFFFFFFFUL;
58
}
59
*/
60
//////////////////////////////////////////////////////////////////////////////////

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.