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