1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4 import static org.junit.jupiter.api.Assertions.assertEquals;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6 import static org.junit.jupiter.api.Assertions.fail;
8 import java.util.Arrays;
10 import org.junit.jupiter.api.Test;
12 import era.mi.logic.Bit;
13 import era.mi.logic.Simulation;
14 import era.mi.logic.components.Merger;
15 import era.mi.logic.components.Mux;
16 import era.mi.logic.components.Splitter;
17 import era.mi.logic.components.TriStateBuffer;
18 import era.mi.logic.components.gates.AndGate;
19 import era.mi.logic.components.gates.NotGate;
20 import era.mi.logic.components.gates.OrGate;
21 import era.mi.logic.wires.WireArray;
22 import era.mi.logic.wires.WireArray.WireArrayInput;
28 void circuitExampleTest()
30 Simulation.TIMELINE.reset();
31 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),
32 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);
33 new AndGate(1, a, b, f);
36 new Mux(1, i, e, h, d);
37 new Splitter(i, k, j);
39 a.createInput().feedSignals(Bit.ZERO);
40 b.createInput().feedSignals(Bit.ONE);
41 c.createInput().feedSignals(Bit.ZERO);
42 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
43 e.createInput().feedSignals(Bit.ZERO);
45 Simulation.TIMELINE.executeAll();
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 Simulation.TIMELINE.executeAll();
61 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues());
62 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues());
63 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues());
69 Simulation.TIMELINE.reset();
70 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
71 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
72 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
73 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
75 new Merger(out, a, b, c);
77 Simulation.TIMELINE.executeAll();
79 assertTrue(Arrays.equals(out.getValues(),
80 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
84 void triStateBufferTest()
86 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
87 notEn = new WireArray(1, 1);
88 new NotGate(1, en, notEn);
89 new TriStateBuffer(1, a, b, en);
90 new TriStateBuffer(1, b, a, notEn);
92 WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
93 enI.feedSignals(Bit.ONE);
94 aI.feedSignals(Bit.ONE);
96 Simulation.TIMELINE.executeAll();
98 assertEquals(Bit.ONE, b.getValue());
100 bI.feedSignals(Bit.ZERO);
102 Simulation.TIMELINE.executeAll();
104 assertEquals(Bit.X, b.getValue());
105 assertEquals(Bit.ONE, a.getValue());
108 enI.feedSignals(Bit.ZERO);
110 Simulation.TIMELINE.executeAll();
112 assertEquals(Bit.ZERO, a.getValue());
119 Simulation.TIMELINE.reset();
120 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
121 select = new WireArray(2, 5), out = new WireArray(4, 1);
122 WireArrayInput selectIn = select.createInput();
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
128 new Mux(1, out, select, a, b, c);
129 Simulation.TIMELINE.executeAll();
131 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues());
132 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
133 Simulation.TIMELINE.executeAll();
135 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues());
137 selectIn.feedSignals(Bit.ONE, Bit.ONE);
138 Simulation.TIMELINE.executeAll();
140 assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues());
147 Simulation.TIMELINE.reset();
148 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
149 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
150 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
152 Simulation.TIMELINE.executeAll();
153 assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues());
159 Simulation.TIMELINE.reset();
160 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
161 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
162 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
164 Simulation.TIMELINE.executeAll();
166 assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues());
170 void rsLatchCircuitTest()
172 Simulation.TIMELINE.reset();
173 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
174 q = new WireArray(1, 1), nq = new WireArray(1, 1);
176 new OrGate(1, r, nq, t2);
177 new OrGate(1, s, q, t1);
178 new NotGate(1, t2, q);
179 new NotGate(1, t1, nq);
181 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
183 sIn.feedSignals(Bit.ONE);
184 rIn.feedSignals(Bit.ZERO);
186 Simulation.TIMELINE.executeAll();
188 assertEquals(Bit.ONE, q.getValue());
189 assertEquals(Bit.ZERO, nq.getValue());
191 sIn.feedSignals(Bit.ZERO);
193 Simulation.TIMELINE.executeAll();
194 assertEquals(Bit.ONE, q.getValue());
195 assertEquals(Bit.ZERO, nq.getValue());
197 rIn.feedSignals(Bit.ONE);
199 Simulation.TIMELINE.executeAll();
201 assertEquals(Bit.ZERO, q.getValue());
202 assertEquals(Bit.ONE, nq.getValue());
206 void numericValueTest()
208 Simulation.TIMELINE.reset();
210 WireArray a = new WireArray(4, 1);
211 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
213 Simulation.TIMELINE.executeAll();
215 assertEquals(15, a.getUnsignedValue());
216 assertEquals(-1, a.getSignedValue());
220 void multipleInputs()
222 Simulation.TIMELINE.reset();
223 WireArray w = new WireArray(2, 1);
224 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
225 wI1.feedSignals(Bit.ONE, Bit.Z);
226 wI2.feedSignals(Bit.Z, Bit.X);
227 Simulation.TIMELINE.executeAll();
228 assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues());
230 wI2.feedSignals(Bit.ZERO, Bit.Z);
231 Simulation.TIMELINE.executeAll();
232 assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues());
234 wI2.feedSignals(Bit.Z, Bit.Z);
235 Simulation.TIMELINE.executeAll();
236 assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
238 wI2.feedSignals(Bit.ONE, Bit.Z);
239 w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
240 Simulation.TIMELINE.executeAll();
241 assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
245 // void wireConnections()
247 // // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
249 // Simulation.TIMELINE.reset();
251 // WireArray a = new WireArray(1, 2);
252 // WireArray b = new WireArray(1, 2);
253 // WireArray c = new WireArray(1, 2);
254 // WireArrayInput aI = a.createInput();
255 // WireArrayInput bI = b.createInput();
256 // WireArrayInput cI = c.createInput();
258 // TestBitDisplay test = new TestBitDisplay(c);
259 // LongConsumer print = time -> System.out.format("Time %2d\n %s\n %s\n %s\n", time, a, b, c);
261 // cI.feedSignals(Bit.ONE);
262 // test.assertAfterSimulationIs(print, Bit.ONE);
264 // cI.feedSignals(Bit.X);
265 // test.assertAfterSimulationIs(print, Bit.X);
267 // cI.feedSignals(Bit.X);
268 // cI.feedSignals(Bit.Z);
269 // test.assertAfterSimulationIs(print, Bit.Z);
271 // Connector c1 = new Connector(b, c);
272 // test.assertAfterSimulationIs(print, Bit.Z);
273 // System.out.println("ONE");
274 // bI.feedSignals(Bit.ONE);
275 // test.assertAfterSimulationIs(print, Bit.ONE);
276 // System.out.println("ZERO");
277 // bI.feedSignals(Bit.ZERO);
278 // test.assertAfterSimulationIs(print, Bit.ZERO);
279 // System.out.println("Z");
280 // bI.feedSignals(Bit.Z);
281 // test.assertAfterSimulationIs(Bit.Z);
284 private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
286 assertArrayEquals(expected, actual);