Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

No Wram Execution Command Line Argument #101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pokemontools/tcgdisasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -622,14 +624,17 @@ 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)

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():
Expand All @@ -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"]:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(":")
Expand Down