-
Notifications
You must be signed in to change notification settings - Fork 0
/
digitallogic.py
355 lines (304 loc) · 11 KB
/
digitallogic.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
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
346
347
348
349
350
351
352
353
# Module to help perform digital logic calculations
# Micah Tseng
# Requites numpy
import numpy as np
# Define standard arrays
two = np.array([[0],
[1]])
four = np.array([[0,0],
[0,1],
[1,0],
[1,1]])
eight = np.array([[0,0,0],
[0,0,1],
[0,1,0],
[0,1,1],
[1,0,0],
[1,0,1],
[1,1,0],
[1,1,1]])
sixteen = np.array([[0,0,0,0],
[0,0,0,1],
[0,0,1,0],
[0,0,1,1],
[0,1,0,0],
[0,1,0,1],
[0,1,1,0],
[0,1,1,1],
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,0,1,1,0],
[1,0,0,1,1,1],
[1,0,1,0,0,0],
[1,0,1,0,0,1],
[1,0,1,0,1,0],
[1,0,1,0,1,1],
[1,0,1,1,0,0],
[1,0,1,1,0,1],
[1,0,1,1,1,0],
[1,0,1,1,1,1],
[1,1,0,0,0,0],
[1,1,0,0,0,1],
[1,1,0,0,1,0],
[1,1,0,0,1,1],
[1,1,0,1,0,0],
[1,1,0,1,0,1],
[1,1,0,1,1,0],
[1,1,0,1,1,1],
[1,1,1,0,0,0],
[1,1,1,0,0,1],
[1,1,1,0,1,0],
[1,1,1,0,1,1],
[1,1,1,1,0,0],
[1,1,1,1,0,1],
[1,1,1,1,1,0],
[1,1,1,1,1,1]])
'''
TPRINT
Parameters
data A tuple, holding the set of arrays to be printed. Rows must be equal.
labels A list of each label in the order of left to right
Returns
nothing...just pretty printing
'''
def tprint(data, labels):
# list of the spacing for each row
spacing = []
# The total sum of the above spacing
total_pad = 0
# total number of data columns
columns = 0
# Get all thel spacing for the labels
for i in range(0,len(labels)):
# Start a spacing variable
spacing.append(0)
# Check to see if you need to print the deviding line
for n in labels[i]:
# Print the label, incromenting the spac counter to find the spacing
spacing[i] += 1
total_pad += spacing[i]
# Take care of the possiblity the user was lazy and didn't want to label every coumn
# first get the total number of columns in the data
for data_set in data:
columns += data_set.shape[1]
# Compare the columns with the number of the spacing entries and make ammends
while len(spacing) < columns:
labels.append(' ')
spacing.append(1)
total_pad += 1
# Print out the top bar
for i in range(0, total_pad + len(spacing) + 2 * len(data)):
print("=", end="", flush=True)
print("=")
# Print out the label header
print("| ", end = '', flush = True)
# i is the iterator through the labels
i = 0
for data_set in data:
for k in range(0, data_set.shape[1]):
for n in labels[i]:
# Print the label, incromenting the spac counter to find the spacing
print(n, end = '', flush = True)
print(' ', end = '', flush = True)
i += 1
print("| ", end = '', flush = True)
print('')
# Print out the deviding bar
for i in range(0, total_pad + len(spacing) + 2 * len(data)):
print("-", end = '', flush=True)
print("-")
# Prinit out the data
# Iterate over each row in the first data set...is this good? No. But I'm lazy
for i in range(0, data[0].shape[0]):
# k is the iterator through the spacing
k = 0
# Go through each data set
for data_set in data:
print("| ", end = '', flush = True)
# Print out the appropriate row with the correct spacing
for n in range(0, data_set.shape[1]):
if data_set[i,n] == 9:
print('-' ,end = '', flush = True)
elif data_set[i,n] == 8:
print('F' ,end = '', flush = True)
else:
print(str(data_set[i,n].tolist()).strip('[],').replace(',', '') ,end = '', flush = True)
for a in range(0,spacing[k]):
print(" ", end = '', flush = True)
k += 1
print("| ", end = '\n', flush = True)
# Print out the bottom bar
for i in range(0, total_pad + len(spacing) + 2 * len(data)):
print("=", end = '', flush=True)
print("=")
'''
APP_TABLE
Parameters:
ff The type of flip flop: jk
present_s An array with the values of the present state
next_s An array with the values of the next state
Returns:
An appropriatly sized array with the application table of the respective flip flop state tables
Due to the nature of integer arrays and my own lazyness, the following is the key for the returning
values. If you use tprint() to print them, ignore this:
0 -> 0
1 -> 1
8 -> forbidden
9 -> Don't care
'''
def app_table(ff, present_s, next_s):
if present_s.shape != next_s.shape:
print("********************************")
print("The present state and next state\narrays are of mismatched shape.")
print("This won't work.\n********************************")
return
if ff == 'jk' or ff == 'JK':
(row, column) = present_s.shape
jk = np.zeros((row, 2 * column), dtype = int)
for x in range(0,row):
for y in range(0,column):
if present_s[x,y] == 0 and next_s[x,y] == 0:
jk[x,2*y] = 0
jk[x,2*y+1] = 9
elif present_s[x,y] == 0 and next_s[x,y] == 1:
jk[x,2*y] = 1
jk[x,2*y+1] = 9
elif present_s[x,y] == 1 and next_s[x,y] == 0:
jk[x,2*y] = 9
jk[x,2*y+1] = 1
elif present_s[x,y] == 1 and next_s[x,y] == 1:
jk[x,2*y] = 9
jk[x,2*y+1] = 0
return jk
elif ff == 'sr' or ff == 'SR':
(row, column) = present_s.shape
sr = np.zeros((row, 2 * column), dtype = int)
for x in range(0,row):
for y in range(0,column):
if present_s[x,y] == 0 and next_s[x,y] == 0:
sr[x,2*y] = 0
sr[x,2*y+1] = 9
elif present_s[x,y] == 0 and next_s[x,y] == 1:
sr[x,2*y] = 1
sr[x,2*y+1] = 9
elif present_s[x,y] == 1 and next_s[x,y] == 0:
sr[x,2*y] = 9
sr[x,2*y+1] = 1
elif present_s[x,y] == 1 and next_s[x,y] == 1:
sr[x,2*y] = 8
sr[x,2*y+1] = 8
return sr
else:
print("The type of FF you are asking for is not implemented")
return np.zeros(present_s.shape, dtype=int)
'''
Due to the fact that numpy stores int arrays as signed 8 bit arrays and the ~ funciton in python takes
the 2' complement, it is necesary to create a function (this one) that can take the one's comp of an
array of one bit values.
'''
def b(base):
# Get the shape of the inputed array
shape = base.shape
# instatiate a new array of the same shape
comp = np.zeros(shape, dtype = int)
# This iteration uses the nditter numpy function to go through the array see:
# https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html
it = np.nditer(base, flags=['multi_index'])
while not it.finished:
if it[0] == 0:
comp[it.multi_index] = 1
else:
comp[it.multi_index] = 0
it.iternext()
# Return the new array
return comp
'''
csv_out takes in the file location and the array to be outputed and outputs the array as an ineger
csv file.
'''
def csv_out(file_loc, array):
np.savetxt(file_loc, array.astype(int), fmt='%1.f', delimiter=",")
# testing
inputs = eight
outputs = eight
if __name__ == "__main__":
label = ['A', 'B', 'C', 'A+', 'B+', 'C+', 'j', 'k', 'j', 'k', 'j', 'k' ]
tprint((inputs, b(outputs), app_table('sr', inputs, b(outputs))), label)
# print(app_table('jk', sixteen, sixteen))