- 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();
-
- assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
- }
-
- @Test
- void rsLatchCircuitTest()
- {
- Simulation.TIMELINE.reset();
- 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, t2, r, nq);
- new OrGate(1, t1, s, q);
- new NotGate(1, t2, q);
- new NotGate(1, t1, nq);
-
- WireArrayInput sIn = s.createInput(), rIn = r.createInput();
-
- sIn.feedSignals(Bit.ONE);
- rIn.feedSignals(Bit.ZERO);
-
- Simulation.TIMELINE.executeAll();
-
- assertEquals(Bit.ONE, q.getValue());
- assertEquals(Bit.ZERO, nq.getValue());
-
- sIn.feedSignals(Bit.ZERO);
-
- Simulation.TIMELINE.executeAll();
- assertEquals(Bit.ONE, q.getValue());
- assertEquals(Bit.ZERO, nq.getValue());
-
- rIn.feedSignals(Bit.ONE);
-
- Simulation.TIMELINE.executeAll();
-
- assertEquals(Bit.ZERO, q.getValue());
- assertEquals(Bit.ONE, nq.getValue());
- }
-
- @Test
- void numericValueTest()
- {
- Simulation.TIMELINE.reset();
-
- WireArray a = new WireArray(4, 1);
- a.createInput().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
-
- Simulation.TIMELINE.executeAll();
-
- assertEquals(15, a.getUnsignedValue());
- assertEquals(-1, a.getSignedValue());
- }
-
- @Test
- void multipleInputs()
- {
- Simulation.TIMELINE.reset();
- WireArray w = new WireArray(2, 1);
- WireArrayInput wI1 = w.createInput(), wI2 = w.createInput();
- wI1.feedSignals(Bit.ONE, Bit.Z);
- wI2.feedSignals(Bit.Z, Bit.X);
- Simulation.TIMELINE.executeAll();
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
-
- wI2.feedSignals(Bit.ZERO, Bit.Z);
- Simulation.TIMELINE.executeAll();
- assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
-
- wI2.feedSignals(Bit.Z, Bit.Z);
- Simulation.TIMELINE.executeAll();
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
-
- wI2.feedSignals(Bit.ONE, Bit.Z);
- w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
- Simulation.TIMELINE.executeAll();
- assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
- }
+ @Test
+ void demuxTest()
+ {
+ Simulation.TIMELINE.reset();
+ Wire a = new Wire(4, 3), b = new Wire(4, 6), c = new Wire(4, 4), select = new Wire(2, 5), in = new Wire(4, 1);
+ WireEnd selectIn = select.createEnd();
+
+ selectIn.feedSignals(Bit.ZERO, Bit.ZERO);
+ in.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
+
+ new Demux(1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createEnd(), b.createEnd(), c.createEnd());
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO);
+ assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
+ assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
+ selectIn.feedSignals(Bit.ZERO, Bit.ONE);
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+ assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U);
+ 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.U, Bit.U, Bit.U, Bit.U);
+ assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z);
+
+ }
+
+ @Test
+ void andTest()
+ {
+ Simulation.TIMELINE.reset();
+ Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
+ new AndGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
+ b.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO);
+ }
+
+ @Test
+ void orTest()
+ {
+ Simulation.TIMELINE.reset();
+ Wire a = new Wire(4, 1), b = new Wire(4, 3), c = new Wire(4, 1);
+ new OrGate(1, c.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd());
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO);
+ b.createEnd().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();
+ Wire a = new Wire(3, 1), b = new Wire(3, 2), c = new Wire(3, 1), d = new Wire(3, 1);
+ new XorGate(1, d.createEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd());
+ a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
+ b.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
+ c.createEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE);
+ }
+
+ @Test
+ void notTest()
+ {
+ Simulation.TIMELINE.reset();
+ Wire a = new Wire(3, 1), b = new Wire(3, 2);
+ new NotGate(1, a.createReadOnlyEnd(), b.createEnd());
+ a.createEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO);
+ }
+
+ @Test
+ void rsLatchCircuitTest()
+ {
+ Simulation.TIMELINE.reset();
+ Wire r = new Wire(1, 1), s = new Wire(1, 1), t1 = new Wire(1, 15), t2 = new Wire(1, 1), q = new Wire(1, 1), nq = new Wire(1, 1);
+
+ new OrGate(1, t2.createEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd());
+ new OrGate(1, t1.createEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd());
+ new NotGate(1, t2.createReadOnlyEnd(), q.createEnd());
+ new NotGate(1, t1.createReadOnlyEnd(), nq.createEnd());
+
+ WireEnd sIn = s.createEnd(), rIn = r.createEnd();
+
+ sIn.feedSignals(Bit.ONE);
+ rIn.feedSignals(Bit.ZERO);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertEquals(Bit.ONE, q.getValue());
+ assertEquals(Bit.ZERO, nq.getValue());
+
+ sIn.feedSignals(Bit.ZERO);
+
+ Simulation.TIMELINE.executeAll();
+ assertEquals(Bit.ONE, q.getValue());
+ assertEquals(Bit.ZERO, nq.getValue());
+
+ rIn.feedSignals(Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertEquals(Bit.ZERO, q.getValue());
+ assertEquals(Bit.ONE, nq.getValue());
+ }
+
+ @Test
+ void numericValueTest()
+ {
+ Simulation.TIMELINE.reset();
+
+ Wire a = new Wire(4, 1);
+ a.createEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE);
+
+ Simulation.TIMELINE.executeAll();
+
+ assertEquals(15, a.getUnsignedValue());
+ assertEquals(-1, a.getSignedValue());
+ }
+
+ @Test
+ void multipleInputs()
+ {
+ Simulation.TIMELINE.reset();
+ Wire w = new Wire(2, 1);
+ WireEnd wI1 = w.createEnd(), wI2 = w.createEnd();
+ wI1.feedSignals(Bit.ONE, Bit.Z);
+ wI2.feedSignals(Bit.Z, Bit.X);
+ Simulation.TIMELINE.executeAll();
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X);
+
+ wI2.feedSignals(Bit.ZERO, Bit.Z);
+ Simulation.TIMELINE.executeAll();
+ assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z);
+
+ wI2.feedSignals(Bit.Z, Bit.Z);
+ Simulation.TIMELINE.executeAll();
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
+
+ wI2.feedSignals(Bit.ONE, Bit.Z);
+ w.addObserver((i, oldValues) -> fail("WireArray notified observer, although value did not change."));
+ Simulation.TIMELINE.executeAll();
+ assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z);
+ }