Merge pull request #30 from kanzure/cleanup-again

More cleanup of crystal.py
This commit is contained in:
Bryan Bishop
2013-09-12 21:45:01 -07:00
10 changed files with 1149 additions and 1118 deletions

14
pokemontools/addresses.py Normal file
View File

@@ -0,0 +1,14 @@
"""
Common methods used against addresses.
"""
def is_valid_address(address):
"""is_valid_rom_address"""
if address == None:
return False
if type(address) == str:
address = int(address, 16)
if 0 <= address <= 2097152:
return True
else:
return False

File diff suppressed because it is too large Load Diff

View File

View File

@@ -0,0 +1,8 @@
class AsmLine:
# TODO: parse label lines
def __init__(self, line, bank=None):
self.line = line
self.bank = bank
def to_asm(self):
return self.line

View File

@@ -0,0 +1,425 @@
"""
Some old methods rescued from crystal.py
"""
import pointers
map_header_byte_size = ...
rom_interval = ...
all_map_headers = []
def old_parse_map_script_header_at(address, map_group=None, map_id=None, debug=True):
logging.debug("starting to parse the map's script header..")
#[[Number1 of pointers] Number1 * [2byte pointer to script][00][00]]
ptr_line_size = 4 #[2byte pointer to script][00][00]
trigger_ptr_cnt = ord(rom[address])
trigger_pointers = helpers.grouper(rom_interval(address+1, trigger_ptr_cnt * ptr_line_size, strings=False), count=ptr_line_size)
triggers = {}
for index, trigger_pointer in enumerate(trigger_pointers):
logging.debug("parsing a trigger header...")
byte1 = trigger_pointer[0]
byte2 = trigger_pointer[1]
ptr = byte1 + (byte2 << 8)
trigger_address = pointers.calculate_pointer(ptr, pointers.calculate_bank(address))
trigger_script = parse_script_engine_script_at(trigger_address, map_group=map_group, map_id=map_id)
triggers[index] = {
"script": trigger_script,
"address": trigger_address,
"pointer": {"1": byte1, "2": byte2},
}
# bump ahead in the byte stream
address += trigger_ptr_cnt * ptr_line_size + 1
#[[Number2 of pointers] Number2 * [hook number][2byte pointer to script]]
callback_ptr_line_size = 3
callback_ptr_cnt = ord(rom[address])
callback_ptrs = helpers.grouper(rom_interval(address+1, callback_ptr_cnt * callback_ptr_line_size, strings=False), count=callback_ptr_line_size)
callback_pointers = {}
callbacks = {}
for index, callback_line in enumerate(callback_ptrs):
logging.debug("parsing a callback header..")
hook_byte = callback_line[0] # 1, 2, 3, 4, 5
callback_byte1 = callback_line[1]
callback_byte2 = callback_line[2]
callback_ptr = callback_byte1 + (callback_byte2 << 8)
callback_address = pointers.calculate_pointer(callback_ptr, pointers.calculate_bank(address))
callback_script = parse_script_engine_script_at(callback_address)
callback_pointers[len(callback_pointers.keys())] = [hook_byte, callback_ptr]
callbacks[index] = {
"script": callback_script,
"address": callback_address,
"pointer": {"1": callback_byte1, "2": callback_byte2},
}
# XXX do these triggers/callbacks call asm or script engine scripts?
return {
#"trigger_ptr_cnt": trigger_ptr_cnt,
"trigger_pointers": trigger_pointers,
#"callback_ptr_cnt": callback_ptr_cnt,
#"callback_ptr_scripts": callback_ptrs,
"callback_pointers": callback_pointers,
"trigger_scripts": triggers,
"callback_scripts": callbacks,
}
def old_parse_map_header_at(address, map_group=None, map_id=None, debug=True):
"""parses an arbitrary map header at some address"""
logging.debug("parsing a map header at {0}".format(hex(address)))
bytes = rom_interval(address, map_header_byte_size, strings=False, debug=debug)
bank = bytes[0]
tileset = bytes[1]
permission = bytes[2]
second_map_header_address = pointers.calculate_pointer(bytes[3] + (bytes[4] << 8), 0x25)
location_on_world_map = bytes[5] # pokegear world map location
music = bytes[6]
time_of_day = bytes[7]
fishing_group = bytes[8]
map_header = {
"bank": bank,
"tileset": tileset,
"permission": permission, # map type?
"second_map_header_pointer": {"1": bytes[3], "2": bytes[4]},
"second_map_header_address": second_map_header_address,
"location_on_world_map": location_on_world_map, # area
"music": music,
"time_of_day": time_of_day,
"fishing": fishing_group,
}
logging.debug("second map header address is {0}".format(hex(second_map_header_address)))
map_header["second_map_header"] = old_parse_second_map_header_at(second_map_header_address, debug=debug)
event_header_address = map_header["second_map_header"]["event_address"]
script_header_address = map_header["second_map_header"]["script_address"]
# maybe event_header and script_header should be put under map_header["second_map_header"]
map_header["event_header"] = old_parse_map_event_header_at(event_header_address, map_group=map_group, map_id=map_id, debug=debug)
map_header["script_header"] = old_parse_map_script_header_at(script_header_address, map_group=map_group, map_id=map_id, debug=debug)
return map_header
all_second_map_headers = []
def old_parse_second_map_header_at(address, map_group=None, map_id=None, debug=True):
"""each map has a second map header"""
bytes = rom_interval(address, second_map_header_byte_size, strings=False)
border_block = bytes[0]
height = bytes[1]
width = bytes[2]
blockdata_bank = bytes[3]
blockdata_pointer = bytes[4] + (bytes[5] << 8)
blockdata_address = pointers.calculate_pointer(blockdata_pointer, blockdata_bank)
script_bank = bytes[6]
script_pointer = bytes[7] + (bytes[8] << 8)
script_address = pointers.calculate_pointer(script_pointer, script_bank)
event_bank = script_bank
event_pointer = bytes[9] + (bytes[10] << 8)
event_address = pointers.calculate_pointer(event_pointer, event_bank)
connections = bytes[11]
return {
"border_block": border_block,
"height": height,
"width": width,
"blockdata_bank": blockdata_bank,
"blockdata_pointer": {"1": bytes[4], "2": bytes[5]},
"blockdata_address": blockdata_address,
"script_bank": script_bank,
"script_pointer": {"1": bytes[7], "2": bytes[8]},
"script_address": script_address,
"event_bank": event_bank,
"event_pointer": {"1": bytes[9], "2": bytes[10]},
"event_address": event_address,
"connections": connections,
}
def old_parse_warp_bytes(some_bytes, debug=True):
"""parse some number of warps from the data"""
assert len(some_bytes) % warp_byte_size == 0, "wrong number of bytes"
warps = []
for bytes in helpers.grouper(some_bytes, count=warp_byte_size):
y = int(bytes[0], 16)
x = int(bytes[1], 16)
warp_to = int(bytes[2], 16)
map_group = int(bytes[3], 16)
map_id = int(bytes[4], 16)
warps.append({
"y": y,
"x": x,
"warp_to": warp_to,
"map_group": map_group,
"map_id": map_id,
})
return warps
def old_parse_signpost_bytes(some_bytes, bank=None, map_group=None, map_id=None, debug=True):
assert len(some_bytes) % signpost_byte_size == 0, "wrong number of bytes"
signposts = []
for bytes in helpers.grouper(some_bytes, count=signpost_byte_size):
y = int(bytes[0], 16)
x = int(bytes[1], 16)
func = int(bytes[2], 16)
additional = {}
if func in [0, 1, 2, 3, 4]:
logging.debug(
"parsing signpost script.. signpost is at x={x} y={y}"
.format(x=x, y=y)
)
script_ptr_byte1 = int(bytes[3], 16)
script_ptr_byte2 = int(bytes[4], 16)
script_pointer = script_ptr_byte1 + (script_ptr_byte2 << 8)
script_address = None
script = None
script_address = pointers.calculate_pointer(script_pointer, bank)
script = parse_script_engine_script_at(script_address, map_group=map_group, map_id=map_id)
additional = {
"script_ptr": script_pointer,
"script_pointer": {"1": script_ptr_byte1, "2": script_ptr_byte2},
"script_address": script_address,
"script": script,
}
elif func in [5, 6]:
logging.debug(
"parsing signpost script.. signpost is at x={x} y={y}"
.format(x=x, y=y)
)
ptr_byte1 = int(bytes[3], 16)
ptr_byte2 = int(bytes[4], 16)
pointer = ptr_byte1 + (ptr_byte2 << 8)
address = pointers.calculate_pointer(pointer, bank)
bit_table_byte1 = ord(rom[address])
bit_table_byte2 = ord(rom[address+1])
script_ptr_byte1 = ord(rom[address+2])
script_ptr_byte2 = ord(rom[address+3])
script_address = calculate_pointer_from_bytes_at(address+2, bank=bank)
script = parse_script_engine_script_at(script_address, map_group=map_group, map_id=map_id)
additional = {
"bit_table_bytes": {"1": bit_table_byte1, "2": bit_table_byte2},
"script_ptr": script_ptr_byte1 + (script_ptr_byte2 << 8),
"script_pointer": {"1": script_ptr_byte1, "2": script_ptr_byte2},
"script_address": script_address,
"script": script,
}
else:
logging.debug(".. type 7 or 8 signpost not parsed yet.")
spost = {
"y": y,
"x": x,
"func": func,
}
spost.update(additional)
signposts.append(spost)
return signposts
def old_parse_people_event_bytes(some_bytes, address=None, map_group=None, map_id=None, debug=True):
"""parse some number of people-events from the data
see http://hax.iimarck.us/files/scriptingcodes_eng.htm#Scripthdr
For example, map 1.1 (group 1 map 1) has four person-events.
37 05 07 06 00 FF FF 00 00 02 40 FF FF
3B 08 0C 05 01 FF FF 00 00 05 40 FF FF
3A 07 06 06 00 FF FF A0 00 08 40 FF FF
29 05 0B 06 00 FF FF 00 00 0B 40 FF FF
"""
assert len(some_bytes) % people_event_byte_size == 0, "wrong number of bytes"
# address is not actually required for this function to work...
bank = None
if address:
bank = pointers.calculate_bank(address)
people_events = []
for bytes in helpers.grouper(some_bytes, count=people_event_byte_size):
pict = int(bytes[0], 16)
y = int(bytes[1], 16) # y from top + 4
x = int(bytes[2], 16) # x from left + 4
face = int(bytes[3], 16) # 0-4 for regular, 6-9 for static facing
move = int(bytes[4], 16)
clock_time_byte1 = int(bytes[5], 16)
clock_time_byte2 = int(bytes[6], 16)
color_function_byte = int(bytes[7], 16) # Color|Function
trainer_sight_range = int(bytes[8], 16)
lower_bits = color_function_byte & 0xF
#lower_bits_high = lower_bits >> 2
#lower_bits_low = lower_bits & 3
higher_bits = color_function_byte >> 4
#higher_bits_high = higher_bits >> 2
#higher_bits_low = higher_bits & 3
is_regular_script = lower_bits == 00
# pointer points to script
is_give_item = lower_bits == 01
# pointer points to [Item no.][Amount]
is_trainer = lower_bits == 02
# pointer points to trainer header
# goldmap called these next two bytes "text_block" and "text_bank"?
script_pointer_byte1 = int(bytes[9], 16)
script_pointer_byte2 = int(bytes[10], 16)
script_pointer = script_pointer_byte1 + (script_pointer_byte2 << 8)
# calculate the full address by assuming it's in the current bank
# but what if it's not in the same bank?
extra_portion = {}
if bank:
ptr_address = pointers.calculate_pointer(script_pointer, bank)
if is_regular_script:
logging.debug(
"parsing a person-script at x={x} y={y} address={address}"
.format(
x=(x-4),
y=(y-4),
address=hex(ptr_address),
)
)
script = parse_script_engine_script_at(ptr_address, map_group=map_group, map_id=map_id)
extra_portion = {
"script_address": ptr_address,
"script": script,
"event_type": "script",
}
if is_give_item:
logging.debug("not parsing give item event.. [item id][quantity]")
extra_portion = {
"event_type": "give_item",
"give_item_data_address": ptr_address,
"item_id": ord(rom[ptr_address]),
"item_qty": ord(rom[ptr_address+1]),
}
if is_trainer:
logging.debug(
"parsing a trainer (person-event) at x={x} y={y}"
.format(x=x, y=y)
)
parsed_trainer = parse_trainer_header_at(ptr_address, map_group=map_group, map_id=map_id)
extra_portion = {
"event_type": "trainer",
"trainer_data_address": ptr_address,
"trainer_data": parsed_trainer,
}
# XXX not sure what's going on here
# bit no. of bit table 1 (hidden if set)
# note: FFFF for none
when_byte = int(bytes[11], 16)
hide = int(bytes[12], 16)
bit_number_of_bit_table1_byte2 = int(bytes[11], 16)
bit_number_of_bit_table1_byte1 = int(bytes[12], 16)
bit_number_of_bit_table1 = bit_number_of_bit_table1_byte1 + (bit_number_of_bit_table1_byte2 << 8)
people_event = {
"pict": pict,
"y": y, # y from top + 4
"x": x, # x from left + 4
"face": face, # 0-4 for regular, 6-9 for static facing
"move": move,
"clock_time": {"1": clock_time_byte1,
"2": clock_time_byte2}, # clock/time setting byte 1
"color_function_byte": color_function_byte, # Color|Function
"trainer_sight_range": trainer_sight_range, # trainer range of sight
"script_pointer": {"1": script_pointer_byte1,
"2": script_pointer_byte2},
#"text_block": text_block, # script pointer byte 1
#"text_bank": text_bank, # script pointer byte 2
"when_byte": when_byte, # bit no. of bit table 1 (hidden if set)
"hide": hide, # note: FFFF for none
"is_trainer": is_trainer,
"is_regular_script": is_regular_script,
"is_give_item": is_give_item,
}
people_event.update(extra_portion)
people_events.append(people_event)
return people_events
def old_parse_trainer_header_at(address, map_group=None, map_id=None, debug=True):
bank = pointers.calculate_bank(address)
bytes = rom_interval(address, 12, strings=False)
bit_number = bytes[0] + (bytes[1] << 8)
trainer_group = bytes[2]
trainer_id = bytes[3]
text_when_seen_ptr = calculate_pointer_from_bytes_at(address+4, bank=bank)
text_when_seen = parse_text_engine_script_at(text_when_seen_ptr, map_group=map_group, map_id=map_id, debug=debug)
text_when_trainer_beaten_ptr = calculate_pointer_from_bytes_at(address+6, bank=bank)
text_when_trainer_beaten = parse_text_engine_script_at(text_when_trainer_beaten_ptr, map_group=map_group, map_id=map_id, debug=debug)
if [ord(rom[address+8]), ord(rom[address+9])] == [0, 0]:
script_when_lost_ptr = 0
script_when_lost = None
else:
logging.debug("parsing script-when-lost")
script_when_lost_ptr = calculate_pointer_from_bytes_at(address+8, bank=bank)
script_when_lost = None
silver_avoids = [0xfa53]
if script_when_lost_ptr > 0x4000 and not script_when_lost_ptr in silver_avoids:
script_when_lost = parse_script_engine_script_at(script_when_lost_ptr, map_group=map_group, map_id=map_id, debug=debug)
logging.debug("parsing script-talk-again") # or is this a text?
script_talk_again_ptr = calculate_pointer_from_bytes_at(address+10, bank=bank)
script_talk_again = None
if script_talk_again_ptr > 0x4000:
script_talk_again = parse_script_engine_script_at(script_talk_again_ptr, map_group=map_group, map_id=map_id, debug=debug)
return {
"bit_number": bit_number,
"trainer_group": trainer_group,
"trainer_id": trainer_id,
"text_when_seen_ptr": text_when_seen_ptr,
"text_when_seen": text_when_seen,
"text_when_trainer_beaten_ptr": text_when_trainer_beaten_ptr,
"text_when_trainer_beaten": text_when_trainer_beaten,
"script_when_lost_ptr": script_when_lost_ptr,
"script_when_lost": script_when_lost,
"script_talk_again_ptr": script_talk_again_ptr,
"script_talk_again": script_talk_again,
}
def old_parse_map_event_header_at(address, map_group=None, map_id=None, debug=True):
"""parse crystal map event header byte structure thing"""
returnable = {}
bank = pointers.calculate_bank(address)
logging.debug("event header address is {0}".format(hex(address)))
filler1 = ord(rom[address])
filler2 = ord(rom[address+1])
returnable.update({"1": filler1, "2": filler2})
# warps
warp_count = ord(rom[address+2])
warp_byte_count = warp_byte_size * warp_count
warps = rom_interval(address+3, warp_byte_count)
after_warps = address + 3 + warp_byte_count
returnable.update({"warp_count": warp_count, "warps": old_parse_warp_bytes(warps)})
# triggers (based on xy location)
trigger_count = ord(rom[after_warps])
trigger_byte_count = trigger_byte_size * trigger_count
triggers = rom_interval(after_warps+1, trigger_byte_count)
after_triggers = after_warps + 1 + trigger_byte_count
returnable.update({"xy_trigger_count": trigger_count, "xy_triggers": old_parse_xy_trigger_bytes(triggers, bank=bank, map_group=map_group, map_id=map_id)})
# signposts
signpost_count = ord(rom[after_triggers])
signpost_byte_count = signpost_byte_size * signpost_count
signposts = rom_interval(after_triggers+1, signpost_byte_count)
after_signposts = after_triggers + 1 + signpost_byte_count
returnable.update({"signpost_count": signpost_count, "signposts": old_parse_signpost_bytes(signposts, bank=bank, map_group=map_group, map_id=map_id)})
# people events
people_event_count = ord(rom[after_signposts])
people_event_byte_count = people_event_byte_size * people_event_count
people_events_bytes = rom_interval(after_signposts+1, people_event_byte_count)
people_events = old_parse_people_event_bytes(people_events_bytes, address=after_signposts+1, map_group=map_group, map_id=map_id)
returnable.update({"people_event_count": people_event_count, "people_events": people_events})
return returnable

View File

@@ -8,7 +8,6 @@ import json
import logging
import pointers
import crystal
class Labels(object):
"""
@@ -32,6 +31,7 @@ class Labels(object):
"Running crystal.scan_for_predefined_labels to create \"{0}\". Trying.."
.format(Labels.filename)
)
import crystal
crystal.scan_for_predefined_labels()
self.labels = json.read(open(self.path, "r").read())
@@ -197,3 +197,13 @@ def get_label_from_line(line):
#split up the line
label = line.split(":")[0]
return label
def find_labels_without_addresses(asm):
"""scans the asm source and finds labels that are unmarked"""
without_addresses = []
for (line_number, line) in enumerate(asm):
if line_has_label(line):
label = get_label_from_line(line)
if not line_has_comment_address(line):
without_addresses.append({"line_number": line_number, "line": line, "label": label})
return without_addresses

View File

@@ -0,0 +1,528 @@
"""
An old implementation of TextScript that may not be useful anymore.
"""
import pokemontools.pointers as pointers
class OldTextScript:
"a text is a sequence of commands different from a script-engine script"
base_label = "UnknownText_"
def __init__(self, address, map_group=None, map_id=None, debug=True, show=True, force=False, label=None):
self.address = address
self.map_group, self.map_id, self.debug, self.show, self.force = map_group, map_id, debug, show, force
if not label:
label = self.base_label + hex(address)
self.label = Label(name=label, address=address, object=self)
self.dependencies = []
self.parse_text_at(address)
@staticmethod
def find_addresses():
"""returns a list of text pointers
useful for testing parse_text_engine_script_at
Note that this list is not exhaustive. There are some texts that
are only pointed to from some script that a current script just
points to. So find_all_text_pointers_in_script_engine_script will
have to recursively follow through each script to find those.
.. it does this now :)
"""
addresses = set()
# for each map group
for map_group in map_names:
# for each map id
for map_id in map_names[map_group]:
# skip the offset key
if map_id == "offset": continue
# dump this into smap
smap = map_names[map_group][map_id]
# signposts
signposts = smap["signposts"]
# for each signpost
for signpost in signposts:
if signpost["func"] in [0, 1, 2, 3, 4]:
# dump this into script
script = signpost["script"]
elif signpost["func"] in [05, 06]:
script = signpost["script"]
else: continue
# skip signposts with no bytes
if len(script) == 0: continue
# find all text pointers in script
texts = find_all_text_pointers_in_script_engine_script(script, smap["event_bank"])
# dump these addresses in
addresses.update(texts)
# xy triggers
xy_triggers = smap["xy_triggers"]
# for each xy trigger
for xy_trigger in xy_triggers:
# dump this into script
script = xy_trigger["script"]
# find all text pointers in script
texts = find_all_text_pointers_in_script_engine_script(script, smap["event_bank"])
# dump these addresses in
addresses.update(texts)
# trigger scripts
triggers = smap["trigger_scripts"]
# for each trigger
for (i, trigger) in triggers.items():
# dump this into script
script = trigger["script"]
# find all text pointers in script
texts = find_all_text_pointers_in_script_engine_script(script, pointers.calculate_bank(trigger["address"]))
# dump these addresses in
addresses.update(texts)
# callback scripts
callbacks = smap["callback_scripts"]
# for each callback
for (k, callback) in callbacks.items():
# dump this into script
script = callback["script"]
# find all text pointers in script
texts = find_all_text_pointers_in_script_engine_script(script, pointers.calculate_bank(callback["address"]))
# dump these addresses in
addresses.update(texts)
# people-events
events = smap["people_events"]
# for each event
for event in events:
if event["event_type"] == "script":
# dump this into script
script = event["script"]
# find all text pointers in script
texts = find_all_text_pointers_in_script_engine_script(script, smap["event_bank"])
# dump these addresses in
addresses.update(texts)
if event["event_type"] == "trainer":
trainer_data = event["trainer_data"]
addresses.update([trainer_data["text_when_seen_ptr"]])
addresses.update([trainer_data["text_when_trainer_beaten_ptr"]])
trainer_bank = pointers.calculate_bank(event["trainer_data_address"])
script1 = trainer_data["script_talk_again"]
texts1 = find_all_text_pointers_in_script_engine_script(script1, trainer_bank)
addresses.update(texts1)
script2 = trainer_data["script_when_lost"]
texts2 = find_all_text_pointers_in_script_engine_script(script2, trainer_bank)
addresses.update(texts2)
return addresses
def parse_text_at(self, address):
"""parses a text-engine script ("in-text scripts")
http://hax.iimarck.us/files/scriptingcodes_eng.htm#InText
This is presently very broken.
see parse_text_at2, parse_text_at, and process_00_subcommands
"""
global rom, text_count, max_texts, texts, script_parse_table
if rom == None:
direct_load_rom()
if address == None:
return "not a script"
map_group, map_id, debug, show, force = self.map_group, self.map_id, self.debug, self.show, self.force
commands = {}
if is_script_already_parsed_at(address) and not force:
logging.debug("text is already parsed at this location: {0}".format(hex(address)))
raise Exception("text is already parsed, what's going on ?")
return script_parse_table[address]
total_text_commands = 0
command_counter = 0
original_address = address
offset = address
end = False
script_parse_table[original_address:original_address+1] = "incomplete text"
while not end:
address = offset
command = {}
command_byte = ord(rom[address])
if debug:
logging.debug(
"TextScript.parse_script_at has encountered a command byte {0} at {1}"
.format(hex(command_byte), hex(address))
)
end_address = address + 1
if command_byte == 0:
# read until $57, $50 or $58
jump57 = how_many_until(chr(0x57), offset, rom)
jump50 = how_many_until(chr(0x50), offset, rom)
jump58 = how_many_until(chr(0x58), offset, rom)
# whichever command comes first
jump = min([jump57, jump50, jump58])
end_address = offset + jump # we want the address before $57
lines = process_00_subcommands(offset+1, end_address, debug=debug)
if show and debug:
text = parse_text_at2(offset+1, end_address-offset+1, debug=debug)
logging.debug("output of parse_text_at2 is {0}".format(text))
command = {"type": command_byte,
"start_address": offset,
"end_address": end_address,
"size": jump,
"lines": lines,
}
offset += jump
elif command_byte == 0x17:
# TX_FAR [pointer][bank]
pointer_byte1 = ord(rom[offset+1])
pointer_byte2 = ord(rom[offset+2])
pointer_bank = ord(rom[offset+3])
pointer = (pointer_byte1 + (pointer_byte2 << 8))
pointer = extract_maps.calculate_pointer(pointer, pointer_bank)
text = TextScript(pointer, map_group=self.map_group, map_id=self.amp_id, debug=self.debug, \
show=self.debug, force=self.debug, label="Target"+self.label.name)
if text.is_valid():
self.dependencies.append(text)
command = {"type": command_byte,
"start_address": offset,
"end_address": offset + 3, # last byte belonging to this command
"pointer": pointer, # parameter
"text": text,
}
offset += 3 + 1
elif command_byte == 0x50 or command_byte == 0x57 or command_byte == 0x58: # end text
command = {"type": command_byte,
"start_address": offset,
"end_address": offset,
}
# this byte simply indicates to end the script
end = True
# this byte simply indicates to end the script
if command_byte == 0x50 and ord(rom[offset+1]) == 0x50: # $50$50 means end completely
end = True
commands[command_counter+1] = command
# also save the next byte, before we quit
commands[command_counter+1]["start_address"] += 1
commands[command_counter+1]["end_address"] += 1
add_command_byte_to_totals(command_byte)
elif command_byte == 0x50: # only end if we started with $0
if len(commands.keys()) > 0:
if commands[0]["type"] == 0x0: end = True
elif command_byte == 0x57 or command_byte == 0x58: # end completely
end = True
offset += 1 # go past this 0x50
elif command_byte == 0x1:
# 01 = text from RAM. [01][2-byte pointer]
size = 3 # total size, including the command byte
pointer_byte1 = ord(rom[offset+1])
pointer_byte2 = ord(rom[offset+2])
command = {"type": command_byte,
"start_address": offset+1,
"end_address": offset+2, # last byte belonging to this command
"pointer": [pointer_byte1, pointer_byte2], # RAM pointer
}
# view near these bytes
# subsection = rom[offset:offset+size+1] #peak ahead
#for x in subsection:
# print hex(ord(x))
#print "--"
offset += 2 + 1 # go to the next byte
# use this to look at the surrounding bytes
if debug:
logging.debug("next command is {0}".format(hex(ord(rom[offset]))))
logging.debug(
".. current command number is {counter} near {offset} on map_id={map_id}"
.format(
counter=command_counter,
offset=hex(offset),
map_id=map_id,
)
)
elif command_byte == 0x7:
# 07 = shift texts 1 row above (2nd line becomes 1st line); address for next text = 2nd line. [07]
size = 1
command = {"type": command_byte,
"start_address": offset,
"end_address": offset,
}
offset += 1
elif command_byte == 0x3:
# 03 = set new address in RAM for text. [03][2-byte RAM address]
size = 3
command = {"type": command_byte, "start_address": offset, "end_address": offset+2}
offset += size
elif command_byte == 0x4: # draw box
# 04 = draw box. [04][2-Byte pointer][height Y][width X]
size = 5 # including the command
command = {
"type": command_byte,
"start_address": offset,
"end_address": offset + size,
"pointer_bytes": [ord(rom[offset+1]), ord(rom[offset+2])],
"y": ord(rom[offset+3]),
"x": ord(rom[offset+4]),
}
offset += size + 1
elif command_byte == 0x5:
# 05 = write text starting at 2nd line of text-box. [05][text][ending command]
# read until $57, $50 or $58
jump57 = how_many_until(chr(0x57), offset, rom)
jump50 = how_many_until(chr(0x50), offset, rom)
jump58 = how_many_until(chr(0x58), offset, rom)
# whichever command comes first
jump = min([jump57, jump50, jump58])
end_address = offset + jump # we want the address before $57
lines = process_00_subcommands(offset+1, end_address, debug=debug)
if show and debug:
text = parse_text_at2(offset+1, end_address-offset+1, debug=debug)
logging.debug("parse_text_at2 text is {0}".format(text))
command = {"type": command_byte,
"start_address": offset,
"end_address": end_address,
"size": jump,
"lines": lines,
}
offset = end_address + 1
elif command_byte == 0x6:
# 06 = wait for keypress A or B (put blinking arrow in textbox). [06]
command = {"type": command_byte, "start_address": offset, "end_address": offset}
offset += 1
elif command_byte == 0x7:
# 07 = shift texts 1 row above (2nd line becomes 1st line); address for next text = 2nd line. [07]
command = {"type": command_byte, "start_address": offset, "end_address": offset}
offset += 1
elif command_byte == 0x8:
# 08 = asm until whenever
command = {"type": command_byte, "start_address": offset, "end_address": offset}
offset += 1
end = True
elif command_byte == 0x9:
# 09 = write hex-to-dec number from RAM to textbox [09][2-byte RAM address][byte bbbbcccc]
# bbbb = how many bytes to read (read number is big-endian)
# cccc = how many digits display (decimal)
#(note: max of decimal digits is 7,i.e. max number correctly displayable is 9999999)
ram_address_byte1 = ord(rom[offset+1])
ram_address_byte2 = ord(rom[offset+2])
read_byte = ord(rom[offset+3])
command = {
"type": command_byte,
"address": [ram_address_byte1, ram_address_byte2],
"read_byte": read_byte, # split this up when we make a macro for this
}
offset += 4
else:
#if len(commands) > 0:
# print "Unknown text command " + hex(command_byte) + " at " + hex(offset) + ", script began with " + hex(commands[0]["type"])
if debug:
logging.debug(
"Unknown text command at {offset} - command: {command} on map_id={map_id}"
.format(
offset=hex(offset),
command=hex(ord(rom[offset])),
map_id=map_id,
)
)
# end at the first unknown command
end = True
commands[command_counter] = command
command_counter += 1
total_text_commands += len(commands)
text_count += 1
#if text_count >= max_texts:
# sys.exit()
self.commands = commands
self.last_address = offset
script_parse_table[original_address:offset] = self
all_texts.append(self)
self.size = self.byte_count = self.last_address - original_address
return commands
def get_dependencies(self, recompute=False, global_dependencies=set()):
global_dependencies.update(self.dependencies)
return self.dependencies
def to_asm(self, label=None):
address = self.address
start_address = address
if label == None: label = self.label.name
# using deepcopy because otherwise additional @s get appended each time
# like to the end of the text for TextScript(0x5cf3a)
commands = deepcopy(self.commands)
# apparently this isn't important anymore?
needs_to_begin_with_0 = True
# start with zero please
byte_count = 0
# where we store all output
output = ""
had_text_end_byte = False
had_text_end_byte_57_58 = False
had_db_last = False
xspacing = ""
# reset this pretty fast..
first_line = True
# for each command..
for this_command in commands.keys():
if not "lines" in commands[this_command].keys():
command = commands[this_command]
if not "type" in command.keys():
logging.debug("ERROR in command: {0}".format(command))
continue # dunno what to do here?
if command["type"] == 0x1: # TX_RAM
p1 = command["pointer"][0]
p2 = command["pointer"][1]
# remember to account for big endian -> little endian
output += "\n" + xspacing + "TX_RAM $%.2x%.2x" %(p2, p1)
byte_count += 3
had_db_last = False
elif command["type"] == 0x17: # TX_FAR
#p1 = command["pointer"][0]
#p2 = command["pointer"][1]
output += "\n" + xspacing + "TX_FAR _" + label + " ; " + hex(command["pointer"])
byte_count += 4 # $17, bank, address word
had_db_last = False
elif command["type"] == 0x9: # TX_RAM_HEX2DEC
# address, read_byte
output += "\n" + xspacing + "TX_NUM $%.2x%.2x, $%.2x" % (command["address"][1], command["address"][0], command["read_byte"])
had_db_last = False
byte_count += 4
elif command["type"] == 0x50 and not had_text_end_byte:
# had_text_end_byte helps us avoid repeating $50s
if had_db_last:
output += ", $50"
else:
output += "\n" + xspacing + "db $50"
byte_count += 1
had_db_last = True
elif command["type"] in [0x57, 0x58] and not had_text_end_byte_57_58:
if had_db_last:
output += ", $%.2x" % (command["type"])
else:
output += "\n" + xspacing + "db $%.2x" % (command["type"])
byte_count += 1
had_db_last = True
elif command["type"] in [0x57, 0x58] and had_text_end_byte_57_58:
pass # this is ok
elif command["type"] == 0x50 and had_text_end_byte:
pass # this is also ok
elif command["type"] == 0x0b:
if had_db_last:
output += ", $0b"
else:
output += "\n" + xspacing + "db $0B"
byte_count += 1
had_db_last = True
elif command["type"] == 0x11:
if had_db_last:
output += ", $11"
else:
output += "\n" + xspacing + "db $11"
byte_count += 1
had_db_last = True
elif command["type"] == 0x6: # wait for keypress
if had_db_last:
output += ", $6"
else:
output += "\n" + xspacing + "db $6"
byte_count += 1
had_db_last = True
else:
logging.debug("ERROR in command: {0}".format(hex(command["type"])))
had_db_last = False
# everything else is for $0s, really
continue
lines = commands[this_command]["lines"]
# reset this in case we have non-$0s later
had_db_last = False
# add the ending byte to the last line- always seems $57
# this should already be in there, but it's not because of a bug in the text parser
lines[len(lines.keys())-1].append(commands[len(commands.keys())-1]["type"])
first = True # first byte
for line_id in lines:
line = lines[line_id]
output += xspacing + "db "
if first and needs_to_begin_with_0:
output += "$0, "
first = False
byte_count += 1
quotes_open = False
first_byte = True
was_byte = False
for byte in line:
if byte == 0x50:
had_text_end_byte = True # don't repeat it
if byte in [0x58, 0x57]:
had_text_end_byte_57_58 = True
if byte in chars.chars:
if not quotes_open and not first_byte: # start text
output += ", \""
quotes_open = True
first_byte = False
if not quotes_open and first_byte: # start text
output += "\""
quotes_open = True
output += chars.chars[byte]
elif byte in constant_abbreviation_bytes:
if quotes_open:
output += "\""
quotes_open = False
if not first_byte:
output += ", "
output += constant_abbreviation_bytes[byte]
else:
if quotes_open:
output += "\""
quotes_open = False
# if you want the ending byte on the last line
#if not (byte == 0x57 or byte == 0x50 or byte == 0x58):
if not first_byte:
output += ", "
output += "$" + hex(byte)[2:]
was_byte = True
# add a comma unless it's the end of the line
#if byte_count+1 != len(line):
# output += ", "
first_byte = False
byte_count += 1
# close final quotes
if quotes_open:
output += "\""
quotes_open = False
output += "\n"
#include_newline = "\n"
#if len(output)!=0 and output[-1] == "\n":
# include_newline = ""
#output += include_newline + "; " + hex(start_address) + " + " + str(byte_count) + " bytes = " + hex(start_address + byte_count)
if len(output) > 0 and output[-1] == "\n":
output = output[:-1]
self.size = self.byte_count = byte_count
return output

View File

@@ -104,5 +104,30 @@ def remove_parentheticals_from_trainer_group_names():
i += 1
return trainer_group_names
def pretty_print_trainer_id_constants(trainer_group_table, trainers):
"""
Prints out some constants for trainer ids, for "constants.asm".
make_trainer_group_name_trainer_ids must be called prior to this.
"""
assert trainer_group_table != None, "must make trainer_group_table first"
assert trainers.trainer_group_names != None, "must have trainers.trainer_group_names available"
assert "trainer_names" in trainers.trainer_group_names[1].keys(), "trainer_names must be set in trainers.trainer_group_names"
output = ""
for (key, value) in trainers.trainer_group_names.items():
if "uses_numeric_trainer_ids" in trainers.trainer_group_names[key].keys():
continue
id = key
group = value
header = group["header"]
name = group["name"]
trainer_names = group["trainer_names"]
output += "; " + name + "\n"
for (id, name) in enumerate(trainer_names):
output += name.upper() + " EQU $%.2x"%(id+1) + "\n"
output += "\n"
return output
# remove [Blue] from each trainer group name
remove_parentheticals_from_trainer_group_names()

View File

@@ -84,7 +84,6 @@ from pokemontools.crystal import (
process_incbins,
get_labels_between,
generate_diff_insert,
find_labels_without_addresses,
rom_text_at,
get_label_for,
split_incbin_line_into_three,

View File

@@ -35,6 +35,7 @@ from pokemontools.labels import (
line_has_comment_address,
line_has_label,
get_label_from_line,
find_labels_without_addresses,
)
from pokemontools.helpers import (
@@ -84,7 +85,6 @@ from pokemontools.crystal import (
process_incbins,
get_labels_between,
generate_diff_insert,
find_labels_without_addresses,
rom_text_at,
get_label_for,
split_incbin_line_into_three,
@@ -389,10 +389,10 @@ class TestAsmList(unittest.TestCase):
def test_find_labels_without_addresses(self):
global asm
asm = ["hello_world: ; 0x1", "hello_world2: ;"]
labels = find_labels_without_addresses()
labels = find_labels_without_addresses(asm)
self.failUnless(labels[0]["label"] == "hello_world2")
asm = ["hello world: ;1", "hello_world: ;2"]
labels = find_labels_without_addresses()
labels = find_labels_without_addresses(asm)
self.failUnless(len(labels) == 0)
asm = None