Fixed ManualSwitch concerning U and added Bit methods
authorChristian Femers <femers@in.tum.de>
Sat, 29 Jun 2019 01:28:14 +0000 (03:28 +0200)
committerChristian Femers <femers@in.tum.de>
Sat, 29 Jun 2019 01:28:14 +0000 (03:28 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java
net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java

index 626804f..7f4f265 100644 (file)
@@ -21,7 +21,6 @@ public class ManualSwitch extends Component implements LogicObservable
 {\r
        private Collection<LogicObserver> observers;\r
        private ReadWriteEnd output;\r
-       private boolean isOn;\r
 \r
        public ManualSwitch(Timeline timeline, ReadWriteEnd output)\r
        {\r
@@ -44,36 +43,32 @@ public class ManualSwitch extends Component implements LogicObservable
 \r
        public void toggle()\r
        {\r
-               setState(!isOn);\r
+               setState(!isOn());\r
        }\r
 \r
        public void setState(boolean isOn)\r
        {\r
-               if (this.isOn == isOn)\r
-                       return;\r
-               this.isOn = isOn;\r
-               output.feedSignals(getValue());\r
-               notifyObservers();\r
+               setToValueOf(isOn ? Bit.ONE : Bit.ZERO);\r
        }\r
 \r
        public void setToValueOf(Bit bit)\r
        {\r
-               if (bit == Bit.ONE)\r
-                       switchOn();\r
-               else if (bit == Bit.ZERO)\r
-                       switchOff();\r
-               else\r
+               if (!bit.isBinary())\r
                        throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);\r
+               if (bit == output.getInputValue())\r
+                       return;\r
+               output.feedSignals(bit);\r
+               notifyObservers();\r
        }\r
 \r
        public boolean isOn()\r
        {\r
-               return isOn;\r
+               return output.getInputValue() == Bit.ONE;\r
        }\r
 \r
        public Bit getValue()\r
        {\r
-               return isOn ? Bit.ONE : Bit.ZERO;\r
+               return output.getInputValue();\r
        }\r
 \r
        @Override\r
index 707c5ad..4fce8dd 100644 (file)
@@ -18,6 +18,11 @@ public enum Bit implements StrictLogicType<Bit>
                this.symbol = symbol;
        }
 
+       public boolean isBinary()
+       {
+               return this == ONE || this == ZERO;
+       }
+
        @Override
        public Bit and(Bit other)
        {