From e199aaa8fd73d932e81ef339f4f826cf95a700b9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 25 Jun 2013 03:21:47 -0400 Subject: [PATCH 1/5] gbz80disasm: data detection the is_data flag never did anything. now it does encountering the end of a branch with outstanding labels will read anything between said labels as data --- extras/gbz80disasm.py | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 2dc57e579..94df7552b 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -589,7 +589,7 @@ def find_label(local_address, bank_id=0): def asm_label(address): # why using a random value when you can use the address? - return ".ASM_" + hex(address)[2:] + return '.ASM_%x' % address def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_address=True, stop_at=[], debug = False): #fs = current_address @@ -626,9 +626,9 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add first_loop = True output = "" keep_reading = True + is_data = False while offset <= end_address and keep_reading: current_byte = rom[offset] - is_data = False maybe_byte = current_byte # stop at any address @@ -654,7 +654,7 @@ 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 += ( rom[offset+1] << 8) - if temp_maybe in opt_table.keys() and rom[offset+1]!=0: + if not is_data and temp_maybe in opt_table.keys() and rom[offset+1]!=0: opstr = opt_table[temp_maybe][0].lower() if "x" in opstr: @@ -686,7 +686,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add current_byte_number += 2 offset += 2 - elif maybe_byte in opt_table.keys(): + elif not is_data and maybe_byte in opt_table.keys(): op_code = opt_table[maybe_byte] op_code_type = op_code[1] op_code_byte = maybe_byte @@ -803,33 +803,41 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add break else: is_data = True - - #stop reading at a jump, relative jump or return - if current_byte in end_08_scripts_with: - if not has_outstanding_labels(byte_labels) or all_outstanding_labels_are_reverse(byte_labels, offset): - keep_reading = False - is_data = False #cleanup - break - else: - is_data = False - keep_reading = True - else: - is_data = False - keep_reading = True else: #if is_data and keep_reading: output += spacing + "db $" + hex(rom[offset])[2:] #+ " ; " + hex(offset) output += "\n" offset += 1 current_byte_number += 1 + if offset in byte_labels.keys(): + is_data = False + keep_reading = True #else the while loop would have spit out the opcode #these two are done prior #offset += 1 #current_byte_number += 1 - if current_byte in relative_unconditional_jumps + end_08_scripts_with: + if not is_data and current_byte in relative_unconditional_jumps + end_08_scripts_with: + #stop reading at a jump, relative jump or return + if not has_outstanding_labels(byte_labels) or all_outstanding_labels_are_reverse(byte_labels, offset): + keep_reading = False + is_data = False #cleanup + break + elif offset not in byte_labels.keys(): + is_data = True + keep_reading = True + else: + is_data = False + keep_reading = True output += "\n" + elif is_data and offset not in byte_labels.keys(): + print hex(offset), output.split('\n')[-2] + is_data = True + keep_reading = True + else: + is_data = False + keep_reading = True first_loop = False From 7804dedce469cd40efbc140b80d84733ec853338 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 25 Jun 2013 03:43:58 -0400 Subject: [PATCH 2/5] gbz80disasm: detect data tables referenced in asm --- extras/gbz80disasm.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 94df7552b..8da28ee0b 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -590,6 +590,8 @@ def find_label(local_address, bank_id=0): def asm_label(address): # why using a random value when you can use the address? return '.ASM_%x' % address +def data_label(address): + return '.DATA_%x' % address def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_address=True, stop_at=[], debug = False): #fs = current_address @@ -622,6 +624,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add end_address = original_offset + max_byte_count byte_labels = {} + data_tables = {} first_loop = True output = "" @@ -772,6 +775,13 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add number = byte1 number += byte2 << 8 + pointer = bank_id * 0x4000 + (number & 0x3fff) + 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) if result != None: @@ -824,7 +834,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(): + elif offset not in byte_labels.keys() or offset in data_tables.keys(): is_data = True keep_reading = True else: @@ -839,6 +849,10 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add is_data = False keep_reading = True + if offset in data_tables.keys(): + output = output.replace('$%x' % ((offset & 0x3fff) + 0x4000 * bool(bank_id)), data_label(offset).lower()) + output += data_label(offset).lower() + '\n' + first_loop = False #clean up unused labels From 6630406043cda9e4ab36f8477c62025249b647d6 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 25 Jun 2013 03:55:38 -0400 Subject: [PATCH 3/5] gbz80disasm: local/global address conversion + fix find_label --- extras/gbz80disasm.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 8da28ee0b..e76e16c9f 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -577,7 +577,7 @@ def find_label(local_address, bank_id=0): if local_address < 0x8000: for label_entry in all_labels: - if label_entry["address"] & 0x7fff == local_address: + if get_local_address(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(): @@ -593,6 +593,12 @@ def asm_label(address): def data_label(address): return '.DATA_%x' % address +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 + def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_address=True, stop_at=[], debug = False): #fs = current_address #b = bank_byte @@ -775,7 +781,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add number = byte1 number += byte2 << 8 - pointer = bank_id * 0x4000 + (number & 0x3fff) + pointer = get_global_address(number, bank_id) if pointer not in data_tables.keys(): data_tables[pointer] = {} data_tables[pointer]['usage'] = 0 @@ -850,7 +856,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add keep_reading = True if offset in data_tables.keys(): - output = output.replace('$%x' % ((offset & 0x3fff) + 0x4000 * bool(bank_id)), data_label(offset).lower()) + output = output.replace('$%x' % (get_local_address(offset)), data_label(offset).lower()) output += data_label(offset).lower() + '\n' first_loop = False From 91f7c123f4a54beddf8c9de0f8a10195ad3d7667 Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 25 Jun 2013 03:57:49 -0400 Subject: [PATCH 4/5] gbz80disasm: fix data handling get the scalpel out of there --- extras/gbz80disasm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index e76e16c9f..29c274a6c 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -848,7 +848,6 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add keep_reading = True output += "\n" elif is_data and offset not in byte_labels.keys(): - print hex(offset), output.split('\n')[-2] is_data = True keep_reading = True else: @@ -858,6 +857,8 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add if offset in data_tables.keys(): output = output.replace('$%x' % (get_local_address(offset)), data_label(offset).lower()) output += data_label(offset).lower() + '\n' + is_data = True + keep_reading = True first_loop = False From 2896cb93d48f6b4fb759c37753db7095d4e69ffd Mon Sep 17 00:00:00 2001 From: yenatch Date: Tue, 25 Jun 2013 04:04:19 -0400 Subject: [PATCH 5/5] gbz80disasm: just use lowercase labels --- extras/gbz80disasm.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/extras/gbz80disasm.py b/extras/gbz80disasm.py index 29c274a6c..ed546ef98 100644 --- a/extras/gbz80disasm.py +++ b/extras/gbz80disasm.py @@ -589,9 +589,9 @@ def find_label(local_address, bank_id=0): def asm_label(address): # why using a random value when you can use the address? - return '.ASM_%x' % address + return '.asm_%x' % address def data_label(address): - return '.DATA_%x' % address + return '.data_%x' % address def get_local_address(address): bank = address / 0x4000 @@ -658,7 +658,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add byte_labels[offset]["name"] = line_label byte_labels[offset]["usage"] = 0 byte_labels[offset]["definition"] = True - output += line_label.lower() + "\n" #" ; " + hex(offset) + "\n" + output += line_label + "\n" #" ; " + hex(offset) + "\n" #find out if there's a two byte key like this temp_maybe = maybe_byte @@ -732,7 +732,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add byte_labels[target_address]["usage"] = 1 byte_labels[target_address]["definition"] = False - insertion = line_label2.lower() + insertion = line_label2 if has_outstanding_labels(byte_labels) and all_outstanding_labels_are_reverse(byte_labels, offset): include_comment = True elif current_byte == 0x3e: @@ -855,8 +855,8 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add keep_reading = True if offset in data_tables.keys(): - output = output.replace('$%x' % (get_local_address(offset)), data_label(offset).lower()) - output += data_label(offset).lower() + '\n' + output = output.replace('$%x' % (get_local_address(offset)), data_label(offset)) + output += data_label(offset) + '\n' is_data = True keep_reading = True @@ -867,7 +867,7 @@ def output_bank_opcodes(original_offset, max_byte_count=0x4000, include_last_add address = label_line label_line = byte_labels[label_line] if label_line["usage"] == 0: - output = output.replace((label_line["name"] + "\n").lower(), "") + output = output.replace((label_line["name"] + "\n"), "") #tone down excessive spacing output = output.replace("\n\n\n","\n\n")