-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadder_circuit.py
More file actions
75 lines (65 loc) · 3.03 KB
/
Copy pathadder_circuit.py
File metadata and controls
75 lines (65 loc) · 3.03 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
from logic_gates import NAND_gate, NOT_gate, AND_gate, OR_gate, XOR_gate
# HALF ADDER
def half_adder(a, b):
s = XOR_gate(a, b)
c = AND_gate(a, b)
return (s, c)
# TEST CASES
print("A: 0, B: 0 | Output: {0}".format(half_adder(0, 0)))
print("A: 0, B: 1 | Output: {0}".format(half_adder(0, 1)))
print("A: 1, B: 0 | Output: {0}".format(half_adder(1, 0)))
print("A: 1, B: 1 | Output: {0}".format(half_adder(1, 1)))
# FULL ADDER
def full_adder(a, b, c):
s = XOR_gate(XOR_gate(a, b), c)
c_out = OR_gate(AND_gate(OR_gate(a, b), c), AND_gate(a, b))
return (s, c_out)
# TEST CASES
print("A: 0, B: 0, C: 0 | Output: {0}".format(full_adder(0, 0, 0)))
print("A: 0, B: 0, C: 1 | Output: {0}".format(full_adder(0, 0, 1)))
print("A: 0, B: 1, C: 0 | Output: {0}".format(full_adder(0, 1, 0)))
print("A: 1, B: 0, C: 0 | Output: {0}".format(full_adder(1, 0, 0)))
print("A: 0, B: 1, C: 1 | Output: {0}".format(full_adder(0, 1, 1)))
print("A: 1, B: 1, C: 0 | Output: {0}".format(full_adder(1, 1, 0)))
print("A: 1, B: 1, C: 1 | Output: {0}".format(full_adder(1, 1, 1)))
print("A: 1, B: 0, C: 1 | Output: {0}".format(full_adder(1, 0, 1)))
# FULL ADDER 2 - using half adder
def full_adder2(a, b, c):
s1, c1 = half_adder(a, b)
s2, c2 = half_adder(c1, c)
s = XOR_gate(s1, c)
c_out = OR_gate(c1, AND_gate(s1, c))
return (s, c_out)
# TEST CASES
print("A: 0, B: 0, C: 0 | Output: {0}".format(full_adder2(0, 0, 0)))
print("A: 0, B: 0, C: 1 | Output: {0}".format(full_adder2(0, 0, 1)))
print("A: 0, B: 1, C: 0 | Output: {0}".format(full_adder2(0, 1, 0)))
print("A: 1, B: 0, C: 0 | Output: {0}".format(full_adder2(1, 0, 0)))
print("A: 0, B: 1, C: 1 | Output: {0}".format(full_adder2(0, 1, 1)))
print("A: 1, B: 1, C: 0 | Output: {0}".format(full_adder2(1, 1, 0)))
print("A: 1, B: 1, C: 1 | Output: {0}".format(full_adder2(1, 1, 1)))
print("A: 1, B: 0, C: 1 | Output: {0}".format(full_adder2(1, 0, 1)))
# ARITHMETIC LOGIC UNIT
def ALU(a, b, c, opcode):
if opcode == 0:
s, c = half_adder(a, b)
if opcode == 1:
s, c = full_adder(a, b, c)
return (s, c)
# TEST CASES
print("A: 0, B: 0, C: 0 | Output: {0}".format(ALU(0, 0, 0, 0)))
print("A: 0, B: 0, C: 1 | Output: {0}".format(ALU(0, 0, 1, 0)))
print("A: 0, B: 1, C: 0 | Output: {0}".format(ALU(0, 1, 0, 0)))
print("A: 1, B: 0, C: 0 | Output: {0}".format(ALU(1, 0, 0, 0)))
print("A: 0, B: 1, C: 1 | Output: {0}".format(ALU(0, 1, 1, 0)))
print("A: 1, B: 1, C: 0 | Output: {0}".format(ALU(1, 1, 0, 0)))
print("A: 1, B: 1, C: 1 | Output: {0}".format(ALU(1, 1, 1, 0)))
print("A: 1, B: 0, C: 1 | Output: {0}".format(ALU(1, 0, 1, 0)))
print("A: 0, B: 0, C: 0 | Output: {0}".format(ALU(0, 0, 0, 1)))
print("A: 0, B: 0, C: 1 | Output: {0}".format(ALU(0, 0, 1, 1)))
print("A: 0, B: 1, C: 0 | Output: {0}".format(ALU(0, 1, 0, 1)))
print("A: 1, B: 0, C: 0 | Output: {0}".format(ALU(1, 0, 0, 1)))
print("A: 0, B: 1, C: 1 | Output: {0}".format(ALU(0, 1, 1, 1)))
print("A: 1, B: 1, C: 0 | Output: {0}".format(ALU(1, 1, 0, 1)))
print("A: 1, B: 1, C: 1 | Output: {0}".format(ALU(1, 1, 1, 1)))
print("A: 1, B: 0, C: 1 | Output: {0}".format(ALU(1, 0, 1, 1)))