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