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