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