1 package era.mi.logic.tests;
3 import static org.junit.jupiter.api.Assertions.*;
5 import java.util.Arrays;
7 import org.junit.jupiter.api.Test;
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;
25 // void circuitExampleTest()
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);
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);
42 // while(Simulation.TIMELINE.hasNext())
44 // Simulation.TIMELINE.executeNext();
47 // assertEquals(Simulation.TIMELINE.getSimulationTime(), 14);
48 // assertEquals(Bit.ONE, j.getValue());
49 // assertEquals(Bit.ZERO, k.getValue());
53 // void splitterTest()
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);
60 // while(Simulation.TIMELINE.hasNext())
62 // Simulation.TIMELINE.executeNext();
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 }));
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);
79 // new Merger2(out, a, b, c);
81 // while(Simulation.TIMELINE.hasNext())
83 // Simulation.TIMELINE.executeNext();
86 // assertTrue(Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
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();
96 selectIn.feedSignals(Bit.ZERO);
97 a.createInput().feedSignals(Bit.ONE);
98 b.createInput().feedSignals(Bit.ZERO);
100 new Mux2(1, out, select, a, b);
101 assertEquals(Bit.Z, out.getValue());
102 while(Simulation.TIMELINE.hasNext())
104 Simulation.TIMELINE.executeNext();
107 assertEquals(Bit.ONE, out.getValue());
108 selectIn.feedSignals(Bit.ONE);
109 while(Simulation.TIMELINE.hasNext())
111 Simulation.TIMELINE.executeNext();
114 assertEquals(out.getValue(), Bit.ZERO);
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);
126 while(Simulation.TIMELINE.hasNext())
128 Simulation.TIMELINE.executeNext();
130 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
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);
141 while(Simulation.TIMELINE.hasNext())
143 Simulation.TIMELINE.executeNext();
146 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
150 void rsLatchCircuitTest()
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);
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);
161 WireArrayInput sIn = s.createInput(), rIn = r.createInput();
163 sIn.feedSignals(Bit.ONE);
164 rIn.feedSignals(Bit.ZERO);
166 while(Simulation.TIMELINE.hasNext())
168 Simulation.TIMELINE.executeNext();
171 assertEquals(q.getValue(), Bit.ONE);
172 assertEquals(nq.getValue(), Bit.ZERO);
174 sIn.feedSignals(Bit.ZERO);
176 while(Simulation.TIMELINE.hasNext())
178 Simulation.TIMELINE.executeNext();
181 assertEquals(q.getValue(), Bit.ONE);
182 assertEquals(nq.getValue(), Bit.ZERO);
184 rIn.feedSignals(Bit.ONE);
186 while(Simulation.TIMELINE.hasNext())
188 Simulation.TIMELINE.executeNext();
191 assertEquals(q.getValue(), Bit.ZERO);
192 assertEquals(nq.getValue(), Bit.ONE);
196 void numericValueTest()
198 Simulation.TIMELINE.reset();
200 WireArray a = new WireArray(4, 1);
201 a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
203 while(Simulation.TIMELINE.hasNext())
205 Simulation.TIMELINE.executeNext();
208 assertEquals(a.getUnsignedValue(), 15);
209 assertEquals(a.getSignedValue(), -1);
213 void multipleInputs()
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())
222 Simulation.TIMELINE.executeNext();
224 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.X }));
226 wI2.feedSignals(Bit.ZERO, Bit.Z);
227 while(Simulation.TIMELINE.hasNext())
229 Simulation.TIMELINE.executeNext();
231 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.X, Bit.Z }));
233 wI2.feedSignals(Bit.Z, Bit.Z);
234 while(Simulation.TIMELINE.hasNext())
236 Simulation.TIMELINE.executeNext();
238 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
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())
244 Simulation.TIMELINE.executeNext();
246 assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));