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