-
Notifications
You must be signed in to change notification settings - Fork 0
/
FLOP.py
557 lines (493 loc) · 17.7 KB
/
FLOP.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
The following classes are defined:
SRLatch
GatedSRLatch
GatedDLatch
DFlipFlop
DFlipFlopPresetClear
TFlipFlop
TFlipFlopPresetClear
JKFlipFlop
JKFlipFlopPresetClear
"""
from .. import wire
from .. import gate
from .. import signal
Wire = wire.Wire
class SRLatch:
"""Construct a new SR latch.
Args:
set: An object of type Wire. The set input to the latch.
reset: An object of type Wire. The reset input to the latch.
output: An object of type Wire. The output of the latch. Takes on the
value of 1 if the value of set is 1 and the value of 0 if the value
of reset is 1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, set, reset, output, output_not):
gate.NORGate2(set, output, output_not)
gate.NORGate2(reset, output_not, output)
self.set = set
self.reset = reset
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "set: " + str(self.set.value) + "\n"
str_ += "reset: " + str(self.reset.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
set=None,
reset=None,
output=None,
output_not=None
):
if set is not None:
self.set.value = set
if reset is not None:
self.reset.value = reset
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class GatedSRLatch:
"""Construct a new gated SR latch.
Args:
set: An object of type Wire. The set input to the latch.
reset: An object of type Wire. The reset input to the latch.
clock: An object of type Wire or Clock. The clock input to the latch.
output: An object of type Wire. The output of the latch. When the value
of clock is 1, takes on the value of 1 if the value of set is 1 and
the value of 0 if the value of reset is 1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, set, reset, clock, output, output_not):
wire_1 = Wire()
wire_2 = Wire()
gate.ANDGate2(clock, set, wire_1)
gate.ANDGate2(clock, reset, wire_2)
SRLatch(wire_1, wire_2, output, output_not)
self.set = set
self.reset = reset
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "set: " + str(self.set.value) + "\n"
str_ += "reset: " + str(self.reset.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
set=None,
reset=None,
clock=None,
output=None,
output_not=None
):
if set is not None:
self.set.value = set
if reset is not None:
self.reset.value = reset
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class GatedDLatch:
"""Construct a new gated D latch.
Args:
data: An object of type Wire. The data input to the latch.
clock: An object of type Wire or Clock. The clock input to the latch.
output: An object of type Wire. The output of the latch. Takes on the
value of data if the value of clock is 1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, data, clock, output, output_not):
wire_1 = Wire()
gate.NOTGate(data, wire_1)
GatedSRLatch(data, wire_1, clock, output, output_not)
self.data = data
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "data: " + str(self.data.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
data=None,
clock=None,
output=None,
output_not=None
):
if data is not None:
self.data.value = data
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class DFlipFlop:
"""Construct a new positive edge-triggered D flip-flop.
Args:
data: An object of type Wire. The data input to the flip-flop.
clock: An object of type Wire or Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. Takes on
the value of data on the positive edges of clock.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, data, clock, output, output_not):
q_1 = Wire()
q_not_1 = Wire()
not_clock = Wire()
gate.NOTGate(clock, not_clock)
GatedDLatch(data, not_clock, q_1, q_not_1)
GatedDLatch(q_1, clock, output, output_not)
self.data = data
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "data: " + str(self.data.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
data=None,
clock=None,
output=None,
output_not=None
):
if data is not None:
self.data.value = data
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class DFlipFlopPresetClear:
"""Construct a new positive edge-triggered D flip-flop with preset/clear
capabilities.
Args:
data: An object of type Wire. The data input to the flip-flop.
preset_n: An object of type Wire. Presets output to 1 and output_not to
0 asynchronously if its value is 0.
clear_n: An object of type Wire. Clears output to 0 and output_not to 1
asynchronously if its value is 0.
clock: An object of type Wireor Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. Takes on
the value of data on the positive edges of clock.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, data, preset_n, clear_n, clock, output, output_not):
not_clock = Wire()
not_data = Wire()
wire_1 = Wire()
wire_2 = Wire()
wire_3 = Wire()
wire_4 = Wire()
wire_5 = Wire()
wire_6 = Wire()
gate.NOTGate(clock, not_clock)
gate.NOTGate(data, not_data)
gate.NANDGate2(data, not_clock, wire_1)
gate.NANDGate2(not_data, not_clock, wire_2)
gate.NANDGate3(preset_n, wire_1, wire_3, wire_4)
gate.NANDGate3(wire_4, wire_2, clear_n, wire_3)
gate.NANDGate2(wire_4, clock, wire_5)
gate.NANDGate2(wire_3, clock, wire_6)
gate.NANDGate3(preset_n, wire_5, output_not, output)
gate.NANDGate3(output, wire_6, clear_n, output_not)
self.data = data
self.preset_n = preset_n
self.clear_n = clear_n
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "data: " + str(self.data.value) + "\n"
str_ += "preset_n: " + str(self.preset_n.value) + "\n"
str_ += "clear_n: " + str(self.clear_n.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
data=None,
preset_n=None,
clear_n=None,
clock=None,
output=None,
output_not=None
):
if data is not None:
self.data.value = data
if preset_n is not None:
self.preset_n.value = preset_n
if clear_n is not None:
self.clear_n.value = clear_n
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class TFlipFlop:
"""Construct a new positive edge-triggered T flip-flop.
Args:
toggle: An object of type Wire. The toggle input to the flip-flop.
clock: An object of type Wire or Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. Toggles
its value on the positive edges of clock if the value of toggle is
1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, toggle, clock, output, output_not):
mux_output = Wire()
enable = Wire()
enable.value = 1
signal.Multiplexer2To1(enable, toggle, output_not, output, mux_output)
DFlipFlop(mux_output, clock, output, output_not)
self.toggle = toggle
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "toggle: " + str(self.toggle.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
toggle=None,
clock=None,
output=None,
output_not=None
):
if toggle is not None:
self.toggle.value = toggle
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class TFlipFlopPresetClear:
"""Construct a new positive edge-triggered T flip-flop with preset/clear
capabilities.
Args:
toggle: An object of type Wire. The toggle input to the flip-flop.
preset_n: An object of type Wire. Presets output to 1 and output_not to
0 asynchronously if its value is 0.
clear_n: An object of type Wire. Clears output to 0 and output_not to 1
asynchronously if its value is 0.
clock: An object of type Wire or Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. Toggles
its value on the positive edges of clock if the value of toggle is
1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, toggle, preset_n, clear_n, clock, output, output_not):
mux_output = Wire()
enable = Wire()
enable.value = 1
signal.Multiplexer2To1(enable, toggle, output_not, output, mux_output)
DFlipFlopPresetClear(
mux_output,
preset_n,
clear_n,
clock,
output,
output_not
)
self.toggle = toggle
self.preset_n = preset_n
self.clear_n = clear_n
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "toggle: " + str(self.toggle.value) + "\n"
str_ += "preset_n: " + str(self.preset_n.value) + "\n"
str_ += "clear_n: " + str(self.clear_n.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
toggle=None,
preset_n=None,
clear_n=None,
clock=None,
output=None,
output_not=None
):
if toggle is not None:
self.toggle.value = toggle
if preset_n is not None:
self.preset_n.value = preset_n
if clear_n is not None:
self.clear_n.value = clear_n
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class JKFlipFlop:
"""Construct a new positive edge-triggered JK flip-flop.
Args:
J: An object of type Wire. The J input to the flip-flop.
K: An object of type Wire. The K input to the flip-flop.
clock: An object of type Wire or Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. On the
positive edges of clock, takes on the value of 1 if the value of J
is 1, takes on the value of 0 if the value of K is 1, and toggles
its value if both J and K have value 1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, J, K, clock, output, output_not):
and_1 = Wire()
and_2 = Wire()
or_1 = Wire()
not_1 = Wire()
gate.NOTGate(K, not_1)
gate.ANDGate2(J, output_not, and_1)
gate.ANDGate2(not_1, output, and_2)
gate.ORGate2(and_1, and_2, or_1)
DFlipFlop(or_1, clock, output, output_not)
self.J = J
self.K = K
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "J: " + str(self.J.value) + "\n"
str_ += "K: " + str(self.K.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
J=None,
K=None,
clock=None,
output=None,
output_not=None
):
if J is not None:
self.J.value = J
if K is not None:
self.K.value = K
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not
class JKFlipFlopPresetClear:
"""Construct a new positive edge-triggered JK flip-flop with preset/clear
capabilities.
Args:
J: An object of type Wire. The J input to the flip-flop.
K: An object of type Wire. The K input to the flip-flop.
preset_n: An object of type Wire. Presets output to 1 and output_not to
0 asynchronously if its value is 0.
clear_n: An object of type Wire. Clears output to 0 and output_not to 1
asynchronously if its value is 0.
clock: An object of type Wire or Clock. The clock input to the flip-
flop.
output: An object of type Wire. The output of the flip-flop. On the
positive edges of clock, takes on the value of 1 if the value of J
is 1, takes on the value of 0 if the value of K is 1, and toggles
its value if both J and K have value 1.
output_not: An object of type Wire. The complemented form of output.
"""
def __init__(self, J, K, preset_n, clear_n, clock, output, output_not):
and_1 = Wire()
and_2 = Wire()
or_1 = Wire()
not_1 = Wire()
gate.NOTGate(K, not_1)
gate.ANDGate2(J, output_not, and_1)
gate.ANDGate2(not_1, output, and_2)
gate.ORGate2(and_1, and_2, or_1)
DFlipFlopPresetClear(
or_1,
preset_n,
clear_n,
clock,
output,
output_not
)
self.J = J
self.K = K
self.preset_n = preset_n
self.clear_n = clear_n
self.clock = clock
self.output = output
self.output_not = output_not
def __str__(self):
str_ = ""
str_ += "J: " + str(self.J.value) + "\n"
str_ += "K: " + str(self.K.value) + "\n"
str_ += "preset_n: " + str(self.preset_n.value) + "\n"
str_ += "clear_n: " + str(self.clear_n.value) + "\n"
str_ += "clock: " + str(self.clock.value) + "\n"
str_ += "output: " + str(self.output.value) + "\n"
str_ += "output_not: " + str(self.output_not.value)
return str_
def __call__(
self, *,
J=None,
K=None,
preset_n=None,
clear_n=None,
clock=None,
output=None,
output_not=None
):
if J is not None:
self.J.value = J
if K is not None:
self.K.value = K
if preset_n is not None:
self.preset_n.value = preset_n
if clear_n is not None:
self.clear_n.value = clear_n
if clock is not None:
self.clock.value = clock
if output is not None:
self.output.value = output
if output_not is not None:
self.output_not.value = output_not