?? crc.c
字號:
/******************************************************************************
* *
* License Agreement *
* *
* Copyright (c) 2003 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. *
* *
******************************************************************************/
/*
* The software implementation of CRC calulation contained within
* this file is based upon an example written by Michael Barr
* The original copyright notice is shown below
*/
/**********************************************************************
*
* Filename: crc.c
*
* Description: A table-driven implementation of CRC-CCITT checksums.
*
* Notes: Some of the constants in this file are specific to
* Arcom's Target188EB hardware.
*
* This code can be easily modified to implement any
* "non-reflective" CRC algorithm. Simply change the
* constants POLYNOMIAL, INITIAL_REMAINDER, FINAL_XOR,
* and--if an 8 or 32-bit CRC is required--the definition
* of type 'width'.
*
*
* Copyright (c) 1998 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.
**********************************************************************/
// #defines for test purposes
#ifdef ALT_SIM_OPTIMIZE // this is set if the syslib project compiles for modelsim
#define SIM // reduces repititions and printfs for simulation purpose
#define TEST // reduces data block size and fills with known data
#endif
#ifndef TEST
// #defines that determine the CRC operation
// These can be changed by the user for experimentation
#define DATA_BLOCK_BEGIN EXT_FLASH_BASE
#define DATA_BLOCK_END (EXT_FLASH_BASE + 0xffff)
#define DATA_SRAM_COPY (EXT_RAM_BASE + 0xa0000)
#else
// limit the data block size for test purposes
#define DATA_BLOCK_BEGIN EXT_FLASH_BASE
#define DATA_BLOCK_END (EXT_FLASH_BASE + 0xff)
#define DATA_SRAM_COPY (EXT_RAM_BASE + 0xa0000)
#endif
// complete only one run if simulating
#ifdef SIM
#define CRC_RUNS 1
#else
#define CRC_RUNS 0x80
#endif
// These #defines represent the values read by the PIO
// connected to push buttons on the Altera development board
// They should not be changed by the user
#define SW0 0xE
#define SW1 0xD
#define SW2 0xB
#define NONE 0xF
// include libraries
#include <stdio.h>
#include <stdlib.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
#include "alt_types.h"
#include <unistd.h>
#include <sys/alt_timestamp.h>
#include "altera_avalon_dma.h"
#include "sys/alt_dma.h"
// Global variables:
alt_dma_txchan tx; // channel descriptor for DMA transfer
volatile int dma_complete = 0; // used by call back routine to denote completion of DMA
unsigned int timer_overhead; // timer overhead of using the timestamp functions
/********************************************
* Michael Barr's orignial code starts here *
********************************************/
// Any changes to #defines will impact the software routines only
/*
* The CRC parameters. Currently configured for CCITT.
* Simply modify these to switch to another CRC standard.
* NB: THIS ONLY EFFECTS THE SOFTWARE ROUTINE
* THE OTHER ROUTINES ARE HARD CODED IN THE HARDWARE
*/
#define POLYNOMIAL 0x1021
#define INITIAL_REMAINDER 0xFFFF
#define FINAL_XOR_VALUE 0x0000
/*
* The width of the CRC calculation and result.
* Modify the typedef for an 8 or 32-bit CRC standard.
*/
typedef unsigned short width;
#define WIDTH (8 * sizeof(width))
#define TOPBIT (1 << (WIDTH - 1))
/*
* An array containing the pre-computed intermediate result for each
* possible byte of input. This is used to speed up the computation.
*/
width crcTable[256];
/**********************************************************************
*
* Function: crcInit()
*
* Description: Initialize the CRC lookup table. This table is used
* by crcCompute() to make CRC computation faster.
*
* Notes: The mod-2 binary long division is implemented here.
*
* Returns: None defined.
*
**********************************************************************/
void crcInit(void)
{
width remainder;
width dividend;
int bit;
// Perform binary long division, a bit at a time.
for (dividend = 0; dividend < 256; dividend++)
{
// Initialize the remainder.
remainder = dividend << (WIDTH - 8);
// Shift and XOR with the polynomial.
for (bit = 0; bit < 8; bit++)
{
// Try to divide the current data bit.
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = remainder << 1;
}
}
// Save the result in the table.
crcTable[dividend] = remainder;
}
} /* crcInit() */
/**********************************************************************
*
* Function: crcCompute()
*
* Description: Compute the CRC checksum of a binary message block.
*
* Notes: This function expects that crcInit() has been called
* first to initialize the CRC lookup table.
*
* Returns: The CRC of the data.
*
**********************************************************************/
unsigned short crcCompute(unsigned char * message, unsigned int nBytes)
{
unsigned int offset;
unsigned char byte;
width remainder = INITIAL_REMAINDER;
// Divide the message by the polynomial, a byte at time.
for (offset = 0; offset < nBytes; offset++)
{
byte = (remainder >> (WIDTH - 8)) ^ message[offset];
remainder = crcTable[byte] ^ (remainder << 8);
}
// The final remainder is the CRC result.
return (remainder ^ FINAL_XOR_VALUE);
} /* crcCompute() */
/******************************************
* Michael Barr's orignial code ends here *
******************************************/
/**********************************************************************
*
* Function: fast_crcCompute()
*
* Description: Computes the CRC checksum of a binary message block
*
* Notes: Uses a custom instruction to perform the calculation
* The data block must be a factor 0f 4 bytes in length
*
* Returns: The CRC of the data
*
**********************************************************************/
unsigned short fast_crcCompute(unsigned long *data_block, unsigned int nWords)
{
unsigned long* pointer;
unsigned short word;
// #### COMPLETE CODE SEGMENT BELOW ####
// initialise crc reg to 0xFFFF
// call CRC Custom Instruction with DataA = 0xFFF & DataB = 0x1
//
word = /*Insert code*/;
for (pointer = data_block; pointer < (data_block + nWords); pointer ++)
{
// #### COMPLETE CODE SEGMENT BELOW ####
// pass next word to CRC custom instruction
// call CRC Custom Instruction with DataA = *pointer & DataB = 0x0
//
word = /*Insert code*/;
}
return (word);
} /* fast_crcCompute() */
/**********************************************************************
*
* Function: dma_done()
*
* Description: Sets the global variable dma_complete to 1
*
* Notes: Callback function called by DMA routine upon
* completion
*
* Returns: The Global variable dma_done is set to 1.
*
**********************************************************************/
void dma_done (void* handle, void* data){
dma_complete = 1;
} /* dma_done */
/**********************************************************************
*
* Function: faster_crcCompute()
*
* Description: Computes the CRC checksum of a binary message block
*
* Notes: Uses a custom peripheral to perform the calculation
* The data block must be a factor 0f 4 bytes in length
*
* Returns: The CRC of the data
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -