From fe4b13e304decc48af0918fce281ed588f867a72 Mon Sep 17 00:00:00 2001 From: yenatch Date: Thu, 27 Jun 2013 22:28:53 -0400 Subject: [PATCH 1/9] gbz80disasm: use existing labels as addresses --- extras/gbz80disasm.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index ed546ef98..c1b306cc6 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -587,6 +587,12 @@ 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): # why using a random value when you can use the address? return '.asm_%x' % address @@ -898,10 +904,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] From 3a1f843e783a7a654ccd7e72f5e3fa4a40ea4009 Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 00:10:38 -0400 Subject: [PATCH 2/9] gbz80disasm: only track rom addresses for data --- extras/gbz80disasm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index c1b306cc6..f130fb599 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -603,7 +603,9 @@ 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 From fee0f0d6a4ab194f8aa1ecc1a886cbca22052c6b Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 16:06:39 -0400 Subject: [PATCH 3/9] gbz80disasm: don't read data where there is none --- extras/gbz80disasm.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index f130fb599..08fea8b02 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -550,6 +550,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] @@ -789,12 +791,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) @@ -848,7 +851,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: From 622d6b86f71f29f6a03df7cef7e825ee84260ea9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:16:31 -0400 Subject: [PATCH 4/9] crystal: PointerLabelToScriptPointer bank is not reversed --- extras/crystal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extras/crystal.py b/extras/crystal.py index 680a441e3..3f45e27dc 100644 --- a/extras/crystal.py +++ b/extras/crystal.py @@ -1476,7 +1476,7 @@ class PointerLabelToScriptPointer(PointerLabelParam): 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) From 17b5e86311e5dae4609818d6e1d63d23bc6815fa Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:35:44 -0400 Subject: [PATCH 5/9] crystal: 2pt script commands are for wram --- extras/crystal.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/extras/crystal.py b/extras/crystal.py index 3f45e27dc..0b72397b9 100644 --- a/extras/crystal.py +++ b/extras/crystal.py @@ -2657,10 +2657,10 @@ text_command_classes = inspect.getmembers(sys.modules[__name__], \ 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 @@ pksv_crystal_more = { 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]], From daf3930984690e8ce44b375dda35a9d725c0c1d2 Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:49:50 -0400 Subject: [PATCH 6/9] crystal: wram label finding --- extras/crystal.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/extras/crystal.py b/extras/crystal.py index 0b72397b9..7d607f749 100644 --- a/extras/crystal.py +++ b/extras/crystal.py @@ -7353,15 +7353,15 @@ def write_all_labels(all_labels, filename="labels.json"): 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: From ba5cd8bbb8b96a080e9a6fe1b1525cd5381fdb24 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 2 Jul 2013 12:19:14 -0400 Subject: [PATCH 7/9] use wram from outside extras --- extras/wram.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extras/wram.py b/extras/wram.py index bad0b9fa9..961964403 100644 --- 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 = {} @@ -33,7 +37,7 @@ def read_bss_sections(bss): 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 +60,6 @@ def scrape_constants(text): 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()) From 341e11cccb33a8ed459cd9a6c41cf1189c6a4550 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 3 Jul 2013 21:54:33 -0400 Subject: [PATCH 8/9] labels: hex is case-insensitive --- extras/labels.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extras/labels.py b/extras/labels.py index 61ec4c29a..8b4df1638 100644 --- a/extras/labels.py +++ b/extras/labels.py @@ -31,8 +31,10 @@ def line_has_comment_address(line, returnable={}, bank=None): 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 From 6765083c1cb39a93f2b0fb60a7c203725360492d Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 3 Jul 2013 21:55:46 -0400 Subject: [PATCH 9/9] wram.py: allow space definitions using constants --- extras/wram.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/extras/wram.py b/extras/wram.py index 961964403..85057bee4 100644 --- a/extras/wram.py +++ b/extras/wram.py @@ -23,17 +23,33 @@ def read_bss_sections(bss): '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