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