Cyclic redundancy checks
The amaranth.lib.crc module provides utilities for computing cyclic
redundancy checks (CRCs) in software and in hardware.
CRCs are specified using the Algorithm class, which contains
settings for CRC width, polynomial, initial value, input/output reflection, and
output XOR. Many commonly used CRC algorithms are available in the
catalog module, while most other CRC designs can be
accommodated by manually constructing Algorithm.
Call the Algorithm with a data_width to obtain a
Parameters class, which fully defines a CRC computation. The
Parameters class provides the compute() method
to perform software computations, and the create() method
to create a hardware CRC module, Processor.
# Create a predefined CRC16-CCITT hardware module, using the default
# 8-bit data width (in other words, bytes).
from amaranth.lib.crc.catalog import CRC16_CCITT
crc = m.submodules.crc = CRC16_CCITT().create()
# Create a custom CRC algorithm, specify the data width explicitly,
# and use it to compute a CRC value in software.
from amaranth.lib.crc import Algorithm
algo = Algorithm(crc_width=16, polynomial=0x1021, initial_crc=0xffff,
reflect_input=False, reflect_output=False,
xor_output=0x0000)
assert algo(data_width=8).compute(b"123456789") == 0x29b1
- class amaranth.lib.crc.Algorithm(*, crc_width, polynomial, initial_crc, reflect_input, reflect_output, xor_output)
Settings for a CRC algorithm, excluding data width.
The parameter set is based on the Williams model from “A Painless Guide to CRC Error Detection Algorithms”: http://www.ross.net/crc/download/crc_v3.txt
For a reference of standard CRC parameter sets, refer to:
reveng’s catalogue, which uses an identical parameterisation,
crcmod’s list of predefined functions, but remove the leading ‘1’ from the polynominal, XOR the “Init-value” with “XOR-out” to obtain
initial_crc, and where “Reversed” is True, set bothreflect_inputandreflect_outputto True,CRC Zoo, which contains only polynomials; use the “explicit +1” form of polynomial but remove the leading ‘1’.
Many commonly used CRC algorithms are available in the
catalogmodule, which includes all entries in the reveng catalogue.To create a
Parametersinstance, call theAlgorithmobject with the required data width, which defaults to 8 bits.- Parameters:
crc_width (int) – Bit width of CRC word. Also known as “width” in the Williams model.
polynomial (int) – CRC polynomial to use,
crc_widthbits long, without the implicitx**crc_widthterm. Polynomial is always specified with the highest order terms in the most significant bit positions; usereflect_inputandreflect_outputto perform a least significant bit first computation.initial_crc (int) – Initial value of CRC register at reset. Most significant bit always corresponds to the highest order term in the CRC register.
reflect_input (bool) – If True, the input data words are bit-reflected, so that they are processed least significant bit first.
reflect_output (bool) – If True, the output CRC is bit-reflected, so the least-significant bit of the output is the highest-order bit of the CRC register. Note that this reflection is performed over the entire CRC register; for transmission you may want to treat the output as a little-endian multi-word value, so for example the reflected 16-bit output 0x4E4C would be transmitted as the two octets 0x4C 0x4E, each transmitted least significant bit first.
xor_output (int) – The output CRC will be the CRC register XOR’d with this value, applied after any output bit-reflection.
- __call__(data_width=8)
Constructs a
Parametersinstance from thisAlgorithmwith the specifieddata_width.- Parameters:
data_width (int) – Bit width of data words, default 8.
- class amaranth.lib.crc.Parameters(algorithm, data_width=8)
Full set of parameters for a CRC computation.
Contains the settings from
Algorithmand additionallydata_width. Refer toAlgorithmfor details of what each parameter means and how to construct them.From this class, you can directly compute CRCs with the
compute()method, or construct a hardware module with thecreate()method.- Parameters:
- residue()
Compute the residue value for this CRC, which is the value left in the CRC register after processing any valid codeword.
- create()
Returns a
Processorconfigured with these parameters.
- compute(data)
Computes and returns the CRC of all data words in
data.- Parameters:
data (iterable of integers) – The CRC is computed over this complete set of data. Each item is an integer of bitwidth equal to
data_width.
- class amaranth.lib.crc.Processor(*args, src_loc_at=0, **kwargs)
Cyclic redundancy check (CRC) processor module.
This module generates CRCs from an input data stream, which can be used to validate an existing CRC or generate a new CRC. It is configured by the
Parametersclass, which can handle most forms of CRCs. Refer to that class’s documentation for a description of the parameters.The CRC value is updated on any clock cycle where
validis asserted, with the updated value available on thecrcoutput on the subsequent clock cycle. The latency is therefore one clock cycle, and the throughput is one data word per clock cycle.The CRC is reset to its initial value whenever
startis asserted.startandvalidmay be asserted on the same clock cycle, in which case a new CRC computation is started with the current value ofdata.With
data_width=1, a classic bit-serial CRC is implemented for the given polynomial in a Galois-type shift register. For larger values ofdata_width, a similar architecture computes every new bit of the CRC in parallel.The
match_detectedoutput may be used to validate data with a trailing CRC (also known as a codeword). If the most recently processed word(s) form the valid CRC of all the previous data sincestartwas asserted, the CRC register will always take on a fixed value known as the residue. Thematch_detectedoutput indicates whether the CRC register currently contains this residue.- Parameters:
parameters (Parameters) – CRC parameters.
- Variables:
start (Signal(), in) – Assert to indicate the start of a CRC computation, re-initialising the CRC register to the initial value. May be asserted simultaneously with
validor by itself.data (Signal(data_width), in) – Data word to add to CRC when
validis asserted.valid (Signal(), in) – Assert when
datais valid to add the data word to the CRC.crc (Signal(crc_width), out) – Registered CRC output value, updated one clock cycle after
validbecomes asserted.match_detected (Signal(), out) – Asserted if the current CRC value indicates a valid codeword has been received.
The following pre-defined CRC algorithms are available:
- Predefined CRC Algorithms
CRC3_GSMCRC3_ROHCCRC4_G_704CRC4_ITUCRC4_INTERLAKENCRC5_EPC_C1G2CRC5_EPCCRC5_G_704CRC5_ITUCRC5_USBCRC6_CDMA2000_ACRC6_CDMA2000_BCRC6_DARCCRC6_G_704CRC6_ITUCRC6_GSMCRC7_MMCCRC7_ROHCCRC7_UMTSCRC8_AUTOSARCRC8_BLUETOOTHCRC8_CDMA2000CRC8_DARCCRC8_DVB_S2CRC8_GSM_ACRC8_GSM_BCRC8_HITAGCRC8_I_432_1CRC8_ITUCRC8_I_CODECRC8_LTECRC8_MAXIM_DOWCRC8_MAXIMCRC8_MIFARE_MADCRC8_NRSC_5CRC8_OPENSAFETYCRC8_ROHCCRC8_SAE_J1850CRC8_SMBUSCRC8_TECH_3250CRC8_AESCRC8_ETUCRC8_WCDMACRC10_ATMCRC10_I_610CRC10_CDMA2000CRC10_GSMCRC11_FLEXRAYCRC11_UMTSCRC12_CDMA2000CRC12_DECTCRC12_GSMCRC12_UMTSCRC12_3GPPCRC13_BBCCRC14_DARCCRC14_GSMCRC15_CANCRC15_MPT1327CRC16_ARCCRC16_IBMCRC16_CDMA2000CRC16_CMSCRC16_DDS_110CRC16_DECT_RCRC16_DECT_XCRC16_DNPCRC16_EN_13757CRC16_GENIBUSCRC16_DARCCRC16_EPCCRC16_EPC_C1G2CRC16_I_CODECRC16_GSMCRC16_IBM_3740CRC16_AUTOSARCRC16_CCITT_FALSECRC16_IBM_SDLCCRC16_ISO_HDLCCRC16_ISO_IEC_14443_3_BCRC16_X25CRC16_ISO_IEC_14443_3_ACRC16_KERMITCRC16_BLUETOOTHCRC16_CCITTCRC16_CCITT_TRUECRC16_V_41_LSBCRC16_LJ1200CRC16_M17CRC16_MAXIM_DOWCRC16_MAXIMCRC16_MCRF4XXCRC16_MODBUSCRC16_NRSC_5CRC16_OPENSAFETY_ACRC16_OPENSAFETY_BCRC16_PROFIBUSCRC16_IEC_61158_2CRC16_RIELLOCRC16_SPI_FUJITSUCRC16_AUG_CCITTCRC16_T10_DIFCRC16_TELEDISKCRC16_TMS37157CRC16_UMTSCRC16_BUYPASSCRC16_VERIFONECRC16_USBCRC16_XMODEMCRC16_ACORNCRC16_LTECRC16_V_41_MSBCRC16_ZMODEMCRC17_CAN_FDCRC21_CAN_FDCRC24_BLECRC24_FLEXRAY_ACRC24_FLEXRAY_BCRC24_INTERLAKENCRC24_LTE_ACRC24_LTE_BCRC24_OPENPGPCRC24_OS_9CRC30_CDMACRC31_PHILIPSCRC32_AIXMCRC32_AUTOSARCRC32_BASE91_DCRC32_BZIP2CRC32_AAL5CRC32_DECT_BCRC32_CD_ROM_EDCCRC32_CKSUMCRC32_POSIXCRC32_ISCSICRC32_BASE91_CCRC32_CASTAGNOLICRC32_INTERLAKENCRC32_ISO_HDLCCRC32_ADCCPCRC32_V_42CRC32_XZCRC32_PKZIPCRC32_ETHERNETCRC32_JAMCRCCRC32_MEFCRC32_MPEG_2CRC32_XFERCRC40_GSMCRC64_ECMA_182CRC64_GO_ISOCRC64_MSCRC64_REDISCRC64_WECRC64_XZCRC64_ECMACRC82_DARC