make wram.py use config for paths

The WRAMProcessor class handles reading all constants and labels, which
are then used in gbz80disasm.
This commit is contained in:
Bryan Bishop
2013-09-02 12:24:39 -05:00
parent 2cf0cfa34d
commit 5b02c020f4
2 changed files with 96 additions and 52 deletions

View File

@@ -7,7 +7,12 @@ from ctypes import c_int8
import random
import json
from wram import *
import config
import wram
conf = config.Config()
wramp = wram.WRAMProcessor(conf)
wramp.initialize()
# New versions of json don't have read anymore.
if not hasattr(json, "read"):
@@ -593,9 +598,9 @@ def find_label(local_address, bank_id=0):
if get_local_address(label_entry["address"]) == local_address:
if label_entry["bank"] == bank_id or label_entry["bank"] == 0:
return label_entry["label"]
if local_address in wram_labels.keys():
return wram_labels[local_address][-1]
for constants in [gbhw_constants, hram_constants]:
if local_address in wramp.wram_labels.keys():
return wramp.wram_labels[local_address][-1]
for constants in [wramp.gbhw_constants, wramp.hram_constants]:
if local_address in constants.keys() and local_address >= 0xff00:
return constants[local_address]
return None

View File

@@ -4,7 +4,15 @@ RGBDS BSS section and constant parsing.
"""
import os
path = os.path.dirname(os.path.abspath(__file__))
def make_wram_labels(wram_sections):
wram_labels = {}
for section in wram_sections:
for label in section['labels']:
if label['address'] not in wram_labels.keys():
wram_labels[label['address']] = []
wram_labels[label['address']] += [label['label']]
return wram_labels
def read_bss_sections(bss):
sections = []
@@ -55,34 +63,6 @@ def read_bss_sections(bss):
sections.append(section)
return sections
def read_wram_sections():
"""
Opens the wram file and calls read_bss_sections.
"""
wram_content = None
wram_file_path = os.path.join(os.path.dirname(path), 'wram.asm')
try:
wram_file_handler = open(wram_file_path, 'r')
except IOError as exception:
wram_content = [""]
else:
wram_content = wram_file_handler.readlines()
wram_sections = read_bss_sections(wram_content)
return wram_sections
wram_sections = read_wram_sections()
def make_wram_labels(wram_sections):
wram_labels = {}
for section in wram_sections:
for label in section['labels']:
if label['address'] not in wram_labels.keys():
wram_labels[label['address']] = []
wram_labels[label['address']] += [label['label']]
return wram_labels
wram_labels = make_wram_labels(wram_sections)
def constants_to_dict(constants):
return dict((eval(constant[constant.find('EQU')+3:constant.find(';')].replace('$','0x')), constant[:constant.find('EQU')].strip()) for constant in constants)
@@ -95,31 +75,90 @@ def read_constants(filepath):
"""
Load lines from a file and call scrape_constants.
"""
try:
file_handler = open(filepath, "r")
except IOError as exception:
lines = [""]
else:
lines = None
with open(filepath, "r") as file_handler:
lines = file_handler.readlines()
constants = scrape_constants(lines)
return constants
def read_hram_constants():
class WRAMProcessor(object):
"""
Load constants from hram.asm.
RGBDS BSS section and constant parsing.
"""
hram_path = os.path.join(os.path.dirname(path), 'hram.asm')
return read_constants(hram_path)
# TODO: get rid of this global
hram_constants = read_hram_constants()
def __init__(self, config):
"""
Setup for WRAM parsing.
"""
self.config = config
def read_gbhw_constants():
"""
Load constants from gbhw.asm.
"""
gbhw_path = os.path.join(os.path.dirname(path), 'gbhw.asm')
return read_constants(gbhw_path)
self.paths = {}
self.paths["wram"] = os.path.join(self.config.path, "wram.asm")
self.paths["hram"] = os.path.join(self.config.path, "hram.asm")
self.paths["gbhw"] = os.path.join(self.config.path, "gbhw.asm")
# TODO: get rid of this global
gbhw_constants = read_gbhw_constants()
def initialize(self):
"""
Read constants.
"""
self.setup_wram_sections()
self.setup_wram_labels()
self.setup_hram_constants()
self.setup_gbhw_constants()
def read_wram_sections(self):
"""
Opens the wram file and calls read_bss_sections.
"""
wram_content = None
wram_file_path = self.paths["wram"]
with open(wram_file_path, "r") as wram:
wram_content = wram.readlines()
wram_sections = read_bss_sections(wram_content)
return wram_sections
def setup_wram_sections(self):
"""
Call read_wram_sections and set a variable.
"""
self.wram_sections = self.read_wram_sections()
return self.wram_sections
def setup_wram_labels(self):
"""
Make wram labels based on self.wram_sections as input.
"""
self.wram_labels = make_wram_labels(self.wram_sections)
return self.wram_labels
def read_hram_constants(self):
"""
Read constants from hram.asm using read_constants.
"""
hram_constants = read_constants(self.paths["hram"])
return hram_constants
def setup_hram_constants(self):
"""
Call read_hram_constants and set a variable.
"""
self.hram_constants = self.read_hram_constants()
return self.hram_constants
def read_gbhw_constants(self):
"""
Read constants from gbhw.asm using read_constants.
"""
gbhw_constants = read_constants(self.paths["gbhw"])
return gbhw_constants
def setup_gbhw_constants(self):
"""
Call read_gbhw_constants and set a variable.
"""
self.gbhw_constants = self.read_gbhw_constants()
return self.gbhw_constants