1 package era.mi.logic.tests;
\r
3 import static org.junit.jupiter.api.Assertions.*;
\r
5 import java.util.function.LongConsumer;
\r
7 import org.junit.jupiter.api.Test;
\r
9 import era.mi.logic.Simulation;
\r
10 import era.mi.logic.components.Connector;
\r
11 import era.mi.logic.components.Demux;
\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.components.gates.XorGate;
\r
20 import era.mi.logic.types.Bit;
\r
21 import era.mi.logic.types.BitVector;
\r
22 import era.mi.logic.wires.Wire;
\r
23 import era.mi.logic.wires.Wire.WireEnd;
\r
25 @SuppressWarnings("unused")
\r
30 void circuitExampleTest()
\r
32 Simulation.TIMELINE.reset();
\r
33 Wire a = new Wire(1, 1), b = new Wire(1, 1), c = new Wire(1, 10), d = new Wire(2, 1), e = new Wire(1, 1), f = new Wire(1, 1),
\r
34 g = new Wire(1, 1), h = new Wire(2, 1), i = new Wire(2, 1), j = new Wire(1, 1), k = new Wire(1, 1);
\r
35 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
\r
36 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
\r
37 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
\r
38 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
\r
39 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
\r
41 a.createEnd().feedSignals(Bit.ZERO);
\r
42 b.createEnd().feedSignals(Bit.ONE);
\r
43 c.createEnd().feedSignals(Bit.ZERO);
\r
44 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
\r
45 e.createEnd().feedSignals(Bit.ZERO);
\r
47 Simulation.TIMELINE.executeAll();
\r
49 assertEquals(Bit.ONE, j.getValue());
\r
50 assertEquals(Bit.ZERO, k.getValue());
\r
56 Simulation.TIMELINE.reset();
\r
57 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
\r
58 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
59 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
\r
61 Simulation.TIMELINE.executeAll();
\r
63 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
64 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
\r
65 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
\r
71 Simulation.TIMELINE.reset();
\r
72 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
\r
73 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
74 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
\r
75 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
77 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
\r
79 Simulation.TIMELINE.executeAll();
\r
81 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
85 void triStateBufferTest()
\r
87 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
\r
88 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
\r
89 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
\r
90 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
\r
92 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
\r
93 enI.feedSignals(Bit.ONE);
\r
94 aI.feedSignals(Bit.ONE);
\r
95 bI.feedSignals(Bit.Z);
\r
97 Simulation.TIMELINE.executeAll();
\r
99 assertEquals(Bit.ONE, b.getValue());
\r
101 bI.feedSignals(Bit.ZERO);
\r
103 Simulation.TIMELINE.executeAll();
\r
105 assertEquals(Bit.X, b.getValue());
\r
106 assertEquals(Bit.ONE, a.getValue());
\r
109 enI.feedSignals(Bit.ZERO);
\r
111 Simulation.TIMELINE.executeAll();
\r
113 assertEquals(Bit.ZERO, a.getValue());
\r
120 Simulation.TIMELINE.reset();
\r
121 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), out = new Wire(4, 1);
\r
122 WireEnd selectIn = select.createEnd();
\r
124 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
125 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
126 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
128 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
\r
129 Simulation.TIMELINE.executeAll();
\r
131 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
132 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
133 Simulation.TIMELINE.executeAll();
\r
135 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
137 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
138 Simulation.TIMELINE.executeAll();
\r
140 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
147 Simulation.TIMELINE.reset();
\r
148 Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), in = new Wire(4, 1);
\r
149 WireEnd selectIn = select.createEnd();
\r
151 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
\r
152 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
154 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
\r
155 Simulation.TIMELINE.executeAll();
\r
157 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
158 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
159 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
160 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
\r
161 Simulation.TIMELINE.executeAll();
\r
163 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
164 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
165 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
\r
167 selectIn.feedSignals(Bit.ONE, Bit.ONE);
\r
168 Simulation.TIMELINE.executeAll();
\r
170 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
171 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
\r
172 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
\r
179 Simulation.TIMELINE.reset();
\r
180 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
\r
181 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
\r
182 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
183 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
185 Simulation.TIMELINE.executeAll();
\r
187 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
193 Simulation.TIMELINE.reset();
\r
194 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
\r
195 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
\r
196 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
197 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
199 Simulation.TIMELINE.executeAll();
\r
201 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
\r
207 Simulation.TIMELINE.reset();
\r
208 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
\r
209 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
\r
210 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
\r
211 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
212 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
\r
214 Simulation.TIMELINE.executeAll();
\r
216 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
\r
222 Simulation.TIMELINE.reset();
\r
223 Wire a = new Wire(3, 1), b = new Wire(3, 2);
\r
224 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
\r
225 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
\r
227 Simulation.TIMELINE.executeAll();
\r
229 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
\r
233 void rsLatchCircuitTest()
\r
235 Simulation.TIMELINE.reset();
\r
236 Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1), q = new Wire(1, 1), nq = new Wire(1, 1);
\r
238 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
\r
239 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
\r
240 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
\r
241 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
\r
243 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
\r
245 sIn.feedSignals(Bit.ONE);
\r
246 rIn.feedSignals(Bit.ZERO);
\r
248 Simulation.TIMELINE.executeAll();
\r
250 assertEquals(Bit.ONE, q.getValue());
\r
251 assertEquals(Bit.ZERO, nq.getValue());
\r
253 sIn.feedSignals(Bit.ZERO);
\r
255 Simulation.TIMELINE.executeAll();
\r
256 assertEquals(Bit.ONE, q.getValue());
\r
257 assertEquals(Bit.ZERO, nq.getValue());
\r
259 rIn.feedSignals(Bit.ONE);
\r
261 Simulation.TIMELINE.executeAll();
\r
263 assertEquals(Bit.ZERO, q.getValue());
\r
264 assertEquals(Bit.ONE, nq.getValue());
\r
268 void numericValueTest()
\r
270 Simulation.TIMELINE.reset();
\r
272 Wire a = new Wire(4, 1);
\r
273 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
\r
275 Simulation.TIMELINE.executeAll();
\r
277 assertEquals(15, a.getUnsignedValue());
\r
278 assertEquals(-1, a.getSignedValue());
\r
282 void multipleInputs()
\r
284 Simulation.TIMELINE.reset();
\r
285 Wire w = new Wire(2, 1);
\r
286 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
\r
287 wI1.feedSignals(Bit.ONE, Bit.Z);
\r
288 wI2.feedSignals(Bit.Z, Bit.X);
\r
289 Simulation.TIMELINE.executeAll();
\r
290 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
\r
292 wI2.feedSignals(Bit.ZERO, Bit.Z);
\r
293 Simulation.TIMELINE.executeAll();
\r
294 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
\r
296 wI2.feedSignals(Bit.Z, Bit.Z);
\r
297 Simulation.TIMELINE.executeAll();
\r
298 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
300 wI2.feedSignals(Bit.ONE, Bit.Z);
\r
301 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
\r
302 Simulation.TIMELINE.executeAll();
\r
303 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
\r
307 void wireConnections()
\r
309 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
\r
311 Simulation.TIMELINE.reset();
\r
313 Wire a = new Wire(1, 2);
\r
314 Wire b = new Wire(1, 2);
\r
315 Wire c = new Wire(1, 2);
\r
316 WireEnd aI = a.createEnd();
\r
317 WireEnd bI = b.createEnd();
\r
318 WireEnd cI = c.createEnd();
\r
320 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
\r
321 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
\r
322 LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c);
\r
324 cI.feedSignals(Bit.ONE);
\r
325 test.assertAfterSimulationIs(print, Bit.ONE);
\r
327 cI.feedSignals(Bit.X);
\r
328 test.assertAfterSimulationIs(print, Bit.X);
\r
330 cI.feedSignals(Bit.X);
\r
331 cI.feedSignals(Bit.Z);
\r
332 test.assertAfterSimulationIs(print, Bit.Z);
\r
334 new Connector(b.createEnd(), c.createEnd()).connect();
\r
335 test.assertAfterSimulationIs(print, Bit.Z);
\r
336 System.err.println("ONE");
\r
337 bI.feedSignals(Bit.ONE);
\r
338 test.assertAfterSimulationIs(print, Bit.ONE);
\r
339 System.err.println("ZERO");
\r
340 bI.feedSignals(Bit.ZERO);
\r
341 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
342 System.err.println("Z");
\r
343 bI.feedSignals(Bit.Z);
\r
344 test.assertAfterSimulationIs(print, Bit.Z);
\r
346 new Connector(a.createEnd(), b.createEnd()).connect();
\r
347 System.err.println("Z 2");
\r
348 aI.feedSignals(Bit.Z);
\r
349 test.assertAfterSimulationIs(print, Bit.Z);
\r
350 test2.assertAfterSimulationIs(Bit.Z);
\r
351 System.err.println("ONE 2");
\r
352 aI.feedSignals(Bit.ONE);
\r
353 test.assertAfterSimulationIs(print, Bit.ONE);
\r
354 test2.assertAfterSimulationIs(Bit.ONE);
\r
355 System.err.println("ZERO 2");
\r
356 aI.feedSignals(Bit.ZERO);
\r
357 test.assertAfterSimulationIs(print, Bit.ZERO);
\r
358 test2.assertAfterSimulationIs(Bit.ZERO);
\r
359 System.err.println("Z 2 II");
\r
360 aI.feedSignals(Bit.Z);
\r
361 test.assertAfterSimulationIs(print, Bit.Z);
\r
362 test2.assertAfterSimulationIs(Bit.Z);
\r
364 System.err.println("No Conflict yet");
\r
365 bI.feedSignals(Bit.ONE);
\r
366 test.assertAfterSimulationIs(print, Bit.ONE);
\r
367 test2.assertAfterSimulationIs(Bit.ONE);
\r
368 aI.feedSignals(Bit.ONE);
\r
369 test.assertAfterSimulationIs(print, Bit.ONE);
\r
370 test2.assertAfterSimulationIs(Bit.ONE);
\r
371 System.err.println("Conflict");
\r
372 aI.feedSignals(Bit.ZERO);
\r
373 test.assertAfterSimulationIs(print, Bit.X);
\r
374 test2.assertAfterSimulationIs(Bit.X);
\r
375 aI.feedSignals(Bit.ONE);
\r
376 test.assertAfterSimulationIs(print, Bit.ONE);
\r
377 test2.assertAfterSimulationIs(Bit.ONE);
\r
380 private static void assertBitArrayEquals(BitVector actual, Bit... expected)
\r
382 assertArrayEquals(expected, actual.getBits());
\r