-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcounterGen.py
99 lines (84 loc) · 3.27 KB
/
counterGen.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
import cm2py as cm2
import math
def standard(bits, bounds=None):
save = cm2.Save()
ands = []
flips = []
clock = save.addBlock(cm2.OR, (bits,0,1))
for i in range(bits):
ands.append(save.addBlock(cm2.AND, (i,0,1)))
flips.append(save.addBlock(cm2.FLIPFLOP, (i,0,0)))
save.addConnection(ands[-1], flips[-1])
save.addConnection(clock, ands[-1])
for j in ands[:-1]:
save.addConnection(flips[-1], j)
if bounds:
nand = save.addBlock(cm2.NAND, (-1,0,0), state=True)
intermediate = save.addBlock(cm2.AND, (-1,0,1))
resetAnd = save.addBlock(cm2.AND, (-1,0,2))
save.addConnection(intermediate, resetAnd)
save.addConnection(clock,resetAnd)
maxBinary = format(bounds[1], f"0{bits}b")
minBinary = format(bounds[0], f"0{bits}b")
for i,bit in enumerate(maxBinary):
if bit == "1":
save.addConnection(flips[i], nand)
save.addConnection(flips[i], intermediate)
if bit != minBinary[i]:
save.addConnection(resetAnd, flips[i])
save.addConnection(nand, ands[i])
for i,bit in enumerate(minBinary):
if bit == "1":
flips[i].properties = [2,0]
return save
def reverse(bits, bounds=None):
save = cm2.Save()
ands = []
flips = []
clock = save.addBlock(cm2.NOR, (bits,0,1), state=True)
for i in range(bits):
ands.append(save.addBlock(cm2.NOR, (i,0,1)))
flips.append(save.addBlock(cm2.FLIPFLOP, (i,0,0)))
save.addConnection(ands[-1], flips[-1])
save.addConnection(clock, ands[-1])
for j in ands[:-1]:
save.addConnection(flips[-1], j)
if bounds:
nor = save.addBlock(cm2.NOR, (-1,0,0), state=True)
intermediate = save.addBlock(cm2.OR, (-1,0,1))
resetNor = save.addBlock(cm2.NOR, (-1,0,2))
save.addConnection(intermediate, resetNor)
save.addConnection(clock,resetNor)
maxBinary = format(bounds[1], f"0{bits}b")
minBinary = format(bounds[0], f"0{bits}b")
for i,bit in enumerate(minBinary):
if bit == "0":
save.addConnection(flips[i], nor)
save.addConnection(flips[i], intermediate)
if bit != maxBinary[i]:
save.addConnection(resetNor, flips[i])
save.addConnection(nor, ands[i])
for i,bit in enumerate(maxBinary):
if bit == "1":
flips[i].properties = [2,0]
return save
def both(bits):
save = cm2.Save()
ands = []
nors = []
flips = []
clockUp = save.addBlock(cm2.OR, (bits,0,1))
clockDown = save.addBlock(cm2.NOR, (bits,0,2), state=True)
for i in range(bits):
ands.append(save.addBlock(cm2.AND, (i,0,1)))
nors.append(save.addBlock(cm2.NOR, (i,0,2), state=False))
flips.append(save.addBlock(cm2.FLIPFLOP, (i,0,0)))
save.addConnection(ands[-1], flips[-1])
save.addConnection(clockUp, ands[-1])
save.addConnection(nors[-1], flips[-1])
save.addConnection(clockDown, nors[-1])
for j in ands[:-1]:
save.addConnection(flips[-1], j)
for j in nors[:-1]:
save.addConnection(flips[-1], j)
return save