summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Huderle <huderlem@gmail.com>2015-10-19 20:35:42 -0700
committerMarcus Huderle <huderlem@gmail.com>2015-10-19 20:35:42 -0700
commit23fb97bf2638fcab85e90c538033583c40fa32f7 (patch)
tree1901dc6b132b3a765558251371a1ce8df8d53bf9
parent2ca31f660bf00a2892b8c07c9c81d456dc9b67e7 (diff)
parent7a6efcfdf1500b01517c2bebf73313ec1ddee805 (diff)
Merge pull request #94 from huderlem/master
Read labels from symfile, not mapfile.
-rw-r--r--pokemontools/labels.py14
-rw-r--r--pokemontools/sym.py35
2 files changed, 42 insertions, 7 deletions
diff --git a/pokemontools/labels.py b/pokemontools/labels.py
index 72700a5..59a6160 100644
--- a/pokemontools/labels.py
+++ b/pokemontools/labels.py
@@ -15,7 +15,7 @@ class Labels(object):
Store all labels.
"""
- def __init__(self, config, filename="pokecrystal.map"):
+ def __init__(self, config, filename="pokecrystal.sym"):
"""
Setup the instance.
"""
@@ -27,18 +27,18 @@ class Labels(object):
"""
Handle anything requiring file-loading and such.
"""
- # Look for a mapfile if it's not given
+ # Look for a symfile if it's not given
if not os.path.exists(self.path):
- self.filename = find_mapfile_in_dir(self.config.path)
+ self.filename = find_symfile_in_dir(self.config.path)
if self.filename == None:
- raise Exception, "Couldn't find any mapfiles. Run rgblink -m to create a mapfile."
+ raise Exception, "Couldn't find any .sym files. Run rgblink -n to create a .sym file."
self.path = os.path.join(self.config.path, self.filename)
- self.labels = sym.read_mapfile(self.path)
+ self.labels = sym.read_symfile(self.path)
-def find_mapfile_in_dir(path):
+def find_symfile_in_dir(path):
for filename in os.listdir(path):
- if os.path.splitext(filename)[1] == '.map':
+ if os.path.splitext(filename)[1] == '.sym':
return filename
return None
diff --git a/pokemontools/sym.py b/pokemontools/sym.py
index b1e755f..3c9914d 100644
--- a/pokemontools/sym.py
+++ b/pokemontools/sym.py
@@ -1,6 +1,7 @@
# coding: utf-8
import os
+import re
import sys
import json
@@ -81,6 +82,40 @@ def make_sym_from_mapfile(filename = '../pokecrystal.sym', mapfile = '../mapfile
with open(filename, 'w') as sym:
sym.write(output)
+def read_symfile(filename='pokecrystal.sym'):
+ """
+ Scrape label addresses from an rgbds .sym file.
+ """
+ labels = []
+
+ with open(filename, 'r') as symfile:
+ lines = symfile.readlines()
+
+ # Example line from sym file: "06:5531 Func_19531"
+ label_regex = re.compile('([0-9A-Fa-f]+):([0-9A-Fa-f]+) (\S+)')
+
+ for line in lines:
+ match = label_regex.match(line)
+ if match:
+ bank = int(match.group(1), 16)
+ local_address = int(match.group(2), 16)
+ label = match.group(3)
+ absolute_address = local_address
+
+ if local_address < 0x8000 and bank > 0:
+ absolute_address += (bank - 1) * 0x4000
+
+ labels += [{
+ 'label': label,
+ 'bank': bank,
+ 'address': absolute_address,
+ 'offset': absolute_address,
+ 'local_address': local_address,
+ }]
+
+ return labels
+
+
if __name__ == "__main__":
#if os.path.exists('../pokecrystal.sym'):
# sys.exit()