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