Skip to content
Permalink
Browse files

added checksum byte to each ROM

  • Loading branch information...
mist64 committed Apr 14, 2019
1 parent 77e0b5e commit 724a3be1ca622940d529e9118157b09da40493bf
Showing with 58 additions and 0 deletions.
  1. +7 −0 Makefile
  2. +51 −0 checksum.py
@@ -2,6 +2,13 @@ all:
ca65 -g basic/basic.s
ca65 -g kernal/kernal.s
ld65 -C rom.cfg -o rom.bin basic/basic.o kernal/kernal.o -Ln rom.txt
# if it's the unchanged 901226-01 image, use old checksum algorithm
if [[ $$(crc32 basic.bin) == cfdebff8 ]]; then \
python checksum.py --old basic.bin 0xa0 0x1f52; \
else \
python checksum.py --new basic.bin 0xa0 0x1f52; \
fi
python checksum.py --new kernal.bin 0xe0 0x4ac

clean:
rm -f basic/basic.o kernal/kernal.o basic.bin kernal.bin rom.bin
@@ -0,0 +1,51 @@
import sys

def calc_checksum(data, old):
a = 0
c = 0
for b in data:
a = a + b + c
c = 0
if a > 255:
a = a - 256
c = 1
if not old:
a += c
return a

def usage():
print "Usage: python checksum.py {--old|--new} <file.bin> [<checksum> <offset>]"
exit(1)

# main

if len(sys.argv) != 3 and len(sys.argv) != 5:
usage()

if sys.argv[1] == "--old":
old = True
elif sys.argv[1] == "--new":
old = False
else:
usage()

filename = sys.argv[2]

data = bytearray(open(filename, 'rb').read())

checksum = calc_checksum(data, old)

if len(sys.argv) != 5:
print filename + ": " + hex(checksum)
exit(0)

desired_checksum = int(sys.argv[3], 16)
offset = int(sys.argv[4], 16)

if checksum < desired_checksum:
data[offset] = desired_checksum - checksum
else:
data[offset] = 0xff - (checksum - desired_checksum)

file = open(filename, "wb")
file.write(data)

0 comments on commit 724a3be

Please sign in to comment.
You can’t perform that action at this time.