-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicSim.py
More file actions
292 lines (236 loc) · 7.7 KB
/
LogicSim.py
File metadata and controls
292 lines (236 loc) · 7.7 KB
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
288
289
290
291
292
# from AlgoUtils import *
dbg_step_msg = False
class DataGateBlock(object):
"""
:type ports: list[int]
:type data: list[int]
:type dirty: bool
:type extData: ((T, DataGateBlock) -> None, T)|None
"""
__slots__ = ["ports", "data", "dirty", "extData"]
def __init__(self, Ports, Data, ExtData=None):
"""
:param list[int] Ports:
:param list[int] Data:
:param any ExtData:
"""
self.ports = Ports
self.data = Data
self.dirty = False
self.extData = ExtData
def Eval(self):
raise NotImplementedError("Not Implemented")
class DataConnection(object):
"""
:type members: list[(int, int)]
:type isOn: bool
:type extData: ((T, DataConnection) -> None, T)|None
"""
__slots__ = ["members", "isOn", "extData"]
def __init__(self, Members):
"""
:param list[(int, int)] Members:
"""
self.members = Members
self.isOn = False
self.extData = None
def addPort(self, blkI, portI): # TODO: use a sorted list and do binary search
"""
:param int blkI:
:param int portI:
"""
self.members.append((blkI, portI))
def remPort(self, blkI, portI):
"""
:param int blkI:
:param int portI:
:rtype: bool
"""
try:
self.members.remove((blkI, portI))
return True
except ValueError:
return False
def on_remove(self, group):
"""
:param DataBlockGroup group:
"""
for blkI, portI in self.members:
blk = group.blocks[blkI]
if blk.ports[portI] > 0:
continue
if blk.data[portI] == 1:
blk.data[portI] = 0
blk.dirty = True
def update(self, group):
"""
:param DataBlockGroup group:
"""
# print "DO Update"
newIsOn = False
for blkI, portI in self.members:
blk = group.blocks[blkI]
if blk.ports[portI] < 0:
if dbg_step_msg:
print "Skipped input port: blkI = %u, portI = %u, blk = %r" % (blkI, portI, blk)
continue
if blk.data[portI] == 2:
newIsOn = True
break
if dbg_step_msg:
print "Skipped input port: blkI = %u, portI = %u, blk = %r" % (blkI, portI, blk)
if newIsOn == self.isOn:
if dbg_step_msg:
print "Connection Skipped: already isOn = " + repr(newIsOn)
return
self.isOn = newIsOn
newV = int(newIsOn)
for blkI, portI in self.members:
blk = group.blocks[blkI]
if blk.ports[portI] > 0: continue
val = blk.data[portI]
if val == 2 or val == newV: continue
blk.data[portI] = newV
blk.dirty = True
if self.extData is None:
print "Skip ExtData"
return
fn, data = self.extData
fn(data, self)
# extData is (fn, data)
# fn is function(dat: is data, blk: DataGateBlock) -> None
class InpNode(DataGateBlock):
def __init__(self):
super(InpNode, self).__init__([-1], [0])
def Eval(self):
if self.dirty:
if self.extData is not None:
fn, dat = self.extData
fn(dat, self)
self.dirty = False
class OutNode(DataGateBlock):
def __init__(self):
super(OutNode, self).__init__([1], [0])
def Eval(self):
if self.dirty:
if self.extData is not None:
fn, dat = self.extData
fn(dat, self)
self.dirty = False
class HybNode(DataGateBlock):
def __init__(self):
super(HybNode, self).__init__([0], [0])
def Eval(self):
if self.dirty:
if self.extData is not None:
fn, dat = self.extData
fn(dat, self)
self.dirty = False
class NotGate(DataGateBlock):
def __init__(self):
super(NotGate, self).__init__([-1, 1], [0, 2])
def Eval(self):
self.data[1] = 0 if self.data[0] else 2
self.dirty = False
class OrGate(DataGateBlock):
def __init__(self):
super(OrGate, self).__init__([-1, -1, 1], [0, 0, 0])
def Eval(self):
self.data[2] = 2 if self.data[0] or self.data[1] else 0
self.dirty = False
class AndGate(DataGateBlock):
def __init__(self):
super(AndGate, self).__init__([-1, -1, 1], [0, 0, 0])
def Eval(self):
self.data[2] = 2 if self.data[0] and self.data[1] else 0
self.dirty = False
class NorGate(DataGateBlock):
def __init__(self):
super(NorGate, self).__init__([-1, -1, 1], [0, 0, 2])
def Eval(self):
self.data[2] = 0 if self.data[0] or self.data[1] else 2
self.dirty = False
class NandGate(DataGateBlock):
def __init__(self):
super(NandGate, self).__init__([-1, -1, 1], [0, 0, 2])
def Eval(self):
self.data[2] = 0 if self.data[0] and self.data[1] else 2
self.dirty = False
class XorGate(DataGateBlock):
def __init__(self):
super(XorGate, self).__init__([-1, -1, 1], [0, 0, 0])
def Eval(self):
self.data[2] = 2 if bool(self.data[0]) ^ bool(self.data[1]) else 0
self.dirty = False
class XnorGate(DataGateBlock):
def __init__(self):
super(XnorGate, self).__init__([-1, -1, 1], [0, 0, 2])
def Eval(self):
self.data[2] = 0 if bool(self.data[0]) ^ bool(self.data[1]) else 2
self.dirty = False
class BoundData(object):
"""
:type data: list[int]
:type watching: list[DataGateBlock|None]
:type reporter: ((str|unicode) -> any)|None
"""
__slots__ = ["data", "watching", "reporter"]
def __init__(self):
self.data = []
self.watching = []
self.reporter = None
def allocItems(self, n):
self.data = [0] * n
self.watching = [None] * n
@staticmethod
def changeData(data, blk):
"""
:param (BoundData, int|long) data:
:param DataGateBlock blk:
"""
self, index = data
if self.data[index] == 2:
blk.data[0] = 2
else:
self.data[index] = blk.data[0]
def watchItem(self, i, dataGateBlock):
"""
:param int|long i:
:param DataGateBlock dataGateBlock:
"""
self.watching[i] = dataGateBlock
def __setitem__(self, i, v):
watch = self.watching[i]
self.data[i] = v
if watch is None:
if self.reporter is not None:
self.reporter("CHANGED: i = %u, v = %r\n" % (i, v))
return
if watch.data[0] != v:
watch.data[0] = v
watch.dirty = True
def __getitem__(self, i):
return self.data[i]
def __len__(self):
return len(self.data)
class DataBlockGroup(object):
"""
:type blocks: list[DataGateBlock]
:type connections: list[DataConnection]
"""
__slots__ = ["blocks", "connections"]
def __init__(self):
self.blocks = []
self.connections = []
def Step(self):
# print "Blocks: " + repr(self.blocks)
for blk in self.blocks:
if blk.dirty:
blk.Eval()
for conn in self.connections:
conn.update(self)
def removeConn(self, Conn):
"""
:param DataConnection Conn:
"""
Conn.on_remove(self)