diff --git a/pokemontools/tcgdisasm.py b/pokemontools/tcgdisasm.py index 2e9e8b3..6189332 100755 --- a/pokemontools/tcgdisasm.py +++ b/pokemontools/tcgdisasm.py @@ -558,6 +558,8 @@ call_commands = [0xdc, 0xd4, 0xc4, 0xcc, 0xcd] +avoid_wram_execution = False + def asm_label(address): """ Return the ASM label using the address. @@ -622,7 +624,7 @@ def initialize(self): rom_path = os.path.join(self.config.path, "baserom.gbc") self.rom = bytearray(open(rom_path, "rb").read()) - def find_label(self, local_address, bank_id=0): + def find_label(self, local_address, bank_id=0, wram_suitable = True): # keep an integer if type(local_address) == str: local_address = int(local_address.replace("$", "0x"), 16) @@ -630,6 +632,9 @@ def find_label(self, local_address, bank_id=0): if local_address < 0x8000: for label_entry in self.labels.labels: if get_local_address(label_entry["address"]) == local_address: + if label_entry["label"][0] == "w": # we assume this means it'll be wram + if avoid_wram_execution and not wram_suitable: + continue if "bank" in label_entry and (label_entry["bank"] == bank_id or label_entry["bank"] == 0): return label_entry["label"] if local_address in self.wram.wram_labels.keys(): @@ -639,6 +644,19 @@ def find_label(self, local_address, bank_id=0): return constants[local_address] return None + def check_if_wram_label_suitable(self, opcode): + if opcode in call_commands: + return False + if opcode in relative_unconditional_jumps: + return False + if opcode in relative_jumps: + return False + if opcode in discrete_jumps: + return False + if opcode == 0xdf or opcode == 0xef: + return False + return True + def find_address_from_label(self, label): for label_entry in self.labels.labels: if label == label_entry["label"]: @@ -838,7 +856,7 @@ def output_bank_opcodes(self, original_offset, max_byte_count=0x4000, include_la data_tables[pointer]['usage'] += 1 insertion = "$%.4x" % (number) - result = self.find_label(insertion, bank_id) + result = self.find_label(insertion, bank_id, self.check_if_wram_label_suitable(current_byte) ) if result != None: insertion = result @@ -890,7 +908,7 @@ def output_bank_opcodes(self, original_offset, max_byte_count=0x4000, include_la number += byte2 << 8 insertion = "$%.4x" % (number) - result = self.find_label(insertion, temp_bank) + result = self.find_label(insertion, temp_bank, self.check_if_wram_label_suitable(current_byte)) if op_code_byte == 0xef: if result != None: insertion = result @@ -977,6 +995,8 @@ def output_bank_opcodes(self, original_offset, max_byte_count=0x4000, include_la disasm = Disassembler(conf) disasm.initialize() + if "-nwe" in sys.argv: + avoid_wram_execution = True addr = sys.argv[1] if ":" in addr: addr = addr.split(":")