diff options
| author | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 12:24:39 -0500 | 
|---|---|---|
| committer | Bryan Bishop <kanzure@gmail.com> | 2013-09-02 12:33:03 -0500 | 
| commit | 5b02c020f4784ba570f7da73a9835f5e984b8a90 (patch) | |
| tree | 0ab6294cad1eaba379dad95d718312bda530b22b /pokemontools/wram.py | |
| parent | 2cf0cfa34d008d3f845082a86ec037311570396b (diff) | |
make wram.py use config for paths
The WRAMProcessor class handles reading all constants and labels, which
are then used in gbz80disasm.
Diffstat (limited to 'pokemontools/wram.py')
| -rw-r--r-- | pokemontools/wram.py | 139 | 
1 files changed, 89 insertions, 50 deletions
| diff --git a/pokemontools/wram.py b/pokemontools/wram.py index 5a5fa75..60001aa 100644 --- a/pokemontools/wram.py +++ b/pokemontools/wram.py @@ -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(): -    """ -    Load constants from hram.asm. -    """ -    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 read_gbhw_constants(): +class WRAMProcessor(object):      """ -    Load constants from gbhw.asm. +    RGBDS BSS section and constant parsing.      """ -    gbhw_path = os.path.join(os.path.dirname(path), 'gbhw.asm') -    return read_constants(gbhw_path) -# TODO: get rid of this global -gbhw_constants = read_gbhw_constants() +    def __init__(self, config): +        """ +        Setup for WRAM parsing. +        """ +        self.config = config + +        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") + +    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 | 
