forked from markmoxon/elite-source-code-bbc-micro-cassette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelite-checksum.py
203 lines (145 loc) · 5.13 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
#
# ******************************************************************************
#
# ELITE CHECKSUM SCRIPT
#
# Written by Kieran Connell and Mark Moxon
#
# This script applies encryption, checksums and obfuscation to the compiled
# binaries for the main game and the loader. The script has two parts:
#
# * The first part generates an encrypted version of the main game's "ELTcode"
# binary, based on the code in the original "S.BCFS" BASIC source program
#
# * The second part generates an encrypted version of the main game's "ELITE"
# binary, based on the code in the original "ELITES" BASIC source program
#
# ******************************************************************************
from __future__ import print_function
import sys
argv = sys.argv
argc = len(argv)
Encrypt = True
if argc > 1 and argv[1] == "-u":
Encrypt = False
print("Cassette 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
BLOCK_offset = 0x14B0
ENDBLOCK_offset = 0x1530
MAINSUM_offset = 0x1335
TUT_offset = 0x13E1
CHECKbyt_offset = 0x1334
CODE_offset = 0x0F86
# Load assembled code files that make up big code file
data_block = bytearray()
eliteb_offset = 0
# Append all assembled code files
elite_names = ("ELThead", "ELTA", "ELTB", "ELTC", "ELTD", "ELTE", "ELTF", "ELTG")
for file_name in elite_names:
print(str(len(data_block)), file_name)
if file_name == "ELTB":
eliteb_offset = len(data_block)
elite_file = open("3-assembled-output/" + file_name + ".bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
# Commander data checksum
commander_offset = 0x52
CH = 0x4B - 2
CY = 0
for i in range(CH, 0, -1):
CH = CH + CY + data_block[eliteb_offset + i + 7]
CY = (CH > 255) & 1
CH = CH % 256
CH = CH ^ data_block[eliteb_offset + i + 8]
print("Commander checksum = ", hex(CH))
# Must have Commander checksum otherwise game will lock:
if Encrypt:
data_block[eliteb_offset + commander_offset] = CH ^ 0xA9
data_block[eliteb_offset + commander_offset + 1] = CH
# Skip one byte for checksum0
checksum0_offset = len(data_block)
data_block.append(0)
# Append SHIPS file
ships_file = open("3-assembled-output/SHIPS.bin", "rb")
data_block.extend(ships_file.read())
ships_file.close()
print("3-assembled-output/SHIPS.bin file read")
# Calculate checksum0
checksum0 = 0
for n in range(0x0, 0x4600):
checksum0 += data_block[n + 0x28]
print("checksum 0 = ", hex(checksum0))
if Encrypt:
data_block[checksum0_offset] = checksum0 % 256
# Encrypt data block
if Encrypt:
for n in range(0x0, len(data_block) - 0x28):
data_block[n + 0x28] ^= (n % 256)
# Calculate checksum1
checksum1 = 0
for n in range(0x0, 0x28):
checksum1 += data_block[n]
print("checksum 1 = ", hex(checksum1))
# Write output file for ELTcode
output_file = open("3-assembled-output/ELTcode.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/ELTcode.bin file saved")
output_file = None
data_block = None
# Start again but for loader
print("Elite Loader Checksums")
loader_block = bytearray()
loader_file = open("3-assembled-output/ELITE.unprot.bin", "rb")
loader_block.extend(loader_file.read())
loader_file.close()
# Reverse bytes between BLOCK and ENDBLOCK
for i in range(0, int((ENDBLOCK_offset - BLOCK_offset) / 2)):
temp = loader_block[BLOCK_offset + i]
loader_block[BLOCK_offset + i] = loader_block[ENDBLOCK_offset - i - 1]
loader_block[ENDBLOCK_offset - i - 1] = temp
# Compute MAINSUM
MAINSUM = 0
for i in range(0, 0x400):
MAINSUM += loader_block[i]
print("MAINSUM = ", hex(MAINSUM))
if Encrypt:
loader_block[MAINSUM_offset + 1] = MAINSUM % 256
# Compute CHECKbyt
CHECKbyt = 0
for i in range(1, 384):
CHECKbyt += loader_block[CHECKbyt_offset + i]
print("CHECKbyt = ", hex(CHECKbyt))
if Encrypt:
loader_block[CHECKbyt_offset] = CHECKbyt % 256
if Encrypt:
print("Encypting...")
# EOR TUT BLOCK
for i in range(0, ENDBLOCK_offset - BLOCK_offset):
loader_block[TUT_offset + i] ^= loader_block[BLOCK_offset + i]
# EOR CODE words
for i in range(0, 2):
for j in range(0, 256):
if (j + i * 256 + CHECKbyt_offset) < len(loader_block):
loader_block[j + i * 256 + CHECKbyt_offset] ^= loader_block[j + CODE_offset]
# EOR DATA block at beginning of loader
for i in range(0, 0xf):
for j in range(0, 256):
loader_block[j + i * 256] ^= loader_block[j + CODE_offset]
# Write output file for ELITE
output_file = open("3-assembled-output/ELITE.bin", "wb")
output_file.write(loader_block)
output_file.close()
print("3-assembled-output/ELITE.bin file saved")