-
Notifications
You must be signed in to change notification settings - Fork 8
/
model.py
287 lines (247 loc) · 7.14 KB
/
model.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/local/bin/python3.2
#
# A list-based language to model instructions.
#
# Values are a tuple of (width, value) where value can be None if
# it is unknown, and False if it is unused.
#
# Syntax:
# ( Verb, arg, ...)
# "#0b..." binary constant, number of bits given sets width
# "#0x..." hex constant, number of nybbles given sets width
# "/..." register
import random
class ModelError(Exception):
def __init__(self, reason):
self.reason = reason
self.value = str(self.reason)
def __str__(self):
return repr(self.value)
class model(object):
def __init__(self):
self.verbs = {
"SEQ": (self.verb_seq, "..."),
">>": (self.verb_right_shift, "val", "bits"),
"<<": (self.verb_left_shift, "val", "bits" "[bi]" "[bo]"),
"+": (self.verb_add, "val", "val" "[ci]" "[co]"),
"-": (self.verb_sub, "val", "val" "[bi]" "[bo]"),
"=": (self.verb_assign, "dest", "val"),
"AND": (self.verb_and, "val1", "val2"),
"OR": (self.verb_or, "val1", "val2"),
"RND": (self.verb_rnd, None),
"XOR": (self.verb_xor, "val1", "val2"),
"INC": (self.verb_increment, "val", "[bits]"),
"DEC": (self.verb_decrement, "val", "[bits]"),
"TRIM": (self.verb_trim, "val", "bits"),
"ZERO?": (self.verb_zero, "val", "cond1", "cond2"),
}
def setreg(self, p, state, reg, val):
if not reg in state:
raise ModelError("Set: register %s not in state" % reg)
if state[reg][0] != val[0]:
raise ModelError("Setting wrong width %d should be %d"
% (val[0], state[reg][0]))
state[reg] = val
def getreg(self, p, state, reg):
if not reg in state:
raise ModelError("Get: register %s not in state" % reg)
return state[reg]
def getint(self, p, state, val):
assert type(val) == str
w = 0
v = 0
if val[:3] == "#0b":
for i in val[3:]:
v <<= 1
w += 1
if i >= '0' and i <= '1':
v |= int(i)
else:
raise ModelError(
"Int: Illegal binary: %s" % val)
elif val[:3] == "#0x":
for i in val[3:]:
v <<= 4
w += 4
try:
v |= int(i, 16)
except:
raise ModelError(
"Int: Illegal octal: %s" % val)
else:
raise ModelError("Int: Illegal integer: %s" % val)
return (w,v)
def eval(self, p, state, expr):
#print("Eval:", expr)
#print(" State:", state)
if state == None:
return None
if type(expr) == str and expr[0] == "/":
retval = self.getreg(p, state, expr);
elif type(expr) == str and expr[0] == "#":
retval = self.getint(p, state, expr);
elif type(expr) == tuple and expr[0] in self.verbs:
x = self.verbs[expr[0]]
if x[1] == None:
retval = x[0](p, state, expr)
elif len(x) == len(expr):
retval = x[0](p, state, expr)
elif len(x) <= len(expr) and x[-1] == "...":
retval = x[0](p, state, expr)
else:
for i in range(1,len(x)):
if x[i][0] == "[":
break;
if len(expr) < i + 1:
raise ModelError("Syntax: arg_count:" + str(expr))
retval = x[0](p, state, expr)
elif type(expr) == tuple and not expr[0] in self.verbs:
raise ModelError("Unknown verb" + str(expr[0]))
else:
raise ModelError("Unknown expression" + str(expr))
#print(" Return(", expr, "):", retval)
return retval
def verb_seq(self, p, state, expr):
for i in expr[1:]:
self.eval(p, state, i)
def verb_right_shift(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
if v1[1] == None:
return v1
if len(expr) > 2:
v2 = self.eval(p, state, expr[2])
else:
v2 = (8, 1)
if v2[1] == None:
raise ModelError("Verb: >> BITS unknown")
return (v1[0], v1[1] >> v2[1])
def verb_left_shift(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
if v1[1] == None:
return v1
if len(expr) > 2:
v2 = self.eval(p, state, expr[2])
else:
v2 = (8, 1)
if v2[1] == None:
raise ModelError("Verb: << BITS unknown")
return (v1[0], v1[1] << v2[1])
def verb_assign(self, p, state, expr):
v2 = self.eval(p, state, expr[2])
self.setreg(p, state, expr[1], v2)
def verb_increment(self, p, state, expr):
if len(expr) > 2:
v2 = self.eval(p, state, expr[2])
else:
v2 = (8, 1)
v1 = self.getreg(p, state, expr[1])
if v1[1] != None and v2[1] != None:
vn = (v1[0], (v1[1] + v2[1]) & ((1 << v1[0]) - 1))
else:
vn = (v1[0], None)
self.setreg(p, state, expr[1], vn)
return vn
def verb_decrement(self, p, state, expr):
if len(expr) > 2:
v2 = self.eval(p, state, expr[2])
else:
v2 = (8, 1)
v1 = self.getreg(p, state, expr[1])
if v1[1] != None and v2[1] != None:
vn = (v1[0], (v1[1] - v2[1]) & ((1 << v1[0]) - 1))
else:
vn = (v1[0], None)
self.setreg(p, state, expr[1], vn)
return vn
def verb_or(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
assert v1[0] == v2[0]
if v1[1] == None or v2[1] == None:
return (v1[0], None)
return (v1[0], v1[1] | v2[1])
def verb_xor(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
assert v1[0] == v2[0]
if v1[1] == None or v2[1] == None:
return (v1[0], None)
return (v1[0], v1[1] ^ v2[1])
def verb_and(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
assert v1[0] == v2[0]
if v1[1] == None or v2[1] == None:
return (v1[0], None)
return (v1[0], v1[1] & v2[1])
def verb_add(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
assert v1[0] == v2[0]
if len(expr) > 3:
ci = self.eval(p, state, expr[3])
else:
ci = (1,0)
if v1[1] == None or v2[1] == None or ci[1] == None:
if len(expr) > 4:
self.setreg(p, state, expr[4], (1, None))
return (v1[0], None)
s = v1[1] + v2[1] + ci[1]
if len(expr) > 4:
self.setreg(p, state, expr[4], (1, s >> v1[0]))
return (v1[0], s & ((1 << v1[0]) - 1))
def verb_sub(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
assert v1[0] == v2[0]
if len(expr) > 3:
bi = self.eval(p, state, expr[3])
else:
bi = (1,0)
if v1[1] == None or v2[1] == None or bi[1] == None:
if len(expr) > 4:
self.setreg(p, state, expr[4], (1, None))
return (v1[0], None)
s = v1[1] - (v2[1] + bi)
if len(expr) > 4:
self.setreg(p, state, expr[4], (1, s >> v1[0]))
return (v1[0], s & ((1 << v1[0]) - 1))
def verb_trim(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
v2 = self.eval(p, state, expr[2])
return(v2[1], v1[1] & ((1 << v2[1]) - 1))
def verb_zero(self, p, state, expr):
v1 = self.eval(p, state, expr[1])
if v1[1] == 0:
self.eval(p, state, expr[2])
elif v1[1] != None:
self.eval(p, state, expr[3])
else:
# XXX: what ?
self.eval(p, state, expr[3])
def verb_rnd(self, p, state, expr):
return (1, 1)
if random.random() >= .5:
return (1, 1)
else:
return (1, 0)
if __name__ == "__main__":
m = model()
si = {
"/R1": (16,0),
}
m.eval(None, si, "/R1")
m.eval(None, si, ("SEQ", "#0b001", "#0x001"))
m.eval(None, si, (">>", "#0b101", "#0x001"))
m.eval(None, si, ("=", "/R1", "#0x0001"))
print(si)
m.eval(None, si, ("INC", "/R1"))
print(si)
m.eval(None, si, ("DEC", "/R1", "#0x3"))
print(si)
try:
m.eval(None, si, ("DEC", "/R2", "#0x3"))
assert(False)
except:
pass
m.eval(None, si, ("DEC",))