-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounters.py
More file actions
138 lines (109 loc) · 4.02 KB
/
Copy pathcounters.py
File metadata and controls
138 lines (109 loc) · 4.02 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
# Design counters
'''
counters\n
type:class\n
name-format: counter_[name]\n
Ring counter\n
Jonhson counter\n
Ripple counter
'''
from LogicPy.gates import logic_not
from LogicPy.conversion import decimal_to_binary , binary_to_decimal
'''Ring counter'''
class counter_ring:
# initializing counter
def __init__(self , count , start = None):
if start == None:
start = [0 for i in range(count)]
start[0]=1
elif isinstance(count,int) == False:
raise ValueError("Invalid value for count")
elif isinstance(start , list) == False:
raise ValueError("Invalid value for start , Values should be in list")
elif len(start) != count:
raise ValueError(f"Invalid values for start , Exact {count} required")
for i in start:
if i not in [0,1]:
raise ValueError("Binary values only")
if start.count(1) != 1:
raise ValueError("Invalid starting Value , There will be only one 1")
self.count_bits = count
self.start = start
self.__current_value = start
# to get next value
def next(self):
temp = self.__current_value.pop()
self.__current_value.insert(0,temp)
return self.__current_value
# to get current value
def now(self):
return self.__current_value
'''Jonhson counter or twisted ring counter'''
class counter_johnson:
# initializing counter
def __init__(self , count , start = None):
if start == None:
start = [0 for i in range(count)]
elif isinstance(count,int) == False:
raise ValueError("Invalid value for count")
elif isinstance(start , list) == False:
raise ValueError("Invalid value for start , Values should be in list")
elif len(start) != count:
raise ValueError(f"Invalid values for start , Exact {count} required")
for i in start:
if i not in [0,1]:
raise ValueError("Binary values only")
temp = start[0]
flag = False
for i in range(len(start)):
if start[i] == temp:
continue
else:
if flag:
raise ValueError("Invalid value for Johnson counter")
flag = True
temp = start[i]
self.count_bits = count
self.start = start
self.__current_value = start
# to get next value
def next(self):
temp = self.__current_value.pop()
self.__current_value.insert(0,logic_not(temp))
return self.__current_value
# to get current value
def now(self):
return self.__current_value
'''Ripple counter or basic binary counter'''
class counter_ripple:
# initializing counter
def __init__(self , count , start = None):
if start == None:
start = [0 for i in range(count)]
elif isinstance(count,int) == False:
raise ValueError("Invalid value for count")
elif isinstance(start , list) == False:
raise ValueError("Invalid value for start , Values should be in list")
elif len(start) != count:
raise ValueError(f"Invalid values for start , Exact {count} required")
for i in start:
if i not in [0,1]:
raise ValueError("Binary values only")
self.count_bits = count
self.start = start
self.__current_value = start
# to get next value
def next(self):
decimal = binary_to_decimal(self.__current_value)
decimal += 1
if decimal >= 2**(self.count_bits):
decimal = 0
temp = decimal_to_binary(decimal)
temp = [str(i) for i in str(temp)]
while len(temp) < len(self.__current_value):
temp.insert(0 , "0")
self.__current_value = [int(i) for i in temp]
return self.__current_value
# to get current value
def now(self):
return self.__current_value