--- /dev/null
+package era.mi.logic.components;
+
+import java.util.Arrays;
+
+import era.mi.logic.Bit;
+import era.mi.logic.wires.WireArray;
+
+public class BitDisplay extends BasicComponent
+{
+ private final WireArray in;
+ private Bit[] displayedValue;
+
+ public BitDisplay(WireArray in)
+ {
+ super(1);
+ this.in = in;
+ in.addObserver(this);
+ }
+
+ @Override
+ protected void compute()
+ {
+ displayedValue = in.getValues();
+ }
+
+ public Bit[] getDisplayedValue()
+ {
+ return displayedValue;
+ }
+
+ public boolean isDisplaying(Bit... values)
+ {
+ return Arrays.equals(displayedValue, values);
+ }
+}
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
+import java.util.function.LongConsumer;
import org.junit.jupiter.api.Test;
import era.mi.logic.Bit;
import era.mi.logic.Simulation;
-import era.mi.logic.components.Merger2;
-import era.mi.logic.components.Mux;
import era.mi.logic.components.Mux2;
-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;
}
assertTrue(Arrays.equals(w.getValues(), new Bit[] { Bit.ONE, Bit.Z }));
}
+
+ @Test
+ void wireConnections()
+ {
+ // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde
+
+ Simulation.TIMELINE.reset();
+
+ WireArray a = new WireArray(1, 2);
+ WireArray b = new WireArray(1, 2);
+ WireArray c = new WireArray(1, 2);
+ WireArrayInput aI = a.createInput();
+ WireArrayInput bI = b.createInput();
+ WireArrayInput cI = c.createInput();
+
+ TestBitDisplay test = new TestBitDisplay(c);
+ LongConsumer print = time -> System.out.format("Time %2d\n %s\n %s\n %s\n", time, a, b, c);
+
+ cI.feedSignals(Bit.ONE);
+ test.assertAfterSimulationIs(print, Bit.ONE);
+
+ cI.feedSignals(Bit.X);
+ test.assertAfterSimulationIs(print, Bit.X);
+
+ cI.feedSignals(Bit.X);
+ cI.feedSignals(Bit.Z);
+ test.assertAfterSimulationIs(print, Bit.Z);
+
+ Connector c1 = new Connector(b, c);
+ test.assertAfterSimulationIs(print, Bit.Z);
+ System.out.println("ONE");
+ bI.feedSignals(Bit.ONE);
+ test.assertAfterSimulationIs(print, Bit.ONE);
+ System.out.println("ZERO");
+ bI.feedSignals(Bit.ZERO);
+ test.assertAfterSimulationIs(print, Bit.ZERO);
+ System.out.println("Z");
+ bI.feedSignals(Bit.Z);
+ test.assertAfterSimulationIs(Bit.Z);
+ }
+
+ private static void assertBitArrayEquals(Bit[] actual, Bit... expected)
+ {
+ assertArrayEquals(expected, actual);
+ }
}
--- /dev/null
+package era.mi.logic.tests;
+
+import era.mi.logic.Simulation;
+import era.mi.logic.wires.WireArray;
+import era.mi.logic.wires.WireArray.WireArrayInput;
+import era.mi.logic.wires.WireArrayObserver;
+
+public class Connector implements WireArrayObserver
+{
+ private final WireArray a;
+ private final WireArray b;
+ private final WireArrayInput aI;
+ private final WireArrayInput bI;
+
+ public Connector(WireArray a, WireArray b)
+ {
+ if (a.length != b.length)
+ throw new IllegalArgumentException("WireArray width does not match: " + a.length + ", " + b.length);
+ this.a = a;
+ this.b = b;
+ a.addObserver(this);
+ b.addObserver(this);
+ aI = a.createInput();
+ bI = b.createInput();
+ }
+
+ @Override
+ public void update(WireArray initiator)
+ {
+ Simulation.TIMELINE.addEvent((e) ->
+ {
+ if (initiator == a)
+ bI.feedSignals(a.getValues());
+ else
+ aI.feedSignals(b.getValues());
+ }, 1);
+ }
+}
--- /dev/null
+package era.mi.logic.tests;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+import java.util.Arrays;
+import java.util.function.LongConsumer;
+
+import era.mi.logic.Bit;
+import era.mi.logic.Simulation;
+import era.mi.logic.components.BitDisplay;
+import era.mi.logic.wires.WireArray;
+
+public final class TestBitDisplay extends BitDisplay
+{
+
+ public TestBitDisplay(WireArray in)
+ {
+ super(in);
+ }
+
+ public void assertDisplays(Bit... expected)
+ {
+ assertArrayEquals(expected, getDisplayedValue());
+ }
+
+ public void assertAfterSimulationIs(Bit... expected)
+ {
+ Simulation.TIMELINE.executeAll();
+ assertDisplays(expected);
+ }
+
+ public void assertAfterSimulationIs(LongConsumer r, Bit... expected)
+ {
+ while (Simulation.TIMELINE.hasNext())
+ {
+ Simulation.TIMELINE.executeNext();
+ r.accept(Simulation.TIMELINE.getSimulationTime());
+ }
+ assertDisplays(expected);
+ }
+
+ @Override
+ protected void compute()
+ {
+ super.compute();
+ System.out.println("update: value is " + Arrays.toString(getDisplayedValue()));
+ }
+}