1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.*;
5 import java.util.function.LongConsumer;
7 import org.junit.jupiter.api.Test;
9 import era.mi.logic.Simulation;
10 import era.mi.logic.components.Connector;
11 import era.mi.logic.components.Demux;
12 import era.mi.logic.components.Merger;
13 import era.mi.logic.components.Mux;
14 import era.mi.logic.components.Splitter;
15 import era.mi.logic.components.TriStateBuffer;
16 import era.mi.logic.components.gates.AndGate;
17 import era.mi.logic.components.gates.NotGate;
18 import era.mi.logic.components.gates.OrGate;
19 import era.mi.logic.components.gates.XorGate;
20 import era.mi.logic.types.Bit;
21 import era.mi.logic.types.BitVector;
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();
81 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
85 void triStateBufferTest()
87 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
88 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
89 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
90 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
92 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
93 enI.feedSignals(Bit.ONE);
94 aI.feedSignals(Bit.ONE);
95 bI.feedSignals(Bit.Z);
97 Simulation.TIMELINE.executeAll();
99 assertEquals(Bit.ONE, b.getValue());
101 bI.feedSignals(Bit.ZERO);
103 Simulation.TIMELINE.executeAll();
105 assertEquals(Bit.X, b.getValue());
106 assertEquals(Bit.ONE, a.getValue());
109 enI.feedSignals(Bit.ZERO);
111 Simulation.TIMELINE.executeAll();
113 assertEquals(Bit.ZERO, a.getValue());
120 Simulation.TIMELINE.reset();
121 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);
122 WireEnd selectIn = select.createEnd();
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
128 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
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 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);
149 WireEnd selectIn = select.createEnd();
151 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
152 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
154 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
155 Simulation.TIMELINE.executeAll();
157 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
158 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
159 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
160 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
161 Simulation.TIMELINE.executeAll();
163 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
164 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
165 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
167 selectIn.feedSignals(Bit.ONE, Bit.ONE);
168 Simulation.TIMELINE.executeAll();
170 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
171 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
172 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
179 Simulation.TIMELINE.reset();
180 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
181 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
182 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
183 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
185 Simulation.TIMELINE.executeAll();
187 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
193 Simulation.TIMELINE.reset();
194 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
195 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
196 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
197 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
199 Simulation.TIMELINE.executeAll();
201 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
207 Simulation.TIMELINE.reset();
208 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
209 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
210 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
211 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
212 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
214 Simulation.TIMELINE.executeAll();
216 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
222 Simulation.TIMELINE.reset();
223 Wire a = new Wire(3, 1), b = new Wire(3, 2);
224 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
225 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
227 Simulation.TIMELINE.executeAll();
229 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
233 void rsLatchCircuitTest()
235 Simulation.TIMELINE.reset();
236 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);
238 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
239 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
240 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
241 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
243 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
245 sIn.feedSignals(Bit.ONE);
246 rIn.feedSignals(Bit.ZERO);
248 Simulation.TIMELINE.executeAll();
250 assertEquals(Bit.ONE, q.getValue());
251 assertEquals(Bit.ZERO, nq.getValue());
253 sIn.feedSignals(Bit.ZERO);
255 Simulation.TIMELINE.executeAll();
256 assertEquals(Bit.ONE, q.getValue());
257 assertEquals(Bit.ZERO, nq.getValue());
259 rIn.feedSignals(Bit.ONE);
261 Simulation.TIMELINE.executeAll();
263 assertEquals(Bit.ZERO, q.getValue());
264 assertEquals(Bit.ONE, nq.getValue());
268 void numericValueTest()
270 Simulation.TIMELINE.reset();
272 Wire a = new Wire(4, 1);
273 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
275 Simulation.TIMELINE.executeAll();
277 assertEquals(15, a.getUnsignedValue());
278 assertEquals(-1, a.getSignedValue());
282 void multipleInputs()
284 Simulation.TIMELINE.reset();
285 Wire w = new Wire(2, 1);
286 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
287 wI1.feedSignals(Bit.ONE, Bit.Z);
288 wI2.feedSignals(Bit.Z, Bit.X);
289 Simulation.TIMELINE.executeAll();
290 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
292 wI2.feedSignals(Bit.ZERO, Bit.Z);
293 Simulation.TIMELINE.executeAll();
294 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
296 wI2.feedSignals(Bit.Z, Bit.Z);
297 Simulation.TIMELINE.executeAll();
298 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
300 wI2.feedSignals(Bit.ONE, Bit.Z);
301 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
302 Simulation.TIMELINE.executeAll();
303 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
307 void wireConnections()
309 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
311 Simulation.TIMELINE.reset();
313 Wire a = new Wire(1, 2);
314 Wire b = new Wire(1, 2);
315 Wire c = new Wire(1, 2);
316 WireEnd aI = a.createEnd();
317 WireEnd bI = b.createEnd();
318 WireEnd cI = c.createEnd();
320 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
321 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
322 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
324 cI.feedSignals(Bit.ONE);
325 test.assertAfterSimulationIs(print, Bit.ONE);
327 cI.feedSignals(Bit.X);
328 test.assertAfterSimulationIs(print, Bit.X);
330 cI.feedSignals(Bit.X);
331 cI.feedSignals(Bit.Z);
332 test.assertAfterSimulationIs(print, Bit.Z);
334 new Connector(b.createEnd(), c.createEnd()).connect();
335 test.assertAfterSimulationIs(print, Bit.Z);
336 System.err.println("ONE");
337 bI.feedSignals(Bit.ONE);
338 test.assertAfterSimulationIs(print, Bit.ONE);
339 System.err.println("ZERO");
340 bI.feedSignals(Bit.ZERO);
341 test.assertAfterSimulationIs(print, Bit.ZERO);
342 System.err.println("Z");
343 bI.feedSignals(Bit.Z);
344 test.assertAfterSimulationIs(print, Bit.Z);
346 new Connector(a.createEnd(), b.createEnd()).connect();
347 System.err.println("Z 2");
348 aI.feedSignals(Bit.Z);
349 test.assertAfterSimulationIs(print, Bit.Z);
350 test2.assertAfterSimulationIs(Bit.Z);
351 System.err.println("ONE 2");
352 aI.feedSignals(Bit.ONE);
353 test.assertAfterSimulationIs(print, Bit.ONE);
354 test2.assertAfterSimulationIs(Bit.ONE);
355 System.err.println("ZERO 2");
356 aI.feedSignals(Bit.ZERO);
357 test.assertAfterSimulationIs(print, Bit.ZERO);
358 test2.assertAfterSimulationIs(Bit.ZERO);
359 System.err.println("Z 2 II");
360 aI.feedSignals(Bit.Z);
361 test.assertAfterSimulationIs(print, Bit.Z);
362 test2.assertAfterSimulationIs(Bit.Z);
364 System.err.println("No Conflict yet");
365 bI.feedSignals(Bit.ONE);
366 test.assertAfterSimulationIs(print, Bit.ONE);
367 test2.assertAfterSimulationIs(Bit.ONE);
368 aI.feedSignals(Bit.ONE);
369 test.assertAfterSimulationIs(print, Bit.ONE);
370 test2.assertAfterSimulationIs(Bit.ONE);
371 System.err.println("Conflict");
372 aI.feedSignals(Bit.ZERO);
373 test.assertAfterSimulationIs(print, Bit.X);
374 test2.assertAfterSimulationIs(Bit.X);
375 aI.feedSignals(Bit.ONE);
376 test.assertAfterSimulationIs(print, Bit.ONE);
377 test2.assertAfterSimulationIs(Bit.ONE);
380 private static void assertBitArrayEquals(BitVector actual, Bit... expected)
382 assertArrayEquals(expected, actual.getBits());