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