-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
194 lines (146 loc) · 3.77 KB
/
util.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
from struct import pack
from functools import wraps
from myhdl import intbv, concat
ADDRESS_MAX = 0x3fff
def update(a, b):
'''
In-place update of an intbv with the value of another one.
'''
for n in range(len(a)):
a[n] = b[n]
def int2addr(i):
return intbv(i, min=0, max=ADDRESS_MAX+1)
def low(i):
i = ibv(i)
return i[8:]
def high(i):
i = ibv(i)
return i[16:8]
def ibv(i):
if isinstance(i, (int, long)):
return intbv(i)
if not isinstance(i, intbv):
raise ValueError
return i
def compute_dw(values):
accumulator = []
for value in values:
accumulator.append(pack('H', value))
return ''.join(accumulator)
def compute_db(values):
accumulator = []
for value in values:
if isinstance(value, str):
accumulator.append(value)
elif isinstance(value, int):
accumulator.append(pack('B', value))
data = ''.join(accumulator)
if len(data) % 2:
data = data + '\0'
return data
def instr(method):
op = method.__name__
@wraps(method)
def inner(self, arg0=None, arg1=None):
addr = self._get_here()
if arg0 is None:
assert arg1 is None
print 'assembling %s instruction at %s' % (op, addr)
instruction = (op,)
method(self)
elif arg1 is not None:
arg0, arg1 = method(self, arg0, arg1)
tname, taddress = self._name_or_addr(arg0)
name, address = self._name_or_addr(arg1)
print 'assembling %s instruction at %s %s <- %s' % (op, addr, tname, name)
instruction = (op, taddress, address)
else:
arg0 = method(self, arg0)
name, address = self._name_or_addr(arg0)
print 'assembling %s instruction at %s to %s' % (op, addr, name)
instruction = (op, address)
self.data[addr] = instruction
self.here += 2
return inner
def spec(method):
@wraps(method)
def inner(self, ptr, register):
op = method.__name__
if ptr == 26:
op = op + '_X'
elif ptr == 28:
op = op + '_Y'
elif ptr == 30:
op = op + '_Z'
else:
raise Exception("Invalid target for %s: %#x" % (op, ptr,))
self._one(op, register)
return inner
def spec_reversed(method):
method = spec(method)
@wraps(method)
def inner(self, ptr, register):
return method(self, register, ptr)
return inner
def A(func):
@wraps(func)
def inner(address):
return K(func.__doc__, k=address >> 1)
return inner
def B(func):
@wraps(func)
def inner(register, address):
return K(func.__doc__, d=register, k=address >> 1)
return inner
def B_reversed(func):
@wraps(func)
def inner(address, register):
return K(func.__doc__, d=register, k=address >> 1)
return inner
def C(func):
@wraps(func)
def inner(io_port, register):
return K(func.__doc__, a=io_port, r=register)
return inner
def D(func):
@wraps(func)
def inner(Rd, Rr):
return K(func.__doc__, d=Rd, r=Rr)
return inner
def E(func):
@wraps(func)
def inner(Rr, bit):
return K(func.__doc__, r=Rr, b=bit)
return inner
def F(func):
@wraps(func)
def inner(Rr):
return K(func.__doc__, r=Rr)
return inner
def G(func):
@wraps(func)
def inner(Rd):
return K(func.__doc__, d=Rd)
return inner
def H(func):
@wraps(func)
def inner(register, bit):
return K(func.__doc__, A=register, b=bit)
return inner
def K(pattern, **values):
counts = dict((variable_letter, 0) for variable_letter in values)
p = ''.join(pattern.lower().split())[::-1]
n = len(p)
assert n in (16, 32), repr(pattern)
accumulator = []
for i, bit in enumerate(p):
if bit in '10':
accumulator.append(bool(int(bit)))
continue
assert bit in values, repr((i, bit, values))
index, value = counts[bit], values[bit]
counts[bit] += 1
bit = value[index]
accumulator.append(bit)
data = concat(*reversed(accumulator))
return n, data