-
Notifications
You must be signed in to change notification settings - Fork 4
/
elite-checksum.py
138 lines (106 loc) · 3.7 KB
/
elite-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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
#
# ******************************************************************************
#
# BBC MASTER ELITE CHECKSUM SCRIPT
#
# Written by Mark Moxon, and inspired by Kieran Connell's version for the
# cassette version of Elite
#
# This script applies encryption and checksums to the compiled binary for the
# main parasite game code. It reads the unencrypted "BCODE.unprot.bin" and
# "BDATA.unprot.bin" binaries and generates encrypted versions as "BCODE.bin"
# and "BDATA.bin"
#
# ******************************************************************************
from __future__ import print_function
import sys
argv = sys.argv
encrypt = True
release = 1
for arg in argv[1:]:
if arg == "-u":
encrypt = False
if arg == "-rel1":
release = 1
if arg == "-rel2":
release = 2
print("Master Elite Checksum")
print("Encryption = ", encrypt)
# Configuration variables for scrambling code and calculating checksums
#
# Values must match those in 3-assembled-output/compile.txt
#
# If you alter the source code, then you should extract the correct values for
# the following variables and plug them into the following, otherwise the game
# will fail the checksum process and will hang on loading
#
# You can find the correct values for these variables by building your updated
# source, and then searching compile.txt for "elite-checksum.py", where the new
# values will be listed
if release == 1:
# SNG47
f = 0x7F48 # F%
scramble_from = 0x2CC1 # G%
na2_per_cent = 0x34CD # NA2%
elif release == 2:
# Compact
f = 0x7FED # F%
scramble_from = 0x2CC1 # G%
na2_per_cent = 0x34CD # NA2%
# Configuration variables for BCODE
load_address = 0x1300
seed = 0x19
if release == 1:
# SNG47
scramble_to = f - 1
elif release == 2:
# Compact
scramble_to = f - 1
# Load assembled code file for BCODE
data_block = bytearray()
elite_file = open("3-assembled-output/BCODE.unprot.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
# Commander data checksum
commander_start = na2_per_cent - load_address
commander_offset = 0x52
CH = 0x4B - 2
CY = 0
for i in range(CH, 0, -1):
CH = CH + CY + data_block[commander_start + i + 7]
CY = (CH > 255) & 1
CH = CH % 256
CH = CH ^ data_block[commander_start + i + 8]
print("Commander checksum = ", hex(CH))
data_block[commander_start + commander_offset] = CH ^ 0xA9
data_block[commander_start + commander_offset + 1] = CH
# Encrypt game code
if encrypt:
for n in range(scramble_from, scramble_to):
data_block[n - load_address] = (data_block[n - load_address] + data_block[n + 1 - load_address]) % 256
data_block[scramble_to - load_address] = (data_block[scramble_to - load_address] + seed) % 256
# Write output file for BCODE
output_file = open("3-assembled-output/BCODE.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/BCODE.bin file saved")
# Configuration variables for BDATA
load_address = 0x1300 + 0x5D00
seed = 0x62
scramble_from = 0x8000
scramble_to = 0xB1FF
data_block = bytearray()
# Load assembled code file for BDATA
elite_file = open("3-assembled-output/BDATA.unprot.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
if encrypt:
for n in range(scramble_from, scramble_to):
data_block[n - load_address] = (data_block[n - load_address] + data_block[n + 1 - load_address]) % 256
data_block[scramble_to - load_address] = (data_block[scramble_to - load_address] + seed) % 256
# Write output file for BDATA
output_file = open("3-assembled-output/BDATA.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/BDATA.bin file saved")