-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile_sfr.py
150 lines (104 loc) · 2.69 KB
/
compile_sfr.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
#!/usr/bin/python
import sys
###### Our generic parse exception class - used to teach davidc__ exceptions!
class ParseError:
error=""
def __init__(self, error_str):
self.error = error_str
def __str__(self):
return self.error
##### Our global SFR type
class SFR:
start_addr = 0
elem_size = 0
elem_count = 0
name = "UNASSIGNED"
elem_init = []
def __init__(self, sfr_addr, sfr_name, size=4, count=1, init=[0]):
self.start_addr = sfr_addr
self.name = sfr_name
self.elem_size = size
self.elem_count = count
self.elem_init = init
def __str__ (self):
return "%#x %s %d %d %s" % (self.start_addr, self.name,
self.elem_size, self.elem_count, self.elem_init)
###### define our globals
global sfr_list
sfr_list = []
global proc_type
proc_type = "UNKNOWN"
################# CODE GENERATORS ####################
################# PARSING CODE #######################
###### proc
def handle_proc(line):
global proc_type
proc_type = line[1]
###### sfr
def create_default_sfr(base, name, args):
# We need to parse the args
s = SFR(base,name)
sfr_list.append(s)
def handle_sfr(line):
start_addr_text = line[1]
# Remove colon separators
start_addr_text = start_addr_text.replace(":","")
# Add an 0x
if (start_addr_text[:1].lower() != "0x"):
start_addr_text = "0x" + start_addr_text
start_addr = int(start_addr_text,16)
name = line[2]
if (len(line) > 3):
type = line[3].upper()
else:
type = "DEFAULT"
# Now - decide based upon the type, what kind of SFR to instantiate
if (type == "DEFAULT"):
create_default_sfr(start_addr, name, line[4:])
else:
raise ParseError ("Unrecognized SFR type %s" % type)
###### line parse
def handle_line(line):
kwd = line[0].upper();
if (kwd == "PROC"):
handle_proc(line)
elif (kwd == "SFR"):
handle_sfr(line)
###### main
# Make sure the args are sane
if len(sys.argv) != 2:
print "Error - usage is " + sys.argv[0] + " filename\n"
file_name = sys.argv[1]
# calculate the file prefix we'll write to
file_per_ind = file_name.rfind(".")
if file_per_ind == -1:
print "Error - could not understand filename"
sys.exit(0)
file_prefix = file_name[:file_per_ind]
# open our file
f = open(file_name)
line_num = 0
# iterate through all the lines
line = f.readline()
while (line != ""):
line_num = line_num + 1
comment_end = line.find("#")
if (comment_end != -1):
line = line[0:comment_end]
# strip off all excess whitespace
line = line.strip();
# split the line
line_ar = line.split();
try:
if (len(line_ar)!=0):
handle_line(line_ar)
except ParseError, err:
print "[%4d] Parse error: " % (line_num) + str(err)
line = f.readline()
f.close()
print proc_type
print "["
for s in sfr_list:
print s
print "]"
###### end main