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.wires.WireArray;
21 import era.mi.logic.wires.WireArray.WireArrayInput;
27 void circuitExampleTest()
29 Simulation.TIMELINE.reset();
30 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),
31 f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);
32 new AndGate(1, a, b, f);
35 new Mux(1, i, e, h, d);
36 new Splitter(i, k, j);
38 a.createInput().feedSignals(Bit.ZERO);
39 b.createInput().feedSignals(Bit.ONE);
40 c.createInput().feedSignals(Bit.ZERO);
41 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
42 e.createInput().feedSignals(Bit.ZERO);
44 Simulation.TIMELINE.executeAll();
46 assertEquals(Bit.ONE, j.getValue());
47 assertEquals(Bit.ZERO, k.getValue());
53 Simulation.TIMELINE.reset();
54 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
55 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
56 new Splitter(in, a, b, c);
58 Simulation.TIMELINE.executeAll();
60 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
61 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
62 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
68 Simulation.TIMELINE.reset();
69 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
70 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
71 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
72 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
74 new Merger(out, a, b, c);
76 Simulation.TIMELINE.executeAll();
78 assertTrue(Arrays.equals(out.getValues(),
79 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
83 void triStateBufferTest()
85 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
86 notEn = new WireArray(1, 1);
87 new NotGate(1, en, notEn);
88 new TriStateBuffer(1, a, b, en);
89 new TriStateBuffer(1, b, a, notEn);
91 WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
92 enI.feedSignals(Bit.ONE);
93 aI.feedSignals(Bit.ONE);
95 Simulation.TIMELINE.executeAll();
97 assertEquals(Bit.ONE, b.getValue());
99 bI.feedSignals(Bit.ZERO);
101 Simulation.TIMELINE.executeAll();
103 assertEquals(Bit.X, b.getValue());
104 assertEquals(Bit.ONE, a.getValue());
107 enI.feedSignals(Bit.ZERO);
109 Simulation.TIMELINE.executeAll();
111 assertEquals(Bit.ZERO, a.getValue());
118 Simulation.TIMELINE.reset();
119 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
120 select = new WireArray(2, 5), out = new WireArray(4, 1);
121 WireArrayInput selectIn = select.createInput();
123 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
124 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
125 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
127 new Mux(1, out, select, a, b, c);
128 Simulation.TIMELINE.executeAll();
130 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
131 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
132 Simulation.TIMELINE.executeAll();
134 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
136 selectIn.feedSignals(Bit.ONE, Bit.ONE);
137 Simulation.TIMELINE.executeAll();
139 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
146 Simulation.TIMELINE.reset();
147 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
148 select = new WireArray(2, 5), in = new WireArray(4, 1);
149 WireArrayInput selectIn = select.createInput();
151 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
152 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
154 new Demux(1, in, select, a, b, c);
155 Simulation.TIMELINE.executeAll();
157 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
158 assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
159 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
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.Z, Bit.Z, Bit.Z, Bit.Z);
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.Z, Bit.Z, Bit.Z, Bit.Z);
172 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
179 Simulation.TIMELINE.reset();
180 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
181 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
182 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
184 Simulation.TIMELINE.executeAll();
185 assertBitArrayEquals(gate.getOut().getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
191 Simulation.TIMELINE.reset();
192 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
193 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
194 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
196 Simulation.TIMELINE.executeAll();
198 assertBitArrayEquals(gate.getOut().getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
202 void rsLatchCircuitTest()
204 Simulation.TIMELINE.reset();
205 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
206 q = new WireArray(1, 1), nq = new WireArray(1, 1);
208 new OrGate(1, r, nq, t2);
209 new OrGate(1, s, q, t1);
210 new NotGate(1, t2, q);
211 new NotGate(1, t1, nq);
213 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
215 sIn.feedSignals(Bit.ONE);
216 rIn.feedSignals(Bit.ZERO);
218 Simulation.TIMELINE.executeAll();
220 assertEquals(Bit.ONE, q.getValue());
221 assertEquals(Bit.ZERO, nq.getValue());
223 sIn.feedSignals(Bit.ZERO);
225 Simulation.TIMELINE.executeAll();
226 assertEquals(Bit.ONE, q.getValue());
227 assertEquals(Bit.ZERO, nq.getValue());
229 rIn.feedSignals(Bit.ONE);
231 Simulation.TIMELINE.executeAll();
233 assertEquals(Bit.ZERO, q.getValue());
234 assertEquals(Bit.ONE, nq.getValue());
238 void numericValueTest()
240 Simulation.TIMELINE.reset();
242 WireArray a = new WireArray(4, 1);
243 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
245 Simulation.TIMELINE.executeAll();
247 assertEquals(15, a.getUnsignedValue());
248 assertEquals(-1, a.getSignedValue());
252 void multipleInputs()
254 Simulation.TIMELINE.reset();
255 WireArray w = new WireArray(2, 1);
256 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
257 wI1.feedSignals(Bit.ONE, Bit.Z);
258 wI2.feedSignals(Bit.Z, Bit.X);
259 Simulation.TIMELINE.executeAll();
260 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
262 wI2.feedSignals(Bit.ZERO, Bit.Z);
263 Simulation.TIMELINE.executeAll();
264 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
266 wI2.feedSignals(Bit.Z, Bit.Z);
267 Simulation.TIMELINE.executeAll();
268 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
270 wI2.feedSignals(Bit.ONE, Bit.Z);
271 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
272 Simulation.TIMELINE.executeAll();
273 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
277 void wireConnections()
279 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
281 Simulation.TIMELINE.reset();
283 WireArray a = new WireArray(1, 2);
284 WireArray b = new WireArray(1, 2);
285 WireArray c = new WireArray(1, 2);
286 WireArrayInput aI = a.createInput();
287 WireArrayInput bI = b.createInput();
288 WireArrayInput cI = c.createInput();
290 TestBitDisplay test = new TestBitDisplay(c);
291 TestBitDisplay test2 = new TestBitDisplay(a);
292 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
294 cI.feedSignals(Bit.ONE);
295 test.assertAfterSimulationIs(print, Bit.ONE);
297 cI.feedSignals(Bit.X);
298 test.assertAfterSimulationIs(print, Bit.X);
300 cI.feedSignals(Bit.X);
301 cI.feedSignals(Bit.Z);
302 test.assertAfterSimulationIs(print, Bit.Z);
304 Connector c1 = new Connector(b, c);
305 test.assertAfterSimulationIs(print, Bit.Z);
306 System.err.println("ONE");
307 bI.feedSignals(Bit.ONE);
308 test.assertAfterSimulationIs(print, Bit.ONE);
309 System.err.println("ZERO");
310 bI.feedSignals(Bit.ZERO);
311 test.assertAfterSimulationIs(print, Bit.ZERO);
312 System.err.println("Z");
313 bI.feedSignals(Bit.Z);
314 test.assertAfterSimulationIs(print, Bit.Z);
316 Connector c2 = new Connector(a, b);
317 System.err.println("Z 2");
318 aI.feedSignals(Bit.Z);
319 test.assertAfterSimulationIs(print, Bit.Z);
320 test2.assertAfterSimulationIs(Bit.Z);
321 System.err.println("ONE 2");
322 aI.feedSignals(Bit.ONE);
323 test.assertAfterSimulationIs(print, Bit.ONE);
324 test2.assertAfterSimulationIs(Bit.ONE);
325 System.err.println("ZERO 2");
326 aI.feedSignals(Bit.ZERO);
327 test.assertAfterSimulationIs(print, Bit.ZERO);
328 test2.assertAfterSimulationIs(Bit.ZERO);
329 System.err.println("Z 2 II");
330 aI.feedSignals(Bit.Z);
331 test.assertAfterSimulationIs(print, Bit.Z);
332 test2.assertAfterSimulationIs(Bit.Z);
334 System.err.println("No Conflict yet");
335 bI.feedSignals(Bit.ONE);
336 test.assertAfterSimulationIs(print, Bit.ONE);
337 test2.assertAfterSimulationIs(Bit.ONE);
338 aI.feedSignals(Bit.ONE);
339 test.assertAfterSimulationIs(print, Bit.ONE);
340 test2.assertAfterSimulationIs(Bit.ONE);
341 System.err.println("Conflict");
342 aI.feedSignals(Bit.ZERO);
343 test.assertAfterSimulationIs(print, Bit.X);
344 test2.assertAfterSimulationIs(Bit.X);
345 aI.feedSignals(Bit.ONE);
346 test.assertAfterSimulationIs(print, Bit.ONE);
347 test2.assertAfterSimulationIs(Bit.ONE);
350 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
352 assertArrayEquals(expected, actual);