2013-01-27 15:03:00 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-21 01:37:13 -07:00
|
|
|
|
|
|
|
import networkx as nx
|
|
|
|
|
2013-01-27 15:03:51 -08:00
|
|
|
from romstr import (
|
|
|
|
RomStr,
|
|
|
|
relative_jumps,
|
|
|
|
call_commands,
|
|
|
|
relative_unconditional_jumps,
|
|
|
|
)
|
2012-06-21 01:37:13 -07:00
|
|
|
|
|
|
|
class RomGraph(nx.DiGraph):
|
|
|
|
""" Graphs various functions pointing to each other.
|
|
|
|
|
|
|
|
TODO: Bank switches are nasty. They should be detected. Otherwise,
|
|
|
|
functions will point to non-functions within the same bank. Another way
|
|
|
|
to detect bankswitches is retroactively. By disassembling one function
|
|
|
|
after another within the function banks, it can be roughly assumed that
|
|
|
|
anything pointing to something else (within the same bank) is really
|
|
|
|
actually a bankswitch. An even better method to handle bankswitches
|
|
|
|
would be to just detect those situations in the asm (but I presently
|
|
|
|
forget how bankswitches are performed in pokecrystal).
|
|
|
|
"""
|
|
|
|
|
|
|
|
# some areas shouldn't be parsed as asm
|
|
|
|
exclusions = []
|
|
|
|
|
|
|
|
# where is the first function located?
|
|
|
|
start_address = 0x150
|
|
|
|
|
|
|
|
# and where is a good place to stop?
|
2012-07-01 06:41:38 -07:00
|
|
|
end_address = 0x4000 * 0x03 # only do the first bank? sure..
|
2012-06-21 01:37:13 -07:00
|
|
|
|
|
|
|
# where is the rom stored?
|
|
|
|
rompath = "../baserom.gbc"
|
|
|
|
|
|
|
|
def __init__(self, rom=None, **kwargs):
|
|
|
|
""" Loads and parses the ROM into a function graph.
|
|
|
|
"""
|
|
|
|
# continue the initialization
|
|
|
|
nx.DiGraph.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
# load the graph
|
|
|
|
if rom == None:
|
|
|
|
self.load_rom()
|
|
|
|
else:
|
|
|
|
self.rom = rom
|
|
|
|
|
|
|
|
# start parsing the ROM
|
|
|
|
self.parse()
|
|
|
|
|
|
|
|
def load_rom(self):
|
|
|
|
""" Creates a RomStr from rompath.
|
|
|
|
"""
|
|
|
|
file_handler = open(self.rompath, "r")
|
|
|
|
self.rom = RomStr(file_handler.read())
|
|
|
|
file_handler.close()
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
""" Parses the ROM starting with the first function address. Each
|
|
|
|
function is disassembled and parsed to find where else it leads to.
|
|
|
|
"""
|
|
|
|
functions = {}
|
|
|
|
|
|
|
|
address = self.start_address
|
|
|
|
|
|
|
|
other_addresses = set()
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
while True:
|
2012-07-01 06:41:38 -07:00
|
|
|
if count > 3000:
|
2012-06-21 01:37:13 -07:00
|
|
|
break
|
|
|
|
|
2012-07-01 06:41:38 -07:00
|
|
|
if address < self.end_address and (address not in functions.keys()) and address >= 0x150:
|
2012-06-21 01:37:13 -07:00
|
|
|
# address is okay to parse at, keep going
|
|
|
|
pass
|
|
|
|
elif len(other_addresses) > 0:
|
|
|
|
# parse some other address possibly in a remote bank
|
|
|
|
address = other_addresses.pop()
|
|
|
|
else:
|
|
|
|
# no more addresses detected- exit loop
|
|
|
|
break
|
|
|
|
|
|
|
|
# parse the asm
|
|
|
|
func = self.rom.to_asm(address)
|
|
|
|
|
2012-07-01 06:41:38 -07:00
|
|
|
# check if there are any nops (probably not a function)
|
|
|
|
nops = 0
|
|
|
|
for (id, command) in func.asm_commands.items():
|
|
|
|
if command.has_key("id") and command["id"] == 0x0:
|
|
|
|
nops += 1
|
|
|
|
|
|
|
|
# skip this function
|
|
|
|
if nops > 1:
|
|
|
|
address = 0
|
|
|
|
continue
|
|
|
|
|
2012-06-21 01:37:13 -07:00
|
|
|
# store this parsed function
|
|
|
|
functions[address] = func
|
|
|
|
|
|
|
|
# where does this function jump to?
|
|
|
|
used_addresses = set(func.used_addresses())
|
|
|
|
|
|
|
|
# add this information to the graph
|
|
|
|
for used_address in used_addresses:
|
|
|
|
# only add this remote address if it's not yet parsed
|
|
|
|
if used_address not in functions.keys():
|
|
|
|
other_addresses.update([used_address])
|
|
|
|
|
|
|
|
# add this other address to the graph
|
2012-07-01 06:41:38 -07:00
|
|
|
if used_address > 100:
|
|
|
|
self.add_node(used_address)
|
2012-06-21 01:37:13 -07:00
|
|
|
|
|
|
|
# add this as an edge between the two nodes
|
|
|
|
self.add_edge(address, used_address)
|
|
|
|
|
|
|
|
# setup the next function to be parsed
|
|
|
|
address = func.last_address
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
self.functions = functions
|
|
|
|
|
|
|
|
def pretty_printer(self):
|
|
|
|
""" Shows some text output describing which nodes point to which other
|
|
|
|
nodes.
|
|
|
|
"""
|
|
|
|
print self.edges()
|
|
|
|
|
|
|
|
def to_d3(self):
|
|
|
|
""" Exports to d3.js because we're gangster like that.
|
|
|
|
"""
|
|
|
|
import networkx.readwrite.json_graph as json_graph
|
|
|
|
content = json_graph.dumps(self)
|
2012-07-01 06:41:38 -07:00
|
|
|
fh = open("crystal/crystal.json", "w")
|
2012-06-21 01:37:13 -07:00
|
|
|
fh.write(content)
|
|
|
|
fh.close()
|
|
|
|
|
2012-07-01 06:41:38 -07:00
|
|
|
def to_gephi(self):
|
|
|
|
""" Generates a gexf file.
|
|
|
|
"""
|
|
|
|
nx.write_gexf(self, "graph.gexf")
|
|
|
|
|
2012-06-21 01:37:13 -07:00
|
|
|
class RedGraph(RomGraph):
|
|
|
|
""" Not implemented. Go away.
|
|
|
|
"""
|
|
|
|
|
|
|
|
rompath = "../pokered-baserom.gbc"
|
|
|
|
|
|
|
|
class CryGraph(RomGraph):
|
|
|
|
exclusions = [
|
|
|
|
[0x000, 0x149],
|
|
|
|
]
|
|
|
|
|
|
|
|
rompath = "../baserom.gbc"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
crygraph = CryGraph()
|
|
|
|
crygraph.pretty_printer()
|
2012-07-01 06:41:38 -07:00
|
|
|
crygraph.to_gephi()
|