-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_gates.py
More file actions
346 lines (281 loc) · 9.92 KB
/
logic_gates.py
File metadata and controls
346 lines (281 loc) · 9.92 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Implementation of the full logic gate ADT, child logic gate inheritances, and gate pin connections.
class LogicGate:
def __init__(self, name=''):
self.id = name
self.output = None
self.a = None
append_group(self)
def __str__(self):
"""Returns a word representation of self."""
return self.id + ' (' + str(self.__class__.__name__) + ')'
def get_output(self):
"""
Returns the calculated output for this gate, after pin inputs and logic is calculated.
If this gate is connected to another gate as a FromLogicGate, our output will become the pin input for the
ToLogicGate; this occurs in the get_input() function for the ToLogicGate object.
"""
self.output = self.logic()
return self.output
def get_input(self, alphabet):
"""
This function gets an alphabet and transforms that alphabet into self.a, self.b, or self.c (denoted as pin_x)
Then we check if pin_x is empty. If it is, we run a while loop to accept a valid user input and return it.
Otherwise, we check if pin_x is a connector. If it is, we do several things:
1) We call the FromLogicGate answer function and store the output into a variable (fr_answer).
2) We check if fr_answer is a list. If it is, we determine which element we return from the list by
checking the connector switch.
3) If fr_answer is not a list, that means it does not have 2 outputs, thus we return fr_answer.
Lastly, there are error functions on the bottom catch any bad switch, user, or gate inputs.
"""
pin_x = eval('self.' + alphabet)
if pin_x is None:
# If our pin is empty, get a valid pin state from user.
while True:
# Only accepts an input of 0 or 1.
pin_x = input('Enter Pin ' + alphabet.upper() + ' for ' + str(self) + ' --> ')
if pin_x == '0' or pin_x == '1':
return int(pin_x)
if isinstance(pin_x, Connector):
# If our pin is a connector, get FromLogicGate answer and Connector switch.
fr_answer = pin_x.fr.get_output()
if isinstance(fr_answer, list):
# If the answer is a list, we consult the connector switch to determine which output we are returning.
if pin_x.switch == 'sum':
return fr_answer[0]
elif pin_x.switch == 'carry':
return fr_answer[1]
# Error to catch invalid switch statements.
self.switch_error()
else:
# If the answer is not a list, return FromLogicGate answer.
return fr_answer
# Error to catch pins not empty and not a connector.
self.pin_error()
def connect(self, connector):
"""Links Connector to the next get_available ToLogicGate Pin as it meets class conditions."""
if not self.a and isinstance(self, (UnaryGate, BinaryGate, TrinaryGate)):
self.a = connector
elif not self.b and isinstance(self, (BinaryGate, TrinaryGate)):
self.b = connector
elif not self.c and isinstance(self, TrinaryGate):
self.c = connector
else:
# Error when connector is assigned a logic gate with no available inputs.
self.pin_error()
def pin_error(self):
raise RuntimeError('Error: No empty input pins get_available for connector, or pin has an invalid input.')
def switch_error(self):
raise RuntimeError('Error: We have 2 possible inputs but there is no valid switch to choose between them.')
def logic(self):
"""The grandchild classes replace this function."""
pass
class Connector:
"""A Connector works to bridge the answer of one gate to the input of another. Also holds switch statement."""
def __init__(self, fr, to, switch=None):
self.fr = fr
self.to = to
self.switch = switch
to.connect(self)
class TrinaryGate(LogicGate):
""""Has three pins."""
def __init__(self, name=''):
super().__init__(name)
self.b = None
self.c = None
class BinaryGate(LogicGate):
"""Has two pins."""
def __init__(self, name=''):
super().__init__(name)
self.b = None
class UnaryGate(LogicGate):
"""Has one pin."""
def __init__(self, name=''):
super().__init__(name)
class FullAdderGate(TrinaryGate):
"""
Gets three input values. Returns a [sum, carry] list.
A | B | C | s | c
------------------
0 | 0 | 0 | 0 | 0
1 | 0 | 0 | 1 | 0
0 | 1 | 0 | 1 | 0
0 | 0 | 1 | 1 | 0
1 | 1 | 0 | 0 | 1
0 | 1 | 1 | 0 | 1
1 | 0 | 1 | 0 | 1
1 | 1 | 1 | 1 | 1
"""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
self.c = self.get_input('c')
if self.a == 1 and self.b == 1 and self.c == 1:
return [1, 1]
elif self.a == 0 and self.b == 0 and self.c == 0:
return [0, 0]
elif (self.a == 1 and self.b == 1) or (self.b == 1 and self.c == 1) or (self.c == 1 and self.a == 1):
return [0, 1]
else:
return [1, 0]
class HalfAdderGate(BinaryGate):
"""
Gets two input values. Returns a [sum, carry] list.
A | B | s | c
--------------
0 | 0 | 0 | 0
0 | 1 | 1 | 0
1 | 0 | 1 | 0
1 | 1 | 0 | 1
"""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a != self.b:
return [1, 0]
elif self.a == 1 and self.b == 1:
return [0, 1]
else:
return [0, 0]
class AndGate(BinaryGate):
"""Gets two input values. Returns 1 if both values are 1."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a == 1 and self.b == 1:
return 1
else:
return 0
class NandGate(BinaryGate):
"""Gets two input values. Returns 0 if both values are 1."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a == 1 and self.b == 1:
return 0
else:
return 1
class OrGate(BinaryGate):
"""Gets two input values. Returns 1 if at least one values is 1."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a == 1 or self.b == 1:
return 1
else:
return 0
class NorGate(BinaryGate):
"""Gets two input values. Returns 0 if at least one value is 1."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a == 1 or self.b == 1:
return 0
else:
return 1
class XorGate(BinaryGate):
"""Gets two input values. Returns 1 if one value, but not both, is 1."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
self.b = self.get_input('b')
if self.a != self.b:
return 1
else:
return 0
class NotGate(UnaryGate):
"""Gets one input value. Returns the opposite value."""
def __init__(self, name=''):
super().__init__(name)
def logic(self):
self.a = self.get_input('a')
if self.a == 0:
return 1
else:
return 0
def append_group(gate):
"""Contains all created logic gates."""
group.append(gate)
def test_1(group):
"""Test Case 1: A 4-Bit Adder. Input 2 binary numbers in backward order to see the sum."""
gate_list = []
answer_string = ''
g1 = FullAdderGate('G1')
g2 = FullAdderGate('G2')
g3 = FullAdderGate('G3')
g4 = FullAdderGate('G4')
c1 = Connector(g1, g2, 'carry')
c2 = Connector(g2, g3, 'carry')
c3 = Connector(g3, g4, 'carry')
# This gets the sum of every single FullAdderGate.
group[-1].get_output()
for gate in group:
gate_list.append(gate.output[0])
gate_list.append(group[-1].output[1])
# Then it formats it into an answer which is then printed.
gate_list.reverse()
for num in gate_list:
answer_string += str(num)
print('The sum is %s.' % answer_string)
def test_2():
"""
Test Case 2.1: A fuller adder circuit. Input the gates and compare the results with the matrix table.
Test Case 2.2: A half adder circuit. Input the gates and compare the results with the matrix table.
"""
# A | B | C | s | c
# ------------------
# 0 | 0 | 0 | 0 | 0
# 1 | 0 | 0 | 1 | 0
# 0 | 1 | 0 | 1 | 0
# 0 | 0 | 1 | 1 | 0
# 1 | 1 | 0 | 0 | 1
# 0 | 1 | 1 | 0 | 1
# 1 | 0 | 1 | 0 | 1
# 1 | 1 | 1 | 1 | 1
g1 = FullAdderGate('G1')
print(g1.get_output())
# A | B | s | c
# --------------
# 0 | 0 | 0 | 0
# 0 | 1 | 1 | 0
# 1 | 0 | 1 | 0
# 1 | 1 | 0 | 1
g1 = HalfAdderGate('G1')
print(g1.get_output())
def test_3():
"""Test Case 3: Expected G8 output is 1 only if all inputs from G1 to G8 are 0."""
# Block 1
g1 = AndGate('G1')
g2 = OrGate('G2')
g3 = NotGate('G3')
c1 = Connector(g1, g2)
c2 = Connector(g2, g3)
# Block 2
g4 = NandGate('G4')
g5 = XorGate('G5')
c3 = Connector(g4, g5)
c4 = Connector(g3, g5)
# Block 3
g6 = NotGate('G6')
g7 = NorGate('G7')
g8 = AndGate('G8')
c5 = Connector(g5, g6)
c6 = Connector(g7, g8)
c7 = Connector(g6, g8)
print(g8.get_output())
group = []
test_1(group)
test_2()
test_3()