1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6 import static org.junit.jupiter.api.Assertions.fail;
8 import java.util.Arrays;
9 import java.util.function.LongConsumer;
11 import org.junit.jupiter.api.Test;
13 import era.mi.logic.Bit;
14 import era.mi.logic.Simulation;
15 import era.mi.logic.components.Connector;
16 import era.mi.logic.components.Demux;
17 import era.mi.logic.components.Merger;
18 import era.mi.logic.components.Mux;
19 import era.mi.logic.components.Splitter;
20 import era.mi.logic.components.TriStateBuffer;
21 import era.mi.logic.components.gates.AndGate;
22 import era.mi.logic.components.gates.NotGate;
23 import era.mi.logic.components.gates.OrGate;
24 import era.mi.logic.components.gates.XorGate;
25 import era.mi.logic.wires.Wire;
26 import era.mi.logic.wires.Wire.WireEnd;
28 @SuppressWarnings("unused")
33 void circuitExampleTest()
35 Simulation.TIMELINE.reset();
36 Wire a = new Wire(1, 1), b = new Wire(1, 1), c = new Wire(1, 10), d = new Wire(2, 1), e = new Wire(1, 1), f = new Wire(1, 1),
37 g = new Wire(1, 1), h = new Wire(2, 1), i = new Wire(2, 1), j = new Wire(1, 1), k = new Wire(1, 1);
38 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
39 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
40 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
41 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
42 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
44 a.createEnd().feedSignals(Bit.ZERO);
45 b.createEnd().feedSignals(Bit.ONE);
46 c.createEnd().feedSignals(Bit.ZERO);
47 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
48 e.createEnd().feedSignals(Bit.ZERO);
50 Simulation.TIMELINE.executeAll();
52 assertEquals(Bit.ONE, j.getValue());
53 assertEquals(Bit.ZERO, k.getValue());
59 Simulation.TIMELINE.reset();
60 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
61 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
62 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
64 Simulation.TIMELINE.executeAll();
66 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
67 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
68 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
74 Simulation.TIMELINE.reset();
75 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
76 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
77 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
78 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
80 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
82 Simulation.TIMELINE.executeAll();
85 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
89 void triStateBufferTest()
91 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
92 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
93 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
94 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
96 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
97 enI.feedSignals(Bit.ONE);
98 aI.feedSignals(Bit.ONE);
99 bI.feedSignals(Bit.Z);
101 Simulation.TIMELINE.executeAll();
103 assertEquals(Bit.ONE, b.getValue());
105 bI.feedSignals(Bit.ZERO);
107 Simulation.TIMELINE.executeAll();
109 assertEquals(Bit.X, b.getValue());
110 assertEquals(Bit.ONE, a.getValue());
113 enI.feedSignals(Bit.ZERO);
115 Simulation.TIMELINE.executeAll();
117 assertEquals(Bit.ZERO, a.getValue());
124 Simulation.TIMELINE.reset();
125 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), out = new Wire(4, 1);
126 WireEnd selectIn = select.createEnd();
128 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
129 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
130 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
132 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
133 Simulation.TIMELINE.executeAll();
135 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
136 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
137 Simulation.TIMELINE.executeAll();
139 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
141 selectIn.feedSignals(Bit.ONE, Bit.ONE);
142 Simulation.TIMELINE.executeAll();
144 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
151 Simulation.TIMELINE.reset();
152 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), in = new Wire(4, 1);
153 WireEnd selectIn = select.createEnd();
155 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
156 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
158 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
159 Simulation.TIMELINE.executeAll();
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();
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);
171 selectIn.feedSignals(Bit.ONE, Bit.ONE);
172 Simulation.TIMELINE.executeAll();
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);
183 Simulation.TIMELINE.reset();
184 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
185 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
186 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
187 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
189 Simulation.TIMELINE.executeAll();
191 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
197 Simulation.TIMELINE.reset();
198 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
199 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
200 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
201 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
203 Simulation.TIMELINE.executeAll();
205 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
211 Simulation.TIMELINE.reset();
212 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
213 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
214 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
215 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
216 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
218 Simulation.TIMELINE.executeAll();
220 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
226 Simulation.TIMELINE.reset();
227 Wire a = new Wire(3, 1), b = new Wire(3, 2);
228 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
229 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
231 Simulation.TIMELINE.executeAll();
233 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
237 void rsLatchCircuitTest()
239 Simulation.TIMELINE.reset();
240 Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1), q = new Wire(1, 1), nq = new Wire(1, 1);
242 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
243 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
244 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
245 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
247 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
249 sIn.feedSignals(Bit.ONE);
250 rIn.feedSignals(Bit.ZERO);
252 Simulation.TIMELINE.executeAll();
254 assertEquals(Bit.ONE, q.getValue());
255 assertEquals(Bit.ZERO, nq.getValue());
257 sIn.feedSignals(Bit.ZERO);
259 Simulation.TIMELINE.executeAll();
260 assertEquals(Bit.ONE, q.getValue());
261 assertEquals(Bit.ZERO, nq.getValue());
263 rIn.feedSignals(Bit.ONE);
265 Simulation.TIMELINE.executeAll();
267 assertEquals(Bit.ZERO, q.getValue());
268 assertEquals(Bit.ONE, nq.getValue());
272 void numericValueTest()
274 Simulation.TIMELINE.reset();
276 Wire a = new Wire(4, 1);
277 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
279 Simulation.TIMELINE.executeAll();
281 assertEquals(15, a.getUnsignedValue());
282 assertEquals(-1, a.getSignedValue());
286 void multipleInputs()
288 Simulation.TIMELINE.reset();
289 Wire w = new Wire(2, 1);
290 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
291 wI1.feedSignals(Bit.ONE, Bit.Z);
292 wI2.feedSignals(Bit.Z, Bit.X);
293 Simulation.TIMELINE.executeAll();
294 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
296 wI2.feedSignals(Bit.ZERO, Bit.Z);
297 Simulation.TIMELINE.executeAll();
298 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
300 wI2.feedSignals(Bit.Z, Bit.Z);
301 Simulation.TIMELINE.executeAll();
302 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
304 wI2.feedSignals(Bit.ONE, Bit.Z);
305 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
306 Simulation.TIMELINE.executeAll();
307 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
311 void wireConnections()
313 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
315 Simulation.TIMELINE.reset();
317 Wire a = new Wire(1, 2);
318 Wire b = new Wire(1, 2);
319 Wire c = new Wire(1, 2);
320 WireEnd aI = a.createEnd();
321 WireEnd bI = b.createEnd();
322 WireEnd cI = c.createEnd();
324 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
325 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
326 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
328 cI.feedSignals(Bit.ONE);
329 test.assertAfterSimulationIs(print, Bit.ONE);
331 cI.feedSignals(Bit.X);
332 test.assertAfterSimulationIs(print, Bit.X);
334 cI.feedSignals(Bit.X);
335 cI.feedSignals(Bit.Z);
336 test.assertAfterSimulationIs(print, Bit.Z);
338 new Connector(b.createEnd(), c.createEnd()).connect();
339 test.assertAfterSimulationIs(print, Bit.Z);
340 System.err.println("ONE");
341 bI.feedSignals(Bit.ONE);
342 test.assertAfterSimulationIs(print, Bit.ONE);
343 System.err.println("ZERO");
344 bI.feedSignals(Bit.ZERO);
345 test.assertAfterSimulationIs(print, Bit.ZERO);
346 System.err.println("Z");
347 bI.feedSignals(Bit.Z);
348 test.assertAfterSimulationIs(print, Bit.Z);
350 new Connector(a.createEnd(), b.createEnd()).connect();
351 System.err.println("Z 2");
352 aI.feedSignals(Bit.Z);
353 test.assertAfterSimulationIs(print, Bit.Z);
354 test2.assertAfterSimulationIs(Bit.Z);
355 System.err.println("ONE 2");
356 aI.feedSignals(Bit.ONE);
357 test.assertAfterSimulationIs(print, Bit.ONE);
358 test2.assertAfterSimulationIs(Bit.ONE);
359 System.err.println("ZERO 2");
360 aI.feedSignals(Bit.ZERO);
361 test.assertAfterSimulationIs(print, Bit.ZERO);
362 test2.assertAfterSimulationIs(Bit.ZERO);
363 System.err.println("Z 2 II");
364 aI.feedSignals(Bit.Z);
365 test.assertAfterSimulationIs(print, Bit.Z);
366 test2.assertAfterSimulationIs(Bit.Z);
368 System.err.println("No Conflict yet");
369 bI.feedSignals(Bit.ONE);
370 test.assertAfterSimulationIs(print, Bit.ONE);
371 test2.assertAfterSimulationIs(Bit.ONE);
372 aI.feedSignals(Bit.ONE);
373 test.assertAfterSimulationIs(print, Bit.ONE);
374 test2.assertAfterSimulationIs(Bit.ONE);
375 System.err.println("Conflict");
376 aI.feedSignals(Bit.ZERO);
377 test.assertAfterSimulationIs(print, Bit.X);
378 test2.assertAfterSimulationIs(Bit.X);
379 aI.feedSignals(Bit.ONE);
380 test.assertAfterSimulationIs(print, Bit.ONE);
381 test2.assertAfterSimulationIs(Bit.ONE);
384 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
386 assertArrayEquals(expected, actual);