-
Notifications
You must be signed in to change notification settings - Fork 4
/
elite-checksum.py
233 lines (179 loc) · 6.22 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
#
# ******************************************************************************
#
# DISC ELITE CHECKSUM SCRIPT
#
# Written by Mark Moxon
#
# This script applies encryption and checksums to the compiled binary for the
# main game code. It reads these unencrypted binary files:
#
# * ELITE4.unprot.bin
# * D.CODE.unprot.bin
# * T.CODE.unprot.bin
#
# and generates encrypted versions as follows:
#
# * ELITE4.bin
# * D.CODE.bin
# * T.CODE.bin
#
# ******************************************************************************
from __future__ import print_function
import sys
argv = sys.argv
argc = len(argv)
encrypt = True
Scramble = True
release = 1
for arg in argv[1:]:
if arg == "-u":
encrypt = False
if arg == "-rel1":
release = 1
if arg == "-rel2":
release = 2
if arg == "-rel3":
Scramble = False
release = 3
print("Disc Elite Checksum")
print("Encryption = ", encrypt)
print("Scramble main code = ", Scramble)
# 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
load_address = 0x1900
# TVT1code block
scramble1_from = 0x2962 # TVT1code
scramble1_to = 0x2A62 # ELITE
scramble1_eor = 0xA5
# LOADcode block
scramble2_from = 0x1AED # LOADcode
scramble2_to = 0x1B4F # CATDcode
scramble2_eor = 0x18
# DIALS, SHIP_MISSILE and WORDS blocks
scramble3_from = 0x1D4B # DIALS
scramble3_to = 0x294B # OSBmod
scramble3_eor = 0xA5
# ELITE, ASOFT and CpASOFT blocks, plus padding to the end of the file
if release == 1 or release == 2:
scramble4_from = 0x2A62 # ELITE
scramble4_to = 0x2E00 # End of ELITE4 file
scramble4_eor = 0xA5
elif release == 3:
scramble4_from = 0x2A62 # ELITE
scramble4_to = 0x2DF0 # End of ELITE4 file (at PROT4)
scramble4_eor = 0xA5
# Commander file checksum
tvt1_code = 0x2962 # TVT1code
tvt1 = 0x1100 # TVT1
na_per_cent = 0x1181 # NA%
chk2 = 0x11D3 # CHK2
# Load assembled code file for ELITE4
data_block = bytearray()
elite_file = open("3-assembled-output/ELITE4.unprot.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
# Commander data checksum
na_per_cent_offset = na_per_cent - tvt1 + tvt1_code - load_address
checksum_offset = chk2 - tvt1 + tvt1_code - load_address
CH = 0x4B - 2
CY = 0
for i in range(CH, 0, -1):
CH = CH + CY + data_block[na_per_cent_offset + i + 7]
CY = (CH > 255) & 1
CH = CH % 256
CH = CH ^ data_block[na_per_cent_offset + i + 8]
print("Commander checksum = ", hex(CH))
data_block[checksum_offset] = CH ^ 0xA9
data_block[checksum_offset + 1] = CH
# Extract unscrambled &1100-&11E3 for use in &55FF checksum below
start_1100 = scramble1_from - load_address
end_1100 = start_1100 + 0xE3
block_1100 = data_block[start_1100:end_1100]
# EOR bytes in the various blocks
for n in range(scramble1_from, scramble1_to):
data_block[n - load_address] = data_block[n - load_address] ^ scramble1_eor
for n in range(scramble2_from, scramble2_to):
data_block[n - load_address] = data_block[n - load_address] ^ scramble2_eor
for n in range(scramble3_from, scramble3_to):
data_block[n - load_address] = data_block[n - load_address] ^ scramble3_eor
for n in range(scramble4_from, scramble4_to):
data_block[n - load_address] = data_block[n - load_address] ^ scramble4_eor
# Write output file for ELITE4
output_file = open("3-assembled-output/ELITE4.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/ELITE4.bin file saved")
# Configuration variables for D.CODE
load_address = 0x11E3
scramble_from = 0x1300
scramble_to = 0x5600
scramble_eor = 0x33
# Load assembled code file for D.CODE
data_block = bytearray()
elite_file = open("3-assembled-output/D.CODE.unprot.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
# SC routine, which EORs bytes between &1300 and &55FF
if Scramble:
for n in range(scramble_from, scramble_to):
data_block[n - load_address] = data_block[n - load_address] ^ (n % 256) ^ scramble_eor
# Write output file for D.CODE
output_file = open("3-assembled-output/D.CODE.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/D.CODE.bin file saved")
# Configuration variables for T.CODE
load_address = 0x11E3
scramble_from = 0x1300
scramble_to = 0x6000
scramble_eor = 0x33
# Load assembled code file for T.CODE
data_block = bytearray()
elite_file = open("3-assembled-output/T.CODE.unprot.bin", "rb")
data_block.extend(elite_file.read())
elite_file.close()
# SC routine, which EORs bytes between &1300 and &9FFF
if Scramble:
for n in range(scramble_from, scramble_to):
data_block[n - load_address] = data_block[n - load_address] ^ (n % 256) ^ scramble_eor
# LOAD routine, which calculates checksum at &55FF in docked code
# This checksum is not correct - need to fix this at some point
checksum_address = 0x55FF
block_to_checksum = block_1100 + data_block
d_checksum = 0x11
carry = 1
for x in range(0x11, 0x54):
for y in [0] + list(range(255, 0, -1)):
i = x * 256 + y
d_checksum += block_to_checksum[i - 0x1100] + carry
if d_checksum > 255:
carry = 1
else:
carry = 0
d_checksum = d_checksum % 256
carry = 0
d_checksum = d_checksum % 256
d_checksum = d_checksum % 256
if release == 3:
# Override the checksum to match value in binary, as the
# checksum is disabled in LOAD in the sideways RAM variant
d_checksum = 0xE6
if encrypt:
data_block[checksum_address - load_address] = d_checksum
print("&55FF docked code checksum = ", hex(d_checksum))
# Write output file for T.CODE
output_file = open("3-assembled-output/T.CODE.bin", "wb")
output_file.write(data_block)
output_file.close()
print("3-assembled-output/T.CODE.bin file saved")