Added era.mi; Project containing provisional simulator core
[Mograsim.git] / era.mi / src / era / mi / logic / tests / ComponentTest.java
1 package era.mi.logic.tests;
2
3 import static org.junit.jupiter.api.Assertions.*;
4
5 import java.util.Arrays;
6
7 import org.junit.jupiter.api.Test;
8
9 import era.mi.logic.Bit;
10 import era.mi.logic.Simulation;
11 import era.mi.logic.WireArray;
12 import era.mi.logic.components.Merger2;
13 import era.mi.logic.components.Mux;
14 import era.mi.logic.components.Splitter;
15 import era.mi.logic.components.gates.AndGate;
16 import era.mi.logic.components.gates.NotGate;
17 import era.mi.logic.components.gates.OrGate;
18
19 class ComponentTest
20 {
21
22         @Test
23         void circuitExampleTest()
24         {
25                 Simulation.TIMELINE.reset();
26                 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),
27                                 f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);
28                 new AndGate(1, a, b, f);
29                 new NotGate(1, f, g);
30                 new Merger2(h, c, g);
31                 new Mux(1, h, d, e, i);
32                 new Splitter(i, k, j);
33                 
34                 a.feedSignals(Bit.ZERO);
35                 b.feedSignals(Bit.ONE);
36                 c.feedSignals(Bit.ZERO);
37                 d.feedSignals(Bit.ONE, Bit.ONE);
38                 e.feedSignals(Bit.ONE);
39                 
40                 while(Simulation.TIMELINE.hasNext())
41                 {
42                         Simulation.TIMELINE.executeNext();
43                 }
44                 
45                 assertEquals(Simulation.TIMELINE.getSimulationTime(), 14);
46                 assertEquals(Bit.ONE, j.getValue());
47                 assertEquals(Bit.ZERO, k.getValue());
48         }
49
50         @Test
51         void splitterTest()
52         {
53                 Simulation.TIMELINE.reset();
54                 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
55                 in.feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO,Bit.ONE, Bit.ZERO, Bit.ONE);
56                 new Splitter(in, a, b, c);
57                 
58                 while(Simulation.TIMELINE.hasNext())
59                 {
60                         Simulation.TIMELINE.executeNext();
61                 }
62                 
63                 assertTrue(Arrays.equals(a.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }));
64                 assertTrue(Arrays.equals(b.getValues(), new Bit[] { Bit.ONE, Bit.ZERO }));
65                 assertTrue(Arrays.equals(c.getValues(), new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }));
66         }
67         
68         @Test
69         void mergerTest()
70         {
71                 Simulation.TIMELINE.reset();
72                 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
73                 a.feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
74                 b.feedSignals(Bit.ONE, Bit.ZERO);
75                 c.feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
76                 
77                 new Merger2(out, a, b, c);
78                 
79                 while(Simulation.TIMELINE.hasNext())
80                 {
81                         Simulation.TIMELINE.executeNext();
82                 }
83                 
84                 assertTrue(Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
85         }
86         
87         @Test
88         void muxTest()
89         {
90                 Simulation.TIMELINE.reset();
91                 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), select = new WireArray(1, 1), out = new WireArray(1, 1);
92                 
93                 select.feedSignals(Bit.ONE);
94                 a.feedSignals(Bit.ONE);
95                 b.feedSignals(Bit.ZERO);
96                 
97                 new Mux(1, a, b, select, out);
98                 assertEquals(out.getValue(), Bit.X);
99                 while(Simulation.TIMELINE.hasNext())
100                 {
101                         Simulation.TIMELINE.executeNext();
102                 }
103
104                 assertEquals(out.getValue(), Bit.ONE);
105                 select.feedSignals(Bit.ZERO);
106                 while(Simulation.TIMELINE.hasNext())
107                 {
108                         Simulation.TIMELINE.executeNext();
109                 }
110                 
111                 assertEquals(out.getValue(), Bit.ZERO);
112         }
113         
114         @Test
115         void andTest()
116         {
117                 Simulation.TIMELINE.reset();
118                 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
119                 gate.getA().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
120                 gate.getB().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
121                 
122                 
123                 while(Simulation.TIMELINE.hasNext())
124                 {
125                         Simulation.TIMELINE.executeNext();
126                 }
127                 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
128         }
129         
130         @Test
131         void orTest()
132         {
133                 Simulation.TIMELINE.reset();
134                 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
135                 gate.getA().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
136                 gate.getB().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
137                 
138                 
139                 while(Simulation.TIMELINE.hasNext())
140                 {
141                         Simulation.TIMELINE.executeNext();
142                 }
143                 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
144         }
145         
146         @Test
147         void rsLatchCircuitTest()
148         {
149                 Simulation.TIMELINE.reset();
150                 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1), q = new WireArray(1, 1),
151                                 nq = new WireArray(1, 1);
152                 
153                 new OrGate(1, r, nq, t2);
154                 new OrGate(1, s, q, t1);
155                 new NotGate(1, t2, q);
156                 new NotGate(1, t1, nq);
157         
158                 s.feedSignals(Bit.ONE);
159                 r.feedSignals(Bit.ZERO);
160                 
161                 while(Simulation.TIMELINE.hasNext())
162                 {
163                         Simulation.TIMELINE.executeNext();
164                 }
165                 
166                 assertEquals(q.getValue(), Bit.ONE);
167                 assertEquals(nq.getValue(), Bit.ZERO);
168                 
169                 s.feedSignals(Bit.ZERO);
170                 
171                 while(Simulation.TIMELINE.hasNext())
172                 {
173                         Simulation.TIMELINE.executeNext();
174                 }
175                 
176                 assertEquals(q.getValue(), Bit.ONE);
177                 assertEquals(nq.getValue(), Bit.ZERO);
178                 
179                 r.feedSignals(Bit.ONE);
180                 
181                 while(Simulation.TIMELINE.hasNext())
182                 {
183                         Simulation.TIMELINE.executeNext();
184                 }
185                 
186                 assertEquals(q.getValue(), Bit.ZERO);
187                 assertEquals(nq.getValue(), Bit.ONE);
188         }
189         
190         @Test
191         void numericValueTest()
192         {
193                 Simulation.TIMELINE.reset();
194                 
195                 WireArray a = new WireArray(4, 1);
196                 a.feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
197                 
198                 while(Simulation.TIMELINE.hasNext())
199                 {
200                         Simulation.TIMELINE.executeNext();
201                 }
202                 
203                 assertEquals(a.getUnsignedValue(), 15);
204                 assertEquals(a.getSignedValue(), -1);
205         }
206 }