1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.*;
5 import java.util.Arrays;
7 import org.junit.jupiter.api.Test;
9 import era.mi.logic.Bit;
10 import era.mi.logic.Simulation;
11 import era.mi.logic.components.Merger;
12 import era.mi.logic.components.Mux;
13 import era.mi.logic.components.Splitter;
14 import era.mi.logic.components.TriStateBuffer;
15 import era.mi.logic.components.gates.AndGate;
16 import era.mi.logic.components.gates.NotGate;
17 import era.mi.logic.components.gates.OrGate;
18 import era.mi.logic.wires.WireArray;
19 import era.mi.logic.wires.WireArray.WireArrayInput;
25 void circuitExampleTest()
27 Simulation.TIMELINE.reset();
28 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),
29 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);
30 new AndGate(1, a, b, f);
33 new Mux(1, i, e, h, d);
34 new Splitter(i, k, j);
36 a.createInput().feedSignals(Bit.ZERO);
37 b.createInput().feedSignals(Bit.ONE);
38 c.createInput().feedSignals(Bit.ZERO);
39 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
40 e.createInput().feedSignals(Bit.ZERO);
42 while(Simulation.TIMELINE.hasNext())
44 Simulation.TIMELINE.executeNext();
47 assertEquals(Bit.ONE, j.getValue());
48 assertEquals(Bit.ZERO, k.getValue());
54 Simulation.TIMELINE.reset();
55 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
56 in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
57 new Splitter(in, a, b, c);
59 while (Simulation.TIMELINE.hasNext())
61 Simulation.TIMELINE.executeNext();
64 assertTrue(Arrays.equals(a.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }));
65 assertTrue(Arrays.equals(b.getValues(), new Bit[] { Bit.ONE, Bit.ZERO }));
66 assertTrue(Arrays.equals(c.getValues(), new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }));
72 Simulation.TIMELINE.reset();
73 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
74 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
75 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
76 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
78 new Merger(out, a, b, c);
80 while (Simulation.TIMELINE.hasNext())
82 Simulation.TIMELINE.executeNext();
85 assertTrue(Arrays.equals(out.getValues(),
86 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
90 void triStateBufferTest()
92 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
93 notEn = new WireArray(1, 1);
94 new NotGate(1, en, notEn);
95 new TriStateBuffer(1, a, b, en);
96 new TriStateBuffer(1, b, a, notEn);
98 WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
99 enI.feedSignals(Bit.ONE);
100 aI.feedSignals(Bit.ONE);
102 while (Simulation.TIMELINE.hasNext())
104 Simulation.TIMELINE.executeNext();
107 assertEquals(Bit.ONE, b.getValue());
109 bI.feedSignals(Bit.ZERO);
111 while (Simulation.TIMELINE.hasNext())
113 Simulation.TIMELINE.executeNext();
116 assertEquals(Bit.X, b.getValue());
117 assertEquals(Bit.ONE, a.getValue());
120 enI.feedSignals(Bit.ZERO);
122 while (Simulation.TIMELINE.hasNext())
124 Simulation.TIMELINE.executeNext();
127 assertEquals(Bit.ZERO, a.getValue());
134 Simulation.TIMELINE.reset();
135 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
136 select = new WireArray(2, 5), out = new WireArray(4, 1);
137 WireArrayInput selectIn = select.createInput();
139 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
140 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
141 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
143 new Mux(1, out, select, a, b, c);
144 while (Simulation.TIMELINE.hasNext())
146 Simulation.TIMELINE.executeNext();
149 assertTrue(Arrays.equals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues()));
150 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
151 while (Simulation.TIMELINE.hasNext())
153 Simulation.TIMELINE.executeNext();
156 assertTrue(Arrays.equals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues()));
158 selectIn.feedSignals(Bit.ONE, Bit.ONE);
159 while (Simulation.TIMELINE.hasNext())
161 Simulation.TIMELINE.executeNext();
164 assertTrue(Arrays.equals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues()));
171 Simulation.TIMELINE.reset();
172 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
173 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
174 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
176 while (Simulation.TIMELINE.hasNext())
178 Simulation.TIMELINE.executeNext();
180 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
186 Simulation.TIMELINE.reset();
187 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
188 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
189 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
191 while (Simulation.TIMELINE.hasNext())
193 Simulation.TIMELINE.executeNext();
196 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
200 void rsLatchCircuitTest()
202 Simulation.TIMELINE.reset();
203 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
204 q = new WireArray(1, 1), nq = new WireArray(1, 1);
206 new OrGate(1, r, nq, t2);
207 new OrGate(1, s, q, t1);
208 new NotGate(1, t2, q);
209 new NotGate(1, t1, nq);
211 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
213 sIn.feedSignals(Bit.ONE);
214 rIn.feedSignals(Bit.ZERO);
216 while (Simulation.TIMELINE.hasNext())
218 Simulation.TIMELINE.executeNext();
221 assertEquals(q.getValue(), Bit.ONE);
222 assertEquals(nq.getValue(), Bit.ZERO);
224 sIn.feedSignals(Bit.ZERO);
226 while (Simulation.TIMELINE.hasNext())
228 Simulation.TIMELINE.executeNext();
231 assertEquals(q.getValue(), Bit.ONE);
232 assertEquals(nq.getValue(), Bit.ZERO);
234 rIn.feedSignals(Bit.ONE);
236 while (Simulation.TIMELINE.hasNext())
238 Simulation.TIMELINE.executeNext();
241 assertEquals(q.getValue(), Bit.ZERO);
242 assertEquals(nq.getValue(), Bit.ONE);
246 void numericValueTest()
248 Simulation.TIMELINE.reset();
250 WireArray a = new WireArray(4, 1);
251 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
253 while (Simulation.TIMELINE.hasNext())
255 Simulation.TIMELINE.executeNext();
258 assertEquals(a.getUnsignedValue(), 15);
259 assertEquals(a.getSignedValue(), -1);
263 void multipleInputs()
265 Simulation.TIMELINE.reset();
266 WireArray w = new WireArray(2, 1);
267 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
268 wI1.feedSignals(Bit.ONE, Bit.Z);
269 wI2.feedSignals(Bit.Z, Bit.X);
270 while (Simulation.TIMELINE.hasNext())
272 Simulation.TIMELINE.executeNext();
274 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.X }));
276 wI2.feedSignals(Bit.ZERO, Bit.Z);
277 while (Simulation.TIMELINE.hasNext())
279 Simulation.TIMELINE.executeNext();
281 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.X, Bit.Z }));
283 wI2.feedSignals(Bit.Z, Bit.Z);
284 while (Simulation.TIMELINE.hasNext())
286 Simulation.TIMELINE.executeNext();
288 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
290 wI2.feedSignals(Bit.ONE, Bit.Z);
291 w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
292 while (Simulation.TIMELINE.hasNext())
294 Simulation.TIMELINE.executeNext();
296 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
300 // void wireConnections()
302 // // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
304 // Simulation.TIMELINE.reset();
306 // WireArray a = new WireArray(1, 2);
307 // WireArray b = new WireArray(1, 2);
308 // WireArray c = new WireArray(1, 2);
309 // WireArrayInput aI = a.createInput();
310 // WireArrayInput bI = b.createInput();
311 // WireArrayInput cI = c.createInput();
313 // TestBitDisplay test = new TestBitDisplay(c);
314 // LongConsumer print = time -> System.out.format("Time %2d\n %s\n %s\n %s\n", time, a, b, c);
316 // cI.feedSignals(Bit.ONE);
317 // test.assertAfterSimulationIs(print, Bit.ONE);
319 // cI.feedSignals(Bit.X);
320 // test.assertAfterSimulationIs(print, Bit.X);
322 // cI.feedSignals(Bit.X);
323 // cI.feedSignals(Bit.Z);
324 // test.assertAfterSimulationIs(print, Bit.Z);
326 // Connector c1 = new Connector(b, c);
327 // test.assertAfterSimulationIs(print, Bit.Z);
328 // System.out.println("ONE");
329 // bI.feedSignals(Bit.ONE);
330 // test.assertAfterSimulationIs(print, Bit.ONE);
331 // System.out.println("ZERO");
332 // bI.feedSignals(Bit.ZERO);
333 // test.assertAfterSimulationIs(print, Bit.ZERO);
334 // System.out.println("Z");
335 // bI.feedSignals(Bit.Z);
336 // test.assertAfterSimulationIs(Bit.Z);
339 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
341 assertArrayEquals(expected, actual);