From 117c433dc2cea58a18e7dfba5c1e0c5860136f78 Mon Sep 17 00:00:00 2001 From: yenatch Date: Thu, 27 Jun 2013 22:28:53 -0400 Subject: gbz80disasm: use existing labels as addresses original-commit-id: fe4b13e304decc48af0918fce281ed588f867a72 --- gbz80disasm.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/gbz80disasm.py b/gbz80disasm.py index ed546ef..c1b306c 100644 --- a/gbz80disasm.py +++ b/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] -- cgit v1.2.3 From dbaaef11fb1d5dab0c627e2c9340d860f766dc7c Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 00:10:38 -0400 Subject: gbz80disasm: only track rom addresses for data original-commit-id: 3a1f843e783a7a654ccd7e72f5e3fa4a40ea4009 --- gbz80disasm.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gbz80disasm.py b/gbz80disasm.py index c1b306c..f130fb5 100644 --- a/gbz80disasm.py +++ b/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 -- cgit v1.2.3 From 8ae5e00188a832a7b95b3c8a202aa51b709aaafc Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 16:06:39 -0400 Subject: gbz80disasm: don't read data where there is none original-commit-id: fee0f0d6a4ab194f8aa1ecc1a886cbca22052c6b --- gbz80disasm.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/gbz80disasm.py b/gbz80disasm.py index f130fb5..08fea8b 100644 --- a/gbz80disasm.py +++ b/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: -- cgit v1.2.3 From 42cdc63dff19a6bfaf8fd4e7b875c8b46680cd62 Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:16:31 -0400 Subject: crystal: PointerLabelToScriptPointer bank is not reversed original-commit-id: 622d6b86f71f29f6a03df7cef7e825ee84260ea9 --- crystal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crystal.py b/crystal.py index 680a441..3f45e27 100644 --- a/crystal.py +++ b/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) -- cgit v1.2.3 From 3c2125a7fc37c0bebfec65f8b231c1e5f7c66c69 Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:35:44 -0400 Subject: crystal: 2pt script commands are for wram original-commit-id: 17b5e86311e5dae4609818d6e1d63d23bc6815fa --- crystal.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crystal.py b/crystal.py index 3f45e27..0b72397 100644 --- a/crystal.py +++ b/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]], -- cgit v1.2.3 From 5ee3ead5fadc4bc39cd6f556bbda63eb217efafd Mon Sep 17 00:00:00 2001 From: yenatch Date: Fri, 28 Jun 2013 21:49:50 -0400 Subject: crystal: wram label finding original-commit-id: daf3930984690e8ce44b375dda35a9d725c0c1d2 --- crystal.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crystal.py b/crystal.py index 0b72397..7d607f7 100644 --- a/crystal.py +++ b/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: -- cgit v1.2.3 From 936c80e75e79222fa90c963d4c6daf5751d541c3 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 2 Jul 2013 12:19:14 -0400 Subject: use wram from outside extras original-commit-id: ba5cd8bbb8b96a080e9a6fe1b1525cd5381fdb24 --- wram.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/wram.py b/wram.py index bad0b9f..9619644 100644 --- a/wram.py +++ b/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()) -- cgit v1.2.3 From 101c57b58d46048f1b76ce7522df63ead6d3289b Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 3 Jul 2013 21:54:33 -0400 Subject: labels: hex is case-insensitive original-commit-id: 341e11cccb33a8ed459cd9a6c41cf1189c6a4550 --- labels.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/labels.py b/labels.py index 61ec4c2..8b4df16 100644 --- a/labels.py +++ b/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 -- cgit v1.2.3 From f2717519a86edfde72d519d707c50ad08d146255 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 3 Jul 2013 21:55:46 -0400 Subject: wram.py: allow space definitions using constants original-commit-id: 6765083c1cb39a93f2b0fb60a7c203725360492d --- wram.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/wram.py b/wram.py index 9619644..85057be 100644 --- a/wram.py +++ b/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 -- cgit v1.2.3