diff options
Diffstat (limited to 'gbz80disasm.py')
-rw-r--r-- | gbz80disasm.py | 82 |
1 files changed, 60 insertions, 22 deletions
diff --git a/gbz80disasm.py b/gbz80disasm.py index ed546ef..0382418 100644 --- a/gbz80disasm.py +++ b/gbz80disasm.py @@ -13,6 +13,11 @@ if not hasattr(json, "read"): json.read = json.loads def load_rom(filename="../baserom.gbc"): + """ + Load the specified rom. + + If no rom is given, load "../baserom.gbc". + """ global rom rom = bytearray(open(filename,'rb').read()) return rom @@ -550,6 +555,8 @@ end_08_scripts_with = [ ##0x18, #jr ###0xda, 0xe9, 0xd2, 0xc2, 0xca, 0xc3, 0x38, 0x30, 0x20, 0x28, 0x18, 0xd8, 0xd0, 0xc0, 0xc8, 0xc9 ] + +discrete_jumps = [0xda, 0xe9, 0xd2, 0xc2, 0xca, 0xc3] relative_jumps = [0x38, 0x30, 0x20, 0x28, 0x18, 0xc3, 0xda, 0xc2] relative_unconditional_jumps = [0xc3, 0x18] @@ -557,6 +564,11 @@ call_commands = [0xdc, 0xd4, 0xc4, 0xcc, 0xcd] all_labels = {} def load_labels(filename="labels.json"): + """ + Load labels from specified file. + + If no filename is given, loads 'labels.json'. + """ global all_labels # don't re-load labels each time @@ -587,30 +599,49 @@ def find_label(local_address, bank_id=0): return constants[local_address] return None +def find_address_from_label(label): + for label_entry in all_labels: + if label == label_entry["label"]: + return label_entry["address"] + return None + def asm_label(address): + """ + Return the ASM label using the address. + """ # why using a random value when you can use the address? return '.asm_%x' % address + def data_label(address): return '.data_%x' % address def get_local_address(address): bank = address / 0x4000 return (address & 0x3fff) + 0x4000 * bool(bank) + def get_global_address(address, bank): - return (address & 0x3fff) + 0x4000 * bank + if address < 0x8000: + return (address & 0x3fff) + 0x4000 * bank + return None -def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_address=True, stop_at=[], debug = False): - #fs = current_address - #b = bank_byte - #in = input_data -- rom - #bank_size = byte_count - #i = offset - #ad = end_address - #a, oa = current_byte_number + return ".ASM_" + hex(address)[2:] - # stop_at can be used to supply a list of addresses to not disassemble - # over. This is useful if you know in advance that there are a lot of - # fall-throughs. +def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_address=True, stop_at=[], debug=False): + """ + Output bank opcodes. + + fs = current_address + b = bank_byte + in = input_data -- rom + bank_size = byte_count + i = offset + ad = end_address + a, oa = current_byte_number + + stop_at can be used to supply a list of addresses to not disassemble + over. This is useful if you know in advance that there are a lot of + fall-throughs. + """ load_labels() load_rom() @@ -781,12 +812,13 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add number = byte1 number += byte2 << 8 - pointer = get_global_address(number, bank_id) - if pointer not in data_tables.keys(): - data_tables[pointer] = {} - data_tables[pointer]['usage'] = 0 - else: - data_tables[pointer]['usage'] += 1 + if current_byte not in call_commands + discrete_jumps + relative_jumps: + pointer = get_global_address(number, bank_id) + if pointer not in data_tables.keys(): + data_tables[pointer] = {} + data_tables[pointer]['usage'] = 0 + else: + data_tables[pointer]['usage'] += 1 insertion = "$%.4x" % (number) result = find_label(insertion, bank_id) @@ -840,7 +872,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add keep_reading = False is_data = False #cleanup break - elif offset not in byte_labels.keys() or offset in data_tables.keys(): + elif offset not in byte_labels.keys() and offset in data_tables.keys(): is_data = True keep_reading = True else: @@ -880,8 +912,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add def has_outstanding_labels(byte_labels): """ - If a label is used once in the asm output, then that means it has to be - called or specified later. + Check whether a label is used once in the asm output. + + If so, then that means it has to be called or specified later. """ for label_line in byte_labels.keys(): real_line = byte_labels[label_line] @@ -898,10 +931,15 @@ def all_outstanding_labels_are_reverse(byte_labels, offset): if __name__ == "__main__": + load_labels() addr = sys.argv[1] if ":" in addr: addr = addr.split(":") addr = int(addr[0], 16)*0x4000+(int(addr[1], 16)%0x4000) else: - addr = int(addr, 16) + label_addr = find_address_from_label(addr) + if label_addr: + addr = label_addr + else: + addr = int(addr, 16) print output_bank_opcodes(addr)[0] |