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.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;
25 @SuppressWarnings("unused")
30 void circuitExampleTest()
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);
39 new Mux(1, i, e, h, d);
40 new Splitter(i, k, j);
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);
48 Simulation.TIMELINE.executeAll();
50 assertEquals(Bit.ONE, j.getValue());
51 assertEquals(Bit.ZERO, k.getValue());
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);
62 Simulation.TIMELINE.executeAll();
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);
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);
78 new Merger(out, a, b, c);
80 Simulation.TIMELINE.executeAll();
83 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
87 void triStateBufferTest()
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);
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);
99 Simulation.TIMELINE.executeAll();
101 assertEquals(Bit.ONE, b.getValue());
103 bI.feedSignals(Bit.ZERO);
105 Simulation.TIMELINE.executeAll();
107 assertEquals(Bit.X, b.getValue());
108 assertEquals(Bit.ONE, a.getValue());
111 enI.feedSignals(Bit.ZERO);
113 Simulation.TIMELINE.executeAll();
115 assertEquals(Bit.ZERO, a.getValue());
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();
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);
131 new Mux(1, out, select, a, b, c);
132 Simulation.TIMELINE.executeAll();
134 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
135 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
136 Simulation.TIMELINE.executeAll();
138 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
140 selectIn.feedSignals(Bit.ONE, Bit.ONE);
141 Simulation.TIMELINE.executeAll();
143 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
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();
155 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
156 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
158 new Demux(1, in, select, a, b, c);
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 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);
189 Simulation.TIMELINE.executeAll();
191 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
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);
203 Simulation.TIMELINE.executeAll();
205 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
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);
218 Simulation.TIMELINE.executeAll();
220 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
224 void rsLatchCircuitTest()
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);
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);
235 WireArrayEnd sIn = s.createInput(), rIn = r.createInput();
237 sIn.feedSignals(Bit.ONE);
238 rIn.feedSignals(Bit.ZERO);
240 Simulation.TIMELINE.executeAll();
242 assertEquals(Bit.ONE, q.getValue());
243 assertEquals(Bit.ZERO, nq.getValue());
245 sIn.feedSignals(Bit.ZERO);
247 Simulation.TIMELINE.executeAll();
248 assertEquals(Bit.ONE, q.getValue());
249 assertEquals(Bit.ZERO, nq.getValue());
251 rIn.feedSignals(Bit.ONE);
253 Simulation.TIMELINE.executeAll();
255 assertEquals(Bit.ZERO, q.getValue());
256 assertEquals(Bit.ONE, nq.getValue());
260 void numericValueTest()
262 Simulation.TIMELINE.reset();
264 WireArray a = new WireArray(4, 1);
265 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
267 Simulation.TIMELINE.executeAll();
269 assertEquals(15, a.getUnsignedValue());
270 assertEquals(-1, a.getSignedValue());
274 void multipleInputs()
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);
284 wI2.feedSignals(Bit.ZERO, Bit.Z);
285 Simulation.TIMELINE.executeAll();
286 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
288 wI2.feedSignals(Bit.Z, Bit.Z);
289 Simulation.TIMELINE.executeAll();
290 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
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);
299 void wireConnections()
301 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
303 Simulation.TIMELINE.reset();
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();
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);
316 cI.feedSignals(Bit.ONE);
317 test.assertAfterSimulationIs(print, Bit.ONE);
319 cI.feedSignals(Bit.X);
320 test.assertAfterSimulationIs(print, Bit.X);
322 cI.feedSignals(Bit.X);
323 cI.feedSignals(Bit.Z);
324 test.assertAfterSimulationIs(print, Bit.Z);
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);
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);
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);
372 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
374 assertArrayEquals(expected, actual);