fixed Connector and added some useful methods to WireArray and Util
[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 import java.util.function.LongConsumer;
7
8 import org.junit.jupiter.api.Test;
9
10 import era.mi.logic.Bit;
11 import era.mi.logic.Simulation;
12 import era.mi.logic.components.Merger;
13 import era.mi.logic.components.Mux;
14 import era.mi.logic.components.Splitter;
15 import era.mi.logic.components.TriStateBuffer;
16 import era.mi.logic.components.gates.AndGate;
17 import era.mi.logic.components.gates.NotGate;
18 import era.mi.logic.components.gates.OrGate;
19 import era.mi.logic.wires.WireArray;
20 import era.mi.logic.wires.WireArray.WireArrayInput;
21
22 class ComponentTest
23 {
24     
25         @Test
26         void circuitExampleTest()
27         {
28                 Simulation.TIMELINE.reset();
29                 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),
30                                 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);
31                 new AndGate(1, a, b, f);
32                 new NotGate(1, f, g);
33                 new Merger(h, c, g);
34                 new Mux(1, i, e, h, d);
35                 new Splitter(i, k, j);
36                 
37                 a.createInput().feedSignals(Bit.ZERO);
38                 b.createInput().feedSignals(Bit.ONE);
39                 c.createInput().feedSignals(Bit.ZERO);
40                 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
41                 e.createInput().feedSignals(Bit.ZERO);
42                 
43                 Simulation.TIMELINE.executeAll();
44                 
45                 assertEquals(Bit.ONE, j.getValue());
46                 assertEquals(Bit.ZERO, k.getValue());
47         }
48
49     @Test
50     void splitterTest()
51     {
52         Simulation.TIMELINE.reset();
53         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
54         in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
55         new Splitter(in, a, b, c);
56
57         Simulation.TIMELINE.executeAll();
58
59         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues());
60         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues());
61         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues());
62     }
63
64     @Test
65     void mergerTest()
66     {
67         Simulation.TIMELINE.reset();
68         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
69         a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
70         b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
71         c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
72
73         new Merger(out, a, b, c);
74
75         Simulation.TIMELINE.executeAll();
76
77         assertTrue(Arrays.equals(out.getValues(),
78                 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
79     }
80
81     @Test
82     void triStateBufferTest()
83     {
84         WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
85                 notEn = new WireArray(1, 1);
86         new NotGate(1, en, notEn);
87         new TriStateBuffer(1, a, b, en);
88         new TriStateBuffer(1, b, a, notEn);
89
90         WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
91         enI.feedSignals(Bit.ONE);
92         aI.feedSignals(Bit.ONE);
93
94         Simulation.TIMELINE.executeAll();
95
96         assertEquals(Bit.ONE, b.getValue());
97
98         bI.feedSignals(Bit.ZERO);
99
100         Simulation.TIMELINE.executeAll();
101
102         assertEquals(Bit.X, b.getValue());
103         assertEquals(Bit.ONE, a.getValue());
104
105         aI.clearSignals();
106         enI.feedSignals(Bit.ZERO);
107
108         Simulation.TIMELINE.executeAll();
109
110         assertEquals(Bit.ZERO, a.getValue());
111
112     }
113
114     @Test
115     void muxTest()
116     {
117         Simulation.TIMELINE.reset();
118         WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
119                 select = new WireArray(2, 5), out = new WireArray(4, 1);
120         WireArrayInput selectIn = select.createInput();
121
122         selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
123         a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
124         c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
125
126         new Mux(1, out, select, a, b, c);
127         Simulation.TIMELINE.executeAll();
128
129         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues());
130         selectIn.feedSignals(Bit.ZERO, Bit.ONE);
131         Simulation.TIMELINE.executeAll();
132
133         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues());
134
135         selectIn.feedSignals(Bit.ONE, Bit.ONE);
136         Simulation.TIMELINE.executeAll();
137
138         assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues());
139
140     }
141
142     @Test
143     void andTest()
144     {
145         Simulation.TIMELINE.reset();
146         AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
147         gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
148         gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
149
150         Simulation.TIMELINE.executeAll();
151         assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues());
152     }
153
154     @Test
155     void orTest()
156     {
157         Simulation.TIMELINE.reset();
158         OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
159         gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
160         gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
161
162         Simulation.TIMELINE.executeAll();
163
164         assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues());
165     }
166
167     @Test
168     void rsLatchCircuitTest()
169     {
170         Simulation.TIMELINE.reset();
171         WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
172                 q = new WireArray(1, 1), nq = new WireArray(1, 1);
173
174         new OrGate(1, r, nq, t2);
175         new OrGate(1, s, q, t1);
176         new NotGate(1, t2, q);
177         new NotGate(1, t1, nq);
178
179         WireArrayInput sIn = s.createInput(), rIn = r.createInput();
180
181         sIn.feedSignals(Bit.ONE);
182         rIn.feedSignals(Bit.ZERO);
183
184         Simulation.TIMELINE.executeAll();
185
186         assertEquals(Bit.ONE, q.getValue());
187         assertEquals(Bit.ZERO, nq.getValue());
188
189         sIn.feedSignals(Bit.ZERO);
190
191         Simulation.TIMELINE.executeAll();
192         assertEquals(Bit.ONE, q.getValue());
193         assertEquals(Bit.ZERO, nq.getValue());
194
195         rIn.feedSignals(Bit.ONE);
196
197         Simulation.TIMELINE.executeAll();
198
199         assertEquals(Bit.ZERO, q.getValue());
200         assertEquals(Bit.ONE, nq.getValue());
201     }
202
203     @Test
204     void numericValueTest()
205     {
206         Simulation.TIMELINE.reset();
207
208         WireArray a = new WireArray(4, 1);
209         a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
210
211         Simulation.TIMELINE.executeAll();
212
213         assertEquals(15, a.getUnsignedValue());
214         assertEquals(-1, a.getSignedValue());
215     }
216
217     @Test
218     void multipleInputs()
219     {
220         Simulation.TIMELINE.reset();
221         WireArray w = new WireArray(2, 1);
222         WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
223         wI1.feedSignals(Bit.ONE, Bit.Z);
224         wI2.feedSignals(Bit.Z, Bit.X);
225         Simulation.TIMELINE.executeAll();
226         assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues());
227
228         wI2.feedSignals(Bit.ZERO, Bit.Z);
229         Simulation.TIMELINE.executeAll();
230         assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues());
231
232         wI2.feedSignals(Bit.Z, Bit.Z);
233         Simulation.TIMELINE.executeAll();
234         assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
235
236         wI2.feedSignals(Bit.ONE, Bit.Z);
237         w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
238         Simulation.TIMELINE.executeAll();
239         assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
240     }
241
242         @Test
243         void wireConnections()
244         {
245                 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
246                 
247                 Simulation.TIMELINE.reset();
248
249                 WireArray a = new WireArray(1, 2);
250                 WireArray b = new WireArray(1, 2);
251                 WireArray c = new WireArray(1, 2);
252                 WireArrayInput aI = a.createInput();
253                 WireArrayInput bI = b.createInput();
254                 WireArrayInput cI = c.createInput();
255
256                 TestBitDisplay test = new TestBitDisplay(c);
257                 TestBitDisplay test2 = new TestBitDisplay(a);
258                 LongConsumer print = time -> System.out.format("Time %2d\n   a: %s\n   b: %s\n   c: %s\n", time, a, b, c);
259
260                 cI.feedSignals(Bit.ONE);
261                 test.assertAfterSimulationIs(print, Bit.ONE);
262
263                 cI.feedSignals(Bit.X);
264                 test.assertAfterSimulationIs(print, Bit.X);
265
266                 cI.feedSignals(Bit.X);
267                 cI.feedSignals(Bit.Z);
268                 test.assertAfterSimulationIs(print, Bit.Z);
269
270                 Connector c1 = new Connector(b, c);
271                 test.assertAfterSimulationIs(print, Bit.Z);
272                 System.err.println("ONE");
273                 bI.feedSignals(Bit.ONE);
274                 test.assertAfterSimulationIs(print, Bit.ONE);
275                 System.err.println("ZERO");
276                 bI.feedSignals(Bit.ZERO);
277                 test.assertAfterSimulationIs(print, Bit.ZERO);
278                 System.err.println("Z");
279                 bI.feedSignals(Bit.Z);
280                 test.assertAfterSimulationIs(print, Bit.Z);
281                 
282                 Connector c2 = new Connector(a, b);
283                 System.err.println("Z 2");
284                 aI.feedSignals(Bit.Z);
285                 test.assertAfterSimulationIs(print, Bit.Z);
286                 test2.assertAfterSimulationIs(Bit.Z);
287                 System.err.println("ONE 2");
288                 aI.feedSignals(Bit.ONE);
289                 test.assertAfterSimulationIs(print, Bit.ONE);
290                 test2.assertAfterSimulationIs(Bit.ONE);
291                 System.err.println("ZERO 2");
292                 aI.feedSignals(Bit.ZERO);
293                 test.assertAfterSimulationIs(print, Bit.ZERO);
294                 test2.assertAfterSimulationIs(Bit.ZERO);
295                 System.err.println("Z 2 II");
296                 aI.feedSignals(Bit.Z);
297                 test.assertAfterSimulationIs(print, Bit.Z);
298                 test2.assertAfterSimulationIs(Bit.Z);
299                 
300                 System.err.println("No Conflict yet");
301                 bI.feedSignals(Bit.ONE);
302                 test.assertAfterSimulationIs(print, Bit.ONE);
303                 test2.assertAfterSimulationIs(Bit.ONE);
304                 aI.feedSignals(Bit.ONE);
305                 test.assertAfterSimulationIs(print, Bit.ONE);
306                 test2.assertAfterSimulationIs(Bit.ONE);
307                 System.err.println("Conflict");
308                 aI.feedSignals(Bit.ZERO);
309                 test.assertAfterSimulationIs(print, Bit.X);
310                 test2.assertAfterSimulationIs(Bit.X);
311                 aI.feedSignals(Bit.ONE);
312                 test.assertAfterSimulationIs(print, Bit.ONE);
313                 test2.assertAfterSimulationIs(Bit.ONE);
314         }
315
316     private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
317     {
318         assertArrayEquals(expected, actual);
319     }
320 }