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.WireArray;
22 import era.mi.logic.wires.WireArray.WireArrayEnd;
28 void circuitExampleTest()
30 Simulation.TIMELINE.reset();
31 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1),
32 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),
33 j = new WireArray(1, 1), k = new WireArray(1, 1);
34 new AndGate(1, f, a, b);
37 new Mux(1, i, e, h, d);
38 new Splitter(i, k, j);
40 a.createInput().feedSignals(Bit.ZERO);
41 b.createInput().feedSignals(Bit.ONE);
42 c.createInput().feedSignals(Bit.ZERO);
43 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
44 e.createInput().feedSignals(Bit.ZERO);
46 Simulation.TIMELINE.executeAll();
48 assertEquals(Bit.ONE, j.getValue());
49 assertEquals(Bit.ZERO, k.getValue());
55 Simulation.TIMELINE.reset();
56 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
57 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
58 new Splitter(in, a, b, c);
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 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
72 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
73 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
74 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
76 new Merger(out, a, b, c);
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 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1), notEn = new WireArray(1, 1);
88 new NotGate(1, en, notEn);
89 new TriStateBuffer(1, a, b, en);
90 new TriStateBuffer(1, b, a, notEn);
92 WireArrayEnd enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
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 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
121 out = new WireArray(4, 1);
122 WireArrayEnd selectIn = select.createInput();
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
128 new Mux(1, out, select, a, b, c);
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 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
149 in = new WireArray(4, 1);
150 WireArrayEnd selectIn = select.createInput();
152 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
153 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
155 new Demux(1, in, select, a, b, c);
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 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
182 new AndGate(1, c, a, b);
183 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
184 b.createInput().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 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
196 new OrGate(1, c, a, b);
197 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
198 b.createInput().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 WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
210 new XorGate(1, d, a, b, c);
211 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
212 b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
213 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
215 Simulation.TIMELINE.executeAll();
217 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
221 void rsLatchCircuitTest()
223 Simulation.TIMELINE.reset();
224 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
225 q = new WireArray(1, 1), nq = new WireArray(1, 1);
227 new OrGate(1, t2, r, nq);
228 new OrGate(1, t1, s, q);
229 new NotGate(1, t2, q);
230 new NotGate(1, t1, nq);
232 WireArrayEnd sIn = s.createInput(), rIn = r.createInput();
234 sIn.feedSignals(Bit.ONE);
235 rIn.feedSignals(Bit.ZERO);
237 Simulation.TIMELINE.executeAll();
239 assertEquals(Bit.ONE, q.getValue());
240 assertEquals(Bit.ZERO, nq.getValue());
242 sIn.feedSignals(Bit.ZERO);
244 Simulation.TIMELINE.executeAll();
245 assertEquals(Bit.ONE, q.getValue());
246 assertEquals(Bit.ZERO, nq.getValue());
248 rIn.feedSignals(Bit.ONE);
250 Simulation.TIMELINE.executeAll();
252 assertEquals(Bit.ZERO, q.getValue());
253 assertEquals(Bit.ONE, nq.getValue());
257 void numericValueTest()
259 Simulation.TIMELINE.reset();
261 WireArray a = new WireArray(4, 1);
262 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
264 Simulation.TIMELINE.executeAll();
266 assertEquals(15, a.getUnsignedValue());
267 assertEquals(-1, a.getSignedValue());
271 void multipleInputs()
273 Simulation.TIMELINE.reset();
274 WireArray w = new WireArray(2, 1);
275 WireArrayEnd wI1 = w.createInput(), wI2 = w.createInput();
276 wI1.feedSignals(Bit.ONE, Bit.Z);
277 wI2.feedSignals(Bit.Z, Bit.X);
278 Simulation.TIMELINE.executeAll();
279 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
281 wI2.feedSignals(Bit.ZERO, Bit.Z);
282 Simulation.TIMELINE.executeAll();
283 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
285 wI2.feedSignals(Bit.Z, Bit.Z);
286 Simulation.TIMELINE.executeAll();
287 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
289 wI2.feedSignals(Bit.ONE, Bit.Z);
290 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
291 Simulation.TIMELINE.executeAll();
292 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
296 void wireConnections()
298 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
300 Simulation.TIMELINE.reset();
302 WireArray a = new WireArray(1, 2);
303 WireArray b = new WireArray(1, 2);
304 WireArray c = new WireArray(1, 2);
305 WireArrayEnd aI = a.createInput();
306 WireArrayEnd bI = b.createInput();
307 WireArrayEnd cI = c.createInput();
309 TestBitDisplay test = new TestBitDisplay(c);
310 TestBitDisplay test2 = new TestBitDisplay(a);
311 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
313 cI.feedSignals(Bit.ONE);
314 test.assertAfterSimulationIs(print, Bit.ONE);
316 cI.feedSignals(Bit.X);
317 test.assertAfterSimulationIs(print, Bit.X);
319 cI.feedSignals(Bit.X);
320 cI.feedSignals(Bit.Z);
321 test.assertAfterSimulationIs(print, Bit.Z);
324 test.assertAfterSimulationIs(print, Bit.Z);
325 System.err.println("ONE");
326 bI.feedSignals(Bit.ONE);
327 test.assertAfterSimulationIs(print, Bit.ONE);
328 System.err.println("ZERO");
329 bI.feedSignals(Bit.ZERO);
330 test.assertAfterSimulationIs(print, Bit.ZERO);
331 System.err.println("Z");
332 bI.feedSignals(Bit.Z);
333 test.assertAfterSimulationIs(print, Bit.Z);
336 System.err.println("Z 2");
337 aI.feedSignals(Bit.Z);
338 test.assertAfterSimulationIs(print, Bit.Z);
339 test2.assertAfterSimulationIs(Bit.Z);
340 System.err.println("ONE 2");
341 aI.feedSignals(Bit.ONE);
342 test.assertAfterSimulationIs(print, Bit.ONE);
343 test2.assertAfterSimulationIs(Bit.ONE);
344 System.err.println("ZERO 2");
345 aI.feedSignals(Bit.ZERO);
346 test.assertAfterSimulationIs(print, Bit.ZERO);
347 test2.assertAfterSimulationIs(Bit.ZERO);
348 System.err.println("Z 2 II");
349 aI.feedSignals(Bit.Z);
350 test.assertAfterSimulationIs(print, Bit.Z);
351 test2.assertAfterSimulationIs(Bit.Z);
353 System.err.println("No Conflict yet");
354 bI.feedSignals(Bit.ONE);
355 test.assertAfterSimulationIs(print, Bit.ONE);
356 test2.assertAfterSimulationIs(Bit.ONE);
357 aI.feedSignals(Bit.ONE);
358 test.assertAfterSimulationIs(print, Bit.ONE);
359 test2.assertAfterSimulationIs(Bit.ONE);
360 System.err.println("Conflict");
361 aI.feedSignals(Bit.ZERO);
362 test.assertAfterSimulationIs(print, Bit.X);
363 test2.assertAfterSimulationIs(Bit.X);
364 aI.feedSignals(Bit.ONE);
365 test.assertAfterSimulationIs(print, Bit.ONE);
366 test2.assertAfterSimulationIs(Bit.ONE);
369 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
371 assertArrayEquals(expected, actual);