-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgbmapviz.py
202 lines (172 loc) · 8.32 KB
/
gbmapviz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys, os, re, argparse
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
def main(args):
# parse input mapfile
inf_path = Path(args.infname)
rgbasm_map = RGBASMMap(inf_path.read_text(), args.w)
# output html
outf_path = inf_path.with_suffix('.html')
outf_path.write_text(rgbasm_map.to_html())
class RGBASMMap:
"""
Parse .map file generated by rgbds linker (`rgblink`)
into a list of RGBASMMapBank objects
"""
def __init__(self, mapfile_data, wram_single_bank, template_name="template.html"):
self.wram_single_bank = wram_single_bank
self.banks = self.__parse_to_banks(mapfile_data)
self.html_template = Environment(loader=FileSystemLoader(searchpath=os.path.dirname(__file__))).get_template(template_name)
def __parse_to_banks(self, mapfile_data):
# split by bank and parse each
bank_strs = mapfile_data.replace('\r','\n').strip().split('\n\n')
banks = [RGBASMMapBank(bank_str, self.wram_single_bank) for bank_str in bank_strs]
return banks
def __len__(self):
return sum(map(len, self.banks))
def to_html(self):
# pass self to jinja template
return self.html_template.render(gbmap=self)
class RGBASMMapBank:
"""
Parse a single bank string's name and
a sorted list of RGBASMMapSection objects
(Size, start address, and color palette then derive from name)
"""
def __init__(self, bank_str, wram_single_bank):
self.wram_single_bank = wram_single_bank
self.name, self.sections = self.__parse_to_name_and_sections(bank_str)
self.size = self.__get_size()
self.start_address = self.__get_start_address()
self.color_palette = self.__get_color_palette()
@staticmethod
def __parse_to_name_and_sections(bank_str):
# regex split by section
section_strs = re.split(r"\n\s*(?:SECTION:|SLACK:|EMPTY)", bank_str.strip())
# derive abbreviated bank name
bank_name = re.sub(r'^(\S+)(?: Bank #(\S+))?.*$', r'\1\2', section_strs[0].strip()).rstrip(':')
# parse remainder of sections
sections = [RGBASMMapSection(section_str) for section_str in section_strs[1:-1]]
# sort ascending
sections.sort(key=lambda section: section.start)
return bank_name, sections
def __len__(self):
return sum(map(len, self.sections))
def svg_section_info(self, width, height):
result = []
# assign colors and gather info for svg <rect> elements for each section
current_color_i = -1
for section in self.sections:
# modulate x/width to total width
section_x = (section.start - self.start_address) * width / self.size
section_width = len(section) * width / self.size
# if width > 0, cycle to next color in palette
if section_width > 0:
current_color_i = (current_color_i + 1) % len(self.color_palette)
section_title = f'${section.start:04X}{f"–${section.end:04X}" if len(section) > 0 else ""}: {section.name}'
result.append((section_x, section_width, self.color_palette[current_color_i], section_title))
return result
def section_info(self):
result = []
# assign colors and gather info for table rows from each section
current_bg_color_i = -1
for i, section in enumerate(self.sections):
# if length > 0, cycle to next color in palette
if len(section) > 0:
current_bg_color_i = (current_bg_color_i + 1) % len(self.color_palette)
current_bg_color = self.color_palette[current_bg_color_i]
else:
current_bg_color = (255,255,255)
result.append(section.symbol_info(bg_color=current_bg_color))
return result
def __get_size(self):
# based on first letter of bank name
return {'R' : 0x4000, # ROM0/ROMX
'V' : 0x2000, # VRAM
'S' : 0x2000, # SRAM
'W' : 0x2000 if self.wram_single_bank else 0x1000, # WRAM
'O' : 0x00A0, # OAM
'H' : 0x007F }.get(self.name[0]) # HRAM
def __get_start_address(self):
# based on bank name
if self.name == "ROM0":
return 0x0000 # ROM0
if self.name == "WRAM0":
return 0xC000 # WRAM0
return {'R' : 0x4000, # ROMX
'V' : 0x8000, # VRAM
'S' : 0xA000, # SRAM
'W' : 0xD000, # WRAMX
'O' : 0xFE00, # OAM
'H' : 0xFF80 }.get(self.name[0]) # HRAM
PALETTES = {
'red' : ((103, 0, 13),(165, 15, 21),(203, 24, 29),
(239, 59, 44),(251,106, 74),(251,106, 74),
(252,187,161),(254,224,210),(255,245,240)),
'orange' : ((127, 39, 4),(166, 54, 3),(217, 72, 1),
(241,105, 19),(253,141, 60),(253,174,107),
(253,208,162),(254,230,206),(255,245,235)),
'yellow' : ((102, 37, 6),(153, 52, 4),(204, 76, 2),
(236,112, 20),(254,153, 41),(254,196, 79),
(254,227,145),(255,247,188),(255,255,229)),
'green' : (( 0, 68, 27),( 0,109, 44),( 35,139, 69),
( 65,171, 93),(116,196,118),(161,217,155),
(199,233,192),(229,245,224),(247,252,245)),
'blue' : (( 8, 48,107),( 8, 81,156),( 33,113,181),
( 66,146,198),(107,174,214),(158,202,225),
(198,219,239),(222,235,247),(247,251,255)),
'purple' : (( 63, 0,125),( 84, 39,143),(106, 81,163),
(128,125,186),(158,154,200),(188,189,220),
(218,218,235),(239,237,245),(252,251,253))
}
def __get_color_palette(self):
# to use for svg visualization
if self.name == "ROM0":
return self.PALETTES['red'] # ROM0
return {'R' : self.PALETTES['orange'], # ROMX
'V' : self.PALETTES['yellow'], # VRAM
'S' : self.PALETTES['green'], # SRAM
'W' : self.PALETTES['blue'] # WRAM
}.get(self.name[0], self.PALETTES['purple']) # OAM/HRAM
class RGBASMMapSection:
"""
Parse a single section string into its name, start and end address, and list of symbol tuples (addr, name)
"""
def __init__(self, section_str):
self.start, self.end, self.name, self.symbols = self.__parse(section_str)
def __len__(self):
return max(0, self.end - self.start)
def symbol_info(self, bg_color=(255,255,255)):
# gather info for section table header row
# and table row for each symbol within section
section_loc_text_color = 'black' if sum(bg_color)/3 > 127 else 'white'
section_loc = f"${self.start:04X}"
if len(self) > 0:
section_loc += f"–${self.end:04X}"
return (section_loc_text_color, bg_color, section_loc, self.name, [(f"${symbol[0]:04X}",symbol[1]) for symbol in self.symbols])
def __parse(self, section_str):
# split section into locations/location ranges
location_strs = section_str.strip().split('\n')
# head has range and name
section_head_parsed = re.match(r'(\$....(?:\-\$....)?)[^\[]+(?:\["(.*)"\])$', location_strs[0])
section_range, section_name = section_head_parsed.groups()
if len(section_range) == 5:
# single address means the same start and end
section_range = section_range + '-' + section_range
# convert hex range to start/end ints
start_addr, end_addr = int(section_range[1:5], 16), int(section_range[7:11], 16)
symbols = []
for sublocation_str in location_strs[1:]:
sublocation_parsed = re.match(r'(\$....) = (.*)$', sublocation_str.strip())
subloc_addr_str, subloc_name = sublocation_parsed.groups()
subloc_addr = int(subloc_addr_str[1:], 16)
symbols.append((subloc_addr, subloc_name))
symbols.sort()
return start_addr, end_addr, section_name, symbols
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('infname', help="input mapfile filename")
parser.add_argument('-w', help="single WRAM bank mode (see rgblink -w)", action="store_true")
main(parser.parse_args())