-
Notifications
You must be signed in to change notification settings - Fork 8
/
const.py
269 lines (234 loc) · 5.6 KB
/
const.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/local/bin/python
#
from __future__ import print_function
from struct import unpack, pack
import tree
class byte(tree.tree):
def __init__(self, p, adr, len = 1, fmt="0x%02x"):
tree.tree.__init__(self, adr, adr + len, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.fmt = fmt
def rfunc(self, p, t):
s = ".BYTE\t"
d = ""
for i in range(t.start, t.end):
x = p.m.rd(i)
s += d
s += self.fmt % x
d = ", "
return (s,)
class w16(tree.tree):
def __init__(self, p, adr, len = 1, fmt="0x%04x"):
tree.tree.__init__(self, adr, adr + len * 2, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.fmt = fmt
def rfunc(self, p, t):
s = ".WORD\t"
d = ""
for i in range(t.start, t.end, 2):
x = p.m.w16(i)
s += d
s += self.fmt % x
d = ", "
return (s,)
class w32(tree.tree):
def __init__(self, p, adr, len = 1, fmt="0x%08x"):
tree.tree.__init__(self, adr, adr + len * 4, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.fmt = fmt
def rfunc(self, p, t):
s = ".LWORD\t"
d = ""
for i in range(t.start, t.end, 4):
x = p.m.w32(i)
s += d
s += self.fmt % x
d = ", "
return (s,)
class ptr(tree.tree):
def __init__(self, p, adr, width = 2, len = 1):
tree.tree.__init__(self, adr, adr + len * width, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.width = width
def rfunc(self, p, t):
s = ".PTR\t"
d = ""
for i in range(t.start, t.end, self.width):
if self.width == 2:
x = p.m.w16(i)
elif self.width == 4:
x = p.m.w32(i)
else:
assert self.width == "Wrong width"
s += d
if x in p.label:
s += p.label[x]
else:
s += p.m.afmt(x)
d = ", "
return (s,)
class ieee_fp64(tree.tree):
def __init__(self, p, adr, endian=">"):
tree.tree.__init__(self, adr, adr + 8, "ieee_fp64")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
b = bytearray()
for i in range(8):
b.append(p.m.rd(adr + i))
self.fp = unpack(endian + "d", b)
def rfunc(self, p, t):
return (".FP64\t%.15g" % self.fp,)
class ltxt(tree.tree):
def __init__(self, p, adr, align=2):
l = p.m.rd(adr)
len = l + 1
len += align - 1
len &= ~(align -1)
tree.tree.__init__(self, adr, adr + len, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.l = l
self.txt = p.m.ascii(adr + 1, l)
def rfunc(self, p, t):
s = ".LTXT\t%d,'" % self.l + self.txt + "'"
return (s,)
class txtlen(tree.tree):
def __init__(self, p, adr, len, align=1):
llen = len + align - 1
llen &= ~(align -1)
tree.tree.__init__(self, adr, adr + llen, "const")
p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
self.txt = p.m.ascii(adr, len)
def rfunc(self, p, t):
s = ".TXT\t'" + self.txt + "'"
return (s,)
class txt(tree.tree):
def __init__(self, p, adr, len = None, align=1):
elen = 0
self.term=""
if len == None:
len = 0;
while True:
c = p.m.rd(adr + len)
if c == 0x00:
self.term=",0"
elen = 1
break;
len += 1
if c != 0x0a:
continue
# We treat newlines special:
# If it looks like text continues
# start another instance on the next line
# XXX: means length don't match, bad!
c2 = p.m.rd(adr + len)
if c2 == 0x0a or c2 == 0x00:
continue
txt(p, adr + len)
break;
if len <= 0:
print("BOGUS const.txt @%x" % adr)
return
if align == 2 and (adr+len+elen) & 1:
elen += 1
tree.tree.__init__(self, adr, adr + len + elen, "const")
x = p.t.add(self.start, self.end, self.tag, True, self)
self.render = self.rfunc
if len > 0:
self.txt = p.m.ascii(adr, len)
else:
self.txt = ""
def rfunc(self, p, t):
s = ".TXT\t'" + self.txt + "'" + self.term
return (s,)
class fill(tree.tree):
def __init__(self, p, lo = None, mid = None, hi = None, fmt = None, rd = None):
if rd == None:
rd = p.m.rd
if fmt == None:
fmt = p.m.dpct
if lo == None:
if mid != None:
lo = mid
elif hi != None:
lo = hi
else:
print("BOGUS fill(", lo, ",", mid, ",", hi, ")")
return
x = rd(lo)
while x == rd(lo - 1):
lo -= 1
if hi == None:
if mid != None:
hi = mid
elif lo != None:
hi = lo
else:
print("BOGUS fill(", lo, ",", mid, ",", hi, ")")
return
x = rd(hi)
try:
while x == rd(hi + 1):
hi += 1
except:
pass
hi += 1
if lo == None or hi == None:
print("BOGUS fill(", lo, ",", mid, ",", hi, ")")
return
tree.tree.__init__(self, lo, hi, "fill")
x = p.t.add(self.start, self.end, self.tag, True, self)
x.render = ".Fill\t" + \
fmt % rd(lo) + "[" + fmt % (hi-lo) + "]"
x.fold = True
def seven_seg_lcmt(y, val, map = ( 1, 2, 4, 8, 16, 32, 64, 128, 0)):
if val & map[0]:
y.lcmt(" --")
else:
y.lcmt(" ")
if val & map[5]:
s = " |"
else:
s = " "
if val & map[1]:
y.lcmt(s + " |")
else:
y.lcmt(s + " ")
if val & map[6]:
y.lcmt(" --")
else:
y.lcmt(" ")
if val & map[4]:
s = " |"
else:
s = " "
if val & map[2]:
y.lcmt(s + " |")
else:
y.lcmt(s + " ")
if val & map[8]:
s = "."
else:
s = " "
if val & map[3]:
s += " -- "
else:
s += " "
if val & map[7]:
y.lcmt(s + ".")
else:
y.lcmt(s)
y.lcmt(" ")
class seven_segment(tree.tree):
def __init__(self, p, adr,
map = ( 1, 2, 4, 8, 16, 32, 64, 128, 0)):
tree.tree.__init__(self, adr, adr + 1, "7seg")
y = p.t.add(self.start, self.end, self.tag, True, self)
val = p.m.rd(adr)
y.render = ".BYTE 0x%02x" % val
seven_seg_lcmt(y, val, map)