-
Notifications
You must be signed in to change notification settings - Fork 4
/
disass.py
executable file
·398 lines (301 loc) · 11 KB
/
disass.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
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
#!/usr/local/bin/python3
#
# PyDisAss6502 by Ingo Hinterding 2021
# A disassembler for converting 6502 machine code binaries into assembly code
#
# https://github.com/Esshahn/pydisass64
#
import json
import argparse
import os
import sys
def load_json(filename):
with open(filename) as f:
data = json.load(f)
return data
def load_file(filename):
"""
input: filename
returns: startaddress (int), bytes
"""
bytecode = []
file = open(filename, "rb")
byte = file.read(1)
while byte:
i = int.from_bytes(byte, byteorder='big')
byte = file.read(1)
bytecode.append(i)
file.close()
startaddress = (bytecode[1] << 8) + bytecode[0]
bytecode = bytecode[2:] # remove first 2 bytes
print("loading: "+filename)
return(startaddress, bytecode)
def save_file(filename, data):
f = open(filename, "w")
f.write(data)
f.close()
print("saving: "+filename)
def number_to_hex_byte(number):
return ("0" + hex(number)[2:])[-2:]
def hex_to_number(hex):
return (int(hex, 16))
def number_to_hex_word(number):
return ("0" + hex(number)[2:])[-4:]
def bytes_to_addr(hh, ll):
""" takes two hex bytes e.g. d0, 20 and returns their int address e.g. 53280"""
return (int(hh, 16) << 8) + int(ll, 16)
def print_bytes_as_hex(bytes):
for byte in bytes:
print(number_to_hex_byte(byte), end=" ")
print()
def print_bytes_array(bytes_table):
for byte in bytes_table:
print(byte)
def addr_in_program(addr, startaddr, endaddr):
return True if addr >= startaddr and addr < endaddr else False
def get_abs_from_relative(byte, addr):
"""
expects a byte and an absolute address and returns
the absolute address for a relative branching
e.g. for a BNE command
"""
int_byte = hex_to_number(byte)
if int_byte > 127:
# substract (255 - highbyte) from current address
address = addr - (255 - int_byte)
else:
# add highbyte to current address
address = addr + int_byte + 1
return address
def convert_to_program(byte_array, opcodes, mapping, outputfile):
"""formats the assembly code so it can be saved as a program"""
program = "; converted with pydisass6502 by awsm of mayday!"
program += "\n\n* = $" + number_to_hex_word(byte_array[0]["addr"]) + "\n\n"
label_prefix = "l"
end = len(byte_array)
startaddr = byte_array[0]["addr"]
endaddr = startaddr + end
previous_was_data = False # used to collect data bytes in one line
i = 0
while i < end:
# set defaults
label = ""
comment = ""
line_break = "\n"
spaces = 12 * " "
byte = byte_array[i]["byte"]
if byte_array[i]["dest"]:
label = label_prefix + number_to_hex_word(byte_array[i]["addr"])
# mark everything as data that is not explicity set as code
if not byte_array[i]["code"] or byte_array[i]["data"]:
line_break = ""
spaces = ""
if not previous_was_data:
ins = "!byte $"+byte
previous_was_data = True
else:
ins = ", $"+byte
if byte_array[i]["code"]:
previous_was_data = False
opcode = opcodes[byte]
ins = opcode["ins"]
length = get_instruction_length(ins)
if length == 1:
i += 1
high_byte = byte_array[i]["byte"]
int_byte = hex_to_number(high_byte)
# if a relative instruction like BCC or BNE occurs
if "rel" in opcode:
address = number_to_hex_word(
get_abs_from_relative(high_byte, startaddr+i))
ins = ins.replace("$hh", label_prefix + address)
else:
ins = ins.replace("hh", number_to_hex_byte(int_byte))
if length == 2:
i += 1
low_byte = byte_array[i]["byte"]
i += 1
high_byte = byte_array[i]["byte"]
ins = ins.replace("hh", high_byte)
ins = ins.replace("ll", low_byte)
addr = bytes_to_addr(high_byte, low_byte)
# turn absolute address into label if it is within the program code
if addr_in_program(addr, startaddr, endaddr):
ins = ins.replace("$", label_prefix)
if mapping:
# add comments from mapping file
for address in mapping["mapping"]:
if address["addr"] == number_to_hex_word(addr):
comment = address["comm"]
if label:
program += "\n\n" + label + "\n"
if comment:
comment = (32 - len(ins)) * " " + "; " + comment
program += spaces + ins + comment + line_break
i += 1
save_file(outputfile, program)
def get_instruction_length(opcode):
length = 0
if "hh" in opcode:
length += 1
if "ll" in opcode:
length += 1
return length
def generate_byte_array(startaddr, bytes):
""" generates an empty data array for later usage """
bytes_table = []
pc = 0
end = len(bytes)
# generate the object to host all analytics data
while pc < end:
byte = bytes[pc]
bytes_table.append(
{
"addr": startaddr + pc, # address of byte in memory
"byte": number_to_hex_byte(byte),
"dest": 0, # is it the destination of a jump or branch instruction
"code": 0, # is it marked as code?
"data": 0 # is it marked as data?
}
)
pc += 1
return bytes_table
def analyze(startaddr, bytes, opcodes, entrypoints):
bytes_table = generate_byte_array(startaddr, bytes)
# JMP RTS RTI
# used to default back to data for the following instructions
default_to_data_after = ["4c", "60", "40"]
# JMP JSR
# used to identify code sections in the code
abs_branch_mnemonics = ["4c", "20"]
# LDA STA
# used to identify data sections in the code
abs_address_mnemonics = [
"0d", "0e",
"19", "1d", "1e",
"2d", "2e",
"39", "3d", "3e",
"4d", "4e",
"59", "5d", "5e",
"6d", "6e",
"79", "7d", "7e",
"8c", "8d", "8e",
"99", "9d,",
"ac", "ad", "ae",
"b9", "bc", "bd", "be",
"cc", "cd", "ce",
"d9", "dd", "de",
"ec", "ee", "ed",
"f9", "fd", "fe"
]
# our entrypoint is assumed to be code
is_code = 1
is_data = 0
end = len(bytes_table)
if entrypoints:
# add all override entry points from the json file before doing anything else
for entrypoint in entrypoints["entrypoints"]:
addr_int = hex_to_number(entrypoint["addr"])
table_pos = addr_int - startaddr
bytes_table[table_pos]["dest"] = 1
if entrypoint["mode"] == "code":
bytes_table[table_pos]["code"] = 1
bytes_table[table_pos]["data"] = 0
if entrypoint["mode"] == "data":
bytes_table[table_pos]["data"] = 1
bytes_table[table_pos]["code"] = 0
i = 0
while i < end:
byte = bytes_table[i]["byte"]
opcode = opcodes[byte]
hex_address = number_to_hex_word(bytes_table[i]["addr"])
if bytes_table[i]["data"]:
is_data = 1
if bytes_table[i]["code"]:
is_code = 1
if is_code:
# set code
bytes_table[i]["code"] = 1
instruction_length = get_instruction_length(opcode["ins"])
if byte in default_to_data_after:
# we have a branching/jumping instruction, so we can't be sure anymore
# about the following bytes being code
is_code = 0
if "rel" in opcode:
# if the instruction is relative, we have to calculate the
# absolute branching address to add a label later below
destination_address = get_abs_from_relative(
bytes_table[i+1]["byte"], startaddr+i+1)
if instruction_length == 2:
# this is the absolute address
destination_address = bytes_to_addr(
bytes_table[i+2]["byte"], bytes_table[i+1]["byte"])
if byte in abs_branch_mnemonics or "rel" in opcode:
if addr_in_program(destination_address, startaddr, startaddr + end):
# the hhll address must be code, so we mark that entry in the array
table_pos = destination_address - startaddr
bytes_table[table_pos]["code"] = 1
bytes_table[table_pos]["dest"] = 1
if byte in abs_address_mnemonics:
if addr_in_program(destination_address, startaddr, startaddr + end):
# the hhll address must be data, so we mark that entry in the array
table_pos = destination_address - startaddr
bytes_table[table_pos]["data"] = 1
bytes_table[table_pos]["dest"] = 1
i += instruction_length
i += 1
return bytes_table
#
#
# command line interface
#
#
# Create the parser
my_parser = argparse.ArgumentParser(
description='disassembles a 6502 machine code binary file into assembly source.',
epilog='Example: disass.py game.prg game.asm -e')
# Add the arguments
my_parser.add_argument('-i', '--input',
required=True,
help='name of the input binary, e.g. game.prg'
)
my_parser.add_argument('-o', '--output',
help='name of the generated assembly file, e.g. game.asm.')
my_parser.add_argument('-e', '--entrypoints',
help="use entrypoints.json")
my_parser.add_argument('-nc', '--nocomments', action='store_true',
help="do not add any comments to the output file")
# Execute the parse_args() method
args = my_parser.parse_args()
#
#
# file conversion
#
#
dir_path = os.path.dirname(os.path.realpath(__file__))
print("\x1b[33;21m")
# load the opcodes list
opcodes = load_json(dir_path + "/lib/opcodes.json")
if args.output:
output = args.output
else:
output = str(args.input + ".asm")
if args.entrypoints:
entrypoints = load_json(args.entrypoints)
print("using entrypoints file: " + args.entrypoints)
else:
entrypoints = False
if args.nocomments:
mapping = False
print("omitting comments")
else:
mapping = load_json(dir_path + "/lib/c64-mapping.json")
print("using comments")
# load prg
startaddress, bytes = load_file(args.input)
# print_bytes_as_hex(bytes)
# turn bytes into asm code
byte_array = analyze(startaddress, bytes, opcodes, entrypoints)
# print_bytes_array(byte_array)
# convert it into a readable format
convert_to_program(byte_array, opcodes, mapping, output)