import era.mi.logic.Bit;
import era.mi.logic.Simulation;
+import era.mi.logic.components.Demux;
import era.mi.logic.components.Merger;
import era.mi.logic.components.Mux;
import era.mi.logic.components.Splitter;
import era.mi.logic.components.gates.AndGate;
import era.mi.logic.components.gates.NotGate;
import era.mi.logic.components.gates.OrGate;
+import era.mi.logic.components.gates.XorGate;
import era.mi.logic.wires.WireArray;
import era.mi.logic.wires.WireArray.WireArrayInput;
Simulation.TIMELINE.reset();
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),
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);
- new AndGate(1, a, b, f);
+ new AndGate(1, f, a, b);
new NotGate(1, f, g);
new Merger(h, c, g);
new Mux(1, i, e, h, d);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues());
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues());
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues());
+ assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO);
+ assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO);
+ assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE);
}
@Test
new Mux(1, out, select, a, b, c);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues());
+ assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
selectIn.feedSignals(Bit.ZERO, Bit.ONE);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues());
+ assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
selectIn.feedSignals(Bit.ONE, Bit.ONE);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues());
+ assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
}
+ @Test
+ void demuxTest()
+ {
+ Simulation.TIMELINE.reset();
+ WireArray a = new WireArray(4, 3), b = new WireArray(4, 6), c = new WireArray(4, 4),
+ select = new WireArray(2, 5), in = new WireArray(4, 1);
+ WireArrayInput selectIn = select.createInput();
+
+ selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
+ in.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
+
+ new Demux(1, in, select, a, b, c);
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
+ assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ selectIn.feedSignals(Bit.ZERO, Bit.ONE);
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
+
+ selectIn.feedSignals(Bit.ONE, Bit.ONE);
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(b.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+
+ }
+
@Test
void andTest()
{
Simulation.TIMELINE.reset();
- AndGate gate = new AndGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
- gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
- gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
+ WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
+ new AndGate(1, c, a, b);
+ a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
+ b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues());
+
+ assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
}
@Test
void orTest()
{
Simulation.TIMELINE.reset();
- OrGate gate = new OrGate(1, new WireArray(4, 1), new WireArray(4, 1), new WireArray(4, 1));
- gate.getA().createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
- gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
+ WireArray a = new WireArray(4, 1), b = new WireArray(4, 3), c = new WireArray(4, 1);
+ new OrGate(1, c, a, b);
+ a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
+ b.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE);
+ }
+
+ @Test
+ void xorTest()
+ {
+ Simulation.TIMELINE.reset();
+ WireArray a = new WireArray(3, 1), b = new WireArray(3, 2), c = new WireArray(3, 1), d = new WireArray(3, 1);
+ new XorGate(1, d, a, b, c);
+ a.createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
+ b.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
+ c.createInput().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues());
+ assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
}
@Test
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), nq = new WireArray(1, 1);
- new OrGate(1, r, nq, t2);
- new OrGate(1, s, q, t1);
+ new OrGate(1, t2, r, nq);
+ new OrGate(1, t1, s, q);
new NotGate(1, t2, q);
new NotGate(1, t1, nq);
wI1.feedSignals(Bit.ONE, Bit.Z);
wI2.feedSignals(Bit.Z, Bit.X);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues());
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
wI2.feedSignals(Bit.ZERO, Bit.Z);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues());
+ assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
wI2.feedSignals(Bit.Z, Bit.Z);
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
wI2.feedSignals(Bit.ONE, Bit.Z);
- w.addObserver((i) -> fail("WireArray notified observer, although value did not change."));
+ w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
Simulation.TIMELINE.executeAll();
- assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues());
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
}
@Test
cI.feedSignals(Bit.Z);
test.assertAfterSimulationIs(print, Bit.Z);
- Connector c1 = new Connector(b, c);
+ new Connector(b, c);
test.assertAfterSimulationIs(print, Bit.Z);
System.err.println("ONE");
bI.feedSignals(Bit.ONE);
bI.feedSignals(Bit.Z);
test.assertAfterSimulationIs(print, Bit.Z);
- Connector c2 = new Connector(a, b);
+ new Connector(a, b);
System.err.println("Z 2");
aI.feedSignals(Bit.Z);
test.assertAfterSimulationIs(print, Bit.Z);
private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
{
- assertArrayEquals(expected, actual);
+ assertArrayEquals(expected, actual);
}
}