diff options
author | U-Daniel-PC\Daniel <corrnondacqb@yahoo.com> | 2014-07-26 17:22:43 -0500 |
---|---|---|
committer | U-Daniel-PC\Daniel <corrnondacqb@yahoo.com> | 2014-07-26 17:22:43 -0500 |
commit | 579a94910801e88c2cc500e870695f6d3273da22 (patch) | |
tree | d7451abebcb7fe109b10332f3e52f8421df9ce2c /extras/labels.py | |
parent | b2c80cf49bf0e16b4b529ff31221dc65ddce797f (diff) |
Add disassembly python tools
https://github.com/kanzure/pokemon-reverse-engineering-tools
Diffstat (limited to 'extras/labels.py')
-rwxr-xr-x | extras/labels.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/extras/labels.py b/extras/labels.py new file mode 100755 index 0000000..bbd0079 --- /dev/null +++ b/extras/labels.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- +""" +Various label/line-related functions. +""" + +import os + +class Labels(object): + """ + Store all labels. + """ + + def __init__(self, config, filename="tcg.map"): + """ + Setup the instance. + """ + self.config = config + self.filename = filename + self.path = os.path.join(self.config.path, self.filename) + + def initialize(self): + """ + Handle anything requiring file-loading and such. + """ + # Look for a mapfile if it's not given + if not os.path.exists(self.path): + self.filename = find_mapfile_in_dir(self.config.path) + if self.filename == None: + raise Exception, "Couldn't find any mapfiles. Run rgblink -m to create a mapfile." + self.path = os.path.join(self.config.path, self.filename) + + self.labels = read_mapfile(self.path) + +def find_mapfile_in_dir(path): + for filename in os.listdir(path): + if os.path.splitext(filename)[1] == '.map': + return filename + return None + +def read_mapfile(filename='tcg.map'): + """ + Scrape label addresses from an rgbds mapfile. + """ + + labels = [] + + with open(filename, 'r') as mapfile: + lines = mapfile.readlines() + + for line in lines: + if line[0].strip(): # section type def + section_type = line.split(' ')[0] + if section_type == 'Bank': # ROM + cur_bank = int(line.split(' ')[1].split(':')[0][1:]) + elif section_type in ['WRAM0', 'HRAM']: + cur_bank = 0 + elif section_type in ['WRAM, VRAM']: + cur_bank = int(line.split(' ')[2].split(':')[0][1:]) + cur_bank = int(line.split(' ')[2].split(':')[0][1:]) + + # label definition + elif '=' in line: + address, label = line.split('=') + address = int(address.lstrip().replace('$', '0x'), 16) + label = label.strip() + + bank = cur_bank + offset = address + if address < 0x8000 and bank: # ROM + offset += (bank - 1) * 0x4000 + + labels += [{ + 'label': label, + 'bank': bank, + 'address': offset, + 'offset': offset, + 'local_address': address, + }] + + return labels + |