1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.*;
5 import java.util.Arrays;
6 import java.util.function.LongConsumer;
8 import org.junit.jupiter.api.Test;
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.components.gates.XorGate;
21 import era.mi.logic.wires.Wire;
22 import era.mi.logic.wires.Wire.WireEnd;
28 void circuitExampleTest()
30 Simulation.TIMELINE.reset();
31 Wire a = new Wire(1, 1), b = new Wire(1, 1), c = new Wire(1, 10), d = new Wire(2, 1),
32 e = new Wire(1, 1), f = new Wire(1, 1), g = new Wire(1, 1), h = new Wire(2, 1), i = new Wire(2, 1),
33 j = new Wire(1, 1), k = new Wire(1, 1);
34 new AndGate(1, f.createEnd(), a.createEnd(), b.createEnd());
35 new NotGate(1, f.createEnd(), g.createEnd());
36 new Merger(h.createEnd(), c.createEnd(), g.createEnd());
37 new Mux(1, i.createEnd(), e.createEnd(), h.createEnd(), d.createEnd());
38 new Splitter(i.createEnd(), k.createEnd(), j.createEnd());
40 a.createEnd().feedSignals(Bit.ZERO);
41 b.createEnd().feedSignals(Bit.ONE);
42 c.createEnd().feedSignals(Bit.ZERO);
43 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
44 e.createEnd().feedSignals(Bit.ZERO);
46 Simulation.TIMELINE.executeAll();
48 assertEquals(Bit.ONE, j.getValue());
49 assertEquals(Bit.ZERO, k.getValue());
55 Simulation.TIMELINE.reset();
56 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
57 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
58 new Splitter(in.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());
60 Simulation.TIMELINE.executeAll();
62 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
63 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
64 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
70 Simulation.TIMELINE.reset();
71 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
72 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
73 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
74 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
76 new Merger(out.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());
78 Simulation.TIMELINE.executeAll();
81 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
85 void triStateBufferTest()
87 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
88 new NotGate(1, en.createEnd(), notEn.createEnd());
89 new TriStateBuffer(1, a.createEnd(), b.createEnd(), en.createEnd());
90 new TriStateBuffer(1, b.createEnd(), a.createEnd(), notEn.createEnd());
92 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
93 enI.feedSignals(Bit.ONE);
94 aI.feedSignals(Bit.ONE);
96 Simulation.TIMELINE.executeAll();
98 assertEquals(Bit.ONE, b.getValue());
100 bI.feedSignals(Bit.ZERO);
102 Simulation.TIMELINE.executeAll();
104 assertEquals(Bit.X, b.getValue());
105 assertEquals(Bit.ONE, a.getValue());
108 enI.feedSignals(Bit.ZERO);
110 Simulation.TIMELINE.executeAll();
112 assertEquals(Bit.ZERO, a.getValue());
119 Simulation.TIMELINE.reset();
120 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5),
121 out = new Wire(4, 1);
122 WireEnd selectIn = select.createEnd();
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
128 new Mux(1, out.createEnd(), select.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());
129 Simulation.TIMELINE.executeAll();
131 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
132 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
133 Simulation.TIMELINE.executeAll();
135 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
137 selectIn.feedSignals(Bit.ONE, Bit.ONE);
138 Simulation.TIMELINE.executeAll();
140 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
147 Simulation.TIMELINE.reset();
148 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5),
150 WireEnd selectIn = select.createEnd();
152 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
153 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
155 new Demux(1, in.createEnd(), select.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());
156 Simulation.TIMELINE.executeAll();
158 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
159 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
160 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
161 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
162 Simulation.TIMELINE.executeAll();
164 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
165 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
166 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
168 selectIn.feedSignals(Bit.ONE, Bit.ONE);
169 Simulation.TIMELINE.executeAll();
171 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
172 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
173 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
180 Simulation.TIMELINE.reset();
181 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
182 new AndGate(1, c.createEnd(), a.createEnd(), b.createEnd());
183 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
184 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
186 Simulation.TIMELINE.executeAll();
188 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
194 Simulation.TIMELINE.reset();
195 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
196 new OrGate(1, c.createEnd(), a.createEnd(), b.createEnd());
197 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
198 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
200 Simulation.TIMELINE.executeAll();
202 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
208 Simulation.TIMELINE.reset();
209 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
210 new XorGate(1, d.createEnd(), a.createEnd(), b.createEnd(), c.createEnd());
211 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
212 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
213 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
215 Simulation.TIMELINE.executeAll();
217 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
223 Simulation.TIMELINE.reset();
224 Wire a = new Wire(3, 1), b = new Wire(3, 2);
225 new NotGate(1, a.createEnd(), b.createEnd());
226 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
228 Simulation.TIMELINE.executeAll();
230 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
234 void rsLatchCircuitTest()
236 Simulation.TIMELINE.reset();
237 Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1),
238 q = new Wire(1, 1), nq = new Wire(1, 1);
240 new OrGate(1, t2.createEnd(), r.createEnd(), nq.createEnd());
241 new OrGate(1, t1.createEnd(), s.createEnd(), q.createEnd());
242 new NotGate(1, t2.createEnd(), q.createEnd());
243 new NotGate(1, t1.createEnd(), nq.createEnd());
245 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
247 sIn.feedSignals(Bit.ONE);
248 rIn.feedSignals(Bit.ZERO);
250 Simulation.TIMELINE.executeAll();
252 assertEquals(Bit.ONE, q.getValue());
253 assertEquals(Bit.ZERO, nq.getValue());
255 sIn.feedSignals(Bit.ZERO);
257 Simulation.TIMELINE.executeAll();
258 assertEquals(Bit.ONE, q.getValue());
259 assertEquals(Bit.ZERO, nq.getValue());
261 rIn.feedSignals(Bit.ONE);
263 Simulation.TIMELINE.executeAll();
265 assertEquals(Bit.ZERO, q.getValue());
266 assertEquals(Bit.ONE, nq.getValue());
270 void numericValueTest()
272 Simulation.TIMELINE.reset();
274 Wire a = new Wire(4, 1);
275 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
277 Simulation.TIMELINE.executeAll();
279 assertEquals(15, a.getUnsignedValue());
280 assertEquals(-1, a.getSignedValue());
284 void multipleInputs()
286 Simulation.TIMELINE.reset();
287 Wire w = new Wire(2, 1);
288 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
289 wI1.feedSignals(Bit.ONE, Bit.Z);
290 wI2.feedSignals(Bit.Z, Bit.X);
291 Simulation.TIMELINE.executeAll();
292 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
294 wI2.feedSignals(Bit.ZERO, Bit.Z);
295 Simulation.TIMELINE.executeAll();
296 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
298 wI2.feedSignals(Bit.Z, Bit.Z);
299 Simulation.TIMELINE.executeAll();
300 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
302 wI2.feedSignals(Bit.ONE, Bit.Z);
303 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
304 Simulation.TIMELINE.executeAll();
305 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
309 void wireConnections()
311 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
313 Simulation.TIMELINE.reset();
315 Wire a = new Wire(1, 2);
316 Wire b = new Wire(1, 2);
317 Wire c = new Wire(1, 2);
318 WireEnd aI = a.createEnd();
319 WireEnd bI = b.createEnd();
320 WireEnd cI = c.createEnd();
322 TestBitDisplay test = new TestBitDisplay(c.createEnd());
323 TestBitDisplay test2 = new TestBitDisplay(a.createEnd());
324 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
326 cI.feedSignals(Bit.ONE);
327 test.assertAfterSimulationIs(print, Bit.ONE);
329 cI.feedSignals(Bit.X);
330 test.assertAfterSimulationIs(print, Bit.X);
332 cI.feedSignals(Bit.X);
333 cI.feedSignals(Bit.Z);
334 test.assertAfterSimulationIs(print, Bit.Z);
337 test.assertAfterSimulationIs(print, Bit.Z);
338 System.err.println("ONE");
339 bI.feedSignals(Bit.ONE);
340 test.assertAfterSimulationIs(print, Bit.ONE);
341 System.err.println("ZERO");
342 bI.feedSignals(Bit.ZERO);
343 test.assertAfterSimulationIs(print, Bit.ZERO);
344 System.err.println("Z");
345 bI.feedSignals(Bit.Z);
346 test.assertAfterSimulationIs(print, Bit.Z);
349 System.err.println("Z 2");
350 aI.feedSignals(Bit.Z);
351 test.assertAfterSimulationIs(print, Bit.Z);
352 test2.assertAfterSimulationIs(Bit.Z);
353 System.err.println("ONE 2");
354 aI.feedSignals(Bit.ONE);
355 test.assertAfterSimulationIs(print, Bit.ONE);
356 test2.assertAfterSimulationIs(Bit.ONE);
357 System.err.println("ZERO 2");
358 aI.feedSignals(Bit.ZERO);
359 test.assertAfterSimulationIs(print, Bit.ZERO);
360 test2.assertAfterSimulationIs(Bit.ZERO);
361 System.err.println("Z 2 II");
362 aI.feedSignals(Bit.Z);
363 test.assertAfterSimulationIs(print, Bit.Z);
364 test2.assertAfterSimulationIs(Bit.Z);
366 System.err.println("No Conflict yet");
367 bI.feedSignals(Bit.ONE);
368 test.assertAfterSimulationIs(print, Bit.ONE);
369 test2.assertAfterSimulationIs(Bit.ONE);
370 aI.feedSignals(Bit.ONE);
371 test.assertAfterSimulationIs(print, Bit.ONE);
372 test2.assertAfterSimulationIs(Bit.ONE);
373 System.err.println("Conflict");
374 aI.feedSignals(Bit.ZERO);
375 test.assertAfterSimulationIs(print, Bit.X);
376 test2.assertAfterSimulationIs(Bit.X);
377 aI.feedSignals(Bit.ONE);
378 test.assertAfterSimulationIs(print, Bit.ONE);
379 test2.assertAfterSimulationIs(Bit.ONE);
382 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
384 assertArrayEquals(expected, actual);