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