mirror of
https://github.com/marqs85/ossc
synced 2026-11-27 16:32:17 +03:00
Initial public release (FW 0.64)
This commit is contained in:
commit
388c464f63
264 files changed
+66416
No files matched your search
@@ -0,0 +1,224 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* License Agreement *
|
||||
* *
|
||||
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
* copy of this software and associated documentation files (the "Software"), *
|
||||
* to deal in the Software without restriction, including without limitation *
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
* and/or sell copies of the Software, and to permit persons to whom the *
|
||||
* Software is furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
* DEALINGS IN THE SOFTWARE. *
|
||||
* *
|
||||
* This agreement shall be governed in all respects by the laws of the State *
|
||||
* of California and by the laws of the United States of America. *
|
||||
* Altera does not recommend, suggest or require that this reference design *
|
||||
* file be used in conjunction or combination with any other product. *
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Author - JCJB *
|
||||
* *
|
||||
* This design uses the following CRC-32 implementations: *
|
||||
* *
|
||||
* --> Software - Uses modulo 2 division to perform the remainder calculation. *
|
||||
* --> Optimized Software - Uses a lookup table of all possible division *
|
||||
* values. The calculation operates on 8 bit data. *
|
||||
* --> Custom Instruction - Uses a parallel hardware CRC circuit to calculate *
|
||||
* the remainder. The calculation operates on 8, *
|
||||
* 16, 24, or 32 bit data. *
|
||||
* *
|
||||
* The software implementations can be changed to CRC-16 or CRC-CCITT however *
|
||||
* the custom instruction must be modified as well to support the same *
|
||||
* standard. Simply use the values defined in crc.h to change the standard *
|
||||
* used (using the same values in the hardware parameterization) or define *
|
||||
* your own standard. *
|
||||
*******************************************************************************/
|
||||
|
||||
#include "system.h"
|
||||
#include "stdio.h"
|
||||
#include "crc.h"
|
||||
#include "ci_crc.h"
|
||||
#include "sys/alt_timestamp.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
|
||||
/* Modify these values to adjust the test being performed */
|
||||
#define NUMBER_OF_BUFFERS 32
|
||||
#define BUFFER_SIZE 256 /* in bytes */
|
||||
|
||||
/* Change the name of memory device according to what you are using
|
||||
* e.g.: DDR_SDRAM_0 ##_SPAN
|
||||
* SSRAM_0 ##_SPAN
|
||||
*/
|
||||
#define MEMORY_DEVICE_SIZE 32768
|
||||
|
||||
|
||||
/* Make sure there is room left for Nios II text, rodata, rwdata, stack,
|
||||
* and heap. This software and the buffer space must fit within the
|
||||
* size of memory device. A total of 1.5 MBytes is reserved. If BUFFER_SIZE
|
||||
* is a multiple of four then exactly 256kB will be left, otherwise is
|
||||
* amount will be less since the column dimension needs some padding to
|
||||
* stay 32 bit aligned
|
||||
*/
|
||||
#if ((BUFFER_SIZE * NUMBER_OF_BUFFERS) >= MEMORY_DEVICE_SIZE - 10000)
|
||||
#error Your buffer space has exceeded the maximum allowable space. Please\
|
||||
reduce the buffer space so that there is enough room to hold Nios II\
|
||||
code.
|
||||
#endif
|
||||
|
||||
|
||||
/* This will line up the data onto a 32 bit (or greater) boundary. A 2d array
|
||||
* is being used here for simplicity. The first dimension represents a byte
|
||||
* of data and the second dimension represents an individual buffer
|
||||
*/
|
||||
#if ((BUFFER_SIZE & 0x3) == 0)
|
||||
unsigned char data_buffer_region[NUMBER_OF_BUFFERS][BUFFER_SIZE] __attribute__ ((aligned(4)));
|
||||
#else /* need to allocate extra bytes so that all buffers start on a 32 bit
|
||||
boundaries by rounding up the column dimension to the next power of 4
|
||||
*/
|
||||
unsigned char data_buffer_region[NUMBER_OF_BUFFERS][BUFFER_SIZE + 4 - (BUFFER_SIZE&0x3)] __attribute__ ((aligned(4)));
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned long buffer_counter, data_counter;
|
||||
unsigned long sw_slow_results[NUMBER_OF_BUFFERS];
|
||||
unsigned long sw_fast_results[NUMBER_OF_BUFFERS];
|
||||
unsigned long ci_results[NUMBER_OF_BUFFERS];
|
||||
unsigned char random_data = 0x5A;
|
||||
//unsigned long sw_slow_timeA, sw_slow_timeB;
|
||||
// unsigned long sw_fast_timeA, sw_fast_timeB;
|
||||
// unsigned long ci_timeA, ci_timeB;
|
||||
|
||||
alt_u32 sw_slow_timeA, sw_slow_timeB;
|
||||
alt_u32 sw_fast_timeA, sw_fast_timeB;
|
||||
alt_u32 ci_timeA, ci_timeB;
|
||||
|
||||
|
||||
printf("+-----------------------------------------------------------+\n");
|
||||
printf("| Comparison between software and custom instruction CRC32 |\n");
|
||||
printf("+-----------------------------------------------------------+\n\n\n");
|
||||
|
||||
printf("System specification\n");
|
||||
printf("--------------------\n");
|
||||
|
||||
printf("System clock speed = %lu MHz\n", (unsigned long)ALT_CPU_FREQ /(unsigned long)1000000);
|
||||
printf("Number of buffer locations = %d\n", NUMBER_OF_BUFFERS);
|
||||
printf("Size of each buffer = %d bytes\n\n\n", BUFFER_SIZE);
|
||||
|
||||
|
||||
/* Initializing the data buffers */
|
||||
printf("Initializing all of the buffers with pseudo-random data\n");
|
||||
printf("-------------------------------------------------------\n");
|
||||
for(buffer_counter = 0; buffer_counter < NUMBER_OF_BUFFERS; buffer_counter++)
|
||||
{
|
||||
for(data_counter = 0; data_counter < BUFFER_SIZE; data_counter++)
|
||||
{
|
||||
data_buffer_region[buffer_counter][data_counter] = random_data;
|
||||
random_data = (random_data >> 4) + (random_data << 4) + (data_counter & 0xFF);
|
||||
}
|
||||
}
|
||||
printf("Initialization completed\n\n\n");
|
||||
|
||||
|
||||
if(alt_timestamp_start() < 0) // starts the timestamp timer
|
||||
{
|
||||
printf("Please add the high resolution timer to the timestamp timer setting in the syslib properties page.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/* Slow software CRC based on a modulo 2 division implementation */
|
||||
printf("Running the software CRC\n");
|
||||
printf("------------------------\n");
|
||||
sw_slow_timeA = alt_timestamp();
|
||||
|
||||
for(buffer_counter = 0; buffer_counter < NUMBER_OF_BUFFERS; buffer_counter++)
|
||||
{
|
||||
sw_slow_results[buffer_counter] = crcSlow(data_buffer_region[buffer_counter], BUFFER_SIZE);
|
||||
}
|
||||
sw_slow_timeB = alt_timestamp();
|
||||
|
||||
printf("Completed\n\n\n");
|
||||
|
||||
|
||||
/* Fast software CRC based on a lookup table implementation */
|
||||
crcInit();
|
||||
printf("Running the optimized software CRC\n");
|
||||
printf("----------------------------------\n");
|
||||
sw_fast_timeA = alt_timestamp();
|
||||
for(buffer_counter = 0; buffer_counter < NUMBER_OF_BUFFERS; buffer_counter++)
|
||||
{
|
||||
sw_fast_results[buffer_counter] = crcFast(data_buffer_region[buffer_counter], BUFFER_SIZE);
|
||||
}
|
||||
sw_fast_timeB = alt_timestamp();
|
||||
printf("Completed\n\n\n");
|
||||
|
||||
|
||||
/* Custom instruction CRC */
|
||||
printf("Running the custom instruction CRC\n");
|
||||
printf("----------------------------------\n");
|
||||
ci_timeA = alt_timestamp();
|
||||
for(buffer_counter = 0; buffer_counter < NUMBER_OF_BUFFERS; buffer_counter++)
|
||||
{
|
||||
ci_results[buffer_counter] = crcCI(data_buffer_region[buffer_counter], BUFFER_SIZE);
|
||||
}
|
||||
ci_timeB = alt_timestamp();
|
||||
printf("Completed\n\n\n");
|
||||
|
||||
/* Validation of results */
|
||||
printf("Validating the CRC results from all implementations\n");
|
||||
printf("----------------------------------------------------\n");
|
||||
for(buffer_counter = 0; buffer_counter < NUMBER_OF_BUFFERS; buffer_counter++)
|
||||
{
|
||||
/* Test every combination of results to make sure they are consistant */
|
||||
if((sw_slow_results[buffer_counter] != ci_results[buffer_counter]) |
|
||||
(sw_fast_results[buffer_counter] != ci_results[buffer_counter]))
|
||||
{
|
||||
printf("FAILURE! Software CRC = 0x%lx, Optimized Software CRC = 0x%lx, Custom Instruction CRC = 0x%lx,\n",
|
||||
sw_slow_results[buffer_counter], sw_fast_results[buffer_counter], ci_results[buffer_counter]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
printf("All CRC implementations produced the same results\n\n\n");
|
||||
|
||||
|
||||
// Report processing times
|
||||
printf("Processing time for each implementation\n");
|
||||
printf("---------------------------------------\n");
|
||||
printf("Software CRC = %.2lu ms\n", 1000*((unsigned long)(sw_slow_timeB-sw_slow_timeA))/((unsigned long)alt_timestamp_freq()));
|
||||
printf("Optimized software CRC = %.2lu ms\n", 1000*((unsigned long)(sw_fast_timeB-sw_fast_timeA))/((unsigned long)alt_timestamp_freq()));
|
||||
printf("Custom instruction CRC = %.2lu ms\n\n\n", 1000*((unsigned long)(ci_timeB-ci_timeA))/((unsigned long)alt_timestamp_freq()));
|
||||
|
||||
printf("Processing throughput for each implementation\n"); // throughput = total bits / (time(s) * 1000000)
|
||||
printf("---------------------------------------------\n");
|
||||
printf("Software CRC = %.2lu Mbps\n", (8 * NUMBER_OF_BUFFERS * BUFFER_SIZE)/(1000000*(unsigned long)(sw_slow_timeB-sw_slow_timeA)/((unsigned long)alt_timestamp_freq())));
|
||||
printf("Optimized software CRC = %.2lu Mbps\n", (8 * NUMBER_OF_BUFFERS * BUFFER_SIZE)/(1000000*(unsigned long)(sw_fast_timeB-sw_fast_timeA)/((unsigned long)alt_timestamp_freq())));
|
||||
printf("Custom instruction CRC = %.2lu Mbps\n\n\n", (8 * NUMBER_OF_BUFFERS * BUFFER_SIZE)/(1000000*(unsigned long)(ci_timeB-ci_timeA)/((unsigned long)alt_timestamp_freq())));
|
||||
|
||||
printf("Speedup ratio\n");
|
||||
printf("-------------\n");
|
||||
printf("Custom instruction CRC vs software CRC = %lu\n", ((unsigned long)(sw_slow_timeB-sw_slow_timeA))/((unsigned long)(ci_timeB-ci_timeA)));
|
||||
printf("Custom instruction CRC vs optimized software CRC = %lu\n", ((unsigned long)(sw_fast_timeB-sw_fast_timeA))/((unsigned long)(ci_timeB-ci_timeA)));
|
||||
printf("Optimized software CRC vs software CRC= %lu\n", ((unsigned long)(sw_slow_timeB-sw_slow_timeA))/((unsigned long)(sw_fast_timeB-sw_fast_timeA)));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* License Agreement *
|
||||
* *
|
||||
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
* copy of this software and associated documentation files (the "Software"), *
|
||||
* to deal in the Software without restriction, including without limitation *
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
* and/or sell copies of the Software, and to permit persons to whom the *
|
||||
* Software is furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
* DEALINGS IN THE SOFTWARE. *
|
||||
* *
|
||||
* This agreement shall be governed in all respects by the laws of the State *
|
||||
* of California and by the laws of the United States of America. *
|
||||
* Altera does not recommend, suggest or require that this reference design *
|
||||
* file be used in conjunction or combination with any other product. *
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
#ifndef _CRCCI_H_
|
||||
#define _CRCCI_H_
|
||||
|
||||
unsigned long crcCI(unsigned char * input_data, unsigned long input_data_length, int do_initialize);
|
||||
|
||||
#endif //_CRCCI_H_
|
||||
@@ -0,0 +1,109 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* License Agreement *
|
||||
* *
|
||||
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
* copy of this software and associated documentation files (the "Software"), *
|
||||
* to deal in the Software without restriction, including without limitation *
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
* and/or sell copies of the Software, and to permit persons to whom the *
|
||||
* Software is furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
* DEALINGS IN THE SOFTWARE. *
|
||||
* *
|
||||
* This agreement shall be governed in all respects by the laws of the State *
|
||||
* of California and by the laws of the United States of America. *
|
||||
* Altera does not recommend, suggest or require that this reference design *
|
||||
* file be used in conjunction or combination with any other product. *
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* Filename: crc.h
|
||||
*
|
||||
* Description: A header file describing the various CRC standards.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2000 by Michael Barr. This software is placed into
|
||||
* the public domain and may be used for any purpose. However, this
|
||||
* notice must not be changed or removed and no warranty is either
|
||||
* expressed or implied by its publication or distribution.
|
||||
**********************************************************************/
|
||||
|
||||
#ifndef _crc_h
|
||||
#define _crc_h
|
||||
|
||||
|
||||
#define FALSE 0
|
||||
#define TRUE !FALSE
|
||||
|
||||
/*
|
||||
* Select the CRC standard from the list that follows.
|
||||
*/
|
||||
#define CRC32
|
||||
|
||||
|
||||
#if defined(CRC_CCITT)
|
||||
|
||||
typedef unsigned short crc;
|
||||
|
||||
#define CRC_NAME "CRC-CCITT"
|
||||
#define POLYNOMIAL 0x1021
|
||||
#define INITIAL_REMAINDER 0xFFFF
|
||||
#define FINAL_XOR_VALUE 0x0000
|
||||
#define REFLECT_DATA FALSE
|
||||
#define REFLECT_REMAINDER FALSE
|
||||
#define CHECK_VALUE 0x29B1
|
||||
|
||||
#elif defined(CRC16)
|
||||
|
||||
typedef unsigned short crc;
|
||||
|
||||
#define CRC_NAME "CRC-16"
|
||||
#define POLYNOMIAL 0x8005
|
||||
#define INITIAL_REMAINDER 0x0000
|
||||
#define FINAL_XOR_VALUE 0x0000
|
||||
#define REFLECT_DATA TRUE
|
||||
#define REFLECT_REMAINDER TRUE
|
||||
#define CHECK_VALUE 0xBB3D
|
||||
|
||||
#elif defined(CRC32)
|
||||
|
||||
typedef unsigned long crc;
|
||||
|
||||
#define CRC_NAME "CRC-32"
|
||||
#define POLYNOMIAL 0x04C11DB7
|
||||
#define INITIAL_REMAINDER 0xFFFFFFFF
|
||||
#define FINAL_XOR_VALUE 0xFFFFFFFF
|
||||
#define REFLECT_DATA TRUE
|
||||
#define REFLECT_REMAINDER TRUE
|
||||
#define CHECK_VALUE 0xCBF43926
|
||||
|
||||
#else
|
||||
|
||||
#error "One of CRC_CCITT, CRC16, or CRC32 must be #define'd."
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
void crcInit(void);
|
||||
crc crcSlow(unsigned char const message[], int nBytes);
|
||||
crc crcFast(unsigned char const message[], int nBytes);
|
||||
|
||||
|
||||
#endif /* _crc_h */
|
||||
@@ -0,0 +1,97 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* License Agreement *
|
||||
* *
|
||||
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
* copy of this software and associated documentation files (the "Software"), *
|
||||
* to deal in the Software without restriction, including without limitation *
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
* and/or sell copies of the Software, and to permit persons to whom the *
|
||||
* Software is furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
* DEALINGS IN THE SOFTWARE. *
|
||||
* *
|
||||
* This agreement shall be governed in all respects by the laws of the State *
|
||||
* of California and by the laws of the United States of America. *
|
||||
* Altera does not recommend, suggest or require that this reference design *
|
||||
* file be used in conjunction or combination with any other product. *
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* Filename: ci_crc.c
|
||||
*
|
||||
* Description: Custom instruction implementations of the CRC.
|
||||
*
|
||||
* Notes: A macro is defined that is used to access the CRC custom
|
||||
* instruction.
|
||||
*********************************************************************/
|
||||
|
||||
#include "system.h"
|
||||
|
||||
/*The n values and their corresponding operation are as follow:
|
||||
* n = 0, Initialize the custom instruction to the initial remainder value
|
||||
* n = 1, Write 8 bits data to custom instruction
|
||||
* n = 2, Write 16 bits data to custom instruction
|
||||
* n = 3, Write 32 bits data to custom instruction
|
||||
* n = 4, Read 32 bits data from the custom instruction
|
||||
* n = 5, Read 64 bits data from the custom instruction
|
||||
* n = 6, Read 96 bits data from the custom instruction
|
||||
* n = 7, Read 128 bits data from the custom instruction*/
|
||||
#define CRC_CI_MACRO(n, A) __builtin_custom_ini(ALT_CI_NIOS2_HW_CRC32_0_N + (n & 0x7), (A))
|
||||
|
||||
unsigned long crcCI(unsigned char * input_data, unsigned long input_data_length, int do_initialize)
|
||||
{
|
||||
unsigned long index;
|
||||
/* copy of the data buffer pointer so that it can advance by different widths */
|
||||
void * input_data_copy = (void *)input_data;
|
||||
|
||||
/* The custom instruction CRC will initialize to the inital remainder value */
|
||||
if (do_initialize)
|
||||
CRC_CI_MACRO(0,0);
|
||||
|
||||
/* Write 32 bit data to the custom instruction. If the buffer does not end
|
||||
* on a 32 bit boundary then the remaining data will be sent to the custom
|
||||
* instruction in the 'if' statement below.
|
||||
*/
|
||||
for(index = 0; index < (input_data_length & 0xFFFFFFFC); index+=4)
|
||||
{
|
||||
CRC_CI_MACRO(3, *(unsigned long *)input_data_copy);
|
||||
input_data_copy += 4; /* void pointer, must move by 4 for each word */
|
||||
}
|
||||
|
||||
/* Write the remainder of the buffer if it does not end on a word boundary */
|
||||
if((input_data_length & 0x3) == 0x3) /* 3 bytes left */
|
||||
{
|
||||
CRC_CI_MACRO(2, *(unsigned short *)input_data_copy);
|
||||
input_data_copy += 2;
|
||||
CRC_CI_MACRO(1, *(unsigned char *)input_data_copy);
|
||||
}
|
||||
else if((input_data_length & 0x3) == 0x2) /* 2 bytes left */
|
||||
{
|
||||
CRC_CI_MACRO(2, *(unsigned short *)input_data_copy);
|
||||
}
|
||||
else if((input_data_length & 0x3) == 0x1) /* 1 byte left */
|
||||
{
|
||||
CRC_CI_MACRO(1, *(unsigned char *)input_data_copy);
|
||||
}
|
||||
|
||||
/* There are 4 registers in the CRC custom instruction. Since
|
||||
* this example uses CRC-32 only the first register must be read
|
||||
* in order to receive the full result.
|
||||
*/
|
||||
return CRC_CI_MACRO(4, 0);
|
||||
}
|
||||
@@ -0,0 +1,265 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* License Agreement *
|
||||
* *
|
||||
* Copyright (c) 2008 Altera Corporation, San Jose, California, USA. *
|
||||
* All rights reserved. *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a *
|
||||
* copy of this software and associated documentation files (the "Software"), *
|
||||
* to deal in the Software without restriction, including without limitation *
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
|
||||
* and/or sell copies of the Software, and to permit persons to whom the *
|
||||
* Software is furnished to do so, subject to the following conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in *
|
||||
* all copies or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
|
||||
* DEALINGS IN THE SOFTWARE. *
|
||||
* *
|
||||
* This agreement shall be governed in all respects by the laws of the State *
|
||||
* of California and by the laws of the United States of America. *
|
||||
* Altera does not recommend, suggest or require that this reference design *
|
||||
* file be used in conjunction or combination with any other product. *
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
/**********************************************************************
|
||||
*
|
||||
* Filename: crc.c
|
||||
*
|
||||
* Description: Slow and fast implementations of the CRC standards.
|
||||
*
|
||||
* Notes: The parameters for each supported CRC standard are
|
||||
* defined in the header file crc.h. The implementations
|
||||
* here should stand up to further additions to that list.
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2000 by Michael Barr. This software is placed into
|
||||
* the public domain and may be used for any purpose. However, this
|
||||
* notice must not be changed or removed and no warranty is either
|
||||
* expressed or implied by its publication or distribution.
|
||||
**********************************************************************/
|
||||
|
||||
#include "crc.h"
|
||||
|
||||
|
||||
/*
|
||||
* Derive parameters from the standard-specific parameters in crc.h.
|
||||
*/
|
||||
#define WIDTH (8 * sizeof(crc))
|
||||
#define TOPBIT (1 << (WIDTH - 1))
|
||||
|
||||
#if (REFLECT_DATA == TRUE)
|
||||
#undef REFLECT_DATA
|
||||
#define REFLECT_DATA(X) ((unsigned char) reflect((X), 8))
|
||||
#else
|
||||
#undef REFLECT_DATA
|
||||
#define REFLECT_DATA(X) (X)
|
||||
#endif
|
||||
|
||||
#if (REFLECT_REMAINDER == TRUE)
|
||||
#undef REFLECT_REMAINDER
|
||||
#define REFLECT_REMAINDER(X) ((crc) reflect((X), WIDTH))
|
||||
#else
|
||||
#undef REFLECT_REMAINDER
|
||||
#define REFLECT_REMAINDER(X) (X)
|
||||
#endif
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: reflect()
|
||||
*
|
||||
* Description: Reorder the bits of a binary sequence, by reflecting
|
||||
* them about the middle position.
|
||||
*
|
||||
* Notes: No checking is done that nBits <= 32.
|
||||
*
|
||||
* Returns: The reflection of the original data.
|
||||
*
|
||||
*********************************************************************/
|
||||
static unsigned long
|
||||
reflect(unsigned long data, unsigned char nBits)
|
||||
{
|
||||
unsigned long reflection = 0x00000000;
|
||||
unsigned char bit;
|
||||
|
||||
/*
|
||||
* Reflect the data about the center bit.
|
||||
*/
|
||||
for (bit = 0; bit < nBits; ++bit)
|
||||
{
|
||||
/*
|
||||
* If the LSB bit is set, set the reflection of it.
|
||||
*/
|
||||
if (data & 0x01)
|
||||
{
|
||||
reflection |= (1 << ((nBits - 1) - bit));
|
||||
}
|
||||
|
||||
data = (data >> 1);
|
||||
}
|
||||
|
||||
return (reflection);
|
||||
|
||||
} /* reflect() */
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: crcSlow()
|
||||
*
|
||||
* Description: Compute the CRC of a given message.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
* Returns: The CRC of the message.
|
||||
*
|
||||
*********************************************************************/
|
||||
crc
|
||||
crcSlow(unsigned char const message[], int nBytes)
|
||||
{
|
||||
crc remainder = INITIAL_REMAINDER;
|
||||
int byte;
|
||||
unsigned char bit;
|
||||
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a byte at a time.
|
||||
*/
|
||||
for (byte = 0; byte < nBytes; ++byte)
|
||||
{
|
||||
/*
|
||||
* Bring the next byte into the remainder.
|
||||
*/
|
||||
remainder ^= (REFLECT_DATA(message[byte]) << (WIDTH - 8));
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a bit at a time.
|
||||
*/
|
||||
for (bit = 8; bit > 0; --bit)
|
||||
{
|
||||
/*
|
||||
* Try to divide the current data bit.
|
||||
*/
|
||||
if (remainder & TOPBIT)
|
||||
{
|
||||
remainder = (remainder << 1) ^ POLYNOMIAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC result.
|
||||
*/
|
||||
return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
|
||||
|
||||
} /* crcSlow() */
|
||||
|
||||
|
||||
crc crcTable[256];
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: crcInit()
|
||||
*
|
||||
* Description: Populate the partial CRC lookup table.
|
||||
*
|
||||
* Notes: This function must be rerun any time the CRC standard
|
||||
* is changed. If desired, it can be run "offline" and
|
||||
* the table results stored in an embedded system's ROM.
|
||||
*
|
||||
* Returns: None defined.
|
||||
*
|
||||
*********************************************************************/
|
||||
void
|
||||
crcInit(void)
|
||||
{
|
||||
crc remainder;
|
||||
int dividend;
|
||||
unsigned char bit;
|
||||
|
||||
|
||||
/*
|
||||
* Compute the remainder of each possible dividend.
|
||||
*/
|
||||
for (dividend = 0; dividend < 256; ++dividend)
|
||||
{
|
||||
/*
|
||||
* Start with the dividend followed by zeros.
|
||||
*/
|
||||
remainder = dividend << (WIDTH - 8);
|
||||
|
||||
/*
|
||||
* Perform modulo-2 division, a bit at a time.
|
||||
*/
|
||||
for (bit = 8; bit > 0; --bit)
|
||||
{
|
||||
/*
|
||||
* Try to divide the current data bit.
|
||||
*/
|
||||
if (remainder & TOPBIT)
|
||||
{
|
||||
remainder = (remainder << 1) ^ POLYNOMIAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
remainder = (remainder << 1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the result into the table.
|
||||
*/
|
||||
crcTable[dividend] = remainder;
|
||||
}
|
||||
|
||||
} /* crcInit() */
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function: crcFast()
|
||||
*
|
||||
* Description: Compute the CRC of a given message.
|
||||
*
|
||||
* Notes: crcInit() must be called first.
|
||||
*
|
||||
* Returns: The CRC of the message.
|
||||
*
|
||||
*********************************************************************/
|
||||
crc
|
||||
crcFast(unsigned char const message[], int nBytes)
|
||||
{
|
||||
crc remainder = INITIAL_REMAINDER;
|
||||
unsigned char data;
|
||||
int byte;
|
||||
|
||||
|
||||
/*
|
||||
* Divide the message by the polynomial, a byte at a time.
|
||||
*/
|
||||
for (byte = 0; byte < nBytes; ++byte)
|
||||
{
|
||||
data = REFLECT_DATA(message[byte]) ^ (remainder >> (WIDTH - 8));
|
||||
remainder = crcTable[data] ^ (remainder << 8);
|
||||
}
|
||||
|
||||
/*
|
||||
* The final remainder is the CRC.
|
||||
*/
|
||||
return (REFLECT_REMAINDER(remainder) ^ FINAL_XOR_VALUE);
|
||||
|
||||
} /* crcFast() */
|
||||
Reference in new issue
Block a user