-
Notifications
You must be signed in to change notification settings - Fork 13
/
circuits_articles.js
3397 lines (2565 loc) · 88.4 KB
/
circuits_articles.js
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
LogicEmu
Copyright (c) 2018-2023 Lode Vandevenne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
This JS file contains various digital logic tutorials circuits,
and injects them into a dropdown from logicemu.js
*/
registerCircuitGroup('articles');
registerTitle('Binary Logic');
registerCircuit('Logic Gates Tutorial', `
0"A logic gate is a device that implements a boolean function on some amount of"
0"inputs, typically one or two"
0"The OR gate outputs 1 whenever input a OR input b (or both) are on. An OR"
0"gate is denoted with an 'o' in this simulation (but not in real electronics)"
0"Click the switches with the mouse to toggle them on or off and observe the"
0"effect on the output LED:"
"a"s..>o..>l"out"
^
"b"s....
0"A truth table shows the output for each combination of inputs:"
3" a b | OR "
3" -----+---- "
3" 0 0 | 0 "
3" 0 1 | 1 "
3" 1 0 | 1 "
3" 1 1 | 1 "
0"The AND gate outputs 1 only when input a AND input b are on: An AND gate is"
0"denoted with an 'a' in this simulation (again, not in real electronics)"
"a"s..>a..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | AND "
3" -----+---- "
3" 0 0 | 0 "
3" 0 1 | 0 "
3" 1 0 | 0 "
3" 1 1 | 1 "
0"The XOR gate or EXCLUSIVE OR gate outputs 1 whenever exclusively input a OR"
0"exclusively input b (but not both) are on. A XOR gate is denoted with an 'e'"
0"from 'e'xclusive in this simulation (but not in real electronics)"
"a"s..>e..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | XOR "
3" -----+---- "
3" 0 0 | 0 "
3" 0 1 | 1 "
3" 1 0 | 1 "
3" 1 1 | 0 "
0"The NOT gate has a single input and inverts it, so outputs 1 if the switch is"
0"off, and outputs 0 if the switch is on. A NOT gate is denoted with an 'O' in"
0"this simulation (but not in real electronics)"
"a"s..>O..>l"out"
0"Here is its truth table:"
3" a | NOT "
3" --+---- "
3" 0 | 1 "
3" 1 | 0 "
0"A NOR gate is an OR gate with a NOT behind it (so an OR with inverted"
0"output), integrated together as a single gate. The NOR gate outputs 1 only if"
0"both inputs are off. A NOR gate is denoted with an 'O' in this simulation"
0"(again, not in real electronics), in general in this simulation, capital"
0"letters are used as inverted version of the gate (in real life electronics a"
0"little circle at the output is used instead)"
"a"s..>O..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | NOR "
3" -----+---- "
3" 0 0 | 1 "
3" 0 1 | 0 "
3" 1 0 | 0 "
3" 1 1 | 0 "
0"The NAND gate is an AND gate with a NOT behind it, integrated into a single"
0"gate A NAND gate is denoted with an 'A' in this simulation (but not in real"
0"electronics)"
"a"s..>A..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | NAND "
3" -----+----- "
3" 0 0 | 1 "
3" 0 1 | 1 "
3" 1 0 | 1 "
3" 1 1 | 0 "
0"The XNOR gate is a XOR gate with a NOT behind it, integrated into a single"
0"gate. XNOR is sometimes also called EQV from equivalence, because it outputs"
0"1 if both inputs are equal (but this no longer holds true for multi-input"
0"gates, where it instead acts as an inverted parity [odd/even] gate). An XNOR"
0"gate is denoted with an 'E' in this simulation (but not in real electronics)"
"a"s..>E..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | XNOR "
3" -----+----- "
3" 0 0 | 1 "
3" 0 1 | 0 "
3" 1 0 | 0 "
3" 1 1 | 1 "
0"An IMPLY gate outputs 1 except when input a is true and input b is false. Its"
0"name means 'a implies b', and since 'a=true, b=false' is the only combination"
0"that violates that statement, that is the only one where it outputs 0. An"
0"IMPLY gate does not have its own letter notation in this simulation, instead"
0"it can be made with an OR gate with the a input inverted. The inverted input"
0"is denoted with a little circle in this simulation, for once like real"
0"electronics notation actually does it. Real electronics also uses an OR gate"
0"with inverted input as notation for IMPLY, and also uses a little circle at"
0"the input to invert inputs. (note: ensure the render mode is 'graphical' to"
0"see the little circle, in text mode you see a square bracket ']' instead)."
0"Note that this gate is less common compared to the others and is really more"
0"commonly seen as an OR gate with an inverted input, in fact the main goal"
0"here is to demonstrate inverted inputs."
"a"s..]o..>l"out"
^
"b"s....
0"Here is its truth table:"
3" a b | IMPLY "
3" -----+------ "
3" 0 0 | 1 "
3" 0 1 | 1 "
3" 1 0 | 0 "
3" 1 1 | 1 "
0"There are more gates you can make by inverting different inputs or outputs of"
0"any of the gates above, e.g. AND with one inverted input gives NIMPLY. Some"
0"combinations are redundant. In fact, there are 16 possible behaviors in total"
0"for 2-input gates. However the rest are less well established and not"
0"discussed here, you can view them all in the '16 gates' circuit."
0"As a final recap, here is the truth table of all gates seen here:"
3" a b | NOT OR NOR AND NAND XOR XNOR IMPLY NIMPLY"
3" -----+-------------------------------------------------"
3" 0 0 | 1 0 1 0 1 0 1 1 0 "
3" 0 1 | 1 0 0 1 1 0 1 0 "
3" 1 0 | 0 1 0 0 1 1 0 0 1 "
3" 1 1 | 1 0 1 0 0 1 1 0 "
`, 'logic_gates');
registerCircuit('Binary Numbers Tutorial', `
0"In logic circuits, we use binary instead of decimal counting. Decimal uses"
0"ten different digits. Binary uses a similar counting system, but with only 2"
0"digits: 0 and 1. This is because in electronic circuits, it is much simpler"
0"to make a switch with two states than with ten states. Computers typically do"
0"all math and other operations with binary numbers, and only convert the"
0"results to decimal when presenting them to the user."
0"So to be prepared for math circuits coming up later, this is an introduction"
0"to binary numbers and binary counting"
0"Decimal counts up with 10 possible symbols, from 0 to 0. Whenever 9 is"
0"reached, a next digit is incremented (if that next digit is a hidden zero,"
0"since prefix zeroes are usually not shown, it becomes a 1 and thus visible)"
0"With binary, it's just the same, except there are only two possible values,"
0"so next bits get added much faster"
0"Here are the first few decimal and binary numbers:"
3"decimal | binary"
3"--------+-------"
3" 0 | 0"
3" 1 | 1"
3" 2 | 10"
3" 3 | 11"
3" 4 | 100"
3" 5 | 101"
3" 6 | 110"
3" 7 | 111"
3" 8 | 1000"
3" 9 | 1001"
3" 10 | 1010"
3" 11 | 1011"
3" 12 | 1100"
3" 13 | 1101"
3" 14 | 1110"
3" 15 | 1111"
3" 16 | 10000"
3" ^^^^"
3" 8421"
0"Prefix zeroes of a binary number are often shown. For example when one speaks"
0"of an 8-bit binary number, all 8 bits are shown, even zeroes in front. E.g."
0"the number 1 as an 8-bit number is:"
"00000001"
0"To input and output binary numbers, set bits to 1 such that the sum of their"
0"values is the number you want. The value of each bit is a power of two. The"
0"least significant bit has value 1. The more significant bits have higher"
0"powers of two, for example 2 for the second bit, 4 for the third bit, 8 for"
0"the fourth bit, etc..."
0"Form any number you want below. For example, to form the number hundred, set"
0"the bits with values 64, 32 and 4 to 1. The sum of those is 100, and that is"
0"the only possible way to form the number 100. Note that the circuit below"
0"does not actually do anything, it just allows you to toggle some switches and"
0"LEDs to try to form numbers. The sum of all values below is 255 so that is"
0"the highest number you can make, it is the highest unsigned 8-bit value."
"128 64 32 16 8 4 2 1"
l l l l l l l l
^ ^ ^ ^ ^ ^ ^ ^
. . . . . . . .
s s s s s s s s
"128 64 32 16 8 4 2 1"
0"Most circuits operating on numbers will have such switches and LEDs like"
0"above, except some processing with logic gates will happen in-between"
0"We can also add a built-in binary-to-decimal display to switches to allow you"
0"to experiment more easily with binary numbers. The display shows your binary"
0"number in decimal. Try to make '21' for example. You can make all numbers"
0"from 0 to 255, and there is exactly one combination of input switches to make"
0"each number."
T#####################"decimal"
^ ^ ^ ^ ^ ^ ^ ^
. . . . . . . .
s s s s s s s s"binary"
"128 64 32 16 8 4 2 1"
0"As another aid, here you can type any number from 0 to 255 in the box"
0"(click it if it has no blinking cursor) and see its binary output at the LEDs."
"128 64 32 16 8 4 2 1"
l l l l l l l l"binary"
^ ^ ^ ^ ^ ^ ^ ^
. . . . . . . .
T#####################"decimal"
`, 'binary_numbers');
registerCircuit('3-input logic gates', `
0"Gates with 3 or more inputs work similarly to their 2-input counterparts."
0"Multi-input AND only goes on if all inputs are on. Multi-input OR goes on if"
0"any input is on. Multi-input XOR, however, acts differently, it acts as a"
0"parity gate (odd/even), it does not act as a 'one-hot-detector' gate. The"
0"behavior is that of chaining multiple 2-input gates together and that gives"
0"parity gate in case of XOR."
s.... s.... s....
v v v
s..>o..>l s..>a..>l s..>e..>l
^ ^ ^
s.... s.... s....
s.... s.... s....
v v v
s..>O..>l s..>A..>l s..>E..>l
^ ^ ^
s.... s.... s....
0"There are a few other interesting 3-input gates, which we can build"
0"from multiple 2-input gates in our logic notation:"
0"And-Or-Invert (AOI) and Or-And-Invert (OAI), commonly used component"
0"in some electronics applications because it uses a relatively small"
0"amount of transistors"
s.... s....
v v
s..>a>O..>l s..>o>A..>l
^ ^
s...... s......
0"Majority gate"
s....>a..
> v
s.... a>o..>l
> ^
s....>a..
0"One-hot detector gate: outputs only if exactly 1 input is on"
.......
. v
s.+....]a..
. . ] v
s.+.+..>a>o..>l
. . ] ^
s...+..]a..
. ^
.....
0"Equals gate: outputs only if all inputs are equal"
s..>E..
^ v
s.... a..>l
v ^
s..>E..
`, 'gates3');
registerCircuit('De Morgan\'s law', `
0"De Morgan's law: each gate on the right is equivalent to the one on"
0"the left by having both inputs and the output negated"
0"For example, an AND gate can be made by taking an OR gate, inverting"
0"all inputs and the output. And same for all other combinations below."
s..>a..>l s..]O..>l
^ m
s.... s....
s..>o..>l s..]A..>l
^ m
s.... s....
s..>A..>l s..]o..>l
^ m
s.... s....
s..>O..>l s..]a..>l
^ m
s.... s....
s..]o..>l s..>A..>l
^ m
s.... s....
0"You can verify this in the english. E.g.:"
0"'the sun is hot AND round'"
0"has the same meaning as:"
0"'it is NOT so that the sun is NOT hot OR that the sun is NOT round'"
0"Notice the three NOTs there and how the AND is replaced by OR"
`, 'morgans_law');
registerCircuit('NAND logic', `
0"NAND is a universal gate, any other logic gate can be constructed from NANDs"
s....>A....>l"NOT"
s..>A>A....>l"AND"
^
s....
s..>A>A....>l"OR"
^
s..>A..
s..>A..
v ^ v
A.. A....>l"XOR"
^ v ^
s..>A..
0"If negated inputs are permitted (negating in theory requires an extra NAND):"
s..]A>A....>l"XOR"
> ^
s..]A..
s..>A>A>A..>l"NOR"
^
s..>A..
.....>A
. v
s.+..>A>A
. v v
s..>A..>A..>l"XNOR"
0"NAND logic emulating 3-input gates"
0"smallest possible amount of gates used"
s........
v
s..>A>A>A>A......>l"AND"
^
s....
s..>A......
v
s..>A>A>A>A......>l"OR"
^
s..>A..
s........>A..
v ^ v
s..>A.. A.. A....>l"XOR"
v ^ v ^ v ^
A.. A..>A..
^ v ^
s..>A..
s........
v
s..>A>A>A........>l"NAND"
^
s....
s..>A......
v
s..>A>A>A>A>A....>l"NOR"
^
s..>A..
s..........
.
.....>A .>A..
. v v ^ v
s.+..>A>A A.. A..>l"XNOR"
. v v ^ v ^
s..>A..>A..>A..
"a"s....>A.. s...>A>A..>l"MUX"
^ v ^ ^
"s"s....>A A.....>l"MUX" s....+>A
v ^ . ^
"b"s..>A.... "s"s.....>A
...>A..
. ^ .
s.+.... .
. v v
s.+..>A>A>A>A...>l"majority"
. v ^
s..>A........
0"# Wire crossing"
0"It's possible to make a wire crossing from logic gates without any"
0"wire crossing inside of this logic"
0"XOR with 4 NANDs"
s...>A..
v ^ v
A.. A..>l"XOR"
^ v ^
s...>A..
0"wire crossing with 3 XORs"
s....>e...>l
v ^
e..
^ v
s....>e...>l
0"wire crossing with 12 NANDs"
.......>A..
. v ^ v
. A.. A...>l
s...>A.. ^ v ^
v ^ v .>A..
A.. A..
^ v ^ .>A..
s...>A.. v ^ v
. A.. A...>l
. ^ v ^
.......>A..
0"wire crossing with 10 NANDs (various shapes)"
s
...>A.... s..>A..>A..>l .
. ^ v . ^ ^ .......
s...>A..>A>A...>l .>A..>A v v v
v ^ v ^ v ^ v ^ .>A>A..>A
A.. A.. A.. A.. . v v v v
^ v ^ v ^ v ^ v s..>A>A>A>A..>l
s...>A..>A>A...>l .>A..>A . . v
. v ^ . v v . .>A
...>A.... s..>A..>A..>l . v v
.>A>A
.
.
v
l
0"wire crossing with 8 NANDs, of which 2 are 1-input and one is 3-input"
0"(that one is expanded into two-input ones in the version on the right)"
s...>A..>A>A..>l s...>A......>A>A..>l
v ^ v ^ v ^ v ^
A..>A.. A..>A>A>A..
^ v ^ v ^ v ^ v
s...>A..>A>A..>l s...>A......>A>A..>l
0"# More advanced circuits"
0"SR latch (inputs corrected to be not inverted)"
"S"s...>A>A.....>l"Q"
^ .
"R"s...>A>A<.
0"JK flip-flop (edge triggered, from D flip-flop, stable)"
"J" s.....>A>A>A..>A..>A..>A.....>l
^ ^ ^ v ^ . ^ v ^ .
"K" s.....>A>A A>A>A<. .>A>A .
^ ^ ^ . ^ .
"C" s......+.+.......... . .
. . . .
...................
0"half adder"
.....>A..
. ^ v
s...>A.... A....>l"s"
^ . v ^
s......+>A..
.
...>A....>l"c"
0"full adder"
.....>A.. .....>A..
. ^ v . ^ v
s...>A.... A..>A.... A.....>l"s"
^ . v ^ ^ . v ^
s......+>A.. ..+>A..
. . v
s......+........ A.........>l"c"
. ^
...........
`, 'nand_logic');
registerCircuit('NOR logic', `
0"NOR is a universal gate, any other logic gate can be constructed from NORs"
s....>O....>l"NOT"
s..>O>O....>l"AND"
^
s..>O..
s..>O..>O..>l"OR"
^
s....
.....>O
. v
s.+..>O>O
. v v
s..>O..>O..>l"XOR"
s..>O......>l"NOR"
^
s....
s..>O>O>O..>l"NAND"
^
s..>O..
s..>O..
v ^ v
O.. O....>l"XNOR"
^ v ^
s..>O..
0"If negated inputs are permitted (negating in theory requires an extra NOR):"
s..]O>O....>l"XNOR"
> ^
s..]O..
0"NOR logic emulating 3-input gates"
0"smallest possible amount of gates used"
s..........
v
s....>O>O>O>O....>l"OR"
^
s......
s....>O......
v
s....>O>O>O>O....>l"AND"
^
s....>O..
s........>O..
v ^ v
s..>O.. O.. O....>l"XOR"
v ^ v ^ v ^
O.. O..>O..
^ v ^
s..>O..
s..........
v
s....>O>O>O......>l"NOR"
^
s......
s..>O......
v
s..>O>O>O>O>O....>l"NAND"
^
s..>O..
s..........
.
.....>O .>O..
. v v ^ v
s.+..>O>O O.. O..>l"XNOR"
. v v ^ v ^
s..>O..>O..>O..
"a"s....>O.. s...>O>O..>l"MUX"
^ v ^ ^
"s"s....>O O.....>l"MUX" s....+>O
v ^ . ^
"b"s..>O.... "s"s.....>O
...>O..
. ^ .
s.+.... .
. v v
s.+..>O>O>O>O...>l"majority"
. v ^
s..>O........
0"# Wire crossing"
0"It's possible to make a wire crossing from logic gates without any"
0"wire crossing inside of this logic"
0"XNOR with 4 NORs"
s...>O..
v ^ v
O.. O..>l "XNOR"
^ v ^
s...>O..
0"wire crossing with 3 XNORs"
s....>E...>l
v ^
E..
^ v
s....>E...>l
0"wire crossing with 12 NORs"
.......>O..
. v ^ v
. O.. O...>l
s...>O.. ^ v ^
v ^ v .>O..
O.. O..
^ v ^ .>O..
s...>O.. v ^ v
. O.. O...>l
. ^ v ^
.......>O..
0"wire crossing with 10 NORs (various shapes)"
s
...>O.... s..>O..>O..>l .
. ^ v . ^ ^ .......
s...>O..>O>O...>l .>O..>O v v v
v ^ v ^ v ^ v ^ .>O>O..>O
O.. O.. O.. O.. . v v v v
^ v ^ v ^ v ^ v s..>O>O>O>O..>l
s...>O..>O>O...>l .>O..>O . . v
. v ^ . v v . .>O
...>O.... s..>O..>O..>l . v v
.>O>O
.
.
v
l
0"wire crossing with 8 NORs, of which 2 are 1-input and one is 3-input"
0"(that one is expanded in in version on the right)"
s...>O..>O>O..>l s...>O......>O>O..>l
v ^ v ^ v ^ v ^
O..>O.. O..>O>O>O..
^ v ^ v ^ v ^ v
s...>O..>O>O..>l s...>O......>O>O..>l
0"# More advanced circuits"
0"SR latch"
"S"s...>O<....>l"Q"
v .
"R"s...>O..
0"JK flip-flop (edge triggered, from D flip-flop, stable)"
...................
. . . .
"C" s...>O.+.+.......... . .
v v v . v .
"J" s...>O>O>O O>O>O<. .>O>O .
v v v ^ v . v ^ v .
"K" s...>O>O>O>O..>O..>O..>O.....>l
0"half adder"
s......>O
v .
s....>O.+>O..>l"s"
. v ^
.>O>O....>l"c"
0"full adder"
.....>O.. .....>O..
. ^ v . ^ v
s...>O.... O..>O.... O.....>l"s"
^ . v ^ ^ . v ^
s......+>O.. ..+>O..
. . v
s......+........ O.........>l"c"
. ^
...........
`, 'nor_logic');
registerTitle('Flip-flops');
registerCircuit('flip-flops tutorial', `
0"Table of contents:"
0"INSERT:toc"
0"Flip-flops serve as memory elements, or elements that can keep state,"
0"in electronic circuits. This tutorial interactively introduces flip-flops"
0"# Flip-Flop Types"
0"Flip flop types can be broken down into their logic behavior, and their"
0"clock behavior. Logic behavior is discussed here, clock behavior (latch vs"
0"flip-flop) in the next chapter."
0"There are 4 main flip-flop types: SR, D, T and JK. Before looking at how they"
0"work at logic-gate level, let's first use the prebuilt components that this"
0"simulation provides to look at their behavior."
0"All of these types have a clock input C. This clock is edge-triggered, that"
0"is, only at that exact instant where it goes on, it will update its state. At"
0"any other time, while the clock is high or low, it will remember that state."
0"## SR Flip-Flop"
0"The SR flip-flop set-reset flip-flop has two inputs: S and R. Whenever the"
0"clock is edge-triggered, the output will go on if S is enabled, of is R is"
0"enabled, or stay as-is if neither S nor R are enabled. Having both S and R"
0"enabled is considered an invalid combination, and whichever behavior it"
0"happens to show here does not matter as it is unspecified and may violate"
0"assumptions"
"S"s..>j#q..>l"Q"
###
"R"s..>k##
###
"C"s..>c##
0"The truth table of the SR flip-flop is as follows. It shows the transition"
0"(if any) that happens at each clock cycle: What is the next Q depending on"
0"the D input and the current Q"
3"+---+---+-------+"
3"| S | R | Qnext |"
3"+---+---+-------+"
3"| 0 | 0 | Q |"
3"| 0 | 1 | 1 |"
3"| 1 | 0 | 0 |"
3"| 1 | 1 |invalid|"
3"+---+---+-------+"
0"## D Flip-Flop"
0"The D flip-flop or data flip-flop will remember the state of its D input line"
0"whenever the clock goes from low to high. The clock is edge-triggered, that"
0"is, only at that exact instant where it goes on, it will update its state."
0"Try it out: The way to get the output enabled, is to first enable d, then"
0"turn the clock c from off to on."
"D"s..>d#q..>l"Q"
###
"C"s..>c##
0"The truth table of the D flip-flop is as follows. It shows the transition (if"
0"any) that happens at each clock cycle: What is the next Q depending on the D"
0"input and the current Q"
3"+---+-------+"
3"| D | Qnext |"
3"+---+-------+"
3"| 0 | 0 |"
3"| 1 | 1 |"
3"+---+-------+"
0"It looks as if the truth table shows that the state is just equal to the"
0"input D, so it would seem as if it doesn't do much interesting? But, it"
0"remembers the state while the clock is off. A D flip-flop delays the input"
0"by 1 clock cycle. By looping its output back to the input with a"
0"multiplexer, this is a very useful component for registers in CPU's"
0"## T Flip-Flop"
0"The T flip-flop or toggle flip-flop will toggle its state whenever the T"
0"input is enabled and the clock is triggered. If T is off, it will. keep its"
0"state when the clock triggers."
"T"s..>t#q..>l"Q"
###
"C"s..>c##
0"In the truth table, Q' represents the inverse of Q"
3"+---+-------+"
3"| T | Qnext |"
3"+---+-------+"
3"| 0 | Q |"
3"| 1 | Q' |"
3"+---+-------+"
0"## JK Flip-Flop"
0"The JK flip-flop is similar to the SR input, the J input behaves like set and"
0"the k input like reset. But for the JK flip-flop, the combination of having"
0"both j and k inputs enabled also has well defined behavior: it toggles the"
0"output similar to a T flip-flop in that case."
"J"s..>j#q..>l"Q"
###
"K"s..>k##
###
"C"s..>c##
0"The truth table of the JK flip-flop is as follows. It shows the transition"
0"(if any) that happens at each clock cycle: What is the next Q depending on"
0"the D input and the current Q"
3"+---+---+-------+"
3"| S | R | Qnext |"
3"+---+---+-------+"
3"| 0 | 0 | Q |"
3"| 0 | 1 | 1 |"
3"| 1 | 0 | 0 |"
3"| 1 | 1 | Q' |"
3"+---+---+-------+"
0"The JK flip-flop is universal, it can easily be configured to act as SR, D or"
0"T flip-flop: T by connecting J and K together, D by making K the inverse of"
0"J."
0"In practice, many flip-flops also come with an asynchronous reset input"
0"(clear), and somtimes such asynchronous set input (preset), and often also an"
0"inverted version of the output. Asynchronous means it ignores the clock and"
0"overrides everything."
"preset"
s
.
.
v
"J"s..>j#q..>l"Q"
###
"K"s..>k##
###
"C"s..>c#Q..>l"Q'"
^
.