2013-01-27 14:42:20 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-01-27 14:41:35 -08:00
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import datetime
|
2012-06-14 19:54:25 -07:00
|
|
|
from ctypes import c_int8
|
2013-01-27 14:40:09 -08:00
|
|
|
from copy import copy
|
2013-01-27 15:14:55 -08:00
|
|
|
import json
|
|
|
|
|
|
|
|
# New versions of json don't have read anymore.
|
|
|
|
if not hasattr(json, "read"):
|
|
|
|
json.read = json.loads
|
2013-01-27 14:41:35 -08:00
|
|
|
|
|
|
|
from labels import (
|
|
|
|
get_label_from_line,
|
|
|
|
get_address_from_line_comment,
|
|
|
|
)
|
2012-06-06 23:07:48 -07:00
|
|
|
|
2012-06-19 21:42:12 -07:00
|
|
|
relative_jumps = [0x38, 0x30, 0x20, 0x28, 0x18, 0xc3, 0xda, 0xc2, 0x32]
|
2012-06-19 20:12:52 -07:00
|
|
|
relative_unconditional_jumps = [0xc3, 0x18]
|
|
|
|
call_commands = [0xdc, 0xd4, 0xc4, 0xcc, 0xcd]
|
|
|
|
end_08_scripts_with = [
|
|
|
|
0xe9, # jp hl
|
|
|
|
0xc9, # ret
|
|
|
|
] # possibly also:
|
2012-06-21 01:37:13 -07:00
|
|
|
# 0xc3, # jp
|
2012-06-19 20:12:52 -07:00
|
|
|
# 0xc18, # jr
|
2012-06-21 01:37:13 -07:00
|
|
|
# 0xda, 0xe9, 0xd2, 0xc2, 0xca, 0x38, 0x30, 0x20, 0x28, 0x18, 0xd8,
|
2012-06-19 20:12:52 -07:00
|
|
|
# 0xd0, 0xc0, 0xc8, 0xc9
|
|
|
|
|
2012-06-19 21:42:12 -07:00
|
|
|
spacing = "\t"
|
|
|
|
|
2012-05-29 11:08:43 -07:00
|
|
|
class RomStr(str):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Simple wrapper to prevent a giant rom from being shown on screen.
|
2012-05-29 11:08:43 -07:00
|
|
|
"""
|
|
|
|
|
2012-06-19 23:42:48 -07:00
|
|
|
def __init__(self, *args, **kwargs):
|
2012-07-01 06:48:27 -07:00
|
|
|
if "labels" in kwargs.keys() and kwargs["labels"] == True:
|
|
|
|
self.load_labels()
|
2012-06-19 23:42:48 -07:00
|
|
|
str.__init__(self)
|
|
|
|
|
2012-06-06 23:07:48 -07:00
|
|
|
def __repr__(self):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Simplifies this object so that the output doesn't overflow stdout.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
|
|
|
return "RomStr(too long)"
|
|
|
|
|
|
|
|
@classmethod
|
2013-02-03 13:18:06 -08:00
|
|
|
def load(cls, filename=None, crystal=True, red=False):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Loads a ROM into a RomStr.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2013-02-03 13:18:06 -08:00
|
|
|
if crystal and not red and not filename:
|
2012-06-06 23:07:48 -07:00
|
|
|
file_handler = open("../baserom.gbc", "r")
|
2013-02-03 13:18:06 -08:00
|
|
|
elif red and not crystal and not filename:
|
2012-06-06 23:07:48 -07:00
|
|
|
file_handler = open("../pokered-baserom.gbc", "r")
|
2013-02-03 13:18:06 -08:00
|
|
|
elif filename not in ["", None]:
|
|
|
|
file_handler = open(filename, "rb")
|
2012-06-06 23:07:48 -07:00
|
|
|
else:
|
2013-02-03 13:18:06 -08:00
|
|
|
raise Exception("not sure which rom to load?")
|
2012-06-06 23:07:48 -07:00
|
|
|
bytes = file_handler.read()
|
|
|
|
file_handler.close()
|
|
|
|
return RomStr(bytes)
|
|
|
|
|
2012-06-19 23:42:48 -07:00
|
|
|
def load_labels(self, filename="labels.json"):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Loads labels from labels.json, or parses the source code file and
|
|
|
|
generates new labels.
|
2012-06-19 23:42:48 -07:00
|
|
|
"""
|
2012-07-01 06:55:21 -07:00
|
|
|
filename = os.path.join(os.path.dirname(__file__), filename)
|
2012-07-01 06:44:20 -07:00
|
|
|
|
2012-06-19 23:42:48 -07:00
|
|
|
# blank out the hash
|
|
|
|
self.labels = {}
|
|
|
|
|
|
|
|
# check if the labels file exists
|
|
|
|
file_existence = os.path.exists(filename)
|
|
|
|
|
|
|
|
generate_labels = False
|
|
|
|
|
|
|
|
# determine if the labels file needs to be regenerated
|
|
|
|
if file_existence:
|
|
|
|
modified = os.path.getmtime(filename)
|
|
|
|
modified = datetime.datetime.fromtimestamp(modified)
|
|
|
|
current = datetime.datetime.fromtimestamp(time.time())
|
|
|
|
|
|
|
|
is_old = (current - modified) > datetime.timedelta(days=3)
|
|
|
|
|
|
|
|
if is_old:
|
|
|
|
generate_labels = True
|
|
|
|
else:
|
|
|
|
generate_labels = True
|
|
|
|
|
|
|
|
# scan the asm source code for labels
|
|
|
|
if generate_labels:
|
2012-07-01 06:55:21 -07:00
|
|
|
asm = open(os.path.join(os.path.dirname(__file__), "../main.asm"), "r").read().split("\n")
|
2012-06-19 23:42:48 -07:00
|
|
|
|
|
|
|
for line in asm:
|
|
|
|
label = get_label_from_line(line)
|
|
|
|
|
|
|
|
if label:
|
|
|
|
address = get_address_from_line_comment(line)
|
|
|
|
|
|
|
|
self.labels[address] = label
|
|
|
|
|
|
|
|
content = json.dumps(self.labels)
|
|
|
|
file_handler = open(filename, "w")
|
|
|
|
file_handler.write(content)
|
|
|
|
file_handler.close()
|
|
|
|
|
|
|
|
# load the labels from the file
|
2013-01-27 15:14:55 -08:00
|
|
|
self.labels = json.read(open(filename, "r").read())
|
2012-06-19 23:42:48 -07:00
|
|
|
|
2012-10-11 16:52:54 -07:00
|
|
|
def get_address_for(self, label):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Returns the address of a label. This is slow and could be improved
|
2012-10-11 16:52:54 -07:00
|
|
|
dramatically.
|
|
|
|
"""
|
|
|
|
label = str(label)
|
|
|
|
for address in self.labels.keys():
|
|
|
|
if self.labels[address] == label:
|
|
|
|
return address
|
|
|
|
return None
|
|
|
|
|
2012-05-29 11:08:43 -07:00
|
|
|
def length(self):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
len(self)
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2012-05-29 11:08:43 -07:00
|
|
|
return len(self)
|
|
|
|
|
2012-06-06 23:07:48 -07:00
|
|
|
def len(self):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
len(self)
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
|
|
|
return self.length()
|
2012-05-29 11:08:43 -07:00
|
|
|
|
|
|
|
def interval(self, offset, length, strings=True, debug=True):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
returns hex values for the rom starting at offset until offset+length
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
|
|
|
returnable = []
|
2012-05-29 11:08:43 -07:00
|
|
|
for byte in self[offset:offset+length]:
|
|
|
|
if strings:
|
|
|
|
returnable.append(hex(ord(byte)))
|
|
|
|
else:
|
|
|
|
returnable.append(ord(byte))
|
|
|
|
return returnable
|
|
|
|
|
|
|
|
def until(self, offset, byte, strings=True, debug=False):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Returns hex values from rom starting at offset until the given byte.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2012-05-29 11:08:43 -07:00
|
|
|
return self.interval(offset, self.find(chr(byte), offset) - offset, strings=strings)
|
|
|
|
|
2012-06-06 23:07:48 -07:00
|
|
|
def to_asm(self, address, end_address=None, size=None, max_size=0x4000, debug=None):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Disassembles ASM at some address. This will stop disassembling when
|
|
|
|
either the end_address or size is met. Also, there's a maximum size
|
|
|
|
that will be parsed, so that large patches of data aren't parsed as
|
|
|
|
code.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2013-01-27 14:46:52 -08:00
|
|
|
if type(address) in [str, unicode] and "0x" in address:
|
2012-06-19 21:59:49 -07:00
|
|
|
address = int(address, 16)
|
|
|
|
|
2012-06-06 23:07:48 -07:00
|
|
|
start_address = address
|
2012-06-19 21:59:49 -07:00
|
|
|
|
2012-06-06 23:07:48 -07:00
|
|
|
if start_address == None:
|
|
|
|
raise Exception, "address must be given"
|
|
|
|
|
|
|
|
if debug == None:
|
|
|
|
if not hasattr(self, "debug"):
|
|
|
|
debug = False
|
|
|
|
else:
|
|
|
|
debug = self.debug
|
|
|
|
|
|
|
|
# this is probably a terrible idea.. why am i doing this?
|
|
|
|
if size != None and max_size < size:
|
|
|
|
raise Exception, "max_size must be greater than or equal to size"
|
|
|
|
elif end_address != None and (end_address - start_address) > max_size:
|
|
|
|
raise Exception, "end_address is out of bounds"
|
|
|
|
elif end_address != None and size != None:
|
|
|
|
if (end_address - start_address) >= size:
|
|
|
|
size = end_address - start_address
|
|
|
|
else:
|
|
|
|
end_address = start_address + size
|
|
|
|
elif end_address == None and size != None:
|
|
|
|
end_address = start_address + size
|
|
|
|
elif end_address != None and size == None:
|
|
|
|
size = end_address - start_address
|
|
|
|
|
2013-01-27 14:49:35 -08:00
|
|
|
raise NotImplementedError("DisAsm was removed and never worked; hook up another disassembler please.")
|
|
|
|
#return DisAsm(start_address=start_address, end_address=end_address, size=size, max_size=max_size, debug=debug, rom=self)
|
2012-06-06 23:07:48 -07:00
|
|
|
|
2012-05-29 11:19:02 -07:00
|
|
|
class AsmList(list):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Simple wrapper to prevent all asm lines from being shown on screen.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2012-05-29 11:19:02 -07:00
|
|
|
|
|
|
|
def length(self):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
len(self)
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2012-05-29 11:19:02 -07:00
|
|
|
return len(self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2013-03-21 13:15:42 -07:00
|
|
|
"""
|
|
|
|
Simplifies this object so that the output doesn't overflow stdout.
|
2012-06-06 23:07:48 -07:00
|
|
|
"""
|
2012-05-29 11:19:02 -07:00
|
|
|
return "AsmList(too long)"
|
|
|
|
|
2012-06-19 21:59:49 -07:00
|
|
|
if __name__ == "__main__":
|
|
|
|
cryrom = RomStr(open("../pokecrystal.gbc", "r").read());
|
|
|
|
asm = cryrom.to_asm(sys.argv[1])
|
|
|
|
print asm
|