diff options
author | Bryan Bishop <kanzure@gmail.com> | 2012-01-23 16:22:05 -0600 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2012-01-23 16:22:05 -0600 |
commit | 00229b58b45fd0c3446ca1a6013da7e1537650ab (patch) | |
tree | c9dd367bc63f99f31325b35834b2305f41bdd349 /extras | |
parent | b64df5801662b253ff4b0943ea97003e961e7fd0 (diff) |
gbz80disasm now prints out known labels
hg-commit-id: f54b2dfb9512
Diffstat (limited to 'extras')
-rw-r--r-- | extras/analyze_incbins.py | 29 | ||||
-rw-r--r-- | extras/gbz80disasm.py | 55 |
2 files changed, 59 insertions, 25 deletions
diff --git a/extras/analyze_incbins.py b/extras/analyze_incbins.py index 497985c1..ef5405ef 100644 --- a/extras/analyze_incbins.py +++ b/extras/analyze_incbins.py @@ -6,6 +6,7 @@ import sys, os from copy import copy, deepcopy import subprocess +import json from extract_maps import rom, assert_rom, load_rom, calculate_pointer, load_map_pointers, read_all_map_headers, map_headers try: @@ -259,7 +260,7 @@ def is_probably_pointer(input): label_errors = "" def get_labels_between(start_line_id, end_line_id, bank_id): labels = [] - #line = { + #label = { # "line_number": 15, # "bank_id": 32, # "label": "PalletTownText1", @@ -345,9 +346,17 @@ def get_labels_between(start_line_id, end_line_id, bank_id): local_pointer = hex((address % 0x4000) + 0x4000).replace("0x", "$") print line_label + " is at " + hex(address) - - current_line_offset += 1 - + + label = { + "line_number": line_id, + "bank_id": bank_id, + "label": line_label, + "local_pointer": local_pointer, + "address": address + } + labels.append(label) + + current_line_offset += 1 label_errors += errors return labels @@ -364,6 +373,7 @@ def scan_for_predefined_labels(): to grab all label addresses better than this script.. """ bank_intervals = {} + all_labels = [] #figure out line numbers for each bank for bank_id in range(0x2d): @@ -394,7 +404,16 @@ def scan_for_predefined_labels(): end_line_id = bank_data["end"] labels = get_labels_between(start_line_id, end_line_id, bank_id) - bank_intervals[bank_id]["labels"] = labels + #bank_intervals[bank_id]["labels"] = labels + all_labels.extend(labels) + + write_all_labels(all_labels) + return all_labels + +def write_all_labels(all_labels): + fh = open("labels.json", "w") + fh.write(json.dumps(all_labels)) + fh.close() if __name__ == "__main__": #load map headers diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 6f9011dc..3769c2a6 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -2,6 +2,8 @@ #author: Bryan Bishop <kanzure@gmail.com> #date: 2012-01-09 import extract_maps +import os +import json from copy import copy, deepcopy from pretty_map_headers import random_hash, map_name_cleaner from ctypes import c_int8 @@ -552,24 +554,32 @@ relative_unconditional_jumps = [0xc3, 0x18] #TODO: replace call and a pointer with call and a label call_commands = [0xdc, 0xd4, 0xc4, 0xcc, 0xcd] -asm_commands = { - "3c49": "PrintText", - "35d6": "Bankswitch", - "3927": "AddPokemonToParty", - "3e48": "GivePokemon", - "3dd7": "Delay3", - "3e2e": "GiveItem", - "2f9e": "GetMonName", - "3e6d": "Predef", #library of pre-defined asm routines - "00b5": "CopyData", - "2ff3": "GetMachineName", - "24d7": "TextScriptEnd", - "3e5c": "GenRandom", #bank 4 - "6581": "ItemUseNotTime", - "3a87": "AddNTimes", - "3dab": "IsInArray", #bank 3 - "039e": "HandleMidJump", -} + +all_labels = {} + +def load_labels(filename="labels.json"): + global all_labels + if os.path.exists(filename): + all_labels = json.loads(open(filename, "r").read()) + else: + print "You must run analyze_incbins.scan_for_predefined_labels() to create \"labels.json\"." +load_labels() + +def find_label(local_address, bank_id=0): + global all_labels + + #turn local_address into a string + if type(local_address) == str: + if "0x" in local_address: local_address = local_address.replace("0x", "$") + elif not "$" in local_address: local_address = "$" + local_address + if type(local_address) == int: + local_address = "$%.2x" % (local_address) + local_address = local_address.upper() + + for label_entry in all_labels: + if label_entry["local_pointer"].upper() == local_address: + return label_entry["label"] + return None def random_asm_label(): return ".ASM_" + random_hash() @@ -587,6 +597,10 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000): #ad = end_address #a, oa = current_byte_number + bank_id = 0 + if original_offset > 0x4000: + bank_id = original_offset / 0x4000 + last_hl_address = None #for when we're scanning the main map script last_a_address = None used_3d97 = False @@ -726,8 +740,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000): insertion = "$%.4x" % (number) if maybe_byte in call_commands or current_byte in relative_unconditional_jumps or current_byte in relative_jumps: - if insertion[1:] in asm_commands: - insertion = asm_commands[insertion[1:]] + result = find_label(insertion[1:], bank_id) + if result != None: + insertion = result opstr = opstr[:opstr.find("?")].lower() + insertion + opstr[opstr.find("?")+1:].lower() output += spacing + opstr #+ " ; " + hex(offset) |