[203] | 1 | @rem = '--*-Perl-*--
|
---|
| 2 | @echo off
|
---|
| 3 | perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
|
---|
| 4 | goto endofperl
|
---|
| 5 | @rem ';
|
---|
| 6 | #!perl
|
---|
| 7 | #
|
---|
| 8 | # Add checksum byte to ROM image
|
---|
| 9 | #
|
---|
| 10 | # Use a size of 0 to skip this script entirely (file is not modified)
|
---|
| 11 | #
|
---|
| 12 | # On Windows, this file can be renamed to a batch file and invoked directly (for example, "c:\>checksum file size")
|
---|
| 13 | #
|
---|
| 14 |
|
---|
[376] | 15 | #
|
---|
[526] | 16 | # XTIDE Universal BIOS and Associated Tools
|
---|
| 17 | # Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
|
---|
[376] | 18 | #
|
---|
| 19 | # This program is free software; you can redistribute it and/or modify
|
---|
| 20 | # it under the terms of the GNU General Public License as published by
|
---|
| 21 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 22 | # (at your option) any later version.
|
---|
[526] | 23 | #
|
---|
[376] | 24 | # This program is distributed in the hope that it will be useful,
|
---|
| 25 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 26 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
[526] | 27 | # GNU General Public License for more details.
|
---|
[376] | 28 | # Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
| 29 | #
|
---|
| 30 |
|
---|
[203] | 31 | ($ARGV[0] ne "" && $ARGV[1] ne "") || die "usage: checksum filename size\n";
|
---|
| 32 |
|
---|
| 33 | $desiredSize = int($ARGV[1]);
|
---|
| 34 |
|
---|
| 35 | if( $desiredSize == 0 )
|
---|
| 36 | {
|
---|
| 37 | exit( 0 );
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | open FILE, "+<".$ARGV[0] || die "file not found\n";
|
---|
| 41 | binmode FILE;
|
---|
| 42 | $cs = 0;
|
---|
| 43 | $last = 0;
|
---|
| 44 | $bytes = 0;
|
---|
| 45 | while( ($n = read( FILE, $d, 1 )) != 0 )
|
---|
| 46 | {
|
---|
| 47 | $cs = $cs + ord($d);
|
---|
| 48 | $cs = $cs % 256;
|
---|
| 49 | $bytes = $bytes + 1;
|
---|
| 50 | }
|
---|
| 51 | $oldBytes = $bytes;
|
---|
| 52 |
|
---|
| 53 | if( $bytes > $desiredSize - 1 )
|
---|
| 54 | {
|
---|
| 55 | die "ERROR: image is bigger than ".($desiredSize-1).": $bytes\n";
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[526] | 58 | $fixzero = chr(0);
|
---|
[203] | 59 | while( $bytes < $desiredSize - 1 )
|
---|
| 60 | {
|
---|
| 61 | print FILE $fixzero;
|
---|
| 62 | $bytes++;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | $fixl = ($cs == 0 ? 0 : 256 - $cs);
|
---|
| 66 | $fix = chr($fixl);
|
---|
| 67 | print FILE $fix;
|
---|
| 68 |
|
---|
| 69 | close FILE;
|
---|
| 70 |
|
---|
| 71 | open FILE, "<".$ARGV[0];
|
---|
| 72 | binmode FILE;
|
---|
| 73 | $cs = 0;
|
---|
| 74 | $newBytes = 0;
|
---|
| 75 | while( ($n = read( FILE, $d, 1 )) != 0 )
|
---|
| 76 | {
|
---|
| 77 | $cs = $cs + ord($d);
|
---|
| 78 | $cs = $cs % 256;
|
---|
| 79 | $newBytes++;
|
---|
| 80 | }
|
---|
| 81 | $cs == 0 || die "Checksum verification failed\n";
|
---|
| 82 |
|
---|
| 83 | print "checksum: ".$ARGV[0].": $oldBytes bytes before, $newBytes bytes after\n";
|
---|
| 84 |
|
---|
| 85 | __DATA__
|
---|
| 86 | :endofperl
|
---|