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.Connector;
\r
13 import era.mi.logic.components.Demux;
\r
14 import era.mi.logic.components.Merger;
\r
15 import era.mi.logic.components.Mux;
\r
16 import era.mi.logic.components.Splitter;
\r
17 import era.mi.logic.components.TriStateBuffer;
\r
18 import era.mi.logic.components.gates.AndGate;
\r
19 import era.mi.logic.components.gates.NotGate;
\r
20 import era.mi.logic.components.gates.OrGate;
\r
21 import era.mi.logic.components.gates.XorGate;
\r
22 import era.mi.logic.wires.WireArray;
\r
23 import era.mi.logic.wires.WireArray.WireArrayEnd;
\r
25 @SuppressWarnings("unused")
\r
30 void circuitExampleTest()
\r
32 Simulation.TIMELINE.reset();
\r
33 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1),
\r
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),
\r
35 j = new WireArray(1, 1), k = new WireArray(1, 1);
\r
36 new AndGate(1, f, a, b);
\r
37 new NotGate(1, f, g);
\r
38 new Merger(h, c, g);
\r
39 new Mux(1, i, e, h, d);
\r
40 new Splitter(i, k, j);
\r
42 a.createInput().feedSignals(Bit.ZERO);
\r
43 b.createInput().feedSignals(Bit.ONE);
\r
44 c.createInput().feedSignals(Bit.ZERO);
\r
45 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
\r
46 e.createInput().feedSignals(Bit.ZERO);
\r
48 Simulation.TIMELINE.executeAll();
\r
50 assertEquals(Bit.ONE, j.getValue());
\r
51 assertEquals(Bit.ZERO, k.getValue());
\r
57 Simulation.TIMELINE.reset();
\r
58 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
\r
59 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
60 new Splitter(in, a, b, c);
\r
62 Simulation.TIMELINE.executeAll();
\r
64 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
65 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
\r
66 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
\r
72 Simulation.TIMELINE.reset();
\r
73 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
\r
74 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
75 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
\r
76 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
78 new Merger(out, a, b, c);
\r
80 Simulation.TIMELINE.executeAll();
\r
83 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
\r
87 void triStateBufferTest()
\r
89 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1), notEn = new WireArray(1, 1);
\r
90 new NotGate(1, en, notEn);
\r
91 new TriStateBuffer(1, a, b, en);
\r
92 new TriStateBuffer(1, b, a, notEn);
\r
94 WireArrayEnd enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
\r
95 enI.feedSignals(Bit.ONE);
\r
96 aI.feedSignals(Bit.ONE);
\r
97 bI.feedSignals(Bit.Z);
\r
99 Simulation.TIMELINE.executeAll();
\r
101 assertEquals(Bit.ONE, b.getValue());
\r
103 bI.feedSignals(Bit.ZERO);
\r
105 Simulation.TIMELINE.executeAll();
\r
107 assertEquals(Bit.X, b.getValue());
\r
108 assertEquals(Bit.ONE, a.getValue());
\r
111 enI.feedSignals(Bit.ZERO);
\r
113 Simulation.TIMELINE.executeAll();
\r
115 assertEquals(Bit.ZERO, a.getValue());
\r
122 Simulation.TIMELINE.reset();
\r
123 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
124 out = new WireArray(4, 1);
\r
125 WireArrayEnd selectIn = select.createInput();
\r
127 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
128 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
129 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
131 new Mux(1, out, select, a, b, c);
\r
132 Simulation.TIMELINE.executeAll();
\r
134 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
135 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
136 Simulation.TIMELINE.executeAll();
\r
138 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
140 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
141 Simulation.TIMELINE.executeAll();
\r
143 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
150 Simulation.TIMELINE.reset();
\r
151 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
152 in = new WireArray(4, 1);
\r
153 WireArrayEnd selectIn = select.createInput();
\r
155 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
156 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
158 new Demux(1, in, select, a, b, c);
\r
159 Simulation.TIMELINE.executeAll();
\r
161 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
162 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
163 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
164 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
165 Simulation.TIMELINE.executeAll();
\r
167 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
168 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
169 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
171 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
172 Simulation.TIMELINE.executeAll();
\r
174 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
175 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
176 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
183 Simulation.TIMELINE.reset();
\r
184 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
185 new AndGate(1, c, a, b);
\r
186 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
187 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
189 Simulation.TIMELINE.executeAll();
\r
191 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
197 Simulation.TIMELINE.reset();
\r
198 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
199 new OrGate(1, c, a, b);
\r
200 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
201 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
203 Simulation.TIMELINE.executeAll();
\r
205 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
211 Simulation.TIMELINE.reset();
\r
212 WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
\r
213 new XorGate(1, d, a, b, c);
\r
214 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
\r
215 b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
216 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
218 Simulation.TIMELINE.executeAll();
\r
220 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
\r
224 void rsLatchCircuitTest()
\r
226 Simulation.TIMELINE.reset();
\r
227 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
\r
228 q = new WireArray(1, 1), nq = new WireArray(1, 1);
\r
230 new OrGate(1, t2, r, nq);
\r
231 new OrGate(1, t1, s, q);
\r
232 new NotGate(1, t2, q);
\r
233 new NotGate(1, t1, nq);
\r
235 WireArrayEnd sIn = s.createInput(), rIn = r.createInput();
\r
237 sIn.feedSignals(Bit.ONE);
\r
238 rIn.feedSignals(Bit.ZERO);
\r
240 Simulation.TIMELINE.executeAll();
\r
242 assertEquals(Bit.ONE, q.getValue());
\r
243 assertEquals(Bit.ZERO, nq.getValue());
\r
245 sIn.feedSignals(Bit.ZERO);
\r
247 Simulation.TIMELINE.executeAll();
\r
248 assertEquals(Bit.ONE, q.getValue());
\r
249 assertEquals(Bit.ZERO, nq.getValue());
\r
251 rIn.feedSignals(Bit.ONE);
\r
253 Simulation.TIMELINE.executeAll();
\r
255 assertEquals(Bit.ZERO, q.getValue());
\r
256 assertEquals(Bit.ONE, nq.getValue());
\r
260 void numericValueTest()
\r
262 Simulation.TIMELINE.reset();
\r
264 WireArray a = new WireArray(4, 1);
\r
265 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
\r
267 Simulation.TIMELINE.executeAll();
\r
269 assertEquals(15, a.getUnsignedValue());
\r
270 assertEquals(-1, a.getSignedValue());
\r
274 void multipleInputs()
\r
276 Simulation.TIMELINE.reset();
\r
277 WireArray w = new WireArray(2, 1);
\r
278 WireArrayEnd wI1 = w.createInput(), wI2 = w.createInput();
\r
279 wI1.feedSignals(Bit.ONE, Bit.Z);
\r
280 wI2.feedSignals(Bit.Z, Bit.X);
\r
281 Simulation.TIMELINE.executeAll();
\r
282 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
\r
284 wI2.feedSignals(Bit.ZERO, Bit.Z);
\r
285 Simulation.TIMELINE.executeAll();
\r
286 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
\r
288 wI2.feedSignals(Bit.Z, Bit.Z);
\r
289 Simulation.TIMELINE.executeAll();
\r
290 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
292 wI2.feedSignals(Bit.ONE, Bit.Z);
\r
293 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
\r
294 Simulation.TIMELINE.executeAll();
\r
295 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
299 void wireConnections()
\r
301 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
\r
303 Simulation.TIMELINE.reset();
\r
305 WireArray a = new WireArray(1, 2);
\r
306 WireArray b = new WireArray(1, 2);
\r
307 WireArray c = new WireArray(1, 2);
\r
308 WireArrayEnd aI = a.createInput();
\r
309 WireArrayEnd bI = b.createInput();
\r
310 WireArrayEnd cI = c.createInput();
\r
312 TestBitDisplay test = new TestBitDisplay(c);
\r
313 TestBitDisplay test2 = new TestBitDisplay(a);
\r
314 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
\r
316 cI.feedSignals(Bit.ONE);
\r
317 test.assertAfterSimulationIs(print, Bit.ONE);
\r
319 cI.feedSignals(Bit.X);
\r
320 test.assertAfterSimulationIs(print, Bit.X);
\r
322 cI.feedSignals(Bit.X);
\r
323 cI.feedSignals(Bit.Z);
\r
324 test.assertAfterSimulationIs(print, Bit.Z);
\r
326 new Connector(b, c).connect();
\r
327 test.assertAfterSimulationIs(print, Bit.Z);
\r
328 System.err.println("ONE");
\r
329 bI.feedSignals(Bit.ONE);
\r
330 test.assertAfterSimulationIs(print, Bit.ONE);
\r
331 System.err.println("ZERO");
\r
332 bI.feedSignals(Bit.ZERO);
\r
333 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
334 System.err.println("Z");
\r
335 bI.feedSignals(Bit.Z);
\r
336 test.assertAfterSimulationIs(print, Bit.Z);
\r
338 new Connector(a, b).connect();
\r
339 System.err.println("Z 2");
\r
340 aI.feedSignals(Bit.Z);
\r
341 test.assertAfterSimulationIs(print, Bit.Z);
\r
342 test2.assertAfterSimulationIs(Bit.Z);
\r
343 System.err.println("ONE 2");
\r
344 aI.feedSignals(Bit.ONE);
\r
345 test.assertAfterSimulationIs(print, Bit.ONE);
\r
346 test2.assertAfterSimulationIs(Bit.ONE);
\r
347 System.err.println("ZERO 2");
\r
348 aI.feedSignals(Bit.ZERO);
\r
349 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
350 test2.assertAfterSimulationIs(Bit.ZERO);
\r
351 System.err.println("Z 2 II");
\r
352 aI.feedSignals(Bit.Z);
\r
353 test.assertAfterSimulationIs(print, Bit.Z);
\r
354 test2.assertAfterSimulationIs(Bit.Z);
\r
356 System.err.println("No Conflict yet");
\r
357 bI.feedSignals(Bit.ONE);
\r
358 test.assertAfterSimulationIs(print, Bit.ONE);
\r
359 test2.assertAfterSimulationIs(Bit.ONE);
\r
360 aI.feedSignals(Bit.ONE);
\r
361 test.assertAfterSimulationIs(print, Bit.ONE);
\r
362 test2.assertAfterSimulationIs(Bit.ONE);
\r
363 System.err.println("Conflict");
\r
364 aI.feedSignals(Bit.ZERO);
\r
365 test.assertAfterSimulationIs(print, Bit.X);
\r
366 test2.assertAfterSimulationIs(Bit.X);
\r
367 aI.feedSignals(Bit.ONE);
\r
368 test.assertAfterSimulationIs(print, Bit.ONE);
\r
369 test2.assertAfterSimulationIs(Bit.ONE);
\r
372 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
\r
374 assertArrayEquals(expected, actual);
\r