-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrammap_parse.py
55 lines (44 loc) · 1.65 KB
/
rammap_parse.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
import sys
addresses = ["" for i in range(2048)]
if len(sys.argv) != 3:
sys.stderr.write("Usage: %s <input> <output>\n" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1],'r') as inf:
for line in inf:
try:
if line[0] == '\r' or line[0] == '\n':
continue
if line[0].isspace():
continue
start_addr = int(line[0:4], 16)
if start_addr > 2047:
sys.stderr.write("Warning: address %04x > %04x encountered\n" % (start_addr, 2047))
continue
if line[4] == '-':
assert line[10] == '-'
end_addr = int(line[5:9], 16)
for i in range(start_addr, end_addr+1):
if addresses[i] != "":
sys.stderr.write("Warning: duplicate entry for line %04x\n" % i)
addresses[i] = line[11:].strip()
else:
assert line[5] == '-'
if addresses[start_addr] != "":
sys.stderr.write("Warning: duplicate entry for line %04x\n" % start_addr)
addresses[start_addr] = line[6:].strip()
except Exception as e:
sys.stderr.write("Warning: %s\n" % e)
with open(sys.argv[2], 'w') as outf:
first = True
outf.write('[\n')
for i,desc in enumerate(addresses):
if desc == "":
continue
if first:
first = False
else:
outf.write(',\n')
if desc.find('(') > -1:
desc = desc[:desc.find('(')].strip()
outf.write('{"addr":%d,"text":"%s"}' % (i, desc))
outf.write('\n]\n')