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 83842dd..9ec98ed 100644 (file)
@@ -21,7 +21,6 @@ public class ManualSwitch extends Component implements LogicObservable
 {
        private Collection<LogicObserver> observers;
        private ReadWriteEnd output;
-       private boolean isOn;
 
        public ManualSwitch(Timeline timeline, ReadWriteEnd output)
        {
@@ -44,36 +43,32 @@ public class ManualSwitch extends Component implements LogicObservable
 
        public void toggle()
        {
-               setState(!isOn);
+               setState(!isOn());
        }
 
        public void setState(boolean isOn)
        {
-               if (this.isOn == isOn)
-                       return;
-               this.isOn = isOn;
-               output.feedSignals(getValue());
-               notifyObservers();
+               setToValueOf(isOn ? Bit.ONE : Bit.ZERO);
        }
 
        public void setToValueOf(Bit bit)
        {
-               if (bit == Bit.ONE)
-                       switchOn();
-               else if (bit == Bit.ZERO)
-                       switchOff();
-               else
+               if (!bit.isBinary())
                        throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
+               if (bit == output.getInputValue())
+                       return;
+               output.feedSignals(bit);
+               notifyObservers();
        }
 
        public boolean isOn()
        {
-               return isOn;
+               return output.getInputValue() == Bit.ONE;
        }
 
        public Bit getValue()
        {
-               return isOn ? Bit.ONE : Bit.ZERO;
+               return output.getInputValue();
        }
 
        @Override
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)
        {