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.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.wires.WireArray;
20 import era.mi.logic.wires.WireArray.WireArrayInput;
26 void circuitExampleTest()
28 Simulation.TIMELINE.reset();
29 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),
30 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);
31 new AndGate(1, a, b, f);
34 new Mux(1, i, e, h, d);
35 new Splitter(i, k, j);
37 a.createInput().feedSignals(Bit.ZERO);
38 b.createInput().feedSignals(Bit.ONE);
39 c.createInput().feedSignals(Bit.ZERO);
40 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
41 e.createInput().feedSignals(Bit.ZERO);
43 Simulation.TIMELINE.executeAll();
45 assertEquals(Bit.ONE, j.getValue());
46 assertEquals(Bit.ZERO, k.getValue());
52 Simulation.TIMELINE.reset();
53 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
54 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
55 new Splitter(in, a, b, c);
57 Simulation.TIMELINE.executeAll();
59 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues());
60 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues());
61 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues());
67 Simulation.TIMELINE.reset();
68 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
69 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
70 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
71 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
73 new Merger(out, a, b, c);
75 Simulation.TIMELINE.executeAll();
77 assertTrue(Arrays.equals(out.getValues(),
78 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
82 void triStateBufferTest()
84 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
85 notEn = new WireArray(1, 1);
86 new NotGate(1, en, notEn);
87 new TriStateBuffer(1, a, b, en);
88 new TriStateBuffer(1, b, a, notEn);
90 WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
91 enI.feedSignals(Bit.ONE);
92 aI.feedSignals(Bit.ONE);
94 Simulation.TIMELINE.executeAll();
96 assertEquals(Bit.ONE, b.getValue());
98 bI.feedSignals(Bit.ZERO);
100 Simulation.TIMELINE.executeAll();
102 assertEquals(Bit.X, b.getValue());
103 assertEquals(Bit.ONE, a.getValue());
106 enI.feedSignals(Bit.ZERO);
108 Simulation.TIMELINE.executeAll();
110 assertEquals(Bit.ZERO, a.getValue());
117 Simulation.TIMELINE.reset();
118 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
119 select = new WireArray(2, 5), out = new WireArray(4, 1);
120 WireArrayInput selectIn = select.createInput();
122 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
123 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
124 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
126 new Mux(1, out, select, a, b, c);
127 Simulation.TIMELINE.executeAll();
129 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues());
130 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
131 Simulation.TIMELINE.executeAll();
133 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues());
135 selectIn.feedSignals(Bit.ONE, Bit.ONE);
136 Simulation.TIMELINE.executeAll();
138 assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues());
145 Simulation.TIMELINE.reset();
146 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
147 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
148 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
150 Simulation.TIMELINE.executeAll();
151 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues());
157 Simulation.TIMELINE.reset();
158 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
159 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
160 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
162 Simulation.TIMELINE.executeAll();
164 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues());
168 void rsLatchCircuitTest()
170 Simulation.TIMELINE.reset();
171 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
172 q = new WireArray(1, 1), nq = new WireArray(1, 1);
174 new OrGate(1, r, nq, t2);
175 new OrGate(1, s, q, t1);
176 new NotGate(1, t2, q);
177 new NotGate(1, t1, nq);
179 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
181 sIn.feedSignals(Bit.ONE);
182 rIn.feedSignals(Bit.ZERO);
184 Simulation.TIMELINE.executeAll();
186 assertEquals(Bit.ONE, q.getValue());
187 assertEquals(Bit.ZERO, nq.getValue());
189 sIn.feedSignals(Bit.ZERO);
191 Simulation.TIMELINE.executeAll();
192 assertEquals(Bit.ONE, q.getValue());
193 assertEquals(Bit.ZERO, nq.getValue());
195 rIn.feedSignals(Bit.ONE);
197 Simulation.TIMELINE.executeAll();
199 assertEquals(Bit.ZERO, q.getValue());
200 assertEquals(Bit.ONE, nq.getValue());
204 void numericValueTest()
206 Simulation.TIMELINE.reset();
208 WireArray a = new WireArray(4, 1);
209 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
211 Simulation.TIMELINE.executeAll();
213 assertEquals(15, a.getUnsignedValue());
214 assertEquals(-1, a.getSignedValue());
218 void multipleInputs()
220 Simulation.TIMELINE.reset();
221 WireArray w = new WireArray(2, 1);
222 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
223 wI1.feedSignals(Bit.ONE, Bit.Z);
224 wI2.feedSignals(Bit.Z, Bit.X);
225 Simulation.TIMELINE.executeAll();
226 assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues());
228 wI2.feedSignals(Bit.ZERO, Bit.Z);
229 Simulation.TIMELINE.executeAll();
230 assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues());
232 wI2.feedSignals(Bit.Z, Bit.Z);
233 Simulation.TIMELINE.executeAll();
234 assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
236 wI2.feedSignals(Bit.ONE, Bit.Z);
237 w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
238 Simulation.TIMELINE.executeAll();
239 assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
243 void wireConnections()
245 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
247 Simulation.TIMELINE.reset();
249 WireArray a = new WireArray(1, 2);
250 WireArray b = new WireArray(1, 2);
251 WireArray c = new WireArray(1, 2);
252 WireArrayInput aI = a.createInput();
253 WireArrayInput bI = b.createInput();
254 WireArrayInput cI = c.createInput();
256 TestBitDisplay test = new TestBitDisplay(c);
257 TestBitDisplay test2 = new TestBitDisplay(a);
258 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
260 cI.feedSignals(Bit.ONE);
261 test.assertAfterSimulationIs(print, Bit.ONE);
263 cI.feedSignals(Bit.X);
264 test.assertAfterSimulationIs(print, Bit.X);
266 cI.feedSignals(Bit.X);
267 cI.feedSignals(Bit.Z);
268 test.assertAfterSimulationIs(print, Bit.Z);
270 Connector c1 = new Connector(b, c);
271 test.assertAfterSimulationIs(print, Bit.Z);
272 System.err.println("ONE");
273 bI.feedSignals(Bit.ONE);
274 test.assertAfterSimulationIs(print, Bit.ONE);
275 System.err.println("ZERO");
276 bI.feedSignals(Bit.ZERO);
277 test.assertAfterSimulationIs(print, Bit.ZERO);
278 System.err.println("Z");
279 bI.feedSignals(Bit.Z);
280 test.assertAfterSimulationIs(print, Bit.Z);
282 Connector c2 = new Connector(a, b);
283 System.err.println("Z 2");
284 aI.feedSignals(Bit.Z);
285 test.assertAfterSimulationIs(print, Bit.Z);
286 test2.assertAfterSimulationIs(Bit.Z);
287 System.err.println("ONE 2");
288 aI.feedSignals(Bit.ONE);
289 test.assertAfterSimulationIs(print, Bit.ONE);
290 test2.assertAfterSimulationIs(Bit.ONE);
291 System.err.println("ZERO 2");
292 aI.feedSignals(Bit.ZERO);
293 test.assertAfterSimulationIs(print, Bit.ZERO);
294 test2.assertAfterSimulationIs(Bit.ZERO);
295 System.err.println("Z 2 II");
296 aI.feedSignals(Bit.Z);
297 test.assertAfterSimulationIs(print, Bit.Z);
298 test2.assertAfterSimulationIs(Bit.Z);
300 System.err.println("No Conflict yet");
301 bI.feedSignals(Bit.ONE);
302 test.assertAfterSimulationIs(print, Bit.ONE);
303 test2.assertAfterSimulationIs(Bit.ONE);
304 aI.feedSignals(Bit.ONE);
305 test.assertAfterSimulationIs(print, Bit.ONE);
306 test2.assertAfterSimulationIs(Bit.ONE);
307 System.err.println("Conflict");
308 aI.feedSignals(Bit.ZERO);
309 test.assertAfterSimulationIs(print, Bit.X);
310 test2.assertAfterSimulationIs(Bit.X);
311 aI.feedSignals(Bit.ONE);
312 test.assertAfterSimulationIs(print, Bit.ONE);
313 test2.assertAfterSimulationIs(Bit.ONE);
316 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
318 assertArrayEquals(expected, actual);