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.fail;
7 import java.util.function.LongConsumer;
9 import org.junit.jupiter.api.Test;
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.types.Bit;
23 import era.mi.logic.types.BitVector;
24 import era.mi.logic.wires.Wire;
25 import era.mi.logic.wires.Wire.ReadEnd;
26 import era.mi.logic.wires.Wire.ReadWriteEnd;
32 void circuitExampleTest()
34 Simulation.TIMELINE.reset();
35 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),
36 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);
37 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
38 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
39 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
40 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
41 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
43 a.createEnd().feedSignals(Bit.ZERO);
44 b.createEnd().feedSignals(Bit.ONE);
45 c.createEnd().feedSignals(Bit.ZERO);
46 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
47 e.createEnd().feedSignals(Bit.ZERO);
49 Simulation.TIMELINE.executeAll();
51 assertEquals(Bit.ONE, j.getValue());
52 assertEquals(Bit.ZERO, k.getValue());
58 Simulation.TIMELINE.reset();
59 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
60 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
61 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
63 Simulation.TIMELINE.executeAll();
65 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
66 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
67 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
73 Simulation.TIMELINE.reset();
74 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
75 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
76 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
77 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
79 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
81 Simulation.TIMELINE.executeAll();
83 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
87 void triStateBufferTest()
89 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
90 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
91 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
92 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
94 ReadWriteEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
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 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);
124 ReadWriteEnd selectIn = select.createEnd();
126 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
127 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
128 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
130 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
131 Simulation.TIMELINE.executeAll();
133 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
134 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
135 Simulation.TIMELINE.executeAll();
137 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
139 selectIn.feedSignals(Bit.ONE, Bit.ONE);
140 Simulation.TIMELINE.executeAll();
142 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
149 Simulation.TIMELINE.reset();
150 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);
151 ReadWriteEnd selectIn = select.createEnd();
153 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
154 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
156 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
157 Simulation.TIMELINE.executeAll();
159 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
160 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
161 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
162 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
163 Simulation.TIMELINE.executeAll();
165 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
166 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
167 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
169 selectIn.feedSignals(Bit.ONE, Bit.ONE);
170 Simulation.TIMELINE.executeAll();
172 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
173 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
174 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
181 Simulation.TIMELINE.reset();
182 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
183 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
184 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
185 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
187 Simulation.TIMELINE.executeAll();
189 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
195 Simulation.TIMELINE.reset();
196 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
197 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
198 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
199 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
201 Simulation.TIMELINE.executeAll();
203 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
209 Simulation.TIMELINE.reset();
210 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
211 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
212 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
213 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
214 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
216 Simulation.TIMELINE.executeAll();
218 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
224 Simulation.TIMELINE.reset();
225 Wire a = new Wire(3, 1), b = new Wire(3, 2);
226 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
227 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
229 Simulation.TIMELINE.executeAll();
231 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
235 void rsLatchCircuitTest()
237 Simulation.TIMELINE.reset();
238 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);
240 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
241 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
242 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
243 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
245 ReadWriteEnd sIn = s.createEnd(), rIn = r.createEnd();
247 sIn.feedSignals(Bit.ONE);
248 rIn.feedSignals(Bit.ZERO);
250 Simulation.TIMELINE.executeAll();
252 assertEquals(Bit.ONE, q.getValue());
253 assertEquals(Bit.ZERO, nq.getValue());
255 sIn.feedSignals(Bit.ZERO);
257 Simulation.TIMELINE.executeAll();
258 assertEquals(Bit.ONE, q.getValue());
259 assertEquals(Bit.ZERO, nq.getValue());
261 rIn.feedSignals(Bit.ONE);
263 Simulation.TIMELINE.executeAll();
265 assertEquals(Bit.ZERO, q.getValue());
266 assertEquals(Bit.ONE, nq.getValue());
270 void numericValueTest()
272 Simulation.TIMELINE.reset();
274 Wire a = new Wire(4, 1);
275 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
277 Simulation.TIMELINE.executeAll();
279 assertEquals(15, a.getUnsignedValue());
280 assertEquals(-1, a.getSignedValue());
284 void multipleInputs()
286 Simulation.TIMELINE.reset();
287 Wire w = new Wire(2, 1);
288 ReadWriteEnd wI1 = w.createEnd(), wI2 = w.createEnd();
289 wI1.feedSignals(Bit.ONE, Bit.Z);
290 wI2.feedSignals(Bit.Z, Bit.X);
291 Simulation.TIMELINE.executeAll();
292 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
294 wI2.feedSignals(Bit.ZERO, Bit.Z);
295 Simulation.TIMELINE.executeAll();
296 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
298 wI2.feedSignals(Bit.Z, Bit.Z);
299 Simulation.TIMELINE.executeAll();
300 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
302 wI2.feedSignals(Bit.ONE, Bit.Z);
303 ReadEnd rE = w.createReadOnlyEnd();
304 rE.addObserver((i, oldValues) -> fail("WireEnd notified observer, although value did not change."));
305 Simulation.TIMELINE.executeAll();
307 wI1.feedSignals(Bit.X, Bit.X);
308 Simulation.TIMELINE.executeAll();
309 wI1.addObserver((i, oldValues) -> fail("WireEnd notified observer, although it was closed."));
311 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
315 void wireConnections()
317 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
319 Simulation.TIMELINE.reset();
321 Wire a = new Wire(1, 2);
322 Wire b = new Wire(1, 2);
323 Wire c = new Wire(1, 2);
324 ReadWriteEnd aI = a.createEnd();
325 ReadWriteEnd bI = b.createEnd();
326 ReadWriteEnd cI = c.createEnd();
328 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
329 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
330 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
332 cI.feedSignals(Bit.ONE);
333 test.assertAfterSimulationIs(print, Bit.ONE);
335 cI.feedSignals(Bit.X);
336 test.assertAfterSimulationIs(print, Bit.X);
338 cI.feedSignals(Bit.X);
339 cI.feedSignals(Bit.Z);
340 test.assertAfterSimulationIs(print, Bit.Z);
342 new Connector(b.createEnd(), c.createEnd()).connect();
343 test.assertAfterSimulationIs(print, Bit.Z);
344 System.err.println("ONE");
345 bI.feedSignals(Bit.ONE);
346 test.assertAfterSimulationIs(print, Bit.ONE);
347 System.err.println("ZERO");
348 bI.feedSignals(Bit.ZERO);
349 test.assertAfterSimulationIs(print, Bit.ZERO);
350 System.err.println("Z");
351 bI.feedSignals(Bit.Z);
352 test.assertAfterSimulationIs(print, Bit.Z);
354 new Connector(a.createEnd(), b.createEnd()).connect();
355 System.err.println("Z 2");
356 aI.feedSignals(Bit.Z);
357 test.assertAfterSimulationIs(print, Bit.Z);
358 test2.assertAfterSimulationIs(Bit.Z);
359 System.err.println("ONE 2");
360 aI.feedSignals(Bit.ONE);
361 test.assertAfterSimulationIs(print, Bit.ONE);
362 test2.assertAfterSimulationIs(Bit.ONE);
363 System.err.println("ZERO 2");
364 aI.feedSignals(Bit.ZERO);
365 test.assertAfterSimulationIs(print, Bit.ZERO);
366 test2.assertAfterSimulationIs(Bit.ZERO);
367 System.err.println("Z 2 II");
368 aI.feedSignals(Bit.Z);
369 test.assertAfterSimulationIs(print, Bit.Z);
370 test2.assertAfterSimulationIs(Bit.Z);
372 System.err.println("No Conflict yet");
373 bI.feedSignals(Bit.ONE);
374 test.assertAfterSimulationIs(print, Bit.ONE);
375 test2.assertAfterSimulationIs(Bit.ONE);
376 aI.feedSignals(Bit.ONE);
377 test.assertAfterSimulationIs(print, Bit.ONE);
378 test2.assertAfterSimulationIs(Bit.ONE);
379 System.err.println("Conflict");
380 aI.feedSignals(Bit.ZERO);
381 test.assertAfterSimulationIs(print, Bit.X);
382 test2.assertAfterSimulationIs(Bit.X);
383 aI.feedSignals(Bit.ONE);
384 test.assertAfterSimulationIs(print, Bit.ONE);
385 test2.assertAfterSimulationIs(Bit.ONE);
388 private static void assertBitArrayEquals(BitVector actual, Bit... expected)
390 assertArrayEquals(expected, actual.getBits());