Changeset 526 in xtideuniversalbios for trunk/Serial_Server/library/Checksum.cpp
- Timestamp:
- Mar 15, 2013, 1:38:58 AM (12 years ago)
- google:author:
- krille_n_@hotmail.com
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Serial_Server/library/Checksum.cpp
r376 r526 6 6 7 7 // 8 // XTIDE Universal BIOS and Associated Tools 9 // Copyright (C) 2009-2010 by Tomi Tilli, 2011-201 2by 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. 10 10 // 11 11 // This program is free software; you can redistribute it and/or modify … … 13 13 // the Free Software Foundation; either version 2 of the License, or 14 14 // (at your option) any later version. 15 // 15 // 16 16 // This program is distributed in the hope that it will be useful, 17 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 18 // 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. 20 20 // Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 21 21 // 22 22 23 23 // 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 25 25 // to calculate in assembly and offers reasonable error detection. 26 26 // For more information, see http://en.wikipedia.org/wiki/Fletcher%27s_checksum. 27 27 // 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, 29 29 // 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 32 32 // the two results concatenated together, resulting in 16-bits. Although simpler, an early attempt to XOR the 33 33 // 16-bit accumulators results in poorer error detection behavior. Folding as described here results in error … … 35 35 // 36 36 // 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 38 38 // levels of errors introduced. 39 39 // … … 58 58 b = (b & 0xffff) + (b >> 16); 59 59 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 61 61 // algorithm results in many undetected single bit errors and therefore should not be used. 62 62 // return( (unsigned short) (a ^ b) ); … … 68 68 69 69 //==================================================================================================== 70 // 70 // 71 71 // Test Code 72 72 // … … 82 82 unsigned char bit[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 83 83 84 class algorithm 84 class algorithm 85 85 { 86 86 public: … … 113 113 114 114 //---------------------------------------------------------------------------------------------------- 115 // 115 // 116 116 // Standard CRC-16 117 117 // … … 131 131 unsigned short j; 132 132 133 for(i = 0; i < 256; ++i) 133 for(i = 0; i < 256; ++i) 134 134 { 135 135 value = 0; … … 147 147 } 148 148 149 unsigned short checksum( unsigned char *data, int len ); 149 unsigned short checksum( unsigned char *data, int len ); 150 150 151 151 private: … … 158 158 int i; 159 159 160 for(i = 0; i < len; ++i) 160 for(i = 0; i < len; ++i) 161 161 { 162 162 unsigned char index = (unsigned char)(crc ^ data[i]); … … 168 168 169 169 //---------------------------------------------------------------------------------------------------- 170 // 170 // 171 171 // Basic checksum (just add up the bytes) 172 172 // … … 175 175 { 176 176 public: 177 unsigned short checksum( unsigned char *data, int len ); 177 unsigned short checksum( unsigned char *data, int len ); 178 178 basic_algorithm( algorithm *last ) : algorithm( last, (char *) "basic" ) { }; 179 179 }; … … 193 193 { 194 194 public: 195 unsigned short checksum( unsigned char *data, int len ); 195 unsigned short checksum( unsigned char *data, int len ); 196 196 fletcher16_algorithm( algorithm *last ) : algorithm( last, (char *) "f-16" ) { } 197 197 }; … … 213 213 214 214 //---------------------------------------------------------------------------------------------------- 215 // 215 // 216 216 // Folded Fletcher's Checksum (what we use in the serial code, from the top of this file) 217 217 // … … 220 220 { 221 221 public: 222 unsigned short checksum( unsigned char *data, int len ); 222 unsigned short checksum( unsigned char *data, int len ); 223 223 folded_fletcher32_algorithm( algorithm *last ) : algorithm( last, (char *) "fold-f-32" ) { } 224 224 }; … … 230 230 231 231 //---------------------------------------------------------------------------------------------------- 232 // 232 // 233 233 // Test Driver and Support routines 234 234 // … … 283 283 { 284 284 a->found = (unsigned long *) calloc( BUCKETS, sizeof(long) ); 285 285 286 286 a->zero = (unsigned long) a->checksum( bbuff, BBUFF_LENGTH ); 287 287 288 288 a->min = iterations+1; 289 289 } 290 290 291 291 printf( "\n" ); 292 292 PRINTROW( "zero ", "%10d ", a->zero ); … … 344 344 345 345 bbuff[ rand() % 512 ] ^= bit[ rand() % 8 ]; 346 346 347 347 if( b > 0 ) 348 348 { … … 354 354 } 355 355 } 356 } 356 } 357 357 358 358 printf( "\nbit change test:\n" );
Note:
See TracChangeset
for help on using the changeset viewer.