-
Notifications
You must be signed in to change notification settings - Fork 3
/
UnitTest.hpp
2013 lines (1673 loc) · 57.6 KB
/
UnitTest.hpp
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
#ifndef UNITTEST_HPP
#define UNITTEST_HPP
#ifndef NDEBUG // ifndef NDEBUG
/// Uncomment to enable Exception checking and throwing.
#define THROW_EXCEPTIONS
#include <iostream>
#include <cassert> // Debug assertion
#include <bitset>
#include "SignedBitset.hpp"
#include "FloatingBitset.hpp"
#include "Exceptions.hpp"
#include "utils.hpp"
#include "SynchrotronComponent.hpp"
#include "CPUComponents/ANDGate.hpp"
#include "CPUComponents/NANDGate.hpp"
#include "CPUComponents/ORGate.hpp"
#include "CPUComponents/NORGate.hpp"
#include "CPUComponents/XORGate.hpp"
#include "CPUComponents/NOTGate.hpp"
#include "CPUComponents/MemoryCell.hpp"
#include "CPUComponents/SHIFTLeft.hpp"
#include "CPUComponents/SHIFTRight.hpp"
#include "CPUComponents/Memory.hpp"
#include "CPUComponents/Clock.hpp"
#include "CPUComponents/ADD.hpp"
#include "CPUComponents/SUBTRACT.hpp"
#include "CPUComponents/MULTIPLY.hpp"
#include "CPUComponents/DIVIDE.hpp"
#include "CPUComponents/MODULO.hpp"
#include "CPUComponents/COMPERATOR.hpp"
#include "CPUInstructions/Instruction.hpp"
#include "CPUInstructions/ANDInstruction.hpp"
#include "CPUInstructions/NANDInstruction.hpp"
#include "CPUInstructions/ORInstruction.hpp"
#include "CPUInstructions/NORInstruction.hpp"
#include "CPUInstructions/XORInstruction.hpp"
#include "CPUInstructions/NOTInstruction.hpp"
#include "CPUInstructions/SHLInstruction.hpp"
#include "CPUInstructions/SHRInstruction.hpp"
#include "CPUInstructions/ADDInstruction.hpp"
#include "CPUInstructions/SUBInstruction.hpp"
#include "CPUInstructions/MULInstruction.hpp"
#include "CPUInstructions/DIVInstruction.hpp"
#include "CPUInstructions/MODInstruction.hpp"
#include "CPUInstructions/CMPInstruction.hpp"
#include "CPUFactory/SCAMParser.hpp"
#include "CPUFactory/SCAMAssembler.hpp"
/** \brief Boolean used to check if statement threw an exception.
*/
static bool _Error_Assertion_Failed;
/** \brief
* Addition to the assert() MACRO.
*
* Execute F and try to catch error E. If caught, asserts to true.
*
* \param F
* The expression to try executing. This is expected to be a function.
*
* However, a constructor can be called by typedeffing it and calling:
*
* typedef Class<template_args> ctor;
* assert_error(ctor class_instance_name, Exceptions::Exception);
* \param E
* The expected error thrown by executing F.
*/
#define assert_error(F, E) {\
_Error_Assertion_Failed = false; \
try { \
F; \
} catch (E&) { \
_Error_Assertion_Failed = true; \
} \
assert(_Error_Assertion_Failed); \
}
using namespace CPUComponents;
using namespace CPUInstructions;
/*
* Constant bit values.
*/
static std::bitset<1> one_bit_0 = 0, // 0
one_bit_1 = 1; // 1
static std::bitset<2> two_bit_0 = 0, // 00
two_bit_1 = 1, // 01
two_bit_2 = 2, // 10
two_bit_3 = 3; // 11
static std::bitset<3> thr_bit_0 = 0, // 000
thr_bit_1 = 1, // 001
thr_bit_2 = 2, // 010
thr_bit_3 = 3, // 011
thr_bit_4 = 4, // 100
thr_bit_5 = 5, // 101
thr_bit_6 = 6, // 110
thr_bit_7 = 7; // 111
static std::bitset<4> for_bit_0 = 0, // 0000
for_bit_1 = 1, // 0001
for_bit_2 = 2, // 0010
for_bit_3 = 3, // 0011
for_bit_4 = 4, // 0100
for_bit_5 = 5, // 0101
for_bit_7 = 7, // 0111
for_bit_8 = 8, // 1000
for_bit_A = 10, // 1010
for_bit_B = 11, // 1011
for_bit_E = 14, // 1110
for_bit_F = 15; // 1111
/*
* Constant signals and signal providers.
*/
static SynchrotronComponent<1> signal1_0(one_bit_0.to_ulong()), // 0
signal1_0_(one_bit_0.to_ulong()), // 0 other unique
signal1_1(one_bit_1.to_ulong()), // 1
signal1_1_(one_bit_1.to_ulong()); // 1 other unique
static SynchrotronComponent<2> signal2_0(two_bit_0.to_ulong()), // 00
signal2_0_(two_bit_0.to_ulong()), // 00 other unique
signal2_1(two_bit_1.to_ulong()), // 01
signal2_1_(two_bit_1.to_ulong()), // 01 other unique
signal2_2(two_bit_2.to_ulong()), // 10
signal2_2_(two_bit_2.to_ulong()), // 10 other unique
signal2_3(two_bit_3.to_ulong()), // 11
signal2_3_(two_bit_3.to_ulong()); // 11 other unique
static SynchrotronComponent<4> signal4_8( for_bit_8.to_ulong() ), // 1000
signal4_A( for_bit_A.to_ulong() ); // 1010
static SynchrotronComponent<1> signalProvider1_0_0( {&signal1_0, &signal1_0_} ), // 0 0
signalProvider1_0_1( {&signal1_0, &signal1_1} ), // 0 1
signalProvider1_1_0( {&signal1_1, &signal1_0} ), // 1 0
signalProvider1_1_1( {&signal1_1, &signal1_1_} ); // 1 1
static SynchrotronComponent<2> signalProvider2_0_0( {&signal2_0, &signal2_0_} ), // 00 00
signalProvider2_0_1( {&signal2_0, &signal2_1} ), // 00 01
signalProvider2_1_0( {&signal2_1, &signal2_0} ), // 01 00
signalProvider2_1_1( {&signal2_1, &signal2_1_} ), // 01 01
signalProvider2_0_2( {&signal2_0, &signal2_2} ), // 00 10
signalProvider2_1_2( {&signal2_1, &signal2_2} ), // 01 10
signalProvider2_0_3( {&signal2_0, &signal2_3} ), // 00 11
signalProvider2_1_3( {&signal2_1, &signal2_3} ); // 01 11
/** \brief
* Bitset : Test basic bitset methods to show how they work.
*/
void testBitset(void) {
// Needs fixed size at compile time.
std::bitset<4> x = 0xA, y;
assert(x == for_bit_A);
std::cout << "x : " << x << std::endl;
assert(x.to_ulong() == 10ul);
std::cout << "As ulong : " << x.to_ulong() << std::endl;
assert(x.to_string() == "1010");
std::cout << "As string : " << x.to_string() << std::endl;
std::cout << "x |= 4 = " << (x |= 4) << std::endl;
assert(x == 0xE);
std::cout << "x invert = " << x.flip() << std::endl;
assert(x == for_bit_1);
std::cout << "x count = " << x.count() << std::endl;
assert(x.count() == 1);
std::cout << "x any (| all) = " << x.any() << std::endl; // OR
assert(x.any() == true);
std::cout << "x all (& all) = " << x.all() << std::endl; // AND
assert(x.all() == false);
std::cout << "x none(~|all) = " << x.none() << std::endl; // NOR
assert(x.all() == false);
std::cout << "x test.0 = " << x.test(0) << std::endl;
assert(x.test(0) == true);
std::cout << "x test.3 = " << x.test(3) << std::endl;
assert(x.test(3) == false);
std::cout << "x size = " << x.size() << std::endl;
assert(x.size() == 4);
std::cout << "x reset = " << x.reset() << std::endl;
assert(x == for_bit_0);
std::cout << "x count = " << x.count() << std::endl;
assert(x.count() == 0);
std::cout << "x set = " << x.set() << std::endl;
assert(x == for_bit_F);
std::cout << "x set.3=0 = " << x.set(3, false) << std::endl;
assert(x == 0x7);
std::cout << "x << 1 = " << x.operator <<(1) << std::endl;
assert(x == 0x7); // 0111
y = for_bit_A; // 1010
assert((x & y) == 0x2);
}
// TO-DO
//void testSignedBitset(void) {
// SignedBitset<4> x(-1);
//}
/** \brief Test FloatingBitset class
*/
void testFloatingBitset(void) {
FloatingBitset<4> b_8(for_bit_8.to_ullong()),
b0_50(0.50f),
b0_63(0.63f),
b0_99(0.99f),
b1_63(1.63f);
FloatingBitset<16> b0_99_(0.99f),
b0_001(0.001f),
b_l(23210.9876);
assert(SysUtils::epsilon_equals(b_8.to_double(), -8.0));
assert(SysUtils::epsilon_equals(b0_50.to_double(), 0.5));
assert(SysUtils::epsilon_equals(b0_63.to_double(), 0.625));
assert(SysUtils::epsilon_equals(b0_99.to_double(), 0.9375));
assert(SysUtils::epsilon_equals(b1_63.to_double(), 1.625));
assert(SysUtils::epsilon_equals(b0_99_.to_double(), 0.99));
assert(SysUtils::epsilon_equals(b0_001.to_double(), 0.001));
assert(SysUtils::epsilon_equals(b_l.to_double(), 23210.9876));
// for (int i = 0; i <= 0xF; ++i)
// std::cout << "Float: " << new FloatingBitset<4>(i) << std::endl;
}
/** \brief
* SynchrotronComponent : Test SynchrotronComponent class.
*/
void testSynchrotronComponent(void) {
SynchrotronComponent<4> s_with_null,
s_with_1(for_bit_1.to_ulong()),
s_with_2(for_bit_2.to_ulong()),
s_sign_1(for_bit_1.to_ulong()),
*s_pointed = new SynchrotronComponent<4>(for_bit_2.to_ulong());
assert(s_pointed != nullptr);
assert(s_with_null.getState() == for_bit_0);
assert(s_with_null.getBitWidth() == 4);
assert(s_with_null.getInputs().size() == 0);
assert(s_with_null.getOutputs().size() == 0);
assert(s_with_1.getState() == for_bit_1);
assert(s_with_1.getBitWidth() == 4);
assert(s_with_1.getInputs().size() == 0);
assert(s_with_1.getOutputs().size() == 0);
assert(s_pointed->getState() == for_bit_2);
assert(s_pointed->getBitWidth() == 4);
assert(s_pointed->getInputs().size() == 0);
assert(s_pointed->getOutputs().size() == 0);
s_with_null.addOutput( {&s_with_1, &s_with_2} );
assert(s_with_null.getOutputs().size() == 2);
s_with_null.addOutput(s_with_1); // Add again to see if size() increases, it should not.
assert(s_with_null.getOutputs().size() == 2);
assert(s_with_1.getInputs().size() == 1);
s_with_1.addInput(s_with_null); // Add again to see if size() increases, it should not.
assert(s_with_1.getInputs().size() == 1);
assert(s_with_2.getInputs().size() == 1);
s_with_null.emit(); // Internal State is 0x0, gets ORed to subscribers
assert(s_with_1.getState() == for_bit_1);
s_with_null.addInput(*s_pointed);
assert(s_with_null.getInputs().size() == 1);
assert(s_pointed->getOutputs().size() == 1);
s_pointed->emit(); // Internal State is 0x2, gets ORed to subscribers
assert(s_with_null.getState() == for_bit_2); // tick()ed by s_pointed
assert(s_with_1.getState() == for_bit_3); // tick()ed by s_with_null = 0x2 | 0x1
assert(s_with_2.getState() == for_bit_2);
s_with_1.removeInput(s_with_null);
assert(s_with_1.getInputs().size() == 0);
assert(s_with_null.getOutputs().size() == 1);
s_with_null.removeOutput(s_with_1); // Remove again to see if size() decreases, it should not.
assert(s_with_null.getOutputs().size() == 1);
s_sign_1.addOutput(s_with_null);
assert(s_with_null.getInputs().size() == 2);
assert(s_sign_1.getOutputs().size() == 1);
s_with_null.tick(); // Should update from 0x2 to 0x3 (==> s_sign_1(1) | s_pointed(2) == 3)
assert(s_with_null.getState() == for_bit_3);
assert(s_with_1.getState() == for_bit_3); // unchanged
assert(s_with_2.getState() == for_bit_3); // tick()ed by s_with_null = 0x3 | 0x2
// flow: {s_pointed, s_sign_1} -> s_with_null -> s_with_2
SynchrotronComponent<4> s_copy_sign(s_with_2);
// flow: {s_pointed, s_sign_1} -> s_with_null -> {s_with_2, s_copy_sign}
assert(s_copy_sign.getInputs().size() == 1);
assert(s_copy_sign.getOutputs().size() == 0);
assert(s_with_null.getOutputs().size() == 2);
assert(s_copy_sign.getState() == for_bit_0);
s_with_null.emit();
assert(s_copy_sign.getState() == for_bit_3);
{ // Create components in block to check dtors after block
SynchrotronComponent<4> s_copy_both(s_with_null, true),
s_with_8(for_bit_8.to_ulong());
// flow: {s_pointed, s_sign_1} -> {s_with_null, s_copy_both} -> {s_with_2, s_copy_sign}
s_copy_both.addInput(s_with_8);
// flow: s_with_8 -> s_copy_both
assert(s_with_8.getInputs().size() == 0);
assert(s_with_8.getOutputs().size() == 1);
assert(s_copy_both.getInputs().size() == 3);
assert(s_copy_both.getOutputs().size() == 2);
assert(s_copy_sign.getInputs().size() == 2);
assert(s_with_2.getInputs().size() == 2);
assert(s_pointed->getOutputs().size() == 2);
assert(s_sign_1.getOutputs().size() == 2);
s_with_8.emit();
assert(s_copy_both.getState() == for_bit_B); // tick()ed by s_with_8 = 0x0 | 0x8 | 0x3
assert(s_with_2.getState() == for_bit_B); // tick()ed by s_copy_both = 0x3 | 0x8
assert(s_copy_sign.getState() == for_bit_B); // tick()ed by s_copy_both = 0x3 | 0x8
} // s_copy_both and s_with_8 destructed => connections should be gone
// flow: {s_pointed, s_sign_1} -> s_with_null -> {s_with_2, s_copy_sign}
assert(s_pointed->getOutputs().size() == 1);
assert(s_sign_1.getOutputs().size() == 1);
assert(s_copy_sign.getInputs().size() == 1);
assert(s_with_2.getInputs().size() == 1);
delete s_pointed;
}
/** \brief
* AND Gate : Test basic logic.
*/
void testLogic_AND_const(void) {
ANDGate<1> g1,
g1_0_0(signalProvider1_0_0),
g1_0_1(signalProvider1_0_1),
g1_1_0(signalProvider1_1_0),
g1_1_1(signalProvider1_1_1);
ANDGate<2> g2_0_0(signalProvider2_0_0),
g2_0_1(signalProvider2_0_1),
g2_1_0(signalProvider2_1_0),
g2_1_1(signalProvider2_1_1),
g2_0_2(signalProvider2_0_2),
g2_1_2(signalProvider2_1_2),
g2_0_3(signalProvider2_0_3),
g2_1_3(signalProvider2_1_3);
// AND Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// AND Gate with width 1 (2 inputs)
assert(g1_0_0.getState() == one_bit_0);
g1_0_0.tick();
assert(g1_0_0.getState() == one_bit_0);
assert(g1_0_1.getState() == one_bit_0);
g1_0_1.tick();
assert(g1_0_1.getState() == one_bit_0);
assert(g1_1_0.getState() == one_bit_0);
g1_1_0.tick();
assert(g1_1_0.getState() == one_bit_0);
assert(g1_1_1.getState() == one_bit_0);
g1_1_1.tick();
assert(g1_1_1.getState() == one_bit_1); // Output changed to 1 after tick()
// AND Gate with width 2 (2 inputs)
assert(g2_0_0.getState() == two_bit_0);
g2_0_0.tick();
assert(g2_0_0.getState() == two_bit_0);
assert(g2_0_1.getState() == two_bit_0);
g2_0_1.tick();
assert(g2_0_1.getState() == two_bit_0);
assert(g2_1_0.getState() == two_bit_0);
g2_1_0.tick();
assert(g2_1_0.getState() == two_bit_0);
assert(g2_1_1.getState() == two_bit_0);
g2_1_1.tick();
assert(g2_1_1.getState() == two_bit_1); // Output changed to 1 after tick()
assert(g2_0_2.getState() == two_bit_0);
g2_0_2.tick();
assert(g2_0_2.getState() == two_bit_0);
assert(g2_1_2.getState() == two_bit_0);
g2_1_2.tick();
assert(g2_1_2.getState() == two_bit_0);
assert(g2_0_3.getState() == two_bit_0);
g2_0_3.tick();
assert(g2_0_3.getState() == two_bit_0);
assert(g2_1_3.getState() == two_bit_0);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_1); // Output changed to 1 after tick()
}
/** \brief
* AND Gate : Test dynamic logic.
*/
void testLogic_AND_dynamic(void) {
ANDGate<2> g2;
g2.addInput( {&signal2_0, &signal2_0_} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_0);
g2.removeInput(signal2_0_);
assert(g2.getInputs().size() == 0);
g2.addInput( {&signal2_1, &signal2_2} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_2);
assert(g2.getInputs().size() == 1);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_1);
g2.removeInput(signal2_1);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* NAND Gate : Test basic logic.
*/
void testLogic_NAND_const(void) {
NANDGate<1> g1,
g1_0_0(signalProvider1_0_0),
g1_0_1(signalProvider1_0_1),
g1_1_0(signalProvider1_1_0),
g1_1_1(signalProvider1_1_1);
NANDGate<2> g2_0_0(signalProvider2_0_0),
g2_0_1(signalProvider2_0_1),
g2_1_0(signalProvider2_1_0),
g2_1_1(signalProvider2_1_1),
g2_0_2(signalProvider2_0_2),
g2_1_2(signalProvider2_1_2),
g2_0_3(signalProvider2_0_3),
g2_1_3(signalProvider2_1_3);
// NAND Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// NAND Gate with width 1 (2 inputs)
assert(g1_0_0.getState() == one_bit_0);
g1_0_0.tick();
assert(g1_0_0.getState() == one_bit_1);
assert(g1_0_1.getState() == one_bit_0);
g1_0_1.tick();
assert(g1_0_1.getState() == one_bit_1);
assert(g1_1_0.getState() == one_bit_0);
g1_1_0.tick();
assert(g1_1_0.getState() == one_bit_1);
assert(g1_1_1.getState() == one_bit_0);
g1_1_1.tick();
assert(g1_1_1.getState() == one_bit_0); // Output stayed 0 after tick()
// NAND Gate with width 2 (2 inputs)
assert(g2_0_0.getState() == two_bit_0);
g2_0_0.tick();
assert(g2_0_0.getState() == two_bit_3);
assert(g2_0_1.getState() == two_bit_0);
g2_0_1.tick();
assert(g2_0_1.getState() == two_bit_3);
assert(g2_1_0.getState() == two_bit_0);
g2_1_0.tick();
assert(g2_1_0.getState() == two_bit_3);
assert(g2_1_1.getState() == two_bit_0);
g2_1_1.tick();
assert(g2_1_1.getState() == two_bit_2); // Output stayed 0 after tick()
assert(g2_0_2.getState() == two_bit_0);
g2_0_2.tick();
assert(g2_0_2.getState() == two_bit_3);
assert(g2_1_2.getState() == two_bit_0);
g2_1_2.tick();
assert(g2_1_2.getState() == two_bit_3);
assert(g2_0_3.getState() == two_bit_0);
g2_0_3.tick();
assert(g2_0_3.getState() == two_bit_3);
assert(g2_1_3.getState() == two_bit_0);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_2); // Output stayed 0 after tick()
}
/** \brief
* NAND Gate : Test dynamic logic.
*/
void testLogic_NAND_dynamic(void) {
NANDGate<2> g2;
g2.addInput( {&signal2_0, &signal2_0_} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_0);
g2.removeInput(signal2_0_);
assert(g2.getInputs().size() == 0);
g2.addInput( {&signal2_1, &signal2_2} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_2);
assert(g2.getInputs().size() == 1);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_2);
g2.removeInput(signal2_1);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* OR Gate : Test basic logic.
*/
void testLogic_OR_const(void) {
ORGate<1> g1,
g1_0_0(signalProvider1_0_0),
g1_0_1(signalProvider1_0_1),
g1_1_0(signalProvider1_1_0),
g1_1_1(signalProvider1_1_1);
ORGate<2> g2_0_0(signalProvider2_0_0),
g2_0_1(signalProvider2_0_1),
g2_1_0(signalProvider2_1_0),
g2_1_1(signalProvider2_1_1),
g2_0_2(signalProvider2_0_2),
g2_1_2(signalProvider2_1_2),
g2_0_3(signalProvider2_0_3),
g2_1_3(signalProvider2_1_3);
// OR Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// OR Gate with width 1 (2 inputs)
assert(g1_0_0.getState() == one_bit_0);
g1_0_0.tick();
assert(g1_0_0.getState() == one_bit_0);
assert(g1_0_1.getState() == one_bit_0);
g1_0_1.tick();
assert(g1_0_1.getState() == one_bit_1);
assert(g1_1_0.getState() == one_bit_0);
g1_1_0.tick();
assert(g1_1_0.getState() == one_bit_1);
assert(g1_1_1.getState() == one_bit_0);
g1_1_1.tick();
assert(g1_1_1.getState() == one_bit_1); // Output changed to 1 after tick()
// OR Gate with width 2 (2 inputs)
assert(g2_0_0.getState() == two_bit_0);
g2_0_0.tick();
assert(g2_0_0.getState() == two_bit_0);
assert(g2_0_1.getState() == two_bit_0);
g2_0_1.tick();
assert(g2_0_1.getState() == two_bit_1);
assert(g2_1_0.getState() == two_bit_0);
g2_1_0.tick();
assert(g2_1_0.getState() == two_bit_1);
assert(g2_1_1.getState() == two_bit_0);
g2_1_1.tick();
assert(g2_1_1.getState() == two_bit_1); // Output changed to 1 after tick()
assert(g2_0_2.getState() == two_bit_0);
g2_0_2.tick();
assert(g2_0_2.getState() == two_bit_2);
assert(g2_1_2.getState() == two_bit_0);
g2_1_2.tick();
assert(g2_1_2.getState() == two_bit_3);
assert(g2_0_3.getState() == two_bit_0);
g2_0_3.tick();
assert(g2_0_3.getState() == two_bit_3);
assert(g2_1_3.getState() == two_bit_0);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_3); // Output changed to 1 after tick()
}
/** \brief
* OR Gate : Test dynamic logic.
*/
void testLogic_OR_dynamic(void) {
ORGate<2> g2;
g2.addInput( {&signal2_0, &signal2_0_} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_0);
g2.removeInput(signal2_0_);
assert(g2.getInputs().size() == 0);
g2.addInput( {&signal2_1, &signal2_2} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_2);
assert(g2.getInputs().size() == 1);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_1);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* NOR Gate : Test basic logic.
*/
void testLogic_NOR_const(void) {
NORGate<1> g1,
g1_0_0(signalProvider1_0_0),
g1_0_1(signalProvider1_0_1),
g1_1_0(signalProvider1_1_0),
g1_1_1(signalProvider1_1_1);
NORGate<2> g2_0_0(signalProvider2_0_0),
g2_0_1(signalProvider2_0_1),
g2_1_0(signalProvider2_1_0),
g2_1_1(signalProvider2_1_1),
g2_0_2(signalProvider2_0_2),
g2_1_2(signalProvider2_1_2),
g2_0_3(signalProvider2_0_3),
g2_1_3(signalProvider2_1_3);
// NOR Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// NOR Gate with width 1 (2 inputs)
assert(g1_0_0.getState() == one_bit_0);
g1_0_0.tick();
assert(g1_0_0.getState() == one_bit_1);
assert(g1_0_1.getState() == one_bit_0);
g1_0_1.tick();
assert(g1_0_1.getState() == one_bit_0);
assert(g1_1_0.getState() == one_bit_0);
g1_1_0.tick();
assert(g1_1_0.getState() == one_bit_0);
assert(g1_1_1.getState() == one_bit_0);
g1_1_1.tick();
assert(g1_1_1.getState() == one_bit_0); // Output changed to 1 after tick()
// NOR Gate with width 2 (2 inputs)
assert(g2_0_0.getState() == two_bit_0);
g2_0_0.tick();
assert(g2_0_0.getState() == two_bit_3);
assert(g2_0_1.getState() == two_bit_0);
g2_0_1.tick();
assert(g2_0_1.getState() == two_bit_2);
assert(g2_1_0.getState() == two_bit_0);
g2_1_0.tick();
assert(g2_1_0.getState() == two_bit_2);
assert(g2_1_1.getState() == two_bit_0);
g2_1_1.tick();
assert(g2_1_1.getState() == two_bit_2); // Output changed to 1 after tick()
assert(g2_0_2.getState() == two_bit_0);
g2_0_2.tick();
assert(g2_0_2.getState() == two_bit_1);
assert(g2_1_2.getState() == two_bit_0);
g2_1_2.tick();
assert(g2_1_2.getState() == two_bit_0);
assert(g2_0_3.getState() == two_bit_0);
g2_0_3.tick();
assert(g2_0_3.getState() == two_bit_0);
assert(g2_1_3.getState() == two_bit_0);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_0); // Output changed to 1 after tick()
}
/** \brief
* NOR Gate : Test dynamic logic.
*/
void testLogic_NOR_dynamic(void) {
NORGate<2> g2;
g2.addInput( {&signal2_0, &signal2_0_} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_0);
g2.removeInput(signal2_0_);
assert(g2.getInputs().size() == 0);
g2.addInput( {&signal2_1, &signal2_2} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_2);
assert(g2.getInputs().size() == 1);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_1);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* XOR Gate : Test basic logic.
*/
void testLogic_XOR_const(void) {
XORGate<1> g1,
g1_0_0(signalProvider1_0_0),
g1_0_1(signalProvider1_0_1),
g1_1_0(signalProvider1_1_0),
g1_1_1(signalProvider1_1_1);
XORGate<2> g2_0_0(signalProvider2_0_0),
g2_0_1(signalProvider2_0_1),
g2_1_0(signalProvider2_1_0),
g2_1_1(signalProvider2_1_1),
g2_0_2(signalProvider2_0_2),
g2_1_2(signalProvider2_1_2),
g2_0_3(signalProvider2_0_3),
g2_1_3(signalProvider2_1_3);
// XOR Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// XOR Gate with width 1 (2 inputs)
assert(g1_0_0.getState() == one_bit_0);
g1_0_0.tick();
assert(g1_0_0.getState() == one_bit_0);
assert(g1_0_1.getState() == one_bit_0);
g1_0_1.tick();
assert(g1_0_1.getState() == one_bit_1);
assert(g1_1_0.getState() == one_bit_0);
g1_1_0.tick();
assert(g1_1_0.getState() == one_bit_1);
assert(g1_1_1.getState() == one_bit_0);
g1_1_1.tick();
assert(g1_1_1.getState() == one_bit_0); // Output changed to 1 after tick()
// XOR Gate with width 2 (2 inputs)
assert(g2_0_0.getState() == two_bit_0);
g2_0_0.tick();
assert(g2_0_0.getState() == two_bit_0);
assert(g2_0_1.getState() == two_bit_0);
g2_0_1.tick();
assert(g2_0_1.getState() == two_bit_1);
assert(g2_1_0.getState() == two_bit_0);
g2_1_0.tick();
assert(g2_1_0.getState() == two_bit_1);
assert(g2_1_1.getState() == two_bit_0);
g2_1_1.tick();
assert(g2_1_1.getState() == two_bit_0); // Output changed to 1 after tick()
assert(g2_0_2.getState() == two_bit_0);
g2_0_2.tick();
assert(g2_0_2.getState() == two_bit_2);
assert(g2_1_2.getState() == two_bit_0);
g2_1_2.tick();
assert(g2_1_2.getState() == two_bit_3);
assert(g2_0_3.getState() == two_bit_0);
g2_0_3.tick();
assert(g2_0_3.getState() == two_bit_3);
assert(g2_1_3.getState() == two_bit_0);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_2); // Output changed to 1 after tick()
g2_1_3.addInput(signal2_1_); // Test case: 1^1^1 == 1
assert(g2_1_3.getInputs().size() == 3);
g2_1_3.tick();
assert(g2_1_3.getState() == two_bit_3); // 01^11^01 = 11
}
/** \brief
* XOR Gate : Test dynamic logic.
*/
void testLogic_XOR_dynamic(void) {
XORGate<2> g2;
g2.addInput( {&signal2_0, &signal2_0_} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_0);
g2.removeInput(signal2_0_);
assert(g2.getInputs().size() == 0);
g2.addInput( {&signal2_1, &signal2_2} );
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_2);
assert(g2.getInputs().size() == 1);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 2);
g2.tick();
assert(g2.getState() == two_bit_2);
g2.removeInput(signal2_1);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* NOT Gate : Test basic logic.
*/
void testLogic_NOT_const(void) {
NOTGate<1> g1,
g1_0( { &signal1_0 } ), // equivalent to g1_0; g1_0.addInput(signal1_0);
g1_1( { &signal1_1 } );
NOTGate<2> g2_0( { &signal2_0 } ),
g2_1( { &signal2_1 } ),
g2_2( { &signal2_2 } ),
g2_3( { &signal2_3 } );
// NOT Gate with width 1 (0 inputs)
assert_error(g1.tick(), Exceptions::Exception);
// NOT Gate with width 1 (2 inputs)
assert_error( g1_0.addInput(signal1_1), Exceptions::Exception);
// NOT Gate with width 1 (1 input)
assert(g1_0.getState() == one_bit_0);
g1_0.tick();
assert(g1_0.getState() == one_bit_1); // Output changed to 1 after tick()
assert(g1_1.getState() == one_bit_0);
g1_1.tick();
assert(g1_1.getState() == one_bit_0);
// NOT Gate with width 2 (1 input)
assert(g2_0.getState() == two_bit_0);
g2_0.tick();
assert(g2_0.getState() == two_bit_3);
assert(g2_1.getState() == two_bit_0);
g2_1.tick();
assert(g2_1.getState() == two_bit_2);
assert(g2_2.getState() == two_bit_0);
g2_2.tick();
assert(g2_2.getState() == two_bit_1);
assert(g2_3.getState() == two_bit_0);
g2_3.tick();
assert(g2_3.getState() == two_bit_0);
}
/** \brief
* NOT Gate : Test dynamic logic.
*/
void testLogic_NOT_dynamic(void) {
NOTGate<2> g2;
g2.addInput(signal2_0);
assert(g2.getInputs().size() == 1);
g2.tick();
assert(g2.getState() == two_bit_3);
g2.removeInput(signal2_0);
assert(g2.getInputs().size() == 0);
g2.addInput(signal2_1);
assert(g2.getInputs().size() == 1);
g2.tick();
assert(g2.getState() == two_bit_2);
g2.removeInput(signal2_1);
assert(g2.getInputs().size() == 0);
g2.addInput(signal2_3);
assert(g2.getInputs().size() == 1);
g2.tick();
assert(g2.getState() == two_bit_0);
g2.removeInput(signal2_3);
assert(g2.getInputs().size() == 0);
}
/** \brief
* All gates : Test logic combinations.
*/
void testLogic_Combinations(void) {
// TO-DO
// NOT(AND(x, y)) == NAND(x, y)
ANDGate<2> and_2(signalProvider2_1_3);
NOTGate<2> not_and_1( { &and_2 } );
NANDGate<2> nand_2(signalProvider2_1_3);
// NOT(OR(x, y)) == NOR(x, y)
ORGate<2> or_2(signalProvider2_1_3);
NOTGate<2> not_or_1( { &or_2 } );
NORGate<2> nor_2(signalProvider2_1_3);
assert(and_2.getInputs().size() == 2);
assert(and_2.getOutputs().size() == 1);
assert(not_and_1.getInputs().size() == 1);
assert(not_and_1.getOutputs().size()== 0);
assert(nand_2.getInputs().size() == 2);
assert(nand_2.getOutputs().size() == 0);
assert(or_2.getInputs().size() == 2);
assert(or_2.getOutputs().size() == 1);
assert(not_or_1.getInputs().size() == 1);
assert(not_or_1.getOutputs().size() == 0);
assert(nor_2.getInputs().size() == 2);
assert(nor_2.getOutputs().size() == 0);
signal2_1.emit();
// Has signalProvider2_1_3 as Slot, will cause emit() chain:
// signal2_1 -> signalProvider2_1_3.tick()
// { signal2_1, signal2_3 } -> signalProvider2_1_3.emit()
// signalProvider2_1_3 -> { and_2, nand_2, or_2, nor_2 }.tick() ...
assert(not_and_1.getState() == nand_2.getState());
assert(not_or_1.getState() == nor_2.getState());
}
/** \brief
* MemoryCell : Test gates combined to a memory bit.
*/
void testLogic_MemoryCell(void) {
SynchrotronComponent<1> signal_i(one_bit_0.to_ulong()),
signal_s(one_bit_1.to_ulong());
NANDGate<1> nand_1( {&signal_i, &signal_s} ),
nand_2( {&nand_1, &signal_s} ),
nand_3(one_bit_1.to_ulong()),
nand_4(one_bit_1.to_ulong());
nand_3.addInput( {&nand_1, &nand_4} );
nand_4.addInput( {&nand_2, &nand_3} );
std::cout << "States: " << nand_1.getState() << ", "
<< nand_2.getState() << ", "
<< nand_3.getState() << ", "
<< nand_4.getState() << ", "
<< "Mem bit: " << nand_3.getState() << std::endl;
signal_i.emit();
std::cout << "States: " << nand_1.getState() << ", "
<< nand_2.getState() << ", "
<< nand_3.getState() << ", "
<< nand_4.getState() << ", "
<< "Mem bit: " << nand_3.getState() << std::endl;
MemoryCell<1> m({&nand_1});
std::cout << " State: " << nand_1.getState() << " :: " << &nand_1 << std::endl;
std::cout << "Memory: " << m.getInput().getState() << " :: " << &m.getInput() << std::endl;
}