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