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.WireArray;
12 import era.mi.logic.components.Merger2;
13 import era.mi.logic.components.Mux;
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;
23 void circuitExampleTest()
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);
31 new Mux(1, h, d, e, i);
32 new Splitter(i, k, j);
34 a.feedSignals(Bit.ZERO);
35 b.feedSignals(Bit.ONE);
36 c.feedSignals(Bit.ZERO);
37 d.feedSignals(Bit.ONE, Bit.ONE);
38 e.feedSignals(Bit.ONE);
40 while(Simulation.TIMELINE.hasNext())
42 Simulation.TIMELINE.executeNext();
45 assertEquals(Simulation.TIMELINE.getSimulationTime(), 14);
46 assertEquals(Bit.ONE, j.getValue());
47 assertEquals(Bit.ZERO, k.getValue());
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.feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO,Bit.ONE, Bit.ZERO, Bit.ONE);
56 new Splitter(in, a, b, c);
58 while(Simulation.TIMELINE.hasNext())
60 Simulation.TIMELINE.executeNext();
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 }));
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.feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO);
74 b.feedSignals(Bit.ONE, Bit.ZERO);
75 c.feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
77 new Merger2(out, a, b, c);
79 while(Simulation.TIMELINE.hasNext())
81 Simulation.TIMELINE.executeNext();
84 assertTrue(Arrays.equals(out.getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }));
90 Simulation.TIMELINE.reset();
91 WireArray a = new WireArray(1, 1), b = new WireArray(1, 1), select = new WireArray(1, 1), out = new WireArray(1, 1);
93 select.feedSignals(Bit.ONE);
94 a.feedSignals(Bit.ONE);
95 b.feedSignals(Bit.ZERO);
97 new Mux(1, a, b, select, out);
98 assertEquals(out.getValue(), Bit.X);
99 while(Simulation.TIMELINE.hasNext())
101 Simulation.TIMELINE.executeNext();
104 assertEquals(out.getValue(), Bit.ONE);
105 select.feedSignals(Bit.ZERO);
106 while(Simulation.TIMELINE.hasNext())
108 Simulation.TIMELINE.executeNext();
111 assertEquals(out.getValue(), Bit.ZERO);
117 Simulation.TIMELINE.reset();
118 AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
119 gate.getA().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
120 gate.getB().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
123 while(Simulation.TIMELINE.hasNext())
125 Simulation.TIMELINE.executeNext();
127 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }));
133 Simulation.TIMELINE.reset();
134 OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
135 gate.getA().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
136 gate.getB().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
139 while(Simulation.TIMELINE.hasNext())
141 Simulation.TIMELINE.executeNext();
143 assertTrue(Arrays.equals(gate.getOut().getValues(), new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }));
147 void rsLatchCircuitTest()
149 Simulation.TIMELINE.reset();
150 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),
151 nq = new WireArray(1, 1);
153 new OrGate(1, r, nq, t2);
154 new OrGate(1, s, q, t1);
155 new NotGate(1, t2, q);
156 new NotGate(1, t1, nq);
158 s.feedSignals(Bit.ONE);
159 r.feedSignals(Bit.ZERO);
161 while(Simulation.TIMELINE.hasNext())
163 Simulation.TIMELINE.executeNext();
166 assertEquals(q.getValue(), Bit.ONE);
167 assertEquals(nq.getValue(), Bit.ZERO);
169 s.feedSignals(Bit.ZERO);
171 while(Simulation.TIMELINE.hasNext())
173 Simulation.TIMELINE.executeNext();
176 assertEquals(q.getValue(), Bit.ONE);
177 assertEquals(nq.getValue(), Bit.ZERO);
179 r.feedSignals(Bit.ONE);
181 while(Simulation.TIMELINE.hasNext())
183 Simulation.TIMELINE.executeNext();
186 assertEquals(q.getValue(), Bit.ZERO);
187 assertEquals(nq.getValue(), Bit.ONE);
191 void numericValueTest()
193 Simulation.TIMELINE.reset();
195 WireArray a = new WireArray(4, 1);
196 a.feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
198 while(Simulation.TIMELINE.hasNext())
200 Simulation.TIMELINE.executeNext();
203 assertEquals(a.getUnsignedValue(), 15);
204 assertEquals(a.getSignedValue(), -1);