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