shithub: pokecrystal

Download patch

ref: 226729d1750bc6ee0c45d96e3f6b7dcf0844a609
parent: 9b71315d7b42da07b6c5104a93f4f5ca9702a5fb
parent: 6765083c1cb39a93f2b0fb60a7c203725360492d
author: Bryan Bishop <kanzure@gmail.com>
date: Thu Jul 11 05:01:02 EDT 2013

Merge pull request #158 from yenatch/gbz80disasm-more-data

gbz80disasm bugfixes

--- a/extras/crystal.py
+++ b/extras/crystal.py
@@ -1476,7 +1476,7 @@
     def parse(self):
         PointerLabelParam.parse(self)
         address = calculate_pointer_from_bytes_at(self.parsed_address, bank=self.bank)
-        address2 = calculate_pointer_from_bytes_at(address, bank="reverse") # maybe not "reverse"?
+        address2 = calculate_pointer_from_bytes_at(address, bank=True)
         self.script = parse_script_engine_script_at(address2, origin=False, map_group=self.map_group, map_id=self.map_id, force=self.force, debug=self.debug)
 
 
@@ -2657,10 +2657,10 @@
 pksv_crystal_more = {
     0x00: ["2call", ["pointer", ScriptPointerLabelParam]],
     0x01: ["3call", ["pointer", ScriptPointerLabelBeforeBank]],
-    0x02: ["2ptcall", ["pointer", PointerLabelToScriptPointer]],
+    0x02: ["2ptcall", ["pointer", RAMAddressParam]],
     0x03: ["2jump", ["pointer", ScriptPointerLabelParam]],
     0x04: ["3jump", ["pointer", ScriptPointerLabelBeforeBank]],
-    0x05: ["2ptjump", ["pointer", PointerLabelToScriptPointer]],
+    0x05: ["2ptjump", ["pointer", RAMAddressParam]],
     0x06: ["if equal", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
     0x07: ["if not equal", ["byte", SingleByteParam], ["pointer", ScriptPointerLabelParam]],
     0x08: ["iffalse", ["pointer", ScriptPointerLabelParam]],
@@ -2671,7 +2671,7 @@
     0x0D: ["callstd", ["predefined_script", MultiByteParam]],
     0x0E: ["3callasm", ["asm", AsmPointerParam]],
     0x0F: ["special", ["predefined_script", MultiByteParam]],
-    0x10: ["2ptcallasm", ["asm", PointerToAsmPointerParam]],
+    0x10: ["2ptcallasm", ["asm", RAMAddressParam]],
     # should map_group/map_id be dealt with in some special way in the asm?
     0x11: ["checkmaptriggers", ["map_group", SingleByteParam], ["map_id", SingleByteParam]],
     0x12: ["domaptrigger", ["map_group", MapGroupParam], ["map_id", MapIdParam], ["trigger_id", SingleByteParam]],
@@ -7353,15 +7353,15 @@
     fh.close()
     return True
 
-# TODO: implement get_ram_label
-# wram.asm integration would be nice
+from wram import wram_labels
 def get_ram_label(address):
-    """not implemented yet.. supposed to get a label for a particular RAM location
-    like W_PARTYPOKE1HP"""
+    """returns a label assigned to a particular ram address"""
+    if address in wram_labels.keys():
+        return wram_labels[address][-1]
     return None
 
 def get_label_for(address):
-    """returns a label assigned to a particular address"""
+    """returns a label assigned to a particular rom address"""
     global all_labels
 
     if address == None:
--- a/extras/gbz80disasm.py
+++ b/extras/gbz80disasm.py
@@ -555,6 +555,8 @@
 ##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]
 
@@ -597,6 +599,12 @@
             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.
@@ -612,7 +620,9 @@
     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
 
     return ".ASM_" + hex(address)[2:]
 
@@ -802,12 +812,13 @@
                     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)
@@ -861,7 +872,7 @@
                 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:
@@ -920,10 +931,15 @@
 
 
 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]
--- a/extras/labels.py
+++ b/extras/labels.py
@@ -31,8 +31,10 @@
     returnable["bank"] = None
     returnable["offset"] = None
     returnable["address"] = None
-    #only valid characters are 0-9A-F
-    valid = [str(x) for x in range(0,10)] + [chr(x) for x in range(97, 102+1)]
+    #only valid characters are 0-9a-fA-F
+    valid = [str(x) for x in range(10)] + \
+            [chr(x) for x in range(ord('a'), ord('f')+1)] + \
+            [chr(x) for x in range(ord('A'), ord('F')+1)]
     #check if there is a comment in this line
     if ";" not in line:
         return False
--- a/extras/wram.py
+++ b/extras/wram.py
@@ -2,6 +2,10 @@
 
 # RGBDS BSS section and constant parsing.
 
+import os
+path = os.path.dirname(os.path.abspath(__file__))
+
+
 def read_bss_sections(bss):
 	sections = []
 	section = {}
@@ -19,21 +23,37 @@
 				'start': address,
 				'labels': [],
 			}
+
 		elif ':' in line:
-			# the only labels that don't use :s so far are enders,
-			# which we typically don't want to end up in the output
+			# rgbds allows labels without :, but prefer convention
 			label = line[:line.find(':')]
 			if ';' not in label:
-				section['labels'] += [{'label': label, 'address': address, 'length': 0}]
+				section['labels'] += [{
+					'label': label,
+					'address': address,
+					'length': 0,
+				}]
+
 		elif line[:3] == 'ds ':
 			length = eval(line[3:line.find(';')].replace('$','0x'))
 			address += length
-			if section['labels']:
-				section['labels'][-1]['length'] += length
+			# adjacent labels use the same space
+			for label in section['labels'][::-1]:
+				if label['length'] == 0:
+					label['length'] = length
+				else:
+					break
+
+		elif 'EQU' in line:
+			# some space is defined using constants
+			name, value = line.split('EQU')
+			name, value = name.strip(), value.strip().replace('$','0x').replace('%','0b')
+			globals()[name] = eval(value)
+
 	sections.append(section)
 	return sections
 
-wram_sections = read_bss_sections(open('../wram.asm', 'r').readlines())
+wram_sections = read_bss_sections(open(os.path.join(path, '../wram.asm'), 'r').readlines())
 
 
 def make_wram_labels():
@@ -56,6 +76,6 @@
 		text = text.split('\n')
 	return constants_to_dict([line for line in text if 'EQU' in line[:line.find(';')]])
 
-hram_constants = scrape_constants(open('../hram.asm','r').readlines())
-gbhw_constants = scrape_constants(open('../gbhw.asm','r').readlines())
+hram_constants = scrape_constants(open(os.path.join(path, '../hram.asm'),'r').readlines())
+gbhw_constants = scrape_constants(open(os.path.join(path, '../gbhw.asm'),'r').readlines())
 
--