This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
parse_roms.py
executable file
·251 lines (199 loc) · 8.89 KB
/
parse_roms.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
import argparse
import os
import subprocess
from typing import List
ROM_ENTRIES_TEMPLATE = """
const retro_emulator_file_t {name}[] = {{
{body}
}};
const uint32_t {name}_count = {rom_count};
"""
ROM_ENTRY_TEMPLATE = """\t{{
\t\t.name = "{name}",
\t\t.ext = "{extension}",
\t\t.address = {rom_entry},
\t\t.size = {size},
\t\t.save_address = {save_entry},
\t\t.save_size = sizeof({save_entry}),
\t\t.system = &{system},
\t\t.region = {region},
\t}},"""
SYSTEM_PROTO_TEMPLATE = """
extern const rom_system_t {name};
"""
SYSTEM_TEMPLATE = """
const rom_system_t {name} = {{
\t.system_name = "{system_name}",
\t.roms = {variable_name},
\t.extension = "{extension}",
\t.roms_count = {roms_count},
}};
"""
class ROM:
def __init__(self, system_name: str, filepath: str, extension: str):
self.name = (os.path.splitext(os.path.basename(filepath))[0])
self.path = filepath
self.size = os.path.getsize(filepath)
self.ext = extension
obj = "".join([i if i.isalnum() else "_" for i in os.path.basename(filepath)])
symbol_path = os.path.dirname(filepath) + "/" + obj
self.obj_path = "build/roms/" + obj + ".o"
self.symbol = "_binary_" + "".join([i if i.isalnum() else "_" for i in symbol_path]) + "_start"
def __str__(self) -> str:
return self.name + " " + str(self.size)
def __repr__(self):
return str(self)
class ROMParser():
def find_roms(self, system_name: str, folder: str, extension: str) -> [ROM]:
ext = extension
if not extension.startswith("."):
extension = "." + extension
script_path = os.path.dirname(os.path.realpath(__file__))
roms_path = os.path.join(script_path, "roms", folder)
rom_files = os.listdir(roms_path)
found_roms = []
for rom in rom_files:
if not rom.endswith(extension):
continue
rom_path = os.path.join(roms_path, rom)
found_roms.append(ROM(system_name, rom_path, ext))
return found_roms
def generate_rom_entries(self, name: str, roms: [ROM], save_prefix: str, system: str) -> str:
body = ""
for i in range(len(roms)):
rom = roms[i]
is_pal = any(substring in rom.name for substring in ["(E)", "(Europe)", "(Sweden)", "(Germany)", "(Italy)", "(France)", "(A)", "(Australia)"])
region = "REGION_PAL" if is_pal else "REGION_NTSC"
body += ROM_ENTRY_TEMPLATE.format(
name=rom.name,
size=rom.size,
rom_entry=rom.symbol,
save_entry=save_prefix + str(i),
region=region,
extension=rom.ext,
system=system,
)
body += "\n"
return ROM_ENTRIES_TEMPLATE.format(name=name, body=body, rom_count=len(roms))
def generate_object_file(self, ROM: ROM) -> str:
# convert rom to an .o file and place the data in the .extflash_game_rom section
prefix = ""
if "GCC_PATH" in os.environ:
prefix = os.environ["GCC_PATH"]
subprocess.run([ os.path.join(prefix, "arm-none-eabi-objcopy"), "--rename-section", ".data=.extflash_game_rom,alloc,load,readonly,data,contents", \
"-I", "binary", "-O", "elf32-littlearm", ROM.path, ROM.obj_path ])
subprocess.run([ os.path.join(prefix, "arm-none-eabi-ar"), "-cru", "build/roms.a", ROM.obj_path ])
template = "extern const uint8_t {name}[];\n"
return template.format(name=ROM.symbol)
def generate_save_entry(self, name: str, save_size: int) -> str:
return f"uint8_t {name}[{save_size}] __attribute__((section (\".saveflash\"))) __attribute__((aligned(4096)));\n"
def get_gameboy_save_size(self, file: str):
total_size = 4096
with open(file, "rb") as f:
# cgb
f.seek(0x143)
cgb = ord(f.read(1))
# 0x80 = Gameboy color but supports old gameboy
# 0xc0 = Gameboy color only
if cgb & 0x80 or cgb == 0xc0:
# Bank 0 + 1-7 for gameboy color work ram
total_size += 8 * 4096 # irl
# Bank 0 + 1 for gameboy color video ram, 2*8KiB
total_size += 4 * 4096 # vrl
else:
# Bank 0 + 1 for gameboy classic work ram
total_size += 2 * 4096 # irl
# Bank 0 only for gameboy classic video ram, 1*8KiB
total_size += 2 * 4096 # vrl
# Cartridge ram size
f.seek(0x149)
total_size += [1, 1, 1, 4, 16, 8][ord(f.read(1))] * 8 * 1024
return total_size
return 0
def generate_system(self, file: str, system_name: str, variable_name: str, folder: str, extensions: List[str], save_prefix: str) -> int:
f = open(file, "w")
roms = []
for e in extensions:
roms += self.find_roms(system_name, folder, e)
total_save_size = 0
total_rom_size = 0
if folder == "nes":
save_size = 24 * 1024
elif folder == "sms":
save_size = 60 * 1024
elif folder == "gg":
save_size = 60 * 1024
elif folder == "pce":
save_size = 76 * 1024
else:
save_size = 0
f.write(SYSTEM_PROTO_TEMPLATE.format(
name=variable_name))
for i in range(len(roms)):
rom = roms[i]
if folder == "gb":
save_size = self.get_gameboy_save_size(rom.path)
# Aligned
aligned_size = 4 * 1024
total_save_size += ((save_size + aligned_size - 1) // (aligned_size)) * aligned_size
total_rom_size += rom.size
f.write(self.generate_object_file(rom))
f.write(self.generate_save_entry(save_prefix + str(i), save_size))
rom_entries = self.generate_rom_entries(folder + "_roms", roms, save_prefix, variable_name)
f.write(rom_entries)
f.write(SYSTEM_TEMPLATE.format(
name=variable_name,
system_name=system_name,
variable_name=folder + "_roms",
extension=folder,
roms_count=len(roms)))
f.close()
return total_save_size, total_rom_size
def write_if_changed(self, path: str, data: str):
old_data = None
if os.path.exists(path):
with open(path) as f:
old_data = f.read()
if data != old_data:
with open(path, "w") as f:
f.write(data)
def parse(self, args):
total_save_size = 0
total_rom_size = 0
build_config = ""
save_size, rom_size = self.generate_system("Core/Src/retro-go/gb_roms.c", "Nintendo Gameboy", "gb_system", "gb", ["gb", "gbc"], "SAVE_GB_")
total_save_size += save_size
total_rom_size += rom_size
build_config += "#define ENABLE_EMULATOR_GB\n" if rom_size > 0 else ""
save_size, rom_size = self.generate_system("Core/Src/retro-go/nes_roms.c", "Nintendo Entertainment System", "nes_system", "nes", ["nes"], "SAVE_NES_")
total_save_size += save_size
total_rom_size += rom_size
build_config += "#define ENABLE_EMULATOR_NES\n" if rom_size > 0 else ""
save_size, rom_size = self.generate_system("Core/Src/retro-go/sms_roms.c", "Sega Master System", "sms_system", "sms", ["sms"], "SAVE_SMS_")
total_save_size += save_size
total_rom_size += rom_size
build_config += "#define ENABLE_EMULATOR_SMS\n" if rom_size > 0 else ""
save_size, rom_size = self.generate_system("Core/Src/retro-go/gg_roms.c", "Sega Game Gear", "gg_system", "gg", ["gg"], "SAVE_GG_")
total_save_size += save_size
total_rom_size += rom_size
build_config += "#define ENABLE_EMULATOR_GG\n" if rom_size > 0 else ""
save_size, rom_size = self.generate_system("Core/Src/retro-go/pce_roms.c", "PC Engine", "pce_system", "pce", ["pce"], "SAVE_PCE_")
total_save_size += save_size
total_rom_size += rom_size
build_config += "#define ENABLE_EMULATOR_PCE\n" if rom_size > 0 else ""
total_size = total_save_size + total_rom_size
print(f"Save data:\t{total_save_size} bytes\nROM data:\t{total_rom_size} bytes\nTotal:\t\t{total_size} / {args.flash_size} bytes (plus some metadata).")
if total_size > args.flash_size:
print(f"Error: External flash will overflow!")
exit(-1)
self.write_if_changed("build/saveflash.ld", f"__SAVEFLASH_LENGTH__ = {total_save_size};\n")
self.write_if_changed("build/config.h", build_config)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Import ROMs to the build environment')
parser = argparse.ArgumentParser()
parser.add_argument('--flash-size', '-s', type=int, default=1024*1024)
args = parser.parse_args()
if not os.path.isdir("build/roms"):
os.mkdir("build/roms", 0o755)
ROMParser().parse(args)