shithub: pokered

Download patch

ref: 00229b58b45fd0c3446ca1a6013da7e1537650ab
parent: b64df5801662b253ff4b0943ea97003e961e7fd0
author: Bryan Bishop <kanzure@gmail.com>
date: Mon Jan 23 11:22:05 EST 2012

gbz80disasm now prints out known labels

hg-commit-id: f54b2dfb9512


--- a/.hgignore
+++ b/.hgignore
@@ -18,6 +18,9 @@
 #swap files for vim
 .*.swp
 
+#no data from extras/
+extras/*.json
+
 #for any of the poor souls with save game files in their working directory
 baserom.sgm
 baserom.sav
@@ -27,5 +30,3 @@
 #for vim configuration
 #url: http://www.vim.org/scripts/script.php?script_id=441
 .lvimrc
-
-*.exe
\ No newline at end of file
--- 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 @@
 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 @@
                 local_pointer = hex((address % 0x4000) + 0x4000).replace("0x", "$")
 
         print line_label + " is at " + hex(address)
+        
+        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
-   
+        current_line_offset += 1 
     label_errors += errors
     return labels
 
@@ -364,6 +373,7 @@
     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 @@
         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
--- 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,25 +554,33 @@
 #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 @@
     #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 @@
 
                     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)