-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcd.py
114 lines (99 loc) · 2.85 KB
/
bcd.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
from ckmap import kmap
class bcd_circuit:
def __init__(self):
pass
@classmethod
def from_int(cls, lhs_arr, rhs_arr):
cls.lhs = list(map(lambda x:bin(x)[2:], lhs_arr))
cls.longest_lhs = max(map(len, cls.lhs))
cls.lhs = list(map(lambda x:x.zfill(cls.longest_lhs), cls.lhs))
cls.rhs = list(map(lambda x:bin(x)[2:], rhs_arr))
cls.longest_rhs = max(map(len, cls.rhs))
cls.rhs = list(map(lambda x:x.zfill(cls.longest_rhs), cls.rhs))
return cls()
@classmethod
def from_string(cls, lhs_string, rhs_string):
cls.lhs = lhs_string
cls.rhs = rhs_string
return cls()
def solve(self):
for l,r in zip(self.lhs, self.rhs):
print(f"{l} -> {r}")
print()
for id in range(len(self.rhs[0])):
ones = []
for numi, num in enumerate(self.rhs):
kmap_num = int(self.lhs[numi], 2)
if num[id]=="1":
ones.append(kmap_num)
dont_cares = [x for x in range(2**self.longest_lhs) if bin(x)[2:].zfill(self.longest_lhs) not in self.lhs]
k = kmap.from_minterm_and_dont_care(ones, dont_cares)
print(f"Y{len(self.rhs[0])-id-1}: ", end="")
print(k.grouping_to_letter(k.get_minimal_grouping()))
if __name__ == "__main__":
print("A is LSB, Y0 is output LSB.")
# 8421
eft1 = list(range(10))
# 2421
tft1 = [0, 1, 2, 3, 4, 11, 12, 13, 14, 15]
# 4221
ftt1 = [0, 1, 2, 3, 6, 9, 10, 11, 14, 15]
# Excess 3
excess3 = [x+3 for x in range(10)]
# Excess 6
excess6 = [x+6 for x in range(10)]
# 53211
ftht11_string = [
"00000",
"00001",
"00011",
"00110",
"00111",
"01100",
"01101",
"10100",
"11000",
"11001",
]
ftht11 = list(map(lambda x:int(x,2), ftht11_string))
# Johnson's code
johnson_string = [
"00000",
"00001",
"00011",
"00111",
"01111",
"11111",
"11110",
"11100",
"11000",
"10000",
]
johnson = list(map(lambda x:int(x,2), johnson_string))
print("8421 to excess6:")
bc = bcd_circuit.from_int(eft1, excess6)
bc.solve()
print()
print("excess3 to excess6:")
bc = bcd_circuit.from_int(excess3, excess6)
bc.solve()
print()
print("8421 to 2421:")
bc = bcd_circuit.from_int(eft1, tft1)
bc.solve()
print()
print("8421 to 53211:")
bc = bcd_circuit.from_int(eft1, ftht11)
bc.solve()
print()
print("53211 to 8421:")
bc = bcd_circuit.from_int(ftht11, eft1)
bc.solve()
print()
print("8421 to Johnson's code:")
bc = bcd_circuit.from_int(eft1, johnson)
bc.solve()
print()
print("Johnson's code to 8421")
bc = bcd_circuit.from_int(johnson, eft1)
bc.solve()