-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.py
More file actions
311 lines (247 loc) · 11.4 KB
/
build.py
File metadata and controls
311 lines (247 loc) · 11.4 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import sys, os, struct, subprocess, string
from source.nsmbw.lib_address_maps import *
devkitppc = os.environ.get("DEVKITPPC")
path_cc = os.path.join(devkitppc, "bin", "powerpc-eabi-gcc")
path_objcopy = os.path.join(devkitppc, "bin", "powerpc-eabi-objcopy")
ccflags = '-fPIC -Os -fno-rtti -ffreestanding -nodefaultlibs -nostdlib -fno-unwind-tables -fno-exceptions \
-fmerge-all-constants -fno-threadsafe-statics -ffunction-sections -fdata-sections -fno-short-enums -fshort-wchar -std=gnu++2b'
ldflags = '-Wl,--gc-sections -n'
warning_flags = '-Wno-attribute-alias -Wno-invalid-offsetof'
def process_asm(mapper_from, asmdata, region):
offset = -1
while True:
offset = asmdata.find("PORT", offset + 1)
if offset == -1:
break
# make sure it is its own token
pc = " "
nc = " "
try:
pc = asmdata[offset - 1]
nc = asmdata[offset + 4]
except:
pass
if pc.isalnum() or pc == "_":
continue
if nc.isalnum() or nc == "_":
continue
asmdata = asmdata[:offset] + asmdata[offset+4:]
if mapper_from is None or region is None or region == "P1":
continue
npos = 0
while nc != "(":
assert(nc == " " or nc == "\r" or nc == "\n")
npos += 1
nc = asmdata[offset+npos]
npos += 1
nc = asmdata[offset+npos]
while nc.isdigit() == False:
assert(nc == " " or nc == "\r" or nc == "\n")
npos += 1
nc = asmdata[offset+npos]
num_start = npos
if asmdata[offset+num_start:offset+num_start+2] == "0x":
npos += 2
nc = asmdata[offset+npos]
while nc in string.hexdigits:
npos += 1
nc = asmdata[offset+npos]
else:
npos += 1
nc = asmdata[offset+npos]
while nc.isdigit():
npos += 1
nc = asmdata[offset+npos]
assert(nc.isalnum() == False and nc != "_")
num_end = npos
num_string = asmdata[offset+num_start:offset+num_end]
address = int(num_string, 0)
port_address = map_addr_from_to(mapper_from, region, address)
asmdata = asmdata[:offset+num_start] + str(port_address) + asmdata[offset+num_end:]
return asmdata
def process_nsmbw(path_build, asmdata, mapper_from, address_map, revision):
sfile = open(os.path.join(path_build, "gct_out." + revision + ".s"), "wb")
sfile.write(process_asm(mapper_from, asmdata, address_map[revision]).encode("utf-8"))
sfile.close()
subprocess.run([path_cc, "-mregnames", os.path.join(path_build, "gct_out." + revision + ".s"), "-nostdlib", "-ffreestanding", "-nodefaultlibs", "-o" + os.path.join(path_build, "gct_out." + revision + ".elf"), "-Tgct.ld", "-lgcc"] + ldflags.split(" "))
subprocess.run([path_objcopy, "-O", "binary", os.path.join(path_build, "gct_out." + revision + ".elf"), os.path.join(path_build, "gct_out." + revision + ".bin")])
def process_mkw(path_build, asmdata):
sfile = open(os.path.join(path_build, "gct_out.s"), "wb")
sfile.write(process_asm(None, asmdata, None).encode("utf-8"))
sfile.close()
subprocess.run([path_cc, "-mregnames", os.path.join(path_build, "gct_out.s"), "-nostdlib", "-ffreestanding", "-nodefaultlibs", "-o" + os.path.join(path_build, "gct_out.elf"), "-Tgct.ld", "-lgcc"] + ldflags.split(" "))
subprocess.run([path_objcopy, "-O", "binary", os.path.join(path_build, "gct_out.elf"), os.path.join(path_build, "gct_out.bin")])
def process_nsmbu(path_build, asmdata):
sfile = open(os.path.join(path_build, "gct_out.s"), "wb")
sfile.write(process_asm(None, asmdata, None).encode("utf-8"))
sfile.close()
subprocess.run([path_cc, "-mregnames", os.path.join(path_build, "gct_out.s"), "-nostdlib", "-ffreestanding", "-nodefaultlibs", "-o" + os.path.join(path_build, "gct_out.elf"), "-Tgct.ld"])
subprocess.run([path_objcopy, "-O", "binary", os.path.join(path_build, "gct_out.elf"), os.path.join(path_build, "gct_out.bin")])
def process_cpp_gecko(data, gct_start):
i = 0
while i < ((len(data) - 8) - gct_start) >> 3:
j = gct_start + (i * 8)
words = struct.unpack(">II", data[j:j+8])
if (words[0] & ~0x01FFFFFF) == 0xC4000000:
words2 = struct.unpack(">II", data[j+8:j+16])
dest = words2[0]
branch = words2[1]
inst = struct.unpack(">I", data[dest:dest+4])[0]
if (inst & ~0x00FFFFFF) == 0x00000000:
dest += 4
origin = j + 8
branch |= (dest - origin) & 0x03FFFFFC
data[j] = 0xC2 | ((words[0] >> 24) & 0x01)
data[origin] = branch >> 24
data[origin+1] = (branch >> 16) & 0xFF
data[origin+2] = (branch >> 8) & 0xFF
data[origin+3] = branch & 0xFF
data[origin+4] = 0
data[origin+5] = 0
data[origin+6] = 0
data[origin+7] = 0
if (words[0] & ~0x00FFFFFF) == 0x4E000000:
dest = struct.unpack(">I", data[j+4:j+8])[0]
inst = struct.unpack(">I", data[dest:dest+4])[0]
if (inst & ~0x00FFFFFF) == 0x00000000:
dest += 4
origin = j + 4
branch = 0x48000000
branch |= (dest - origin) & 0x03FFFFFC
data[origin] = branch >> 24
data[origin+1] = (branch >> 16) & 0xFF
data[origin+2] = (branch >> 8) & 0xFF
data[origin+3] = branch & 0xFF
# not handled but to skip the necessary amount of lines
if (words[0] & ~0x00FFFFFF) == 0xC2000000:
i += words[1]
elif (words[0] & ~0x00FFFFFF) == 0xC0000000:
i += words[1]
elif (words[0] & ~0x00FFFFFF) == 0x06000000:
i += (words[1] + 7) >> 3
i += 1
return data
def add_readme_code(readme, path, revision):
binary_file = open(path, "rb")
raw = binary_file.read()
binary_file.close()
assert(len(raw) >= 8)
cpp_setup_end = struct.unpack(">I", raw[-4:])[0]
if cpp_setup_end != 0:
raw = process_cpp_gecko(bytearray(raw), cpp_setup_end)
cpp_setup_end >>= 3
raw = raw[-8:-4] + struct.pack(">I", cpp_setup_end) + raw[:-8]
else:
raw = raw[:-8]
# add collapsible code block named after the revision
readme += "<details>\n"
readme += "<summary>" + revision + "</summary>\n"
readme += "\n```hex\n"
while True:
assert(len(raw) >= 8)
readme += "{:02X}{:02X}{:02X}{:02X} {:02X}{:02X}{:02X}{:02X}\n".format(raw[0], raw[1], raw[2], raw[3], raw[4], raw[5], raw[6], raw[7])
raw = raw[8:]
if len(raw) == 0:
break
readme += "```\n"
readme += "</details>\n\n"
return readme
def run_build(game, name):
path = os.path.join(game, name)
path_src = os.path.join("source", path + ".cpp")
path_build = os.path.join("build", path)
if not os.path.exists(path_build):
os.makedirs(path_build)
tmp_ccflags = ccflags + " -Isource"
if game == "nsmbw":
tmp_ccflags += " -DWII=1 -Isource/wii -DNSMBW=1 -Isource/nsmbw/include -DRVL_WBC=1 -DHAS_EGG=1 -lgcc"
elif game == "mkw":
tmp_ccflags += " -DWII=1 -Isource/wii -DMKW=1 -Isource/mkw/include -DHAS_EGG=1 -lgcc"
result = subprocess.run([path_cc, "-S", path_src, "-o-", "-D__POWERPC__"] + tmp_ccflags.split(" ") + warning_flags.split(" "), stdout=subprocess.PIPE)
result.check_returncode()
asmdata = result.stdout.decode("utf-8")
if game == "nsmbw":
address_map = load_address_map(open(os.path.join("source", "nsmbw", "address-map.txt"), "r"))
mapper_from = address_map["P1"]
process_nsmbw(path_build, asmdata, mapper_from, address_map, "P1")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "P2")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "E1")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "E2")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "J1")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "J2")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "K")
process_nsmbw(path_build, asmdata, mapper_from, address_map, "W")
elif game == "mkw":
process_mkw(path_build, asmdata)
elif game == "nsmbu":
process_nsmbu(path_build, asmdata)
readme = ""
# check if the file starts with "// [Gecko]""
source_file = open(path_src, "r")
first_line = source_file.readline()
if first_line.startswith("// [Gecko]"):
title_line = source_file.readline()
# e.g. "// $Lift Anything"
if title_line.startswith("// $"):
readme = "## " + title_line[4:] + "\n"
added = False
while True:
line = source_file.readline()
if not line:
break
if not line.startswith("// *"):
break
readme += line[4:]
added = True
if added:
readme += "\n"
if readme == "":
readme = "## " + name.replace("-", " ") + "\n\n"
if game == "nsmbw":
readme += "This code is for New Super Mario Bros. Wii. Many revisions may be untested.\n\n"
regions = ["P1", "P2", "E1", "E2", "J1", "J2", "K", "W"]
rev_names = ["PAL Rev 1", "PAL Rev 2", "USA Rev 1", "USA Rev 2", "JPN Rev 1", "JPN Rev 2", "KOR", "TWN"]
for i in range(len(regions)):
readme = add_readme_code(readme, os.path.join("build", path, "gct_out." + regions[i] + ".bin"), rev_names[i])
elif game == "mkw":
readme += "This code is for Mario Kart Wii."
readme = add_readme_code(readme, os.path.join("build", path, "gct_out.bin"), "PAL")
elif game == "nsmbu":
readme += "This code is for New Super Mario Bros. U. Please read the [README](README.md) in the current directory for how to use this code.\n\n"
readme = add_readme_code(readme, os.path.join("build", path, "gct_out.bin"), "USA NSMBU + NSLU")
if not os.path.exists(game):
os.makedirs(game)
readme_file = open(path + ".md", "w")
readme_file.write(readme)
readme_file.close()
# New Super Mario Bros. Wii
run_build("nsmbw", "Lift-Anything")
run_build("nsmbw", "Player-1-Can-Change-Character")
run_build("nsmbw", "Duplicate-Anything")
run_build("nsmbw", "Random-Worldmap-Anim")
run_build("nsmbw", "Destroy-Any-Tile")
run_build("nsmbw", "Fully-Open-Areas")
run_build("nsmbw", "Connect-All-Pipes")
run_build("nsmbw", "Cycle-Through-Powerups")
run_build("nsmbw", "Allow-Movement-During-Cutscenes")
run_build("nsmbw", "Fall-Damage")
run_build("nsmbw", "Death-Messages")
run_build("nsmbw", "Load-More-Gecko-Codes")
run_build("nsmbw", "World-Map-Expansion-Support")
run_build("nsmbw", "GameCube-Controller-Support")
run_build("nsmbw", "Save-Anytime")
run_build("nsmbw", "Exit-Course-Anytime")
run_build("nsmbw", "Disable-Powerup-Change-Pause")
run_build("nsmbw", "Freeze-Any-Enemy")
run_build("nsmbw", "No-Projectile-Limits")
run_build("nsmbw", "Midair-Crouching")
run_build("nsmbw", "Draw-Actor-Collision")
run_build("nsmbw", "Speed-Up-Everything-But-Player")
# New Super Mario Bros. U
run_build("nsmbu", "Lift-Anything-NSMBU")
run_build("nsmbu", "Save-Anytime")
run_build("nsmbu", "Arbitrary-Course-Load")
run_build("nsmbu", "Duplicate-Anything-NSMBU")
# Mario Kart Wii
run_build("mkw", "Load-More-Gecko-Codes")
run_build("mkw", "Balance-Board-Support")