mirror of
https://github.com/marqs85/ossc.git
synced 2026-04-25 16:18:03 +00:00
replace nios crcCI with hw_crc32 qsys module
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
#include <string.h>
|
||||
#include "system.h"
|
||||
#include "flash.h"
|
||||
#include "ci_crc.h"
|
||||
#include "utils.h"
|
||||
|
||||
extern alt_epcq_controller_dev epcq_controller_0;
|
||||
|
||||
@@ -50,7 +50,7 @@ int read_flash(alt_u32 offset, alt_u32 length, alt_u8 *dstbuf)
|
||||
return -FLASH_READ_ERROR;
|
||||
|
||||
for (i=0; i<length; i++)
|
||||
dstbuf[i] = ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0(dstbuf[i]) >> 24;
|
||||
dstbuf[i] = bitswap8(dstbuf[i]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -71,7 +71,7 @@ int write_flash_page(alt_u8 *pagedata, alt_u32 length, alt_u32 pagenum)
|
||||
|
||||
// Bit-reverse bytes for flash
|
||||
for (i=0; i<length; i++)
|
||||
pagedata[i] = ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0(pagedata[i]) >> 24;
|
||||
pagedata[i] = bitswap8(pagedata[i]);
|
||||
|
||||
retval = alt_epcq_controller_write_block(&epcq_controller_dev->dev, (pagenum/PAGES_PER_SECTOR)*PAGES_PER_SECTOR*PAGESIZE, pagenum*PAGESIZE, pagedata, length);
|
||||
|
||||
@@ -95,7 +95,7 @@ int verify_flash(alt_u32 offset, alt_u32 length, alt_u32 golden_crc, alt_u8 *tmp
|
||||
if (retval != 0)
|
||||
return retval;
|
||||
|
||||
//crcval = crcCI(tmpbuf, bytes_to_read, (i==0));
|
||||
crcval = crc32(tmpbuf, bytes_to_read, (i==0));
|
||||
}
|
||||
|
||||
if (crcval != golden_crc)
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
#include "tvp7002.h"
|
||||
#include "av_controller.h"
|
||||
#include "lcd.h"
|
||||
#include "ci_crc.h"
|
||||
#include "utils.h"
|
||||
#include "altera_avalon_pio_regs.h"
|
||||
|
||||
extern char menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
|
||||
@@ -48,19 +48,19 @@ static int check_fw_header(alt_u8 *databuf, fw_hdr *hdr)
|
||||
hdr->version_suffix[7] = 0;
|
||||
|
||||
memcpy(&tmp, databuf+14, 4);
|
||||
hdr->hdr_len = ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(tmp);
|
||||
hdr->hdr_len = bswap32(tmp);
|
||||
memcpy(&tmp, databuf+18, 4);
|
||||
hdr->data_len = ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(tmp);
|
||||
hdr->data_len = bswap32(tmp);
|
||||
memcpy(&tmp, databuf+22, 4);
|
||||
hdr->data_crc = ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(tmp);
|
||||
hdr->data_crc = bswap32(tmp);
|
||||
// Always at bytes [508-511]
|
||||
memcpy(&tmp, databuf+508, 4);
|
||||
hdr->hdr_crc = ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(tmp);
|
||||
hdr->hdr_crc = bswap32(tmp);
|
||||
|
||||
if (hdr->hdr_len < 26 || hdr->hdr_len > 508)
|
||||
return FW_HDR_ERROR;
|
||||
|
||||
//crcval = crcCI(databuf, hdr->hdr_len, 1);
|
||||
crcval = crc32(databuf, hdr->hdr_len, 1);
|
||||
|
||||
if (crcval != hdr->hdr_crc)
|
||||
return FW_HDR_CRC_ERROR;
|
||||
@@ -81,7 +81,7 @@ static int check_fw_image(alt_u32 offset, alt_u32 size, alt_u32 golden_crc, alt_
|
||||
if (retval != SD_OK)
|
||||
return retval;
|
||||
|
||||
//crcval = crcCI(tmpbuf, bytes_to_read, (i==0));
|
||||
crcval = crc32(tmpbuf, bytes_to_read, (i==0));
|
||||
}
|
||||
|
||||
if (crcval != golden_crc)
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// Copyright (C) 2018 Markus Hiienkari <mhiienka@niksula.hut.fi>
|
||||
//
|
||||
// This file is part of Open Source Scan Converter project.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#include "utils.h"
|
||||
#include "system.h"
|
||||
#include "io.h"
|
||||
|
||||
unsigned char bitswap8(unsigned char v)
|
||||
{
|
||||
return ((v * 0x0802LU & 0x22110LU) |
|
||||
(v * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
|
||||
}
|
||||
|
||||
alt_u32 bswap32(alt_u32 w)
|
||||
{
|
||||
return (((w << 24) & 0xff000000) |
|
||||
((w << 8) & 0x00ff0000) |
|
||||
((w >> 8) & 0x0000ff00) |
|
||||
((w >> 24) & 0x000000ff));
|
||||
}
|
||||
|
||||
unsigned long crc32(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)
|
||||
IOWR_32DIRECT(HW_CRC32_0_BASE, 0x0, 0x0);
|
||||
|
||||
/* 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)
|
||||
{
|
||||
IOWR_32DIRECT(HW_CRC32_0_BASE, 0x4, *(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 */
|
||||
{
|
||||
IOWR_16DIRECT(HW_CRC32_0_BASE, 0x4, *(unsigned short *)input_data_copy);
|
||||
input_data_copy += 2;
|
||||
IOWR_8DIRECT(HW_CRC32_0_BASE, 0x4, *(unsigned char *)input_data_copy);
|
||||
}
|
||||
else if((input_data_length & 0x3) == 0x2) /* 2 bytes left */
|
||||
{
|
||||
IOWR_16DIRECT(HW_CRC32_0_BASE, 0x4, *(unsigned short *)input_data_copy);
|
||||
}
|
||||
else if((input_data_length & 0x3) == 0x1) /* 1 byte left */
|
||||
{
|
||||
IOWR_8DIRECT(HW_CRC32_0_BASE, 0x4, *(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 IORD_32DIRECT(HW_CRC32_0_BASE, 0x10);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Copyright (C) 2018 Markus Hiienkari <mhiienka@niksula.hut.fi>
|
||||
//
|
||||
// This file is part of Open Source Scan Converter project.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
#ifndef UTILS_H_
|
||||
#define UTILS_H_
|
||||
|
||||
#include <alt_types.h>
|
||||
|
||||
unsigned char bitswap8(unsigned char v);
|
||||
|
||||
alt_u32 bswap32(alt_u32 w);
|
||||
|
||||
unsigned long crc32(unsigned char *input_data, unsigned long input_data_length, int do_initialize);
|
||||
|
||||
#endif
|
||||
@@ -1,38 +0,0 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* 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_
|
||||
@@ -1,109 +0,0 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* 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 */
|
||||
@@ -1,97 +0,0 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* 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);
|
||||
}
|
||||
@@ -1,265 +0,0 @@
|
||||
/******************************************************************************
|
||||
* *
|
||||
* 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() */
|
||||
@@ -133,17 +133,13 @@
|
||||
*
|
||||
*/
|
||||
|
||||
//#define ALT_CI_NIOS2_HW_CRC32_0(n,A) __builtin_custom_ini(ALT_CI_NIOS2_HW_CRC32_0_N+(n&ALT_CI_NIOS2_HW_CRC32_0_N_MASK),(A))
|
||||
/*#define ALT_CI_NIOS2_HW_CRC32_0(n,A) __builtin_custom_ini(ALT_CI_NIOS2_HW_CRC32_0_N+(n&ALT_CI_NIOS2_HW_CRC32_0_N_MASK),(A))
|
||||
#define ALT_CI_NIOS2_HW_CRC32_0_N 0x0
|
||||
#define ALT_CI_NIOS2_HW_CRC32_0_N_MASK ((1<<3)-1)
|
||||
//#define ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0(A) __builtin_custom_ini(ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0_N,(A))
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0(A) __builtin_custom_ini(ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0_N,(A))
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0_N 0x9
|
||||
//#define ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(A) __builtin_custom_ini(ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0_N,(A))
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0_N 0x8
|
||||
|
||||
#define ALT_CI_NIOS2_HW_CRC32_0(n,A) n
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_BITSWAP_0(A) A
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(A) A
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0(A) __builtin_custom_ini(ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0_N,(A))
|
||||
#define ALT_CI_NIOS_CUSTOM_INSTR_ENDIANCONVERTER_0_N 0x8*/
|
||||
|
||||
|
||||
/*
|
||||
@@ -323,6 +319,13 @@
|
||||
#define ONCHIP_MEMORY2_0_TYPE "altera_avalon_onchip_memory2"
|
||||
#define ONCHIP_MEMORY2_0_WRITABLE 1
|
||||
|
||||
/*
|
||||
* hw_crc32_0 configuration
|
||||
*
|
||||
*/
|
||||
#define ALT_MODULE_CLASS_hw_crc32_0 hw_crc32
|
||||
#define HW_CRC32_0_BASE 0x21000
|
||||
|
||||
|
||||
/*
|
||||
* pio_0 configuration
|
||||
|
||||
Reference in New Issue
Block a user