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