From b9bd11e34def340afdc7befc29fcca15e3bf71cb Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 13:38:26 -0400 Subject: fix bank-checking for labels in gbz80disasm bank 1 is not fixed like bank 0 --- extras/gbz80disasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index d57a1905a..b476654b7 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -584,7 +584,7 @@ def find_label(local_address, bank_id=0): for label_entry in all_labels: if label_entry["address"] == local_address: - if label_entry["bank"] == bank_id or (local_address1 < 0x8000 and (label_entry["bank"] == 0 or label_entry["bank"] == 1)): + if label_entry["bank"] == bank_id or label_entry["bank"] == 0: return label_entry["label"] return None -- cgit v1.2.3 From 313e853e391e69f9bca875fa37f431c84fb32cbe Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 13:56:34 -0400 Subject: remove redundant code from find_label in gbz80disasm --- extras/gbz80disasm.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index b476654b7..32edf0096 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -569,18 +569,7 @@ def find_label(local_address, bank_id=0): # keep an integer if type(local_address) == str: - local_address1 = int(local_address.replace("$", "0x"), 16) - else: local_address1 = local_address - - # turn local_address into an integer - if type(local_address) == str: - if "0x" in local_address: - local_address = local_address.replace("0x", "$") - elif "$" in local_address: - local_address = local_address.replace("$", "") - - if type(local_address) == str: - local_address = int(local_address, 16) + local_address = int(local_address.replace("$", "0x"), 16) for label_entry in all_labels: if label_entry["address"] == local_address: -- cgit v1.2.3 From 5b95c5aad7d840a09727d3d56626ba624cddbbf3 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 14:37:40 -0400 Subject: transition gbz80disasm to use a bytearray instead of RomStr --- extras/gbz80disasm.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 32edf0096..325f200e4 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -11,11 +11,9 @@ import json if not hasattr(json, "read"): json.read = json.loads -from romstr import RomStr - def load_rom(filename="../baserom.gbc"): global rom - rom = RomStr.load(filename=filename) + rom = bytearray(open(filename,'rb').read()) return rom spacing = "\t" @@ -619,7 +617,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add output = "" keep_reading = True while offset <= end_address and keep_reading: - current_byte = ord(rom[offset]) + current_byte = rom[offset] is_data = False maybe_byte = current_byte @@ -644,13 +642,13 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add #find out if there's a two byte key like this temp_maybe = maybe_byte - temp_maybe += ( ord(rom[offset+1]) << 8) - if temp_maybe in opt_table.keys() and ord(rom[offset+1])!=0: + temp_maybe += ( rom[offset+1] << 8) + if temp_maybe in opt_table.keys() and rom[offset+1]!=0: opstr = opt_table[temp_maybe][0].lower() if "x" in opstr: for x in range(0, opstr.count("x")): - insertion = ord(rom[offset + 1]) + insertion = rom[offset + 1] insertion = "$" + hex(insertion)[2:] opstr = opstr[:opstr.find("x")].lower() + insertion + opstr[opstr.find("x")+1:].lower() @@ -659,8 +657,8 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add offset += 1 if "?" in opstr: for y in range(0, opstr.count("?")): - byte1 = ord(rom[offset + 1]) - byte2 = ord(rom[offset + 2]) + byte1 = rom[offset + 1] + byte2 = rom[offset + 2] number = byte1 number += byte2 << 8; @@ -684,7 +682,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add #type = -1 when it's the E op #if op_code_type != -1: - if op_code_type == 0 and ord(rom[offset]) == op_code_byte: + if op_code_type == 0 and rom[offset] == op_code_byte: op_str = op_code[0].lower() output += spacing + op_code[0].lower() #+ " ; " + hex(offset) @@ -692,18 +690,18 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add offset += 1 current_byte_number += 1 - elif op_code_type == 1 and ord(rom[offset]) == op_code_byte: + elif op_code_type == 1 and rom[offset] == op_code_byte: oplen = len(op_code[0]) opstr = copy(op_code[0]) xes = op_code[0].count("x") include_comment = False for x in range(0, xes): - insertion = ord(rom[offset + 1]) + insertion = rom[offset + 1] insertion = "$" + hex(insertion)[2:] if current_byte == 0x18 or current_byte==0x20 or current_byte in relative_jumps: #jr or jr nz #generate a label for the byte we're jumping to - target_address = offset + 2 + c_int8(ord(rom[offset + 1])).value + target_address = offset + 2 + c_int8(rom[offset + 1]).value if target_address in byte_labels.keys(): byte_labels[target_address]["usage"] = 1 + byte_labels[target_address]["usage"] line_label2 = byte_labels[target_address]["name"] @@ -717,7 +715,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add insertion = line_label2.lower() include_comment = True elif current_byte == 0x3e: - last_a_address = ord(rom[offset + 1]) + last_a_address = rom[offset + 1] opstr = opstr[:opstr.find("x")].lower() + insertion + opstr[opstr.find("x")+1:].lower() @@ -737,7 +735,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add if include_comment: output += " ; " + hex(offset) if current_byte in relative_jumps: - output += " $" + hex(ord(rom[offset + 1]))[2:] + output += " $" + hex(rom[offset + 1])[2:] output += "\n" current_byte_number += 1 @@ -747,13 +745,13 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add current_byte_number += 1 offset += 1 include_comment = False - elif op_code_type == 2 and ord(rom[offset]) == op_code_byte: + elif op_code_type == 2 and rom[offset] == op_code_byte: oplen = len(op_code[0]) opstr = copy(op_code[0]) qes = op_code[0].count("?") for x in range(0, qes): - byte1 = ord(rom[offset + 1]) - byte2 = ord(rom[offset + 2]) + byte1 = rom[offset + 1] + byte2 = rom[offset + 2] number = byte1 number += byte2 << 8; @@ -805,7 +803,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add keep_reading = True else: #if is_data and keep_reading: - output += spacing + "db $" + hex(ord(rom[offset]))[2:] #+ " ; " + hex(offset) + output += spacing + "db $" + hex(rom[offset])[2:] #+ " ; " + hex(offset) output += "\n" offset += 1 current_byte_number += 1 -- cgit v1.2.3 From aed0202999656cf4cafd26c52bd98d9fcf5e63d4 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 18:18:07 -0400 Subject: gbz80disasm: bank 1 was being read as bank 0 --- extras/gbz80disasm.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 325f200e4..1026572b8 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -595,9 +595,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add load_labels() load_rom() - bank_id = 0 - if original_offset > 0x8000: - bank_id = original_offset / 0x4000 + bank_id = original_offset / 0x4000 if debug: print "bank id is: " + str(bank_id) last_hl_address = None #for when we're scanning the main map script -- cgit v1.2.3 From 1bdcac1fe11746b67f17285d2f04e3b9c52772d1 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 21:01:11 -0400 Subject: gbz80disasm: read labels from wram/gbhw/hram --- extras/gbz80disasm.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 1026572b8..2e800bbbd 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -6,6 +6,7 @@ from copy import copy, deepcopy from ctypes import c_int8 import random import json +from wram import * # New versions of json don't have read anymore. if not hasattr(json, "read"): @@ -563,16 +564,20 @@ def load_labels(filename="labels.json"): crystal.scan_for_predefined_labels() def find_label(local_address, bank_id=0): - global all_labels - # keep an integer if type(local_address) == str: local_address = int(local_address.replace("$", "0x"), 16) - for label_entry in all_labels: - if label_entry["address"] == local_address: - if label_entry["bank"] == bank_id or label_entry["bank"] == 0: - return label_entry["label"] + if local_address < 0x8000: + for label_entry in all_labels: + if label_entry["address"] == local_address: + if label_entry["bank"] == bank_id or label_entry["bank"] == 0: + return label_entry["label"] + if local_address in wram_labels.keys(): + return wram_labels[local_address][-1] + for constants in [gbhw_constants, hram_constants]: + if local_address in constants.keys(): + return constants[local_address] return None def asm_label(address): @@ -726,6 +731,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add second_num = "0x"+second_orig second_val = int(second_num, 16) combined_val = "$" + hex(first_val + second_val)[2:] + result = find_label(combined_val, bank_id) + if result != None: + combined_val = result replacetron = "[$"+first_orig+"+$"+second_orig+"]" opstr = opstr.replace(replacetron, "["+combined_val+"]") @@ -752,13 +760,12 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add byte2 = rom[offset + 2] number = byte1 - number += byte2 << 8; + number += byte2 << 8 insertion = "$%.4x" % (number) - if maybe_byte in call_commands or current_byte in relative_unconditional_jumps or current_byte in relative_jumps: - result = find_label(insertion, bank_id) - if result != None: - insertion = result + result = find_label(insertion, bank_id) + if result != None: + insertion = result opstr = opstr[:opstr.find("?")].lower() + insertion + opstr[opstr.find("?")+1:].lower() output += spacing + opstr #+ " ; " + hex(offset) -- cgit v1.2.3 From 82d723840b14fc6b55c00f107a453bc0b0d80772 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 21:21:26 -0400 Subject: gbz80disasm: clean up $ff00+x handling --- extras/gbz80disasm.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 2e800bbbd..dd69be2e4 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -723,19 +723,20 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add opstr = opstr[:opstr.find("x")].lower() + insertion + opstr[opstr.find("x")+1:].lower() # because the $ff00+$ff syntax is silly - if opstr.count("$") > 0 and "+" in opstr: - first_orig = opstr.split("$")[1].split("+")[0] - first_num = "0x"+first_orig - first_val = int(first_num, 16) - second_orig = opstr.split("+$")[1].split("]")[0] - second_num = "0x"+second_orig - second_val = int(second_num, 16) - combined_val = "$" + hex(first_val + second_val)[2:] + if opstr.count("$") > 1 and "+" in opstr: + first_orig = opstr[opstr.find("$"):opstr.find("+")] + first_val = eval(first_orig.replace("$","0x")) + + second_orig = opstr[opstr.find("+$")+1:opstr.find("]")] + second_val = eval(second_orig.replace("$","0x")) + + combined_val = "$%.4x" % (first_val + second_val) result = find_label(combined_val, bank_id) if result != None: combined_val = result - replacetron = "[$"+first_orig+"+$"+second_orig+"]" - opstr = opstr.replace(replacetron, "["+combined_val+"]") + + replacetron = "[%s+%s]" % (first_orig, second_orig) + opstr = opstr.replace(replacetron, "[%s]" % combined_val) output += spacing + opstr if include_comment: -- cgit v1.2.3 From 3cf6603b455530fa0c17e946118484a0126e2bf9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 21:50:43 -0400 Subject: gbz80disasm: space out blocks of asm --- extras/gbz80disasm.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index dd69be2e4..354490e4f 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -635,6 +635,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add if offset in byte_labels.keys(): line_label = byte_labels[offset]["name"] byte_labels[offset]["usage"] += 1 + output += "\n" else: line_label = asm_label(offset) byte_labels[offset] = {} @@ -819,6 +820,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add #offset += 1 #current_byte_number += 1 + if current_byte in relative_unconditional_jumps + end_08_scripts_with: + output += "\n" + first_loop = False #clean up unused labels @@ -828,6 +832,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add if label_line["usage"] == 0: output = output.replace((label_line["name"] + "\n").lower(), "") + #tone down excessive spacing + output = output.replace("\n\n\n","\n\n") + #add the offset of the final location if include_last_address: output += "; " + hex(offset) -- cgit v1.2.3 From 81e42e41da5a664aa528ad4f9284119456b15817 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 14 May 2013 21:53:19 -0400 Subject: gbz80disasm: reti is an ender --- extras/gbz80disasm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 354490e4f..634509973 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -542,10 +542,11 @@ for line in temp_opt_table: del line end_08_scripts_with = [ +0xc9, #ret +0xd9, #reti 0xe9, #jp hl #0xc3, #jp ##0x18, #jr -0xc9, #ret ###0xda, 0xe9, 0xd2, 0xc2, 0xca, 0xc3, 0x38, 0x30, 0x20, 0x28, 0x18, 0xd8, 0xd0, 0xc0, 0xc8, 0xc9 ] relative_jumps = [0x38, 0x30, 0x20, 0x28, 0x18, 0xc3, 0xda, 0xc2] -- cgit v1.2.3 From a6fee3622a9705d060cbe467bd32e425a1ff2bc4 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 15 May 2013 00:23:30 -0400 Subject: gbz80disasm: don't include comments on relative jumps --- extras/gbz80disasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 634509973..6e8b91526 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -718,7 +718,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add byte_labels[target_address]["definition"] = False insertion = line_label2.lower() - include_comment = True + include_comment = False elif current_byte == 0x3e: last_a_address = rom[offset + 1] -- cgit v1.2.3 From b38e2874095266d45df984be778d9fe68832aaac Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 15 May 2013 14:40:20 -0400 Subject: gbz80disasm: don't look for outstanding labels that are behind the origin --- extras/gbz80disasm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 6e8b91526..a8e468379 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -799,7 +799,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add #stop reading at a jump, relative jump or return if current_byte in end_08_scripts_with: - if not has_outstanding_labels(byte_labels) and all_outstanding_labels_are_reverse(byte_labels, offset): + if not has_outstanding_labels(byte_labels) or all_outstanding_labels_are_reverse(byte_labels, offset): keep_reading = False is_data = False #cleanup break -- cgit v1.2.3 From 5521aa5ce0ad5249ff0d369dd33b4f63cd804b73 Mon Sep 17 00:00:00 2001 From: yenatch Date: Wed, 15 May 2013 14:49:49 -0400 Subject: gbz80disasm: include comments on unresolved backward relative jumps --- extras/gbz80disasm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'extras/gbz80disasm.py') diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index a8e468379..b27ce17ad 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -718,7 +718,8 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add byte_labels[target_address]["definition"] = False insertion = line_label2.lower() - include_comment = False + if has_outstanding_labels(byte_labels) and all_outstanding_labels_are_reverse(byte_labels, offset): + include_comment = True elif current_byte == 0x3e: last_a_address = rom[offset + 1] -- cgit v1.2.3