GUIBitDisplay and GUIManualSwitch now support logicWidth != 1
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sat, 10 Aug 2019 16:32:19 +0000 (18:32 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sat, 10 Aug 2019 16:32:19 +0000 (18:32 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java
net.mograsim.logic.core/test/net/mograsim/logic/core/tests/GUITest.java
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/examples/GUIComponentTestbench.java
net.mograsim.logic.model.am2900/test/net/mograsim/logic/model/am2900/Am2901Testbench.java
net.mograsim.logic.model.am2900/test/net/mograsim/logic/model/am2900/TestableAm2901Impl.java
net.mograsim.logic.model/src/net/mograsim/logic/model/examples/ClickableSubmodelComponentsTest.java
net.mograsim.logic.model/src/net/mograsim/logic/model/examples/RSLatchExample.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIBitDisplay.java
net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/GUIManualSwitch.java
net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java

index 9ec98ed..26238fe 100644 (file)
@@ -8,6 +8,7 @@ import net.mograsim.logic.core.LogicObservable;
 import net.mograsim.logic.core.LogicObserver;
 import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
 
@@ -26,49 +27,50 @@ public class ManualSwitch extends Component implements LogicObservable
        {
                super(timeline);
                observers = new ArrayList<>();
-               if (output.length() != 1)
-                       throw new IllegalArgumentException("Switch output can be only a single wire");
                this.output = output;
        }
 
-       public void switchOn()
+       public void switchFullOn()
        {
-               setState(true);
+               setState(BitVector.of(Bit.ONE, output.length()));
        }
 
-       public void switchOff()
+       public void switchFullOff()
        {
-               setState(false);
+               setState(BitVector.of(Bit.ZERO, output.length()));
        }
 
        public void toggle()
        {
-               setState(!isOn());
+               if (isFullOn())
+                       switchFullOff();
+               else
+                       switchFullOn();
        }
 
-       public void setState(boolean isOn)
+       public void setState(Bit bit)
        {
-               setToValueOf(isOn ? Bit.ONE : Bit.ZERO);
+               setState(BitVector.of(bit));
        }
 
-       public void setToValueOf(Bit bit)
+       public void setState(BitVector bits)
        {
-               if (!bit.isBinary())
-                       throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
-               if (bit == output.getInputValue())
+               if (bits.length() != output.length())
+                       throw new IllegalArgumentException("Incorrect bit vector length");
+               if (bits.equals(output.getInputValues()))
                        return;
-               output.feedSignals(bit);
+               output.feedSignals(bits);
                notifyObservers();
        }
 
-       public boolean isOn()
+       public boolean isFullOn()
        {
-               return output.getInputValue() == Bit.ONE;
+               return BitVector.of(Bit.ONE, output.length()).equals(output.getInputValues());
        }
 
-       public Bit getValue()
+       public BitVector getValues()
        {
-               return output.getInputValue();
+               return output.getInputValues();
        }
 
        @Override
index 544b064..86eb115 100644 (file)
@@ -66,7 +66,7 @@ public class GUITest extends JPanel
                                {
                                        if (dim.getValue().contains(e.getPoint()))
                                        {
-                                               dim.getKey().switchOff();
+                                               dim.getKey().switchFullOff();
                                                repaint();
                                        }
                                }
@@ -79,7 +79,7 @@ public class GUITest extends JPanel
                                {
                                        if (dim.getValue().contains(e.getPoint()))
                                        {
-                                               dim.getKey().switchOn();
+                                               dim.getKey().switchFullOn();
                                                repaint();
                                        }
                                }
@@ -220,7 +220,7 @@ public class GUITest extends JPanel
                        switchMap.put(ms, r);
                }
 
-               g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
+               g.setColor(ms.isFullOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
                g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
                setBlack(g);
                g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
index 7f42c01..b745891 100644 (file)
@@ -54,22 +54,16 @@ public class GUIComponentTestbench
                for (int i = 0; i < inputPinNames.size(); i++)
                {
                        String pinName = inputPinNames.get(i);
-                       if (comp.getPin(pinName).logicWidth == 1)
-                       {
-                               GUIManualSwitch sw = new GUIManualSwitch(model);
-                               sw.moveTo(0, 20 * i);
-                               new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
-                       }
+                       GUIManualSwitch sw = new GUIManualSwitch(model, comp.getPin(pinName).logicWidth);
+                       sw.moveTo(0, 20 * i);
+                       new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
                }
                for (int i = 0; i < outputPinNames.size(); i++)
                {
                        String pinName = outputPinNames.get(i);
-                       if (comp.getPin(pinName).logicWidth == 1)
-                       {
-                               GUIBitDisplay bd = new GUIBitDisplay(model);
-                               bd.moveTo(200, 20 * i);
-                               new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
-                       }
+                       GUIBitDisplay bd = new GUIBitDisplay(model, comp.getPin(pinName).logicWidth);
+                       bd.moveTo(200, 20 * i);
+                       new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
                }
        }
 }
\ No newline at end of file
index 34154e6..731147c 100644 (file)
@@ -31,7 +31,7 @@ public class Am2901Testbench
 
                comp.moveTo(240, 0);
 
-               GUIManualSwitch enable = new GUIManualSwitch(model);
+               GUIManualSwitch enable = new GUIManualSwitch(model, 1);
                WireCrossPoint wcp0 = new WireCrossPoint(model, 1);
                GUINotGate not1 = new GUINotGate(model, 1);
                GUINotGate not2 = new GUINotGate(model, 1);
@@ -71,7 +71,7 @@ public class Am2901Testbench
 
                        WireCrossPoint wcp = new WireCrossPoint(model, 1);
                        GUIComponent d_ff = IndirectGUIComponentCreator.createComponent(model, "GUIdff");
-                       GUIManualSwitch sw = new GUIManualSwitch(model);
+                       GUIManualSwitch sw = new GUIManualSwitch(model, 1);
 
                        tool.connect(last, wcp);
                        tool.connect(wcp, d_ff, "C");
@@ -91,7 +91,7 @@ public class Am2901Testbench
                {
                        double x = 300 + 75 * (i % 2);
                        double y = 10 * i - 2.5;
-                       GUIBitDisplay bd = new GUIBitDisplay(model);
+                       GUIBitDisplay bd = new GUIBitDisplay(model, 1);
                        bd.moveTo(x, y);
                        tool.connect(bd.getInputPin(), comp, outputPinNames.get(i));
 
index 7d44f8b..8985e53 100644 (file)
@@ -117,7 +117,7 @@ public class TestableAm2901Impl implements TestableAm2901
                HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
                for (String id : inputPinNames)
                {
-                       GUIManualSwitch sw = new GUIManualSwitch(viewModel);
+                       GUIManualSwitch sw = new GUIManualSwitch(viewModel, 1);
                        new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
                        idSwitchMap.put(id, sw);
                }
@@ -125,7 +125,7 @@ public class TestableAm2901Impl implements TestableAm2901
                HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
                for (String id : outputPinNames)
                {
-                       GUIBitDisplay bd = new GUIBitDisplay(viewModel);
+                       GUIBitDisplay bd = new GUIBitDisplay(viewModel, 1);
 //                     bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
                        new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
                        idDisplayMap.put(id, bd);
@@ -168,53 +168,53 @@ public class TestableAm2901Impl implements TestableAm2901
        public void setDest(Am2901_Dest dest)
        {
                var bits = of(dest.ordinal(), 3);
-               I8.setToValueOf(bits.getLSBit(2));
-               I7.setToValueOf(bits.getLSBit(1));
-               I6.setToValueOf(bits.getLSBit(0));
+               I8.setState(bits.getLSBit(2));
+               I7.setState(bits.getLSBit(1));
+               I6.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setFunc(Am2901_Func func)
        {
                var bits = of(func.ordinal(), 3);
-               I5.setToValueOf(bits.getLSBit(2));
-               I4.setToValueOf(bits.getLSBit(1));
-               I3.setToValueOf(bits.getLSBit(0));
+               I5.setState(bits.getLSBit(2));
+               I4.setState(bits.getLSBit(1));
+               I3.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setSrc(Am2901_Src src)
        {
                var bits = of(src.ordinal(), 3);
-               I2.setToValueOf(bits.getLSBit(2));
-               I1.setToValueOf(bits.getLSBit(1));
-               I0.setToValueOf(bits.getLSBit(0));
+               I2.setState(bits.getLSBit(2));
+               I1.setState(bits.getLSBit(1));
+               I0.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setReg_A(String val_4_bit)
        {
                var bits = BitVector.parse(val_4_bit);
-               A3.setToValueOf(bits.getLSBit(3));
-               A2.setToValueOf(bits.getLSBit(2));
-               A1.setToValueOf(bits.getLSBit(1));
-               A0.setToValueOf(bits.getLSBit(0));
+               A3.setState(bits.getLSBit(3));
+               A2.setState(bits.getLSBit(2));
+               A1.setState(bits.getLSBit(1));
+               A0.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setReg_B(String val_4_bit)
        {
                var bits = BitVector.parse(val_4_bit);
-               B3.setToValueOf(bits.getLSBit(3));
-               B2.setToValueOf(bits.getLSBit(2));
-               B1.setToValueOf(bits.getLSBit(1));
-               B0.setToValueOf(bits.getLSBit(0));
+               B3.setState(bits.getLSBit(3));
+               B2.setState(bits.getLSBit(2));
+               B1.setState(bits.getLSBit(1));
+               B0.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setCarryIn(String val_1_bit)
        {
-               Cn.setToValueOf(Bit.parse(val_1_bit));
+               Cn.setState(Bit.parse(val_1_bit));
        }
 
        @Override
@@ -227,40 +227,40 @@ public class TestableAm2901Impl implements TestableAm2901
        public void setD(String val_4_bit)
        {
                var bits = BitVector.parse(val_4_bit);
-               D4.setToValueOf(bits.getLSBit(3));
-               D3.setToValueOf(bits.getLSBit(2));
-               D2.setToValueOf(bits.getLSBit(1));
-               D1.setToValueOf(bits.getLSBit(0));
+               D4.setState(bits.getLSBit(3));
+               D3.setState(bits.getLSBit(2));
+               D2.setState(bits.getLSBit(1));
+               D1.setState(bits.getLSBit(0));
        }
 
        @Override
        public void setQ_0(String val_1_bit)
        {
-               IQn.setToValueOf(Bit.parse(val_1_bit));
+               IQn.setState(Bit.parse(val_1_bit));
        }
 
        @Override
        public void setQ_3(String val_1_bit)
        {
-               IQn_3.setToValueOf(Bit.parse(val_1_bit));
+               IQn_3.setState(Bit.parse(val_1_bit));
        }
 
        @Override
        public void setRAM_0(String val_1_bit)
        {
-               IRAMn.setToValueOf(Bit.parse(val_1_bit));
+               IRAMn.setState(Bit.parse(val_1_bit));
        }
 
        @Override
        public void setRAM_3(String val_1_bit)
        {
-               IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
+               IRAMn_3.setState(Bit.parse(val_1_bit));
        }
 
        @Override
        public void clockOn(boolean isClockOn)
        {
-               C.setState(isClockOn);
+               C.setState(isClockOn ? Bit.ONE : Bit.ZERO);
        }
 
        @Override
index 4ac98e3..4121200 100644 (file)
@@ -23,8 +23,8 @@ public class ClickableSubmodelComponentsTest
                                setSubmodelScale(.4);
                                setOutputPins("O0");
 
-                               GUIManualSwitch sw = new GUIManualSwitch(submodelModifiable);
-                               GUIBitDisplay bd = new GUIBitDisplay(submodelModifiable);
+                               GUIManualSwitch sw = new GUIManualSwitch(submodelModifiable, 1);
+                               GUIBitDisplay bd = new GUIBitDisplay(submodelModifiable, 1);
 
                                sw.moveTo(10, 5);
                                bd.moveTo(50, 5);
index 005b39a..58f7555 100644 (file)
@@ -19,9 +19,9 @@ public class RSLatchExample
        @SuppressWarnings("unused") // for GUIWires being created
        public static void createRSLatchExample(ViewModelModifiable model)
        {
-               GUIManualSwitch rIn = new GUIManualSwitch(model);
+               GUIManualSwitch rIn = new GUIManualSwitch(model, 1);
                rIn.moveTo(100, 100);
-               GUIManualSwitch sIn = new GUIManualSwitch(model);
+               GUIManualSwitch sIn = new GUIManualSwitch(model, 1);
                sIn.moveTo(100, 200);
 
                GUIOrGate or1 = new GUIOrGate(model, 1);
index c837fd4..b82d526 100644 (file)
@@ -28,18 +28,18 @@ public class GUIBitDisplay extends GUIComponent
        private final LogicObserver logicObs;
        private BitDisplay bitDisplay;
 
-       public GUIBitDisplay(ViewModelModifiable model)
+       public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
        {
-               this(model, null);
+               this(model, logicWidth, null);
        }
 
-       public GUIBitDisplay(ViewModelModifiable model, String name)
+       public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
        {
                super(model, name);
                logicObs = (i) -> model.requestRedraw();
 
                setSize(width, height);
-               addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
+               addPin(this.inputPin = new Pin(this, "", logicWidth, 0, height / 2));
        }
 
        @Override
@@ -89,6 +89,7 @@ public class GUIBitDisplay extends GUIComponent
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
-               IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(), (m, p, n) -> new GUIBitDisplay(m, n));
+               IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
+                               (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));
        }
 }
\ No newline at end of file
index 611d341..1d38516 100644 (file)
@@ -9,7 +9,7 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.LogicObservable;
 import net.mograsim.logic.core.LogicObserver;
 import net.mograsim.logic.core.components.ManualSwitch;
-import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
 import net.mograsim.logic.core.types.BitVectorFormatter;
 import net.mograsim.logic.core.wires.Wire.ReadEnd;
 import net.mograsim.logic.model.model.ViewModelModifiable;
@@ -32,18 +32,18 @@ public class GUIManualSwitch extends GUIComponent
        private ManualSwitch logicSwitch;
        private ReadEnd end;
 
-       public GUIManualSwitch(ViewModelModifiable model)
+       public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
        {
-               this(model, null);
+               this(model, logicWidth, null);
        }
 
-       public GUIManualSwitch(ViewModelModifiable model, String name)
+       public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
        {
                super(model, name);
                logicObs = (i) -> model.requestRedraw();
 
                setSize(width, height);
-               addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
+               addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
        }
 
        @Override
@@ -88,7 +88,7 @@ public class GUIManualSwitch extends GUIComponent
                {
                case "out":
                        if (logicSwitch != null)
-                               return logicSwitch.getValue();
+                               return logicSwitch.getValues();
                        return null;
                default:
                        return super.getHighLevelState(stateID);
@@ -102,7 +102,7 @@ public class GUIManualSwitch extends GUIComponent
                {
                case "out":
                        if (logicSwitch != null)
-                               logicSwitch.setToValueOf((Bit) newState);
+                               logicSwitch.setState((BitVector) newState);
                        break;
                default:
                        super.setHighLevelState(stateID, newState);
@@ -143,6 +143,7 @@ public class GUIManualSwitch extends GUIComponent
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
-               IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p, n) -> new GUIManualSwitch(m, n));
+               IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
+                               (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));
        }
 }
\ No newline at end of file
index 03cdbb4..42fada8 100644 (file)
@@ -41,9 +41,9 @@ public class SimulationPreview implements IThemePreview
                params.gateProcessTime = 50;
                params.wireTravelTime = 10;
 
-               GUIManualSwitch rIn = new GUIManualSwitch(model);
+               GUIManualSwitch rIn = new GUIManualSwitch(model, 1);
                rIn.moveTo(10, 10);
-               GUIManualSwitch sIn = new GUIManualSwitch(model);
+               GUIManualSwitch sIn = new GUIManualSwitch(model, 1);
                sIn.moveTo(10, 70);
 
                GUIOrGate or1 = new GUIOrGate(model, 1);
index 26d95cf..00b1956 100644 (file)
@@ -100,13 +100,13 @@ public class LogicUIPart extends ViewPart
                comp.moveTo(100, 0);
                for (int i = 0; i < inputPinNames.size(); i++)
                {
-                       GUIManualSwitch sw = new GUIManualSwitch(model);
+                       GUIManualSwitch sw = new GUIManualSwitch(model, 1);
                        sw.moveTo(0, 20 * i);
                        new GUIWire(model, comp.getPin(inputPinNames.get(i)), sw.getOutputPin());
                }
                for (int i = 0; i < outputPinNames.size(); i++)
                {
-                       GUIBitDisplay bd = new GUIBitDisplay(model);
+                       GUIBitDisplay bd = new GUIBitDisplay(model, 1);
                        bd.moveTo(200, 20 * i);
                        new GUIWire(model, comp.getPin(outputPinNames.get(i)), bd.getInputPin());
                }