X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Ftests%2FComponentTest.java;h=d48c5c630c9421d1c9c5ec4261d8003f49182908;hb=a07a799d9d93669126a544b88856a05e11323b79;hp=a407b8dffbe1495e77357a5c879bfce0e04d391c;hpb=830af2a47f501b4d31f68663b609d225eae603cd;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/tests/ComponentTest.java b/era.mi/src/era/mi/logic/tests/ComponentTest.java index a407b8df..d48c5c63 100644 --- a/era.mi/src/era/mi/logic/tests/ComponentTest.java +++ b/era.mi/src/era/mi/logic/tests/ComponentTest.java @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test; 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; @@ -16,6 +17,7 @@ import era.mi.logic.components.TriStateBuffer; 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; @@ -28,7 +30,7 @@ class ComponentTest 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); @@ -56,9 +58,9 @@ class ComponentTest 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 @@ -126,42 +128,93 @@ class ComponentTest 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 @@ -171,8 +224,8 @@ class ComponentTest 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); @@ -223,20 +276,20 @@ class ComponentTest 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 @@ -267,7 +320,7 @@ class ComponentTest 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); @@ -279,7 +332,7 @@ class ComponentTest 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); @@ -315,6 +368,6 @@ class ComponentTest private static void assertBitArrayEquals(Bit[] actual, Bit... expected) { - assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } }