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