-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
412 lines (398 loc) · 14.3 KB
/
converter.py
File metadata and controls
412 lines (398 loc) · 14.3 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import re
from cchardet import UniversalDetector
from enum import IntEnum
from typing import Tuple
bwram_defines = """macro define_bwram(addr, bwram)
if read1($00FFD5) == $23
!<addr> = $<bwram>
else
!<addr> = $<addr>
endif
endmacro
%define_bwram(7F9A7B, 418800) ; ends at 7F9C7A
%define_bwram(700800, 41A000) ; ends at 7027FF
if read1($00FFD5) == $23
!map16_lo_by = $400000
!map16_hi_by = $410000
!save_mem = $41C000
else
!map16_lo_by = $7E0000
!map16_hi_by = $7F0000
!save_mem = $700000
endif
"""
sprite_addr_list = [
0x7FAB10,
0x7FAB1C,
0x7FAB28,
0x7FAB34,
0x7FAB9E,
0x7FAB40,
0x7FAB4C,
0x7FAB58,
0x7FAB64,
0x7FAC00,
0x7FAC08,
0x7FAC10,
0x9E,
0xAA,
0xB6,
0xC2,
0xD8,
0xE4,
0x14C8,
0x14D4,
0x14E0,
0x14EC,
0x14F8,
0x1504,
0x1510,
0x151C,
0x1528,
0x1534,
0x1540,
0x154C,
0x1558,
0x1564,
0x1570,
0x157C,
0x1588,
0x1594,
0x15A0,
0x15AC,
0x15B8,
0x15C4,
0x15D0,
0x15DC,
0x15EA,
0x15F6,
0x1602,
0x160E,
0x161A,
0x1626,
0x1632,
0x163E,
0x164A,
0x1656,
0x1662,
0x166E,
0x167A,
0x1686,
0x186C,
0x187B,
0x190F,
0x1938,
0x7FAF00,
0x1FD6,
0x1FE2,
]
class WordType(IntEnum):
OTHER = -1
ADDR = 1
COMMA = 2
def convert(asmfile, opt, verbose, stdout) -> None:
encoding = "utf-8"
requires_manual_conversion = False
try:
with open(asmfile, "r") as f:
text = f.readlines()
except Exception as e:
detector = UniversalDetector()
for line in open(asmfile, "rb"):
detector.feed(line)
if detector.done:
break
detector.close()
encod = detector.result
if encod["confidence"] is not None:
if encod["confidence"] >= 0.5:
encoding = encod["encoding"]
else:
encoding = "SHIFT_JIS"
else:
encoding = "SHIFT_JIS" # if confidence is low, try japanese
try:
if verbose:
print(f"Guessed encoding {encoding}, will try to parse now...")
with open(asmfile, "r", encoding=encoding) as f:
text = f.readlines()
except Exception as e:
raise e # propagate
bw_defs = []
outputfile = asmfile.replace(".asm", "_sa1.asm")
outfile = open(outputfile, "w", encoding=encoding)
stdout.write(bytes(f"Processing file {asmfile}:\n", encoding=encoding))
outlines = []
if opt:
outfile.write("incsrc conv_defines.asm\n")
tot_conversions = 0
for index, line in enumerate(text, start=1):
outlines.append("")
data_types = ["db", "dw", "dl", "dd"]
in_comment = False
in_data = False
define_found = re.match(
r"![A-Za-z\d_]+\s+=\s+((\$)?[\dA-Fa-f]{2,6})\S*", line.strip()
)
words = re.split(r"([ \t;])", line.rstrip())
if line.strip() == "" or line.lstrip().startswith(";") or define_found:
# shortcuts for comments and blank lines and defines
if define_found:
requires_manual_conversion = True
is_hex = define_found.group(2) is not None
if (
int(
define_found.group(1).replace("$", "0x")
if is_hex
else define_found.group(1),
16 if is_hex else 10,
)
== 12
):
stdout.write(
bytes(
f"There is define {define_found.group(0)} at line {index} which is equal to 12,"
f" this might be a define related to how many sprites can be loaded by the game"
f" if so, change it to 22 or $16, or (even better) use the following\n"
f"\tif read1($00FFD5) == $23\n\t\t{define_found.group(0)}\n\telse\n\t\t"
f'{define_found.group(0).split("=")[0]}= {"$16" if is_hex else "22"}\n\tendif\n',
encoding=encoding,
)
)
elif (
int(define_found.group(1).replace("$", "0x"), 16)
in sprite_addr_list
and is_hex
):
stdout.write(
bytes(
f"There is define {define_found.group(0)} at line {index} which is a sprite "
f"address, usually replacing the $ with ! works in most tools, it didn't get "
f"converted automatically because it might not be necessary to do so, make sure "
f"to convert manually it ONLY if needed.\n",
encoding=encoding,
)
)
elif (
0x0100
<= int(
define_found.group(1).replace("$", "0x")
if is_hex
else define_found.group(1),
16 if is_hex else 10,
)
<= 0x1FFF
):
stdout.write(
bytes(
f"There is define {define_found.group(0)} at line {index} which might be a ram"
f" address, if it is, convert it by adding |!addr at the end of it, if it's not"
f" a ram address leave it alone\n",
encoding=encoding,
)
)
outlines[index - 1] = line.rstrip()
continue
ignore_next_address = False
for og_word in words:
stripped_word = og_word.strip()
to_insert = ""
if in_comment or in_data:
pass
elif stripped_word.startswith(";"):
in_comment = True
elif any([stripped_word.startswith(a) for a in data_types]):
in_data = True
elif stripped_word.startswith("PEA") or stripped_word.startswith("PER"):
ignore_next_address = True
elif addr := re.findall(r"\$[\da-fA-F]{1,6}\|![a-zA-Z\d_]+\b", og_word):
stdout.write(
bytes(
f"Possibly address {addr[0]} at line {index} was already hybrid.\n",
encoding=encoding,
)
)
elif re.findall(r"\$[^, \n()\[\]]{1,6}", og_word):
if ignore_next_address:
ignore_next_address = False
outlines[index - 1] += og_word
continue
splitted = re.split(r"([\[\](), ])", og_word)
word_tuples = []
for i, word in enumerate(splitted):
if word.startswith("$"):
try:
proc_word = eval(word.replace("$", "0x"))
expr = re.split(
r"[+\\\-^*~<>|]", word.replace("$", "")
) # +\-^*~<> some asar math ops
word = "${:0{}X}".format(
proc_word, max([len(e) for e in expr])
)
word_tuples.append((WordType.ADDR, word, i))
except SyntaxError:
bunch = re.split(r"([+\-^*~<>| ])", word)
for w in bunch:
if w.startswith("$"):
word_tuples.append((WordType.ADDR, w, i))
else:
word_tuples.append((WordType.OTHER, w, i))
elif word.startswith(","):
word_tuples.append((WordType.COMMA, word, i))
else:
word_tuples.append((WordType.OTHER, word, i))
for wordtype, word, i in word_tuples:
if wordtype == WordType.ADDR:
try:
try:
comma_index = (
i + 1
if word_tuples[i + 1][0] == WordType.COMMA
else -1
)
except IndexError:
comma_index = -1
(
ww,
bwram_define_needed,
converted,
manual_conversion,
) = process_word(
word.replace("$", ""),
stdout,
encoding,
index,
splitted,
comma_index,
)
if manual_conversion:
requires_manual_conversion = True
if converted:
tot_conversions += 1
stdout.write(
bytes(
f"Conversion: {word} -> {ww}\n",
encoding=encoding,
)
)
bw_defs.append(bwram_define_needed)
to_insert += ww
except ValueError:
to_insert += word
else:
to_insert += word
outlines[index - 1] += to_insert if to_insert != "" else og_word
if any(bw_defs):
outfile.write(bwram_defines)
outfile.write("\n".join(outlines))
outfile.close()
if verbose:
print(f"Processed file {asmfile}\nTotal conversions: {tot_conversions}")
if requires_manual_conversion:
print(
f"File {asmfile} could require manual conversion for some addresses, please check the log file.\n"
)
stdout.write(
bytes(
f"Processed file {asmfile}\nTotal conversions: {tot_conversions}\n\n\n",
encoding=encoding,
)
)
def check_bwram(word: str) -> Tuple[str, bool]:
bwram_word = int(word, 16)
bwram_remapped_list = (
0x7F9A7B,
0x7027FF,
) # Wiggler's segment buffer, Expansion area planned for SMW hacks
map16_lo_by = (0x7EC800, 0x7EFFFF) # Map16 low byte plus Overworld related data.
map16_hi_by = (0x7FC800, 0x7FFFFF) # Map16 high byte.
save_mem = (
0x700000,
0x7007FF,
) # Original save memory (2 kB big). Not everything is used
bwram_list = [map16_lo_by, map16_hi_by, save_mem]
bwram_indexes = [
baddr_begin <= bwram_word <= baddr_end for baddr_begin, baddr_end in bwram_list
]
subs = ["map16_lo_by", "map16_hi_by", "save_mem"]
if any(bwram_indexes):
true_index = bwram_indexes.index(True)
sub = f"${bwram_word:6X}&$00FFFF|!{subs[true_index]}"
return sub, True
elif bwram_word in bwram_remapped_list:
sub = f"!{bwram_word:6X}"
return sub, True
return word, False
def check_if_shortable():
pass
def process_word(word, stdout, encoding, index, splitted, comma_index):
requires_manual_conversion = False
converted = True
add_dp = False
if comma_index != -1:
if (
len(word) == 4
and (splitted[comma_index + 1] == "y" or splitted[comma_index + 1] == "x")
and word[:2] == "00"
):
add_dp = (
True # preserve absolute addressing when used for some weird reason
)
if word.startswith("8") and len(word) == 6:
word = word.replace("8", "0", 1)
try:
numeric_word = int(word, 16) # if it's not a valid hex numeric value, go away
except ValueError as e:
raise e
bwram_define_needed = False
if len(word) == 6:
word, bwram_define_needed = check_bwram(word)
if bwram_define_needed:
return word, bwram_define_needed, converted, requires_manual_conversion
if numeric_word in sprite_addr_list: # if sprite address, use define
word = "!" + (f"{int(word, 16):X}" if not add_dp else f"{int(word, 16):X}|!dp")
elif len(word) == 6 and (0x000000 <= numeric_word <= 0x0FFFFF): # if rom, add !bank
word = "$" + word + "|!bank"
elif len(word) == 6 and (0x7E0000 <= numeric_word <= 0x7E1FFF):
try:
short_word = word[2:]
spr_index = sprite_addr_list.index(int(short_word, 16))
word = f"!{sprite_addr_list[spr_index]:X}"
except ValueError:
word = f"(${word}&$FFFF)|!bankA"
elif len(word) == 2: # if direct page, ignore
converted = False
word = "$" + word
elif 0x0100 <= numeric_word <= 0x1FFF: # else, use |!addr
word = "$" + word + "|!addr"
elif 0x0000 <= numeric_word <= 0x00FF:
word = "$" + word + "|!dp"
else: # if out of range, ignore
converted = False
requires_manual_conversion = True
word = "$" + word
if len(word) == 4:
stdout.write(
bytes(
f"Warning: address ${int(word, 16):04X} at line {index} "
f"couldn't be converted!\n",
encoding=encoding,
)
)
elif len(word) == 6:
stdout.write(
bytes(
f"Warning: address ${int(word, 16):06X} at line {index} "
f"couldn't be converted!\n",
encoding=encoding,
)
)
else:
stdout.write(
bytes(
f"Warning: address ${int(word, 16):X} at line {index} "
f"couldn't be converted!\n",
encoding=encoding,
)
)
return word, bwram_define_needed, converted, requires_manual_conversion