Wire concept was changed to accommodate multiple inputs. Not all
[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
7 import org.junit.jupiter.api.Test;
8
9 import era.mi.logic.Bit;
10 import era.mi.logic.Simulation;
11 import era.mi.logic.components.Merger2;
12 import era.mi.logic.components.Mux;
13 import era.mi.logic.components.Mux2;
14 import era.mi.logic.components.Splitter;
15 import era.mi.logic.components.gates.AndGate;
16 import era.mi.logic.components.gates.NotGate;
17 import era.mi.logic.components.gates.OrGate;
18 import era.mi.logic.wires.WireArray;
19 import era.mi.logic.wires.WireArray.WireArrayInput;
20
21 class ComponentTest
22 {
23
24 //      @Test
25 //      void circuitExampleTest()
26 //      {
27 //              Simulation.TIMELINE.reset();
28 //              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),
29 //                              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);
30 //              new AndGate(1, a, b, f);
31 //              new NotGate(1, f, g);
32 //              new Merger2(h, c, g);
33 //              new Mux(1, h, d, e, i);
34 //              new Splitter(i, k, j);
35 //              
36 //              a.createInput().feedSignals(Bit.ZERO);
37 //              b.createInput().feedSignals(Bit.ONE);
38 //              c.createInput().feedSignals(Bit.ZERO);
39 //              d.createInput().feedSignals(Bit.ONE, Bit.ONE);
40 //              e.createInput().feedSignals(Bit.ONE);
41 //              
42 //              while(Simulation.TIMELINE.hasNext())
43 //              {
44 //                      Simulation.TIMELINE.executeNext();
45 //              }
46 //              
47 //              assertEquals(Simulation.TIMELINE.getSimulationTime(), 14);
48 //              assertEquals(Bit.ONE, j.getValue());
49 //              assertEquals(Bit.ZERO, k.getValue());
50 //      }
51 //
52 //      @Test
53 //      void splitterTest()
54 //      {
55 //              Simulation.TIMELINE.reset();
56 //              WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), in = new WireArray(8, 1);
57 //              in.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO,Bit.ONE, Bit.ZERO, Bit.ONE);
58 //              new Splitter(in, a, b, c);
59 //              
60 //              while(Simulation.TIMELINE.hasNext())
61 //              {
62 //                      Simulation.TIMELINE.executeNext();
63 //              }
64 //              
65 //              assertTrue(Arrays.equals(a.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }));
66 //              assertTrue(Arrays.equals(b.getValues(), new Bit[] { Bit.ONE, Bit.ZERO }));
67 //              assertTrue(Arrays.equals(c.getValues(), new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }));
68 //      }
69 //      
70 //      @Test
71 //      void mergerTest()
72 //      {
73 //              Simulation.TIMELINE.reset();
74 //              WireArray a = new WireArray(3, 1), b = new WireArray(2, 1), c = new WireArray(3, 1), out = new WireArray(8, 1);
75 //              a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
76 //              b.createInput().feedSignals(Bit.ONE, Bit.ZERO);
77 //              c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
78 //              
79 //              new Merger2(out, a, b, c);
80 //              
81 //              while(Simulation.TIMELINE.hasNext())
82 //              {
83 //                      Simulation.TIMELINE.executeNext();
84 //              }
85 //              
86 //              assertTrue(Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
87 //      }
88         
89         @Test
90         void muxTest()
91         {
92                 Simulation.TIMELINE.reset();
93                 WireArray a = new WireArray(1, 3), b = new WireArray(1, 2), select = new WireArray(1, 1), out = new WireArray(1, 1);
94                 WireArrayInput selectIn = select.createInput();
95                 
96                 selectIn.feedSignals(Bit.ZERO);
97                 a.createInput().feedSignals(Bit.ONE);
98                 b.createInput().feedSignals(Bit.ZERO);
99                 
100                 new Mux2(1, out, select, a, b);
101                 assertEquals(Bit.Z, out.getValue());
102                 while(Simulation.TIMELINE.hasNext())
103                 {
104                         Simulation.TIMELINE.executeNext();
105                 }
106
107                 assertEquals(Bit.ONE, out.getValue());
108                 selectIn.feedSignals(Bit.ONE);
109                 while(Simulation.TIMELINE.hasNext())
110                 {
111                         Simulation.TIMELINE.executeNext();
112                 }
113                 
114                 assertEquals(out.getValue(), Bit.ZERO);
115         }
116
117         @Test
118         void andTest()
119         {
120                 Simulation.TIMELINE.reset();
121                 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
122                 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
123                 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
124                 
125                 
126                 while(Simulation.TIMELINE.hasNext())
127                 {
128                         Simulation.TIMELINE.executeNext();
129                 }
130                 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
131         }
132         
133         @Test
134         void orTest()
135         {
136                 Simulation.TIMELINE.reset();
137                 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
138                 gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
139                 gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
140                 
141                 while(Simulation.TIMELINE.hasNext())
142                 {
143                         Simulation.TIMELINE.executeNext();
144                 }
145                 
146                 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
147         }
148         
149         @Test
150         void rsLatchCircuitTest()
151         {
152                 Simulation.TIMELINE.reset();
153                 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),
154                                 nq = new WireArray(1, 1);
155                 
156                 new OrGate(1, r, nq, t2);
157                 new OrGate(1, s, q, t1);
158                 new NotGate(1, t2, q);
159                 new NotGate(1, t1, nq);
160         
161                 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
162                 
163                 sIn.feedSignals(Bit.ONE);
164                 rIn.feedSignals(Bit.ZERO);
165                 
166                 while(Simulation.TIMELINE.hasNext())
167                 {
168                         Simulation.TIMELINE.executeNext();
169                 }
170                 
171                 assertEquals(q.getValue(), Bit.ONE);
172                 assertEquals(nq.getValue(), Bit.ZERO);
173                 
174                 sIn.feedSignals(Bit.ZERO);
175                 
176                 while(Simulation.TIMELINE.hasNext())
177                 {
178                         Simulation.TIMELINE.executeNext();
179                 }
180                 
181                 assertEquals(q.getValue(), Bit.ONE);
182                 assertEquals(nq.getValue(), Bit.ZERO);
183                 
184                 rIn.feedSignals(Bit.ONE);
185                 
186                 while(Simulation.TIMELINE.hasNext())
187                 {
188                         Simulation.TIMELINE.executeNext();
189                 }
190                 
191                 assertEquals(q.getValue(), Bit.ZERO);
192                 assertEquals(nq.getValue(), Bit.ONE);
193         }
194         
195         @Test
196         void numericValueTest()
197         {
198                 Simulation.TIMELINE.reset();
199                 
200                 WireArray a = new WireArray(4, 1);
201                 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
202                 
203                 while(Simulation.TIMELINE.hasNext())
204                 {
205                         Simulation.TIMELINE.executeNext();
206                 }
207                 
208                 assertEquals(a.getUnsignedValue(), 15);
209                 assertEquals(a.getSignedValue(), -1);
210         }
211         
212         @Test
213         void multipleInputs()
214         {
215                 Simulation.TIMELINE.reset();
216                 WireArray w = new WireArray(2, 1);
217                 WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
218                 wI1.feedSignals(Bit.ONE, Bit.Z);
219                 wI2.feedSignals(Bit.Z, Bit.X);
220                 while(Simulation.TIMELINE.hasNext())
221                 {
222                         Simulation.TIMELINE.executeNext();
223                 }
224                 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.X }));
225                 
226                 wI2.feedSignals(Bit.ZERO, Bit.Z);
227                 while(Simulation.TIMELINE.hasNext())
228                 {
229                         Simulation.TIMELINE.executeNext();
230                 }
231                 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.X, Bit.Z }));
232                 
233                 wI2.feedSignals(Bit.Z, Bit.Z);
234                 while(Simulation.TIMELINE.hasNext())
235                 {
236                         Simulation.TIMELINE.executeNext();
237                 }
238                 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
239                 
240                 wI2.feedSignals(Bit.ONE, Bit.Z);
241                 w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
242                 while(Simulation.TIMELINE.hasNext())
243                 {
244                         Simulation.TIMELINE.executeNext();
245                 }
246                 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
247         }
248 }