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.Simulation;
11 import era.mi.logic.components.Connector;
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.types.Bit;
22 import era.mi.logic.wires.Wire;
23 import era.mi.logic.wires.Wire.WireEnd;
25 @SuppressWarnings("unused")
30 void circuitExampleTest()
32 Simulation.TIMELINE.reset();
33 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),
34 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);
35 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
36 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
37 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
38 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
39 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
41 a.createEnd().feedSignals(Bit.ZERO);
42 b.createEnd().feedSignals(Bit.ONE);
43 c.createEnd().feedSignals(Bit.ZERO);
44 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
45 e.createEnd().feedSignals(Bit.ZERO);
47 Simulation.TIMELINE.executeAll();
49 assertEquals(Bit.ONE, j.getValue());
50 assertEquals(Bit.ZERO, k.getValue());
56 Simulation.TIMELINE.reset();
57 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
58 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
59 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
61 Simulation.TIMELINE.executeAll();
63 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
64 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
65 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
71 Simulation.TIMELINE.reset();
72 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
73 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
74 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
75 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
77 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
79 Simulation.TIMELINE.executeAll();
82 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
86 void triStateBufferTest()
88 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
89 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
90 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
91 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
93 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
94 enI.feedSignals(Bit.ONE);
95 aI.feedSignals(Bit.ONE);
96 bI.feedSignals(Bit.Z);
98 Simulation.TIMELINE.executeAll();
100 assertEquals(Bit.ONE, b.getValue());
102 bI.feedSignals(Bit.ZERO);
104 Simulation.TIMELINE.executeAll();
106 assertEquals(Bit.X, b.getValue());
107 assertEquals(Bit.ONE, a.getValue());
110 enI.feedSignals(Bit.ZERO);
112 Simulation.TIMELINE.executeAll();
114 assertEquals(Bit.ZERO, a.getValue());
121 Simulation.TIMELINE.reset();
122 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);
123 WireEnd selectIn = select.createEnd();
125 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
126 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
127 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
129 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
130 Simulation.TIMELINE.executeAll();
132 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
133 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
134 Simulation.TIMELINE.executeAll();
136 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
138 selectIn.feedSignals(Bit.ONE, Bit.ONE);
139 Simulation.TIMELINE.executeAll();
141 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
148 Simulation.TIMELINE.reset();
149 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);
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.createReadOnlyEnd(), select.createReadOnlyEnd(), 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.createReadOnlyEnd(), b.createReadOnlyEnd());
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.createReadOnlyEnd(), b.createReadOnlyEnd());
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.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
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.createReadOnlyEnd(), 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), q = new Wire(1, 1), nq = new Wire(1, 1);
239 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
240 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
241 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
242 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
244 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
246 sIn.feedSignals(Bit.ONE);
247 rIn.feedSignals(Bit.ZERO);
249 Simulation.TIMELINE.executeAll();
251 assertEquals(Bit.ONE, q.getValue());
252 assertEquals(Bit.ZERO, nq.getValue());
254 sIn.feedSignals(Bit.ZERO);
256 Simulation.TIMELINE.executeAll();
257 assertEquals(Bit.ONE, q.getValue());
258 assertEquals(Bit.ZERO, nq.getValue());
260 rIn.feedSignals(Bit.ONE);
262 Simulation.TIMELINE.executeAll();
264 assertEquals(Bit.ZERO, q.getValue());
265 assertEquals(Bit.ONE, nq.getValue());
269 void numericValueTest()
271 Simulation.TIMELINE.reset();
273 Wire a = new Wire(4, 1);
274 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
276 Simulation.TIMELINE.executeAll();
278 assertEquals(15, a.getUnsignedValue());
279 assertEquals(-1, a.getSignedValue());
283 void multipleInputs()
285 Simulation.TIMELINE.reset();
286 Wire w = new Wire(2, 1);
287 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
288 wI1.feedSignals(Bit.ONE, Bit.Z);
289 wI2.feedSignals(Bit.Z, Bit.X);
290 Simulation.TIMELINE.executeAll();
291 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
293 wI2.feedSignals(Bit.ZERO, Bit.Z);
294 Simulation.TIMELINE.executeAll();
295 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
297 wI2.feedSignals(Bit.Z, Bit.Z);
298 Simulation.TIMELINE.executeAll();
299 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
301 wI2.feedSignals(Bit.ONE, Bit.Z);
302 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
303 Simulation.TIMELINE.executeAll();
304 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
308 void wireConnections()
310 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
312 Simulation.TIMELINE.reset();
314 Wire a = new Wire(1, 2);
315 Wire b = new Wire(1, 2);
316 Wire c = new Wire(1, 2);
317 WireEnd aI = a.createEnd();
318 WireEnd bI = b.createEnd();
319 WireEnd cI = c.createEnd();
321 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
322 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
323 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
325 cI.feedSignals(Bit.ONE);
326 test.assertAfterSimulationIs(print, Bit.ONE);
328 cI.feedSignals(Bit.X);
329 test.assertAfterSimulationIs(print, Bit.X);
331 cI.feedSignals(Bit.X);
332 cI.feedSignals(Bit.Z);
333 test.assertAfterSimulationIs(print, Bit.Z);
335 new Connector(b.createEnd(), c.createEnd()).connect();
336 test.assertAfterSimulationIs(print, Bit.Z);
337 System.err.println("ONE");
338 bI.feedSignals(Bit.ONE);
339 test.assertAfterSimulationIs(print, Bit.ONE);
340 System.err.println("ZERO");
341 bI.feedSignals(Bit.ZERO);
342 test.assertAfterSimulationIs(print, Bit.ZERO);
343 System.err.println("Z");
344 bI.feedSignals(Bit.Z);
345 test.assertAfterSimulationIs(print, Bit.Z);
347 new Connector(a.createEnd(), b.createEnd()).connect();
348 System.err.println("Z 2");
349 aI.feedSignals(Bit.Z);
350 test.assertAfterSimulationIs(print, Bit.Z);
351 test2.assertAfterSimulationIs(Bit.Z);
352 System.err.println("ONE 2");
353 aI.feedSignals(Bit.ONE);
354 test.assertAfterSimulationIs(print, Bit.ONE);
355 test2.assertAfterSimulationIs(Bit.ONE);
356 System.err.println("ZERO 2");
357 aI.feedSignals(Bit.ZERO);
358 test.assertAfterSimulationIs(print, Bit.ZERO);
359 test2.assertAfterSimulationIs(Bit.ZERO);
360 System.err.println("Z 2 II");
361 aI.feedSignals(Bit.Z);
362 test.assertAfterSimulationIs(print, Bit.Z);
363 test2.assertAfterSimulationIs(Bit.Z);
365 System.err.println("No Conflict yet");
366 bI.feedSignals(Bit.ONE);
367 test.assertAfterSimulationIs(print, Bit.ONE);
368 test2.assertAfterSimulationIs(Bit.ONE);
369 aI.feedSignals(Bit.ONE);
370 test.assertAfterSimulationIs(print, Bit.ONE);
371 test2.assertAfterSimulationIs(Bit.ONE);
372 System.err.println("Conflict");
373 aI.feedSignals(Bit.ZERO);
374 test.assertAfterSimulationIs(print, Bit.X);
375 test2.assertAfterSimulationIs(Bit.X);
376 aI.feedSignals(Bit.ONE);
377 test.assertAfterSimulationIs(print, Bit.ONE);
378 test2.assertAfterSimulationIs(Bit.ONE);
381 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
383 assertArrayEquals(expected, actual);