-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
274 lines (231 loc) · 6.76 KB
/
processor.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
"""
Contains general concepts useful for all implementations
"""
class Register:
"""
A set of exposed pins, for I/O
Pins function in a boolean way
They can be set together, or addressed individually
Append register pointers to the register field to update them when
the pinset changes
it is built around an bool list, so using the "state" field (which takes or
returns a bool list) is preferable to using "value" (integer)
"""
def __str__(self):
return "Reg {:<10} {:>3} bits {:<8} ({}, 0x{:X})".format(self.name, self.size, getStrFromBoolList(self.state), self.value, self.value)
def __repr__(self):
return "{} {} bits {}".format(self.name, self.size, getStrFromBoolList(self.state))
def __format__(self, spec):
if spec=="indic":
# indicators for subsets
arrows=""
for i in self.pins:
if type(i) is bool:
arrows+="|"
elif i[1] == 0:
arrows+="^"
else:
arrows+=" "
return "Reg {:<10} {:>3} bits {:<12}\n{:>23} {:<12}".format(self.name, self.size, getStrFromBoolList(self.state), "", arrows)
else :
return self.__str__()
def __init__(self, numberOfPins, name="", overflow=True):
"""
if overflow is true, the register will silently overflow or underflow
when given values too big or too small
"""
self.name = name
self.size = numberOfPins
self.maxValue = 2**numberOfPins -1
self.doesOverflow=overflow
self.pins = [False]*numberOfPins
self.subscribers=[]
def addSubscriber(self, object, functionToExecute, testFunction=lambda x:True, passPinset=False):
"""
:param functionToExecute: pointer to a function to be executed when the
state of this changes
:param testFunction: is passed the state, and the functionToExecute
is only called if it returns true
:param passPinset: whether to pass a pointer to this pinset to the func
"""
self.subscribers.append((object, functionToExecute, testFunction, passPinset))
def setSubset(self, index, set):
"""
replace a number of pins by another Pinset
in the _pins table the corresponding pins will be replaced by pointers
to that Pinset
:param index: the index to place this pinset at
:param set: the pinset
"""
for i in range(len(set.pins)):
self.pins[i+index] = [set, i]
def setPinState(self, index, newState):
"""
set state for a single pin
0-indexed from the left
"""
if type(self.pins[index]) is bool:
if self.pins[index] == newState:
return
self.pins[index] = newState
else:
if self.pins[index][0].getPinState(self.pins[index][1]) == newState:
return
self.pins[index][0].setPinState(self.pins[index][1], newState)
for s in self.subscribers:
if s[2](self.state):
if (s[3]):
s[1](s[0],self)
else:
s[1](s[0])
def getPinState(self, index):
"""
get state for a single pin
0-indexed from the left
"""
p = self.pins[index]
if type(p) is bool:
return p
else:
return p[0].getPinState(p[1])
@property
def state(self):
result = []
for p in self.pins:
if type(p) is bool:
result.append(p)
else:
result.append(p[0].getPinState(p[1]))
return result
@state.setter
def state(self, newState):
"""
Set the new state of the pinset.
:param newState: either a list of bools or an int
"""
if type(newState) is int or type(newState) is bool:
newState = getBoolListFromInt(newState)
# pad the front of the list
while len(newState) < len(self.pins):
newState = [False]+newState
if len(newState) > len(self.pins):
raise ValueError("Too many elements")
for i in range(len(newState)):
if type(self.pins[i]) is bool:
self.setPinState(i,bool(newState[i]))
else:
self.pins[i][0].setPinState(self.pins[i][1], bool(newState[i]))
@property
def value(self):
return getIntFromBoolList(self.state)
@value.setter
def value(self, newVal):
if self.doesOverflow:
while newVal < 0 or newVal > self.maxValue :
if (newVal < 0):
newVal += self.maxValue+1
if (newVal > self.maxValue):
newVal -= self.maxValue+1
s = getBoolListFromInt(newVal)
if len(s) > len(self.pins):
raise ValueError("Value too large")
self.state = s
def getIntFromBoolList(boollist, bigEndian=True):
"""
converts a list of bools into an int
"""
result = 0
if not bigEndian:
boollist.reverse()
for i in range(len(boollist)):
if boollist[i]:
result+=1
result<<=1
result>>=1 # else it's one bit too much
return result
def getBoolListFromInt(integer, bigEndian=True):
"""
converts an integer into a list of bools
"""
result=[]
# TODO optimize
# but it's already quite fast actually
integer = bin(integer)[2:]
for i in range(len(integer)):
if integer[i] == "1":
result.append(True)
else:
result.append(False)
i-=1
if bigEndian==False :
result.reverse()
return result
def getStrFromBoolList(boollist):
result=""
for b in boollist:
if b:
result+="1"
else:
result+="0"
return result
class Processor :
pass
def printByteArray(array, groupBytesBy=8, name=None):
"""
Utility for printing byte arrays, in hexadecimal
:param array: the bytes() or bytearray()
:param groupBytesBy: will feed lines every (this) bytes
:param name: will print that at the top
"""
if name != None:
print(name, ":")
outTable = []
i = 0
while i < len(array):
g = 0
thisGroup = (i, bytearray())
while g < groupBytesBy:
thisGroup[1].append(array[i])
i+=1
g+=1
outTable.append(thisGroup)
lineLength = 5+3*groupBytesBy
sameThingLine = " "*(lineLength//4)+"[same]\n"
output = ""
maxLineNoDigits = max(2,len(str(len(outTable))))
maxByteNoDigits = max(2,len(str(len(outTable)*groupBytesBy)))
lineNoField = "{:<"+str(maxLineNoDigits)+"} {:>"+str(maxByteNoDigits)+"} "
output += lineNoField.format("0", outTable[0][0])
for j in outTable[0][1]:
output+=" {:0>2X}".format(j)
output+="\n"
i = 1
while i < len(outTable):
if outTable[i][1] == outTable[i-1][1]:
output += sameThingLine
else:
output += lineNoField.format(i, outTable[i][0])
for j in outTable[i][1]:
output+=" {:0>2X}".format(j)
output+="\n"
i+=1
lines = output.splitlines(keepends=True)
output = ""
i = 0
sameCount = 0
while i < len(lines):
if lines[i] == sameThingLine:
sameCount+=1
else:
if sameCount == 1:
output+=sameThingLine
elif sameCount > 1 :
output+=sameThingLine[:-1]+" x"+str(sameCount)+"\n"
output+=lines[i]
sameCount=0
i+=1
if sameCount == 1:
output+=sameThingLine
elif sameCount > 1 :
output+=sameThingLine[:-2]+" x"+str(sameCount)+"]\n"
print(output)