Changeset 526 in xtideuniversalbios for trunk/Serial_Server/library/Checksum.cpp


Ignore:
Timestamp:
Mar 15, 2013, 1:38:58 AM (11 years ago)
Author:
krille_n_@…
google:author:
krille_n_@hotmail.com
Message:

Changes:

  • Update of the copyright notices to include the year 2013.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Serial_Server/library/Checksum.cpp

    r376 r526  
    66
    77//
    8 // XTIDE Universal BIOS and Associated Tools 
    9 // Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
     8// XTIDE Universal BIOS and Associated Tools
     9// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
    1010//
    1111// This program is free software; you can redistribute it and/or modify
     
    1313// the Free Software Foundation; either version 2 of the License, or
    1414// (at your option) any later version.
    15 // 
     15//
    1616// This program is distributed in the hope that it will be useful,
    1717// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1818// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19 // GNU General Public License for more details.     
     19// GNU General Public License for more details.
    2020// Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
    2121//
    2222
    2323//
    24 // This file implements Fletcher's Checksum.  The serial code uses this checksum, as it is very quick 
     24// This file implements Fletcher's Checksum.  The serial code uses this checksum, as it is very quick
    2525// to calculate in assembly and offers reasonable error detection.
    2626// For more information, see http://en.wikipedia.org/wiki/Fletcher%27s_checksum.
    2727//
    28 // Since it is faster in 8088 assembly code to deal with 16-bit quantities than 8-bit quantities, 
     28// Since it is faster in 8088 assembly code to deal with 16-bit quantities than 8-bit quantities,
    2929// Fletcher's Checksum has been modified to calculate the 32-bit checksum, and then "fold" the result into a
    30 // 16-bit quantity.  Fletcher's 32-bit Checksum consists of two parts: concatenated 16-bit accumulators. 
    31 // To "fold" to 16-bits, The upper and lower 8-bits of each of these accumulators is XOR'd independently, and then 
     30// 16-bit quantity.  Fletcher's 32-bit Checksum consists of two parts: concatenated 16-bit accumulators.
     31// To "fold" to 16-bits, The upper and lower 8-bits of each of these accumulators is XOR'd independently, and then
    3232// the two results concatenated together, resulting in 16-bits.  Although simpler, an early attempt to XOR the
    3333// 16-bit accumulators results in poorer error detection behavior.  Folding as described here results in error
     
    3535//
    3636// With #define CHECKSUM_TEST, this file becomes a self-contained command line program that runs
    37 // some statistical tests comparing various checksum algorithms with random 512-byte sectors and various 
     37// some statistical tests comparing various checksum algorithms with random 512-byte sectors and various
    3838// levels of errors introduced.
    3939//
     
    5858    b = (b & 0xffff) + (b >> 16);
    5959
    60 // Although tempting to use, for its simplicity and size/speed in assembly, the following folding 
     60// Although tempting to use, for its simplicity and size/speed in assembly, the following folding
    6161// algorithm results in many undetected single bit errors and therefore should not be used.
    6262//  return( (unsigned short) (a ^ b) );
     
    6868
    6969//====================================================================================================
    70 // 
     70//
    7171// Test Code
    7272//
     
    8282unsigned char bit[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
    8383
    84 class algorithm 
     84class algorithm
    8585{
    8686public:
     
    113113
    114114//----------------------------------------------------------------------------------------------------
    115 // 
     115//
    116116// Standard CRC-16
    117117//
     
    131131        unsigned short j;
    132132
    133         for(i = 0; i < 256; ++i) 
     133        for(i = 0; i < 256; ++i)
    134134        {
    135135            value = 0;
     
    147147    }
    148148
    149     unsigned short checksum( unsigned char *data, int len ); 
     149    unsigned short checksum( unsigned char *data, int len );
    150150
    151151private:
     
    158158    int i;
    159159
    160     for(i = 0; i < len; ++i) 
     160    for(i = 0; i < len; ++i)
    161161    {
    162162        unsigned char index = (unsigned char)(crc ^ data[i]);
     
    168168
    169169//----------------------------------------------------------------------------------------------------
    170 // 
     170//
    171171// Basic checksum (just add up the bytes)
    172172//
     
    175175{
    176176public:
    177     unsigned short checksum( unsigned char *data, int len ); 
     177    unsigned short checksum( unsigned char *data, int len );
    178178    basic_algorithm( algorithm *last ) : algorithm( last, (char *) "basic" ) { };
    179179};
     
    193193{
    194194public:
    195     unsigned short checksum( unsigned char *data, int len ); 
     195    unsigned short checksum( unsigned char *data, int len );
    196196    fletcher16_algorithm( algorithm *last ) : algorithm( last, (char *) "f-16" ) { }
    197197};
     
    213213
    214214//----------------------------------------------------------------------------------------------------
    215 // 
     215//
    216216// Folded Fletcher's Checksum (what we use in the serial code, from the top of this file)
    217217//
     
    220220{
    221221public:
    222     unsigned short checksum( unsigned char *data, int len ); 
     222    unsigned short checksum( unsigned char *data, int len );
    223223    folded_fletcher32_algorithm( algorithm *last ) : algorithm( last, (char *) "fold-f-32" ) { }
    224224};
     
    230230
    231231//----------------------------------------------------------------------------------------------------
    232 // 
     232//
    233233// Test Driver and Support routines
    234234//
     
    283283    {
    284284        a->found = (unsigned long *) calloc( BUCKETS, sizeof(long) );
    285        
     285
    286286        a->zero = (unsigned long) a->checksum( bbuff, BBUFF_LENGTH );
    287287
    288288        a->min = iterations+1;
    289289    }
    290    
     290
    291291    printf( "\n" );
    292292    PRINTROW( "zero   ", "%10d  ", a->zero );
     
    344344
    345345            bbuff[ rand() % 512 ] ^= bit[ rand() % 8 ];
    346            
     346
    347347            if( b > 0 )
    348348            {
     
    354354            }
    355355        }
    356     } 
     356    }
    357357
    358358    printf( "\nbit change test:\n" );
Note: See TracChangeset for help on using the changeset viewer.