d48c5c630c9421d1c9c5ec4261d8003f49182908
[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     
27         @Test
28         void circuitExampleTest()
29         {
30                 Simulation.TIMELINE.reset();
31                 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), c = new WireArray(1, 10), d = new WireArray(2, 1), e = new WireArray(1, 1),
32                                 f = new WireArray(1, 1), g = new WireArray(1, 1), h = new WireArray(2, 1), i = new WireArray(2, 1), j = new WireArray(1, 1), k = new WireArray(1, 1);
33                 new AndGate(1, f, a, b);
34                 new NotGate(1, f, g);
35                 new Merger(h, c, g);
36                 new Mux(1, i, e, h, d);
37                 new Splitter(i, k, j);
38                 
39                 a.createInput().feedSignals(Bit.ZERO);
40                 b.createInput().feedSignals(Bit.ONE);
41                 c.createInput().feedSignals(Bit.ZERO);
42                 d.createInput().feedSignals(Bit.ONE, Bit.ONE);
43                 e.createInput().feedSignals(Bit.ZERO);
44                 
45                 Simulation.TIMELINE.executeAll();
46                 
47                 assertEquals(Bit.ONE, j.getValue());
48                 assertEquals(Bit.ZERO, k.getValue());
49         }
50
51     @Test
52     void splitterTest()
53     {
54         Simulation.TIMELINE.reset();
55         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
56         in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
57         new Splitter(in, a, b, c);
58
59         Simulation.TIMELINE.executeAll();
60
61         assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
62         assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
63         assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
64     }
65
66     @Test
67     void mergerTest()
68     {
69         Simulation.TIMELINE.reset();
70         WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
71         a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
72         b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
73         c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
74
75         new Merger(out, a, b, c);
76
77         Simulation.TIMELINE.executeAll();
78
79         assertTrue(Arrays.equals(out.getValues(),
80                 new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
81     }
82
83     @Test
84     void triStateBufferTest()
85     {
86         WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), en = new WireArray(1, 1),
87                 notEn = new WireArray(1, 1);
88         new NotGate(1, en, notEn);
89         new TriStateBuffer(1, a, b, en);
90         new TriStateBuffer(1, b, a, notEn);
91
92         WireArrayInput enI = en.createInput(), aI = a.createInput(), bI = b.createInput();
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         WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
121                 select = new WireArray(2, 5), out = new WireArray(4, 1);
122         WireArrayInput selectIn = select.createInput();
123
124         selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
125         a.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
126         c.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
127
128         new Mux(1, out, select, a, b, c);
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         WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
149                 select = new WireArray(2, 5), in = new WireArray(4, 1);
150         WireArrayInput selectIn = select.createInput();
151
152         selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
153         in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
154
155         new Demux(1, in, select, a, b, c);
156         Simulation.TIMELINE.executeAll();
157
158         assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
159         assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
160         assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
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.Z, Bit.Z, Bit.Z, Bit.Z);
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.Z, Bit.Z, Bit.Z, Bit.Z);
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         WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
182         new AndGate(1, c, a, b);
183         a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
184         b.createInput().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         WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
196         new OrGate(1, c, a, b);
197         a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
198         b.createInput().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         WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
210         new XorGate(1, d, a, b, c);
211         a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
212         b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
213         c.createInput().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 rsLatchCircuitTest()
222     {
223         Simulation.TIMELINE.reset();
224         WireArray r = new WireArray(1, 1), s = new WireArray(1, 1), t1 = new WireArray(1, 15), t2 = new WireArray(1, 1),
225                 q = new WireArray(1, 1), nq = new WireArray(1, 1);
226
227         new OrGate(1, t2, r, nq);
228         new OrGate(1, t1, s, q);
229         new NotGate(1, t2, q);
230         new NotGate(1, t1, nq);
231
232         WireArrayInput sIn = s.createInput(), rIn = r.createInput();
233
234         sIn.feedSignals(Bit.ONE);
235         rIn.feedSignals(Bit.ZERO);
236
237         Simulation.TIMELINE.executeAll();
238
239         assertEquals(Bit.ONE, q.getValue());
240         assertEquals(Bit.ZERO, nq.getValue());
241
242         sIn.feedSignals(Bit.ZERO);
243
244         Simulation.TIMELINE.executeAll();
245         assertEquals(Bit.ONE, q.getValue());
246         assertEquals(Bit.ZERO, nq.getValue());
247
248         rIn.feedSignals(Bit.ONE);
249
250         Simulation.TIMELINE.executeAll();
251
252         assertEquals(Bit.ZERO, q.getValue());
253         assertEquals(Bit.ONE, nq.getValue());
254     }
255
256     @Test
257     void numericValueTest()
258     {
259         Simulation.TIMELINE.reset();
260
261         WireArray a = new WireArray(4, 1);
262         a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
263
264         Simulation.TIMELINE.executeAll();
265
266         assertEquals(15, a.getUnsignedValue());
267         assertEquals(-1, a.getSignedValue());
268     }
269
270     @Test
271     void multipleInputs()
272     {
273         Simulation.TIMELINE.reset();
274         WireArray w = new WireArray(2, 1);
275         WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
276         wI1.feedSignals(Bit.ONE, Bit.Z);
277         wI2.feedSignals(Bit.Z, Bit.X);
278         Simulation.TIMELINE.executeAll();
279         assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
280
281         wI2.feedSignals(Bit.ZERO, Bit.Z);
282         Simulation.TIMELINE.executeAll();
283         assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
284
285         wI2.feedSignals(Bit.Z, Bit.Z);
286         Simulation.TIMELINE.executeAll();
287         assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
288
289         wI2.feedSignals(Bit.ONE, Bit.Z);
290         w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
291         Simulation.TIMELINE.executeAll();
292         assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
293     }
294
295         @Test
296         void wireConnections()
297         {
298                 // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
299                 
300                 Simulation.TIMELINE.reset();
301
302                 WireArray a = new WireArray(1, 2);
303                 WireArray b = new WireArray(1, 2);
304                 WireArray c = new WireArray(1, 2);
305                 WireArrayInput aI = a.createInput();
306                 WireArrayInput bI = b.createInput();
307                 WireArrayInput cI = c.createInput();
308
309                 TestBitDisplay test = new TestBitDisplay(c);
310                 TestBitDisplay test2 = new TestBitDisplay(a);
311                 LongConsumer print = time -> System.out.format("Time %2d\n   a: %s\n   b: %s\n   c: %s\n", time, a, b, c);
312
313                 cI.feedSignals(Bit.ONE);
314                 test.assertAfterSimulationIs(print, Bit.ONE);
315
316                 cI.feedSignals(Bit.X);
317                 test.assertAfterSimulationIs(print, Bit.X);
318
319                 cI.feedSignals(Bit.X);
320                 cI.feedSignals(Bit.Z);
321                 test.assertAfterSimulationIs(print, Bit.Z);
322
323                 new Connector(b, c);
324                 test.assertAfterSimulationIs(print, Bit.Z);
325                 System.err.println("ONE");
326                 bI.feedSignals(Bit.ONE);
327                 test.assertAfterSimulationIs(print, Bit.ONE);
328                 System.err.println("ZERO");
329                 bI.feedSignals(Bit.ZERO);
330                 test.assertAfterSimulationIs(print, Bit.ZERO);
331                 System.err.println("Z");
332                 bI.feedSignals(Bit.Z);
333                 test.assertAfterSimulationIs(print, Bit.Z);
334                 
335                 new Connector(a, b);
336                 System.err.println("Z 2");
337                 aI.feedSignals(Bit.Z);
338                 test.assertAfterSimulationIs(print, Bit.Z);
339                 test2.assertAfterSimulationIs(Bit.Z);
340                 System.err.println("ONE 2");
341                 aI.feedSignals(Bit.ONE);
342                 test.assertAfterSimulationIs(print, Bit.ONE);
343                 test2.assertAfterSimulationIs(Bit.ONE);
344                 System.err.println("ZERO 2");
345                 aI.feedSignals(Bit.ZERO);
346                 test.assertAfterSimulationIs(print, Bit.ZERO);
347                 test2.assertAfterSimulationIs(Bit.ZERO);
348                 System.err.println("Z 2 II");
349                 aI.feedSignals(Bit.Z);
350                 test.assertAfterSimulationIs(print, Bit.Z);
351                 test2.assertAfterSimulationIs(Bit.Z);
352                 
353                 System.err.println("No Conflict yet");
354                 bI.feedSignals(Bit.ONE);
355                 test.assertAfterSimulationIs(print, Bit.ONE);
356                 test2.assertAfterSimulationIs(Bit.ONE);
357                 aI.feedSignals(Bit.ONE);
358                 test.assertAfterSimulationIs(print, Bit.ONE);
359                 test2.assertAfterSimulationIs(Bit.ONE);
360                 System.err.println("Conflict");
361                 aI.feedSignals(Bit.ZERO);
362                 test.assertAfterSimulationIs(print, Bit.X);
363                 test2.assertAfterSimulationIs(Bit.X);
364                 aI.feedSignals(Bit.ONE);
365                 test.assertAfterSimulationIs(print, Bit.ONE);
366                 test2.assertAfterSimulationIs(Bit.ONE);
367         }
368
369     private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
370     {
371         assertArrayEquals(expected, actual);
372     }
373 }