f6d801fd9dd5cb5a300b23e0b49468943250f4c1
[Mograsim.git] / era.mi / src / era / mi / logic / tests / ComponentTest.java
1 package era.mi.logic.tests;
2
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;
7
8 import java.util.Arrays;
9 import java.util.function.LongConsumer;
10
11 import org.junit.jupiter.api.Test;
12
13 import era.mi.logic.Bit;
14 import era.mi.logic.Simulation;
15 import era.mi.logic.components.Connector;
16 import era.mi.logic.components.Demux;
17 import era.mi.logic.components.Merger;
18 import era.mi.logic.components.Mux;
19 import era.mi.logic.components.Splitter;
20 import era.mi.logic.components.TriStateBuffer;
21 import era.mi.logic.components.gates.AndGate;
22 import era.mi.logic.components.gates.NotGate;
23 import era.mi.logic.components.gates.OrGate;
24 import era.mi.logic.components.gates.XorGate;
25 import era.mi.logic.wires.Wire;
26 import era.mi.logic.wires.Wire.WireEnd;
27
28 @SuppressWarnings("unused")
29 class ComponentTest
30 {
31
32         @Test
33         void circuitExampleTest()
34         {
35                 Simulation.TIMELINE.reset();
36                 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),
37                                 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);
38                 new AndGate(1, f.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
39                 new NotGate(1, f.createReadOnlyEnd(), g.createEnd());
40                 new Merger(h.createEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd());
41                 new Mux(1, i.createEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd());
42                 new Splitter(i.createReadOnlyEnd(), k.createEnd(), j.createEnd());
43
44                 a.createEnd().feedSignals(Bit.ZERO);
45                 b.createEnd().feedSignals(Bit.ONE);
46                 c.createEnd().feedSignals(Bit.ZERO);
47                 d.createEnd().feedSignals(Bit.ONE, Bit.ONE);
48                 e.createEnd().feedSignals(Bit.ZERO);
49
50                 Simulation.TIMELINE.executeAll();
51
52                 assertEquals(Bit.ONE, j.getValue());
53                 assertEquals(Bit.ZERO, k.getValue());
54         }
55
56         @Test
57         void splitterTest()
58         {
59                 Simulation.TIMELINE.reset();
60                 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), in = new Wire(8, 1);
61                 in.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
62                 new Splitter(in.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
63
64                 Simulation.TIMELINE.executeAll();
65
66                 assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
67                 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
68                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
69         }
70
71         @Test
72         void mergerTest()
73         {
74                 Simulation.TIMELINE.reset();
75                 Wire a = new Wire(3, 1), b = new Wire(2, 1), c = new Wire(3, 1), out = new Wire(8, 1);
76                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
77                 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO);
78                 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
79
80                 new Merger(out.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
81
82                 Simulation.TIMELINE.executeAll();
83
84                 assertTrue(
85                                 Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
86         }
87
88         @Test
89         void triStateBufferTest()
90         {
91                 Wire a = new Wire(1, 1), b = new Wire(1, 1), en = new Wire(1, 1), notEn = new Wire(1, 1);
92                 new NotGate(1, en.createReadOnlyEnd(), notEn.createEnd());
93                 new TriStateBuffer(1, a.createReadOnlyEnd(), b.createEnd(), en.createReadOnlyEnd());
94                 new TriStateBuffer(1, b.createReadOnlyEnd(), a.createEnd(), notEn.createReadOnlyEnd());
95
96                 WireEnd enI = en.createEnd(), aI = a.createEnd(), bI = b.createEnd();
97                 enI.feedSignals(Bit.ONE);
98                 aI.feedSignals(Bit.ONE);
99                 bI.feedSignals(Bit.Z);
100
101                 Simulation.TIMELINE.executeAll();
102
103                 assertEquals(Bit.ONE, b.getValue());
104
105                 bI.feedSignals(Bit.ZERO);
106
107                 Simulation.TIMELINE.executeAll();
108
109                 assertEquals(Bit.X, b.getValue());
110                 assertEquals(Bit.ONE, a.getValue());
111
112                 aI.clearSignals();
113                 enI.feedSignals(Bit.ZERO);
114
115                 Simulation.TIMELINE.executeAll();
116
117                 assertEquals(Bit.ZERO, a.getValue());
118
119         }
120
121         @Test
122         void muxTest()
123         {
124                 Simulation.TIMELINE.reset();
125                 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);
126                 WireEnd selectIn = select.createEnd();
127
128                 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
129                 a.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
130                 c.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
131
132                 new Mux(1, out.createEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
133                 Simulation.TIMELINE.executeAll();
134
135                 assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
136                 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
137                 Simulation.TIMELINE.executeAll();
138
139                 assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
140
141                 selectIn.feedSignals(Bit.ONE, Bit.ONE);
142                 Simulation.TIMELINE.executeAll();
143
144                 assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
145
146         }
147
148         @Test
149         void demuxTest()
150         {
151                 Simulation.TIMELINE.reset();
152                 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);
153                 WireEnd selectIn = select.createEnd();
154
155                 selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
156                 in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
157
158                 new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
159                 Simulation.TIMELINE.executeAll();
160
161                 assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
162                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
163                 assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
164                 selectIn.feedSignals(Bit.ZERO, Bit.ONE);
165                 Simulation.TIMELINE.executeAll();
166
167                 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
168                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
169                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
170
171                 selectIn.feedSignals(Bit.ONE, Bit.ONE);
172                 Simulation.TIMELINE.executeAll();
173
174                 assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
175                 assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
176                 assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
177
178         }
179
180         @Test
181         void andTest()
182         {
183                 Simulation.TIMELINE.reset();
184                 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
185                 new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
186                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
187                 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
188
189                 Simulation.TIMELINE.executeAll();
190
191                 assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
192         }
193
194         @Test
195         void orTest()
196         {
197                 Simulation.TIMELINE.reset();
198                 Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
199                 new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
200                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
201                 b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
202
203                 Simulation.TIMELINE.executeAll();
204
205                 assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
206         }
207
208         @Test
209         void xorTest()
210         {
211                 Simulation.TIMELINE.reset();
212                 Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
213                 new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
214                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
215                 b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
216                 c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
217
218                 Simulation.TIMELINE.executeAll();
219
220                 assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
221         }
222
223         @Test
224         void notTest()
225         {
226                 Simulation.TIMELINE.reset();
227                 Wire a = new Wire(3, 1), b = new Wire(3, 2);
228                 new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
229                 a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
230
231                 Simulation.TIMELINE.executeAll();
232
233                 assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
234         }
235
236         @Test
237         void rsLatchCircuitTest()
238         {
239                 Simulation.TIMELINE.reset();
240                 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);
241
242                 new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
243                 new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
244                 new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
245                 new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
246
247                 WireEnd sIn = s.createEnd(), rIn = r.createEnd();
248
249                 sIn.feedSignals(Bit.ONE);
250                 rIn.feedSignals(Bit.ZERO);
251
252                 Simulation.TIMELINE.executeAll();
253
254                 assertEquals(Bit.ONE, q.getValue());
255                 assertEquals(Bit.ZERO, nq.getValue());
256
257                 sIn.feedSignals(Bit.ZERO);
258
259                 Simulation.TIMELINE.executeAll();
260                 assertEquals(Bit.ONE, q.getValue());
261                 assertEquals(Bit.ZERO, nq.getValue());
262
263                 rIn.feedSignals(Bit.ONE);
264
265                 Simulation.TIMELINE.executeAll();
266
267                 assertEquals(Bit.ZERO, q.getValue());
268                 assertEquals(Bit.ONE, nq.getValue());
269         }
270
271         @Test
272         void numericValueTest()
273         {
274                 Simulation.TIMELINE.reset();
275
276                 Wire a = new Wire(4, 1);
277                 a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
278
279                 Simulation.TIMELINE.executeAll();
280
281                 assertEquals(15, a.getUnsignedValue());
282                 assertEquals(-1, a.getSignedValue());
283         }
284
285         @Test
286         void multipleInputs()
287         {
288                 Simulation.TIMELINE.reset();
289                 Wire w = new Wire(2, 1);
290                 WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
291                 wI1.feedSignals(Bit.ONE, Bit.Z);
292                 wI2.feedSignals(Bit.Z, Bit.X);
293                 Simulation.TIMELINE.executeAll();
294                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
295
296                 wI2.feedSignals(Bit.ZERO, Bit.Z);
297                 Simulation.TIMELINE.executeAll();
298                 assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
299
300                 wI2.feedSignals(Bit.Z, Bit.Z);
301                 Simulation.TIMELINE.executeAll();
302                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
303
304                 wI2.feedSignals(Bit.ONE, Bit.Z);
305                 w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
306                 Simulation.TIMELINE.executeAll();
307                 assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
308         }
309
310         @Test
311         void wireConnections()
312         {
313                 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
314
315                 Simulation.TIMELINE.reset();
316
317                 Wire a = new Wire(1, 2);
318                 Wire b = new Wire(1, 2);
319                 Wire c = new Wire(1, 2);
320                 WireEnd aI = a.createEnd();
321                 WireEnd bI = b.createEnd();
322                 WireEnd cI = c.createEnd();
323
324                 TestBitDisplay test = new TestBitDisplay(c.createReadOnlyEnd());
325                 TestBitDisplay test2 = new TestBitDisplay(a.createReadOnlyEnd());
326                 LongConsumer print = time -> System.out.format("Time %2d\n   a: %s\n   b: %s\n   c: %s\n", time, a, b, c);
327
328                 cI.feedSignals(Bit.ONE);
329                 test.assertAfterSimulationIs(print, Bit.ONE);
330
331                 cI.feedSignals(Bit.X);
332                 test.assertAfterSimulationIs(print, Bit.X);
333
334                 cI.feedSignals(Bit.X);
335                 cI.feedSignals(Bit.Z);
336                 test.assertAfterSimulationIs(print, Bit.Z);
337
338                 new Connector(b.createEnd(), c.createEnd()).connect();
339                 test.assertAfterSimulationIs(print, Bit.Z);
340                 System.err.println("ONE");
341                 bI.feedSignals(Bit.ONE);
342                 test.assertAfterSimulationIs(print, Bit.ONE);
343                 System.err.println("ZERO");
344                 bI.feedSignals(Bit.ZERO);
345                 test.assertAfterSimulationIs(print, Bit.ZERO);
346                 System.err.println("Z");
347                 bI.feedSignals(Bit.Z);
348                 test.assertAfterSimulationIs(print, Bit.Z);
349
350                 new Connector(a.createEnd(), b.createEnd()).connect();
351                 System.err.println("Z 2");
352                 aI.feedSignals(Bit.Z);
353                 test.assertAfterSimulationIs(print, Bit.Z);
354                 test2.assertAfterSimulationIs(Bit.Z);
355                 System.err.println("ONE 2");
356                 aI.feedSignals(Bit.ONE);
357                 test.assertAfterSimulationIs(print, Bit.ONE);
358                 test2.assertAfterSimulationIs(Bit.ONE);
359                 System.err.println("ZERO 2");
360                 aI.feedSignals(Bit.ZERO);
361                 test.assertAfterSimulationIs(print, Bit.ZERO);
362                 test2.assertAfterSimulationIs(Bit.ZERO);
363                 System.err.println("Z 2 II");
364                 aI.feedSignals(Bit.Z);
365                 test.assertAfterSimulationIs(print, Bit.Z);
366                 test2.assertAfterSimulationIs(Bit.Z);
367
368                 System.err.println("No Conflict yet");
369                 bI.feedSignals(Bit.ONE);
370                 test.assertAfterSimulationIs(print, Bit.ONE);
371                 test2.assertAfterSimulationIs(Bit.ONE);
372                 aI.feedSignals(Bit.ONE);
373                 test.assertAfterSimulationIs(print, Bit.ONE);
374                 test2.assertAfterSimulationIs(Bit.ONE);
375                 System.err.println("Conflict");
376                 aI.feedSignals(Bit.ZERO);
377                 test.assertAfterSimulationIs(print, Bit.X);
378                 test2.assertAfterSimulationIs(Bit.X);
379                 aI.feedSignals(Bit.ONE);
380                 test.assertAfterSimulationIs(print, Bit.ONE);
381                 test2.assertAfterSimulationIs(Bit.ONE);
382         }
383
384         private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
385         {
386                 assertArrayEquals(expected, actual);
387         }
388 }