1 package era.mi.logic.tests;
\r
3 import static org.junit.jupiter.api.Assertions.*;
\r
5 import java.util.Arrays;
\r
6 import java.util.function.LongConsumer;
\r
8 import org.junit.jupiter.api.Test;
\r
10 import era.mi.logic.Bit;
\r
11 import era.mi.logic.Simulation;
\r
12 import era.mi.logic.components.Demux;
\r
13 import era.mi.logic.components.Merger;
\r
14 import era.mi.logic.components.Mux;
\r
15 import era.mi.logic.components.Splitter;
\r
16 import era.mi.logic.components.TriStateBuffer;
\r
17 import era.mi.logic.components.gates.AndGate;
\r
18 import era.mi.logic.components.gates.NotGate;
\r
19 import era.mi.logic.components.gates.OrGate;
\r
20 import era.mi.logic.components.gates.XorGate;
\r
21 import era.mi.logic.wires.WireArray;
\r
22 import era.mi.logic.wires.WireArray.WireArrayEnd;
\r
28 void circuitExampleTest()
\r
30 Simulation.TIMELINE.reset();
\r
31 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1),
\r
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),
\r
33 j = new WireArray(1, 1), k = new WireArray(1, 1);
\r
34 new AndGate(1, f, a, b);
\r
35 new NotGate(1, f, g);
\r
36 new Merger(h, c, g);
\r
37 new Mux(1, i, e, h, d);
\r
38 new Splitter(i, k, j);
\r
40 a.createInput().feedSignals(Bit.ZERO);
\r
41 b.createInput().feedSignals(Bit.ONE);
\r
42 c.createInput().feedSignals(Bit.ZERO);
\r
43 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
\r
44 e.createInput().feedSignals(Bit.ZERO);
\r
46 Simulation.TIMELINE.executeAll();
\r
48 assertEquals(Bit.ONE, j.getValue());
\r
49 assertEquals(Bit.ZERO, k.getValue());
\r
55 Simulation.TIMELINE.reset();
\r
56 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
\r
57 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
58 new Splitter(in, a, b, c);
\r
60 Simulation.TIMELINE.executeAll();
\r
62 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
63 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
\r
64 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
\r
70 Simulation.TIMELINE.reset();
\r
71 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
\r
72 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
73 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
\r
74 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
76 new Merger(out, a, b, c);
\r
78 Simulation.TIMELINE.executeAll();
\r
81 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
\r
85 void triStateBufferTest()
\r
87 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1), notEn = new WireArray(1, 1);
\r
88 new NotGate(1, en, notEn);
\r
89 new TriStateBuffer(1, a, b, en);
\r
90 new TriStateBuffer(1, b, a, notEn);
\r
92 WireArrayEnd enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
\r
93 enI.feedSignals(Bit.ONE);
\r
94 aI.feedSignals(Bit.ONE);
\r
96 Simulation.TIMELINE.executeAll();
\r
98 assertEquals(Bit.ONE, b.getValue());
\r
100 bI.feedSignals(Bit.ZERO);
\r
102 Simulation.TIMELINE.executeAll();
\r
104 assertEquals(Bit.X, b.getValue());
\r
105 assertEquals(Bit.ONE, a.getValue());
\r
108 enI.feedSignals(Bit.ZERO);
\r
110 Simulation.TIMELINE.executeAll();
\r
112 assertEquals(Bit.ZERO, a.getValue());
\r
119 Simulation.TIMELINE.reset();
\r
120 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
121 out = new WireArray(4, 1);
\r
122 WireArrayEnd selectIn = select.createInput();
\r
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
125 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
126 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
128 new Mux(1, out, select, a, b, c);
\r
129 Simulation.TIMELINE.executeAll();
\r
131 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
132 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
133 Simulation.TIMELINE.executeAll();
\r
135 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
137 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
138 Simulation.TIMELINE.executeAll();
\r
140 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
147 Simulation.TIMELINE.reset();
\r
148 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
149 in = new WireArray(4, 1);
\r
150 WireArrayEnd selectIn = select.createInput();
\r
152 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
153 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
155 new Demux(1, in, select, a, b, c);
\r
156 Simulation.TIMELINE.executeAll();
\r
158 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
159 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
160 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
161 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
162 Simulation.TIMELINE.executeAll();
\r
164 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
165 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
166 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
168 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
169 Simulation.TIMELINE.executeAll();
\r
171 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
172 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
173 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
180 Simulation.TIMELINE.reset();
\r
181 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
182 new AndGate(1, c, a, b);
\r
183 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
184 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
186 Simulation.TIMELINE.executeAll();
\r
188 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
194 Simulation.TIMELINE.reset();
\r
195 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
196 new OrGate(1, c, a, b);
\r
197 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
198 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
200 Simulation.TIMELINE.executeAll();
\r
202 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
208 Simulation.TIMELINE.reset();
\r
209 WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
\r
210 new XorGate(1, d, a, b, c);
\r
211 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
\r
212 b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
213 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
215 Simulation.TIMELINE.executeAll();
\r
217 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
\r
221 void rsLatchCircuitTest()
\r
223 Simulation.TIMELINE.reset();
\r
224 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
\r
225 q = new WireArray(1, 1), nq = new WireArray(1, 1);
\r
227 new OrGate(1, t2, r, nq);
\r
228 new OrGate(1, t1, s, q);
\r
229 new NotGate(1, t2, q);
\r
230 new NotGate(1, t1, nq);
\r
232 WireArrayEnd sIn = s.createInput(), rIn = r.createInput();
\r
234 sIn.feedSignals(Bit.ONE);
\r
235 rIn.feedSignals(Bit.ZERO);
\r
237 Simulation.TIMELINE.executeAll();
\r
239 assertEquals(Bit.ONE, q.getValue());
\r
240 assertEquals(Bit.ZERO, nq.getValue());
\r
242 sIn.feedSignals(Bit.ZERO);
\r
244 Simulation.TIMELINE.executeAll();
\r
245 assertEquals(Bit.ONE, q.getValue());
\r
246 assertEquals(Bit.ZERO, nq.getValue());
\r
248 rIn.feedSignals(Bit.ONE);
\r
250 Simulation.TIMELINE.executeAll();
\r
252 assertEquals(Bit.ZERO, q.getValue());
\r
253 assertEquals(Bit.ONE, nq.getValue());
\r
257 void numericValueTest()
\r
259 Simulation.TIMELINE.reset();
\r
261 WireArray a = new WireArray(4, 1);
\r
262 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
\r
264 Simulation.TIMELINE.executeAll();
\r
266 assertEquals(15, a.getUnsignedValue());
\r
267 assertEquals(-1, a.getSignedValue());
\r
271 void multipleInputs()
\r
273 Simulation.TIMELINE.reset();
\r
274 WireArray w = new WireArray(2, 1);
\r
275 WireArrayEnd wI1 = w.createInput(), wI2 = w.createInput();
\r
276 wI1.feedSignals(Bit.ONE, Bit.Z);
\r
277 wI2.feedSignals(Bit.Z, Bit.X);
\r
278 Simulation.TIMELINE.executeAll();
\r
279 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
\r
281 wI2.feedSignals(Bit.ZERO, Bit.Z);
\r
282 Simulation.TIMELINE.executeAll();
\r
283 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
\r
285 wI2.feedSignals(Bit.Z, Bit.Z);
\r
286 Simulation.TIMELINE.executeAll();
\r
287 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
289 wI2.feedSignals(Bit.ONE, Bit.Z);
\r
290 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
\r
291 Simulation.TIMELINE.executeAll();
\r
292 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
296 void wireConnections()
\r
298 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
\r
300 Simulation.TIMELINE.reset();
\r
302 WireArray a = new WireArray(1, 2);
\r
303 WireArray b = new WireArray(1, 2);
\r
304 WireArray c = new WireArray(1, 2);
\r
305 WireArrayEnd aI = a.createInput();
\r
306 WireArrayEnd bI = b.createInput();
\r
307 WireArrayEnd cI = c.createInput();
\r
309 TestBitDisplay test = new TestBitDisplay(c);
\r
310 TestBitDisplay test2 = new TestBitDisplay(a);
\r
311 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
\r
313 cI.feedSignals(Bit.ONE);
\r
314 test.assertAfterSimulationIs(print, Bit.ONE);
\r
316 cI.feedSignals(Bit.X);
\r
317 test.assertAfterSimulationIs(print, Bit.X);
\r
319 cI.feedSignals(Bit.X);
\r
320 cI.feedSignals(Bit.Z);
\r
321 test.assertAfterSimulationIs(print, Bit.Z);
\r
323 new Connector(b, c);
\r
324 test.assertAfterSimulationIs(print, Bit.Z);
\r
325 System.err.println("ONE");
\r
326 bI.feedSignals(Bit.ONE);
\r
327 test.assertAfterSimulationIs(print, Bit.ONE);
\r
328 System.err.println("ZERO");
\r
329 bI.feedSignals(Bit.ZERO);
\r
330 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
331 System.err.println("Z");
\r
332 bI.feedSignals(Bit.Z);
\r
333 test.assertAfterSimulationIs(print, Bit.Z);
\r
335 new Connector(a, b);
\r
336 System.err.println("Z 2");
\r
337 aI.feedSignals(Bit.Z);
\r
338 test.assertAfterSimulationIs(print, Bit.Z);
\r
339 test2.assertAfterSimulationIs(Bit.Z);
\r
340 System.err.println("ONE 2");
\r
341 aI.feedSignals(Bit.ONE);
\r
342 test.assertAfterSimulationIs(print, Bit.ONE);
\r
343 test2.assertAfterSimulationIs(Bit.ONE);
\r
344 System.err.println("ZERO 2");
\r
345 aI.feedSignals(Bit.ZERO);
\r
346 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
347 test2.assertAfterSimulationIs(Bit.ZERO);
\r
348 System.err.println("Z 2 II");
\r
349 aI.feedSignals(Bit.Z);
\r
350 test.assertAfterSimulationIs(print, Bit.Z);
\r
351 test2.assertAfterSimulationIs(Bit.Z);
\r
353 System.err.println("No Conflict yet");
\r
354 bI.feedSignals(Bit.ONE);
\r
355 test.assertAfterSimulationIs(print, Bit.ONE);
\r
356 test2.assertAfterSimulationIs(Bit.ONE);
\r
357 aI.feedSignals(Bit.ONE);
\r
358 test.assertAfterSimulationIs(print, Bit.ONE);
\r
359 test2.assertAfterSimulationIs(Bit.ONE);
\r
360 System.err.println("Conflict");
\r
361 aI.feedSignals(Bit.ZERO);
\r
362 test.assertAfterSimulationIs(print, Bit.X);
\r
363 test2.assertAfterSimulationIs(Bit.X);
\r
364 aI.feedSignals(Bit.ONE);
\r
365 test.assertAfterSimulationIs(print, Bit.ONE);
\r
366 test2.assertAfterSimulationIs(Bit.ONE);
\r
369 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
\r
371 assertArrayEquals(expected, actual);
\r