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