pokecrystal-board/extras/wram.py

82 lines
2.2 KiB
Python
Raw Normal View History

2013-05-14 15:11:19 -07:00
# coding: utf-8
# RGBDS BSS section and constant parsing.
2013-07-02 09:19:14 -07:00
import os
path = os.path.dirname(os.path.abspath(__file__))
2013-05-14 15:11:19 -07:00
def read_bss_sections(bss):
sections = []
section = {}
address = None
if type(bss) is not list: bss = bss.split('\n')
for line in bss:
2013-05-14 17:25:43 -07:00
line = line.lstrip()
2013-05-14 15:11:19 -07:00
if 'SECTION' in line:
if section: sections.append(section) # last section
2013-05-14 17:25:43 -07:00
address = eval(line[line.find('[')+1:line.find(']')].replace('$','0x'))
2013-05-14 15:11:19 -07:00
section = {
'name': line.split('"')[1],
#'type': line.split(',')[1].split('[')[0].strip(),
'start': address,
'labels': [],
}
2013-05-14 15:11:19 -07:00
elif ':' in line:
# rgbds allows labels without :, but prefer convention
2013-05-14 17:25:43 -07:00
label = line[:line.find(':')]
2013-05-14 15:11:19 -07:00
if ';' not in label:
section['labels'] += [{
'label': label,
'address': address,
'length': 0,
}]
2013-05-14 17:25:43 -07:00
elif line[:3] == 'ds ':
length = eval(line[3:line.find(';')].replace('$','0x'))
2013-05-14 15:11:19 -07:00
address += length
# adjacent labels use the same space
for label in section['labels'][::-1]:
if label['length'] == 0:
label['length'] = length
else:
break
elif 'EQU' in line:
# some space is defined using constants
name, value = line.split('EQU')
name, value = name.strip(), value.strip().replace('$','0x').replace('%','0b')
globals()[name] = eval(value)
2013-05-14 15:11:19 -07:00
sections.append(section)
return sections
2013-07-02 09:19:14 -07:00
wram_sections = read_bss_sections(open(os.path.join(path, '../wram.asm'), 'r').readlines())
2013-05-14 15:11:19 -07:00
def make_wram_labels():
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()
def constants_to_dict(constants):
2013-05-14 17:25:43 -07:00
return dict((eval(constant[constant.find('EQU')+3:constant.find(';')].replace('$','0x')), constant[:constant.find('EQU')].strip()) for constant in constants)
2013-05-14 15:11:19 -07:00
def scrape_constants(text):
if type(text) is not list:
text = text.split('\n')
2013-05-14 17:25:43 -07:00
return constants_to_dict([line for line in text if 'EQU' in line[:line.find(';')]])
2013-05-14 15:11:19 -07:00
2013-07-02 09:19:14 -07:00
hram_constants = scrape_constants(open(os.path.join(path, '../hram.asm'),'r').readlines())
gbhw_constants = scrape_constants(open(os.path.join(path, '../gbhw.asm'),'r').readlines())
2013-05-14 15:11:19 -07:00