-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.py
50 lines (30 loc) · 1.23 KB
/
checksum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn='another_test_with_graphic_and_palette'
with open(fn+'.bs','rb') as f:
q=f.read()
mode='hirom'
header_start=int('FFB0',16) if mode=='hirom' else int('7FB0',16)
sum_=0
for i in range(len(q)):
if (i>=int('FFB0',16) and i<=int('FFDF',16) ):
sum_+=0
else:
sum_+=q[i]
sum_=sum_%(65536)
ck=str(hex(sum_))[2:].upper()
#print(ck)
inverse_checksum=ck[2:]+' '+ck[:2]
bt1=int(inverse_checksum.split(' ')[0],16)
bt2=int(inverse_checksum.split(' ')[1],16)
checksum=hex(255-bt1)[2:].upper()+' '+hex(255-bt2)[2:].upper()
print(' Calculated checksum:',checksum, inverse_checksum)
original_checksum=' '.join([hex(i)[2:].upper() for i in q[int('FFDC',16):int('FFDF',16)+1]])
print('Actual checksum from ROM:',original_checksum)
new_file=list(q)
if checksum+' '+inverse_checksum!=original_checksum:
print('checksums differ!')
new_file[int('FFDC',16)]=int(checksum.split(' ')[0],16)
new_file[int('FFDD',16)]=int(checksum.split(' ')[1],16)
new_file[int('FFDE',16)]=int(inverse_checksum.split(' ')[0],16)
new_file[int('FFDF',16)]=int(inverse_checksum.split(' ')[1],16)
with open(fn+'_fixed_checksum.bs','wb') as f:
f.write(bytes(new_file))