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.Mux2;
13 import era.mi.logic.components.gates.AndGate;
14 import era.mi.logic.components.gates.NotGate;
15 import era.mi.logic.components.gates.OrGate;
16 import era.mi.logic.wires.WireArray;
17 import era.mi.logic.wires.WireArray.WireArrayInput;
23 // void circuitExampleTest()
25 // Simulation.TIMELINE.reset();
26 // 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),
27 // 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);
28 // new AndGate(1, a, b, f);
29 // new NotGate(1, f, g);
30 // new Merger2(h, c, g);
31 // new Mux(1, h, d, e, i);
32 // new Splitter(i, k, j);
34 // a.createInput().feedSignals(Bit.ZERO);
35 // b.createInput().feedSignals(Bit.ONE);
36 // c.createInput().feedSignals(Bit.ZERO);
37 // d.createInput().feedSignals(Bit.ONE, Bit.ONE);
38 // e.createInput().feedSignals(Bit.ONE);
40 // while(Simulation.TIMELINE.hasNext())
42 // Simulation.TIMELINE.executeNext();
45 // assertEquals(Simulation.TIMELINE.getSimulationTime(), 14);
46 // assertEquals(Bit.ONE, j.getValue());
47 // assertEquals(Bit.ZERO, k.getValue());
51 // void splitterTest()
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 // while(Simulation.TIMELINE.hasNext())
60 // Simulation.TIMELINE.executeNext();
63 // assertTrue(Arrays.equals(a.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }));
64 // assertTrue(Arrays.equals(b.getValues(), new Bit[] { Bit.ONE, Bit.ZERO }));
65 // assertTrue(Arrays.equals(c.getValues(), new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }));
71 // Simulation.TIMELINE.reset();
72 // WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
73 // a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
74 // b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
75 // c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
77 // new Merger2(out, a, b, c);
79 // while(Simulation.TIMELINE.hasNext())
81 // Simulation.TIMELINE.executeNext();
84 // assertTrue(Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
90 Simulation.TIMELINE.reset();
91 WireArray a = new WireArray(1, 3), b = new WireArray(1, 2), select = new WireArray(1, 1), out = new WireArray(1, 1);
92 WireArrayInput selectIn = select.createInput();
94 selectIn.feedSignals(Bit.ZERO);
95 a.createInput().feedSignals(Bit.ONE);
96 b.createInput().feedSignals(Bit.ZERO);
98 new Mux2(1, out, select, a, b);
99 assertEquals(Bit.Z, out.getValue());
100 while(Simulation.TIMELINE.hasNext())
102 Simulation.TIMELINE.executeNext();
105 assertEquals(Bit.ONE, out.getValue());
106 selectIn.feedSignals(Bit.ONE);
107 while(Simulation.TIMELINE.hasNext())
109 Simulation.TIMELINE.executeNext();
112 assertEquals(out.getValue(), Bit.ZERO);
118 Simulation.TIMELINE.reset();
119 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
120 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
121 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
124 while(Simulation.TIMELINE.hasNext())
126 Simulation.TIMELINE.executeNext();
128 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
134 Simulation.TIMELINE.reset();
135 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
136 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
137 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
139 while(Simulation.TIMELINE.hasNext())
141 Simulation.TIMELINE.executeNext();
144 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
148 void rsLatchCircuitTest()
150 Simulation.TIMELINE.reset();
151 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1), q = new WireArray(1, 1),
152 nq = new WireArray(1, 1);
154 new OrGate(1, r, nq, t2);
155 new OrGate(1, s, q, t1);
156 new NotGate(1, t2, q);
157 new NotGate(1, t1, nq);
159 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
161 sIn.feedSignals(Bit.ONE);
162 rIn.feedSignals(Bit.ZERO);
164 while(Simulation.TIMELINE.hasNext())
166 Simulation.TIMELINE.executeNext();
169 assertEquals(q.getValue(), Bit.ONE);
170 assertEquals(nq.getValue(), Bit.ZERO);
172 sIn.feedSignals(Bit.ZERO);
174 while(Simulation.TIMELINE.hasNext())
176 Simulation.TIMELINE.executeNext();
179 assertEquals(q.getValue(), Bit.ONE);
180 assertEquals(nq.getValue(), Bit.ZERO);
182 rIn.feedSignals(Bit.ONE);
184 while(Simulation.TIMELINE.hasNext())
186 Simulation.TIMELINE.executeNext();
189 assertEquals(q.getValue(), Bit.ZERO);
190 assertEquals(nq.getValue(), Bit.ONE);
194 void numericValueTest()
196 Simulation.TIMELINE.reset();
198 WireArray a = new WireArray(4, 1);
199 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
201 while(Simulation.TIMELINE.hasNext())
203 Simulation.TIMELINE.executeNext();
206 assertEquals(a.getUnsignedValue(), 15);
207 assertEquals(a.getSignedValue(), -1);
211 void multipleInputs()
213 Simulation.TIMELINE.reset();
214 WireArray w = new WireArray(2, 1);
215 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
216 wI1.feedSignals(Bit.ONE, Bit.Z);
217 wI2.feedSignals(Bit.Z, Bit.X);
218 while(Simulation.TIMELINE.hasNext())
220 Simulation.TIMELINE.executeNext();
222 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.X }));
224 wI2.feedSignals(Bit.ZERO, Bit.Z);
225 while(Simulation.TIMELINE.hasNext())
227 Simulation.TIMELINE.executeNext();
229 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.X, Bit.Z }));
231 wI2.feedSignals(Bit.Z, Bit.Z);
232 while(Simulation.TIMELINE.hasNext())
234 Simulation.TIMELINE.executeNext();
236 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
238 wI2.feedSignals(Bit.ONE, Bit.Z);
239 w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
240 while(Simulation.TIMELINE.hasNext())
242 Simulation.TIMELINE.executeNext();
244 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
248 void wireConnections()
250 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
252 Simulation.TIMELINE.reset();
254 WireArray a = new WireArray(1, 2);
255 WireArray b = new WireArray(1, 2);
256 WireArray c = new WireArray(1, 2);
257 WireArrayInput aI = a.createInput();
258 WireArrayInput bI = b.createInput();
259 WireArrayInput cI = c.createInput();
261 TestBitDisplay test = new TestBitDisplay(c);
262 LongConsumer print = time -> System.out.format("Time %2d\n %s\n %s\n %s\n", time, a, b, c);
264 cI.feedSignals(Bit.ONE);
265 test.assertAfterSimulationIs(print, Bit.ONE);
267 cI.feedSignals(Bit.X);
268 test.assertAfterSimulationIs(print, Bit.X);
270 cI.feedSignals(Bit.X);
271 cI.feedSignals(Bit.Z);
272 test.assertAfterSimulationIs(print, Bit.Z);
274 Connector c1 = new Connector(b, c);
275 test.assertAfterSimulationIs(print, Bit.Z);
276 System.out.println("ONE");
277 bI.feedSignals(Bit.ONE);
278 test.assertAfterSimulationIs(print, Bit.ONE);
279 System.out.println("ZERO");
280 bI.feedSignals(Bit.ZERO);
281 test.assertAfterSimulationIs(print, Bit.ZERO);
282 System.out.println("Z");
283 bI.feedSignals(Bit.Z);
284 test.assertAfterSimulationIs(Bit.Z);
287 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
289 assertArrayEquals(expected, actual);