source: xtideuniversalbios/trunk/Serial_Server/library/Checksum.cpp@ 590

Last change on this file since 590 was 589, checked in by Krister Nordvall, 9 years ago

Changes:

  • BIOS: Fixed a purely cosmetic bug from r542 where, in builds containing MODULE_EBIOS, the boot menu would display an incorrect drive size (0.4 kB with MODULE_STRINGS_COMPRESSED or 0.5 kB without) for old drives with no support for LBA.
  • Fixed a bug from r392 where Vision_DetectAndReturnIDinAXandPortInDXifControllerPresent would return the ID in AL instead of AH (if DANGEROUS_DETECTION had been defined).
  • Fixed a bug from r587 in AdvAtaInit.asm that would prevent detection of QDI Vision controllers.
  • Also changed how the QDI Vision IDs are defined (removed the need for shifting) to avoid confusion. This fixed a potential bug from r587 in AdvAtaInit.asm where some IDs were not being shifted.
  • Fixed a bug in PDC20x30.asm from r587 where GetPdcIDtoAX would not return with the IDE base port in DX so DisablePdcProgrammingMode would fail.
  • Made some changes to ModuleDependency.inc and other files so that MODULE_ADVANCED_ATA now requires USE_386. Consequently it is no longer included in the regular AT-builds, only in the 386_8k-build.
  • Moved the UNROLL_SECTORS_IN_CX_TO_xWORDS macros from IDE_8bit.inc to IdeIO.inc which means it's now possible to build a BIOS without MODULE_8BIT_IDE.
  • XTIDECFG: Added a minimum DOS version check (since it needs DOS version 2+) to allow the program to quit gracefully in the unlikely scenario where someone tries to run it under DOS version 1.
  • Made some changes to Drive.asm to improve drive enumeration. The old method using GET_DOS_DRIVE_PARAMETER_BLOCK_FOR_SPECIFIC_DRIVE worked well in Windows XP but not in Windows 98 SE (in Windows or in DOS mode). The two problems were; 1) The function call would access the drives which on single floppy drive systems would cause Windows to swap between A: and B: (throwing a blue screen asking the user to insert a disk etc). 2) Only floppy drives and FAT16 drives would be available in the list of drives, no FAT32/optical/network drives.
  • Improved code in IdeControllerMenu.asm so that the default port addresses for all IDE interfaces are now restored when (re-)selecting the (same) type of IDE device.
  • Also made it impossible to select a device type unless the required module is included in the loaded BIOS.
  • The version check done when loading a BIOS now uses the FLASH_SIGNATURE definition from Version.inc. Any changes affecting RomVars now only requires updating that definition. This means that changes to RomVars must be implemented in both the BIOS and XTIDECFG before being committed to the repository.
  • Added a compatibility fix for 3Com 3C503 cards to the ROM checksumming code in Buffers.asm (Buffers_GenerateChecksum).
  • SerDrive: Made some minor changes to file names and paths to improve compatibility with case sensitive environments.
  • BIOSDRVS: Made a minor size optimization which as a side effect also makes it compatible with all DOS versions including DOS version 1.
  • Library: Renamed the WAIT_RETRACE_IF_NECESSARY_THEN macro to CALL_WAIT_FOR_RETRACE_IF_NECESSARY_THEN and made a tail-call-optimized version of it (JMP_WAIT_FOR_RETRACE_IF_NECESSARY_THEN).
  • A speed optimization to the eRCL_IM macro for 386 and higher. This change breaks emulation in the sense that the macro will fail when given a memory operand as the first parameter.
  • Other minor optimizations and fixes.
File size: 9.6 KB
RevLine 
[209]1//======================================================================
2//
3// Project: XTIDE Universal BIOS, Serial Port Server
4//
[589]5// File: Checksum.cpp - Checksum function and test routines
[376]6
[209]7//
[526]8// XTIDE Universal BIOS and Associated Tools
9// Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
[376]10//
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 2 of the License, or
14// (at your option) any later version.
[526]15//
[376]16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
[526]19// GNU General Public License for more details.
[376]20// Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21//
22
23//
[526]24// This file implements Fletcher's Checksum. The serial code uses this checksum, as it is very quick
[209]25// to calculate in assembly and offers reasonable error detection.
26// For more information, see http://en.wikipedia.org/wiki/Fletcher%27s_checksum.
27//
[526]28// Since it is faster in 8088 assembly code to deal with 16-bit quantities than 8-bit quantities,
[209]29// Fletcher's Checksum has been modified to calculate the 32-bit checksum, and then "fold" the result into a
[526]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
[209]32// the two results concatenated together, resulting in 16-bits. Although simpler, an early attempt to XOR the
33// 16-bit accumulators results in poorer error detection behavior. Folding as described here results in error
34// detection on par with Fletcher's 16-bit Checksum.
35//
36// With #define CHECKSUM_TEST, this file becomes a self-contained command line program that runs
[526]37// some statistical tests comparing various checksum algorithms with random 512-byte sectors and various
[209]38// levels of errors introduced.
39//
40
[589]41#include "Library.h"
[209]42
43unsigned short checksum( unsigned short *wbuff, int wlen )
44{
45 unsigned long a = 0xffff;
46 unsigned long b = 0xffff;
47 int t;
48
49 for( t = 0; t < wlen; t++ )
50 {
51 a += wbuff[t];
52 b += a;
53 }
54
55 a = (a & 0xffff) + (a >> 16);
56 b = (b & 0xffff) + (b >> 16);
57 a = (a & 0xffff) + (a >> 16);
58 b = (b & 0xffff) + (b >> 16);
59
[526]60// Although tempting to use, for its simplicity and size/speed in assembly, the following folding
[209]61// algorithm results in many undetected single bit errors and therefore should not be used.
62// return( (unsigned short) (a ^ b) );
63
64 return( (unsigned short) (((a & 0xff) << 8) ^ (a & 0xff00)) + (((b & 0xff00) >> 8) ^ (b & 0xff)) );
65}
66
67#ifdef CHECKSUM_TEST
68
69//====================================================================================================
[526]70//
[209]71// Test Code
72//
73
74#include <stdlib.h>
75#include <stdio.h>
76#include <time.h>
77#include <math.h>
78
79#define BUCKETS 65536
80#define BITTEST 16
81
82unsigned char bit[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
83
[526]84class algorithm
[209]85{
86public:
87 virtual unsigned short checksum( unsigned char *data, int len ) = 0;
88 char *title;
89 unsigned long *found;
90 unsigned long zero;
91 unsigned long total;
92 unsigned long empty;
93 unsigned long min;
94 unsigned long max;
95 double stdev;
96 unsigned long bittest[ BITTEST ];
97 unsigned long missed[ BITTEST ];
98 algorithm *next;
99 algorithm( algorithm *last, char *new_title );
100};
101
102algorithm::algorithm( algorithm *last, char *new_title )
103{
104 zero = total = empty = min = max = 0;
105 stdev = 0.0;
106 for( int t = 0; t < BITTEST; t++ )
107 {
108 bittest[t] = missed[t] = 0;
109 }
110 title = new_title;
111 next = last;
112}
113
114//----------------------------------------------------------------------------------------------------
[526]115//
[209]116// Standard CRC-16
117//
118// http://sanity-free.org/134/standard_crc_16_in_csharp.html
119//
120
121static unsigned short crc16_table[256];
122
123class crc16_algorithm : public algorithm
124{
125public:
126 crc16_algorithm( algorithm *last ) : algorithm( last, (char *) "crc-16" )
127 {
128 unsigned short value;
129 unsigned short temp;
130 unsigned short i;
131 unsigned short j;
132
[526]133 for(i = 0; i < 256; ++i)
[209]134 {
135 value = 0;
136 temp = i;
137 for(j = 0; j < 8; ++j) {
138 if(((value ^ temp) & 0x0001) != 0) {
139 value = (unsigned short)((value >> 1) ^ this->crc16_polynomial);
140 }else {
141 value >>= 1;
142 }
143 temp >>= 1;
144 }
145 crc16_table[i] = value;
146 }
147 }
148
[526]149 unsigned short checksum( unsigned char *data, int len );
[209]150
151private:
152 static const unsigned short crc16_polynomial = 0xA001;
153};
154
155unsigned short crc16_algorithm::checksum( unsigned char *data, int len )
156{
157 unsigned short crc = 0;
158 int i;
159
[526]160 for(i = 0; i < len; ++i)
[209]161 {
162 unsigned char index = (unsigned char)(crc ^ data[i]);
163 crc = (unsigned short)((crc >> 8) ^ crc16_table[index]);
164 }
165
166 return( crc );
167}
168
169//----------------------------------------------------------------------------------------------------
[526]170//
[209]171// Basic checksum (just add up the bytes)
172//
173
174class basic_algorithm : public algorithm
175{
176public:
[526]177 unsigned short checksum( unsigned char *data, int len );
[209]178 basic_algorithm( algorithm *last ) : algorithm( last, (char *) "basic" ) { };
179};
180
181unsigned short basic_algorithm::checksum( unsigned char *bbuff, int blen )
182{
183 unsigned short sum = 0;
184 int i;
185 for( i = 0; i < blen; i++ )
186 {
187 sum += bbuff[ i ];
188 }
189 return( sum );
190}
191
192class fletcher16_algorithm : public algorithm
193{
194public:
[526]195 unsigned short checksum( unsigned char *data, int len );
[209]196 fletcher16_algorithm( algorithm *last ) : algorithm( last, (char *) "f-16" ) { }
197};
198
199unsigned short fletcher16_algorithm::checksum( unsigned char* data, int count )
200{
201 unsigned short sum1 = 0;
202 unsigned short sum2 = 0;
203 int index;
204
205 for( index = 0; index < count; ++index )
206 {
207 sum1 = (sum1 + data[index]) % 255;
208 sum2 = (sum2 + sum1) % 255;
209 }
210
211 return (sum2 << 8) | sum1;
212}
213
214//----------------------------------------------------------------------------------------------------
[526]215//
[209]216// Folded Fletcher's Checksum (what we use in the serial code, from the top of this file)
217//
218
219class folded_fletcher32_algorithm : public algorithm
220{
221public:
[526]222 unsigned short checksum( unsigned char *data, int len );
[209]223 folded_fletcher32_algorithm( algorithm *last ) : algorithm( last, (char *) "fold-f-32" ) { }
224};
225
226unsigned short folded_fletcher32_algorithm::checksum( unsigned char* data, int count )
227{
228 return( ::checksum( (unsigned short *) data, count/2 ) );
229}
230
231//----------------------------------------------------------------------------------------------------
[526]232//
[209]233// Test Driver and Support routines
234//
235
236void randomize_buff( unsigned char *bbuff, int blen )
237{
238 int i;
239 for( i = 0; i < blen; i++ )
240 bbuff[i] = rand() % 255;
241}
242
243#define BBUFF_LENGTH 512
244
245unsigned char bbuff[ BBUFF_LENGTH ];
246
247int main( int argc, char *argv[] )
248{
249 algorithm *a, *algorithms;
250
251 unsigned short c;
252
253 double p;
254 double average;
255
256 unsigned long iterations;
257
258 time_t now;
259
260 algorithms = new folded_fletcher32_algorithm( NULL );
261 algorithms = new fletcher16_algorithm( algorithms );
262 algorithms = new crc16_algorithm( algorithms );
263 algorithms = new basic_algorithm( algorithms );
264
265 time( &now );
266 srand((unsigned int)now);
267
268 if( argc != 2 )
269 {
270 fprintf( stderr, "usage: checksum number_of_iterations\n" );
271 exit( 1 );
272 }
273 else
274 iterations = atol( argv[1] );
275
276#define PRINTROW( E, F, G ) { printf( E ); for( a = algorithms; a; a = a->next ) printf( F, G ); printf( "\n" ); }
277
278 printf( "\nnumber of iterations: %d\n\n", iterations );
279 PRINTROW( " ", "%10s ", a->title );
280 PRINTROW( "=======", "============", NULL );
281
282 for( a = algorithms; a; a = a->next )
283 {
284 a->found = (unsigned long *) calloc( BUCKETS, sizeof(long) );
[526]285
[209]286 a->zero = (unsigned long) a->checksum( bbuff, BBUFF_LENGTH );
287
288 a->min = iterations+1;
289 }
[526]290
[209]291 printf( "\n" );
292 PRINTROW( "zero ", "%10d ", a->zero );
293
294 for( int t = 0; t < iterations; t++ )
295 {
296 randomize_buff( bbuff, BBUFF_LENGTH );
297
298 for( a = algorithms; a; a = a->next )
299 a->found[ a->checksum( bbuff, BBUFF_LENGTH ) ]++;
300 }
301
302 average = iterations / 65536.0;
303
304 for( int t = 0; t < 65536; t++ )
305 {
306 for( a = algorithms; a; a = a->next )
307 {
308 a->total += a->found[ t ];
309 if( !a->found[ t ] )
310 a->empty++;
311 if( a->found[ t ] > a->max )
312 a->max = a->found[ t ];
313 if( a->found[ t ] < a->min )
314 a->min = a->found[ t ];
315 p = a->found[ t ] - average;
316 a->stdev += p*p;
317 }
318 }
319
320 p = 1.0 / (65536.0-1.0);
321 for( a = algorithms; a; a = a->next )
322 {
323 a->stdev = sqrt( p * a->stdev );
324 if( a->total != iterations )
325 fprintf( stderr, "Bad %s\n", a->title );
326 }
327
328 printf( "\nchecksum distribution test:\n" );
329 PRINTROW( "empty ", "%10d ", a->empty );
330 PRINTROW( "min ", "%10d ", a->min );
331 PRINTROW( "max ", "%10d ", a->max );
332 PRINTROW( "stdev ", "%10.4lf ", a->stdev );
333
334 for( int t = 0; t < iterations; t++ )
335 {
336 randomize_buff( bbuff, BBUFF_LENGTH );
337
338 for( int b = 0; b < BITTEST; b++ )
339 {
340 for( a = algorithms; a; a = a->next )
341 {
342 a->bittest[ b ] = (a->checksum)( bbuff, BBUFF_LENGTH );
343 }
344
345 bbuff[ rand() % 512 ] ^= bit[ rand() % 8 ];
[526]346
[209]347 if( b > 0 )
348 {
349 for( a = algorithms; a; a = a->next )
350 {
351 if( a->bittest[ 0 ] == a->bittest[ b ] )
352 a->missed[ b ]++;
353 }
354 }
355 }
[526]356 }
[209]357
358 printf( "\nbit change test:\n" );
359 for( int t = 1; t < BITTEST; t++ )
360 {
361 printf( "%2d ", t );
362 for( a = algorithms; a; a = a->next )
363 printf( "%7d ", a->missed[ t ] );
364 printf( "\n" );
365 }
366}
367
368#endif
369
370
371
372
Note: See TracBrowser for help on using the repository browser.