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