# Perl Script to Generate CRC Verilog Code

Make it to the Right and Larger Audience

# Perl Script to Generate CRC Verilog Code

CRC is widely used to check data corruption. Here is a good article about CRC background:

http://www.lammertbies.nl/comm/info/crc-calculation.html

Quoted from above site:
The idea behind CRC calculation is to look at the data as one large binary number. This number is divided by a certain value and the remainder of the calculation is called the CRC. Dividing in the CRC calculation at first looks to cost a lot of computing power, but it can be performed very quickly.For CRC calculations, no normal subtraction is used, but all calculations are done modulo 2. In that situation you ignore carry bits and in effect the subtraction will be equal to an exclusive or operation. This looks strange, the resulting remainder has a different value, but from an algebraic point of view the functionality is equal.
CRC calculation can be implemented in either hardware or software. Above site also provides C library to calculate CRC using software. Here we’d like to implement CRC calculation in hardware.

A Perl script, crcgen_verilog.pl, is provided which generate CRC calculation Verilog code based on CRC polynomial and data word width.

Usage format:
perl crcgen_verilog.pl <data word width> <CRC polynomial in binary and in reverse order>
Usage example:
For CRC-16, polynomial is 0x8005 (x^16+x^15+x^2+1),run
perl crcgen_verilog.pl 16 10100000000000011
where 16 is data width and 10100000000000011 specifies crc polynomial (x^16+x^15+x^2+1) in binary reverse order. Output file is crc16_dw16.v

crc16_dw16.v is as below:

For your reference, here are some commonly used CRC polynomials:

Polynomial functions for common CRC’s
CRC-16 0x8005 x16 + x15 + x2 + 1
CRC-CCITT 0x1021 x16 + x12 + x5 + 1
CRC-DNP 0x3D65 x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1
CRC-32 0x04C11DB7 x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x1 + 1

Here is crcgen_verilog.pl and zip file.

Senior Engineer at TI
Author brief is empty
Groups:

Tags: