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.Demux;
\r
13 import era.mi.logic.components.Merger;
\r
14 import era.mi.logic.components.Mux;
\r
15 import era.mi.logic.components.Splitter;
\r
16 import era.mi.logic.components.TriStateBuffer;
\r
17 import era.mi.logic.components.gates.AndGate;
\r
18 import era.mi.logic.components.gates.NotGate;
\r
19 import era.mi.logic.components.gates.OrGate;
\r
20 import era.mi.logic.components.gates.XorGate;
\r
21 import era.mi.logic.wires.WireArray;
\r
22 import era.mi.logic.wires.WireArray.WireArrayInput;
\r
24 class ComponentTest {
\r
27 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),
\r
30 e = new WireArray(1, 1), f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1),
\r
31 j = new WireArray(1, 1), k = new WireArray(1, 1);
\r
32 new AndGate(1, f, a, b);
\r
33 new NotGate(1, f, g);
\r
34 new Merger(h, c, g);
\r
35 new Mux(1, i, e, h, d);
\r
36 new Splitter(i, k, j);
\r
38 a.createInput().feedSignals(Bit.ZERO);
\r
39 b.createInput().feedSignals(Bit.ONE);
\r
40 c.createInput().feedSignals(Bit.ZERO);
\r
41 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
\r
42 e.createInput().feedSignals(Bit.ZERO);
\r
44 Simulation.TIMELINE.executeAll();
\r
46 assertEquals(Bit.ONE, j.getValue());
\r
47 assertEquals(Bit.ZERO, k.getValue());
\r
51 void splitterTest() {
\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
66 Simulation.TIMELINE.reset();
\r
67 WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
\r
68 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
69 b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
\r
70 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
72 new Merger(out, a, b, c);
\r
74 Simulation.TIMELINE.executeAll();
\r
77 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
\r
81 void triStateBufferTest() {
\r
82 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1), notEn = new WireArray(1, 1);
\r
83 new NotGate(1, en, notEn);
\r
84 new TriStateBuffer(1, a, b, en);
\r
85 new TriStateBuffer(1, b, a, notEn);
\r
87 WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
\r
88 enI.feedSignals(Bit.ONE);
\r
89 aI.feedSignals(Bit.ONE);
\r
91 Simulation.TIMELINE.executeAll();
\r
93 assertEquals(Bit.ONE, b.getValue());
\r
95 bI.feedSignals(Bit.ZERO);
\r
97 Simulation.TIMELINE.executeAll();
\r
99 assertEquals(Bit.X, b.getValue());
\r
100 assertEquals(Bit.ONE, a.getValue());
\r
103 enI.feedSignals(Bit.ZERO);
\r
105 Simulation.TIMELINE.executeAll();
\r
107 assertEquals(Bit.ZERO, a.getValue());
\r
113 Simulation.TIMELINE.reset();
\r
114 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
115 out = new WireArray(4, 1);
\r
116 WireArrayInput selectIn = select.createInput();
\r
118 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
119 a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
120 c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
122 new Mux(1, out, select, a, b, c);
\r
123 Simulation.TIMELINE.executeAll();
\r
125 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
126 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
127 Simulation.TIMELINE.executeAll();
\r
129 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
131 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
132 Simulation.TIMELINE.executeAll();
\r
134 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
140 Simulation.TIMELINE.reset();
\r
141 WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4), select = new WireArray(2, 5),
\r
142 in = new WireArray(4, 1);
\r
143 WireArrayInput selectIn = select.createInput();
\r
145 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
146 in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
148 new Demux(1, in, select, a, b, c);
\r
149 Simulation.TIMELINE.executeAll();
\r
151 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
152 assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
153 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
154 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
155 Simulation.TIMELINE.executeAll();
\r
157 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
158 assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
159 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
161 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
162 Simulation.TIMELINE.executeAll();
\r
164 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
165 assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
166 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
172 Simulation.TIMELINE.reset();
\r
173 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
174 new AndGate(1, c, a, b);
\r
175 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
176 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
178 Simulation.TIMELINE.executeAll();
\r
180 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
185 Simulation.TIMELINE.reset();
\r
186 WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
\r
187 new OrGate(1, c, a, b);
\r
188 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
189 b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
191 Simulation.TIMELINE.executeAll();
\r
193 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
198 Simulation.TIMELINE.reset();
\r
199 WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
\r
200 new XorGate(1, d, a, b, c);
\r
201 a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
\r
202 b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
203 c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
205 Simulation.TIMELINE.executeAll();
\r
207 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
\r
211 void rsLatchCircuitTest() {
\r
212 Simulation.TIMELINE.reset();
\r
213 WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
\r
214 q = new WireArray(1, 1), nq = new WireArray(1, 1);
\r
216 new OrGate(1, t2, r, nq);
\r
217 new OrGate(1, t1, s, q);
\r
218 new NotGate(1, t2, q);
\r
219 new NotGate(1, t1, nq);
\r
221 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
\r
223 sIn.feedSignals(Bit.ONE);
\r
224 rIn.feedSignals(Bit.ZERO);
\r
226 Simulation.TIMELINE.executeAll();
\r
228 assertEquals(Bit.ONE, q.getValue());
\r
229 assertEquals(Bit.ZERO, nq.getValue());
\r
231 sIn.feedSignals(Bit.ZERO);
\r
233 Simulation.TIMELINE.executeAll();
\r
234 assertEquals(Bit.ONE, q.getValue());
\r
235 assertEquals(Bit.ZERO, nq.getValue());
\r
237 rIn.feedSignals(Bit.ONE);
\r
239 Simulation.TIMELINE.executeAll();
\r
241 assertEquals(Bit.ZERO, q.getValue());
\r
242 assertEquals(Bit.ONE, nq.getValue());
\r
246 void numericValueTest() {
\r
247 Simulation.TIMELINE.reset();
\r
249 WireArray a = new WireArray(4, 1);
\r
250 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
\r
252 Simulation.TIMELINE.executeAll();
\r
254 assertEquals(15, a.getUnsignedValue());
\r
255 assertEquals(-1, a.getSignedValue());
\r
259 void multipleInputs() {
\r
260 Simulation.TIMELINE.reset();
\r
261 WireArray w = new WireArray(2, 1);
\r
262 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
\r
263 wI1.feedSignals(Bit.ONE, Bit.Z);
\r
264 wI2.feedSignals(Bit.Z, Bit.X);
\r
265 Simulation.TIMELINE.executeAll();
\r
266 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
\r
268 wI2.feedSignals(Bit.ZERO, Bit.Z);
\r
269 Simulation.TIMELINE.executeAll();
\r
270 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
\r
272 wI2.feedSignals(Bit.Z, Bit.Z);
\r
273 Simulation.TIMELINE.executeAll();
\r
274 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
276 wI2.feedSignals(Bit.ONE, Bit.Z);
\r
277 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
\r
278 Simulation.TIMELINE.executeAll();
\r
279 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
283 void wireConnections() {
\r
284 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
\r
286 Simulation.TIMELINE.reset();
\r
288 WireArray a = new WireArray(1, 2);
\r
289 WireArray b = new WireArray(1, 2);
\r
290 WireArray c = new WireArray(1, 2);
\r
291 WireArrayInput aI = a.createInput();
\r
292 WireArrayInput bI = b.createInput();
\r
293 WireArrayInput cI = c.createInput();
\r
295 TestBitDisplay test = new TestBitDisplay(c);
\r
296 TestBitDisplay test2 = new TestBitDisplay(a);
\r
297 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
\r
299 cI.feedSignals(Bit.ONE);
\r
300 test.assertAfterSimulationIs(print, Bit.ONE);
\r
302 cI.feedSignals(Bit.X);
\r
303 test.assertAfterSimulationIs(print, Bit.X);
\r
305 cI.feedSignals(Bit.X);
\r
306 cI.feedSignals(Bit.Z);
\r
307 test.assertAfterSimulationIs(print, Bit.Z);
\r
309 new Connector(b, c);
\r
310 test.assertAfterSimulationIs(print, Bit.Z);
\r
311 System.err.println("ONE");
\r
312 bI.feedSignals(Bit.ONE);
\r
313 test.assertAfterSimulationIs(print, Bit.ONE);
\r
314 System.err.println("ZERO");
\r
315 bI.feedSignals(Bit.ZERO);
\r
316 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
317 System.err.println("Z");
\r
318 bI.feedSignals(Bit.Z);
\r
319 test.assertAfterSimulationIs(print, Bit.Z);
\r
321 new Connector(a, b);
\r
322 System.err.println("Z 2");
\r
323 aI.feedSignals(Bit.Z);
\r
324 test.assertAfterSimulationIs(print, Bit.Z);
\r
325 test2.assertAfterSimulationIs(Bit.Z);
\r
326 System.err.println("ONE 2");
\r
327 aI.feedSignals(Bit.ONE);
\r
328 test.assertAfterSimulationIs(print, Bit.ONE);
\r
329 test2.assertAfterSimulationIs(Bit.ONE);
\r
330 System.err.println("ZERO 2");
\r
331 aI.feedSignals(Bit.ZERO);
\r
332 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
333 test2.assertAfterSimulationIs(Bit.ZERO);
\r
334 System.err.println("Z 2 II");
\r
335 aI.feedSignals(Bit.Z);
\r
336 test.assertAfterSimulationIs(print, Bit.Z);
\r
337 test2.assertAfterSimulationIs(Bit.Z);
\r
339 System.err.println("No Conflict yet");
\r
340 bI.feedSignals(Bit.ONE);
\r
341 test.assertAfterSimulationIs(print, Bit.ONE);
\r
342 test2.assertAfterSimulationIs(Bit.ONE);
\r
343 aI.feedSignals(Bit.ONE);
\r
344 test.assertAfterSimulationIs(print, Bit.ONE);
\r
345 test2.assertAfterSimulationIs(Bit.ONE);
\r
346 System.err.println("Conflict");
\r
347 aI.feedSignals(Bit.ZERO);
\r
348 test.assertAfterSimulationIs(print, Bit.X);
\r
349 test2.assertAfterSimulationIs(Bit.X);
\r
350 aI.feedSignals(Bit.ONE);
\r
351 test.assertAfterSimulationIs(print, Bit.ONE);
\r
352 test2.assertAfterSimulationIs(Bit.ONE);
\r
355 private static void assertBitArrayEquals(Bit[] actual, Bit... expected) {
\r
356 assertArrayEquals(expected, actual);
\r