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