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