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