Maybe needs review: fixed bug in ManualSwitch concerning the input
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / ManualSwitch.java
index 83842dd..fa31f0c 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;
 
@@ -21,59 +22,58 @@ public class ManualSwitch extends Component implements LogicObservable
 {
        private Collection<LogicObserver> observers;
        private ReadWriteEnd output;
-       private boolean isOn;
+       private BitVector inputValues;
 
        public ManualSwitch(Timeline timeline, ReadWriteEnd output)
        {
                super(timeline);
                observers = new ArrayList<>();
-               if (output.length() != 1)
-                       throw new IllegalArgumentException("Switch output can be only a single wire");
                this.output = output;
+               this.inputValues = output.getInputValues();
        }
 
-       public void switchOn()
+       public void switchFullOn()
        {
-               setState(true);
+               setState(BitVector.of(Bit.ONE, output.width()));
        }
 
-       public void switchOff()
+       public void switchFullOff()
        {
-               setState(false);
+               setState(BitVector.of(Bit.ZERO, output.width()));
        }
 
        public void toggle()
        {
-               setState(!isOn);
+               if (isFullOn())
+                       switchFullOff();
+               else
+                       switchFullOn();
        }
 
-       public void setState(boolean isOn)
+       public void setState(Bit bit)
        {
-               if (this.isOn == isOn)
-                       return;
-               this.isOn = isOn;
-               output.feedSignals(getValue());
-               notifyObservers();
+               setState(BitVector.of(bit));
        }
 
-       public void setToValueOf(Bit bit)
+       public void setState(BitVector bits)
        {
-               if (bit == Bit.ONE)
-                       switchOn();
-               else if (bit == Bit.ZERO)
-                       switchOff();
-               else
-                       throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
+               if (bits.length() != output.width())
+                       throw new IllegalArgumentException("Incorrect bit vector length");
+               if (bits.equals(inputValues))
+                       return;
+               inputValues = bits;
+               output.feedSignals(bits);
+               notifyObservers();
        }
 
-       public boolean isOn()
+       public boolean isFullOn()
        {
-               return isOn;
+               return BitVector.of(Bit.ONE, output.width()).equals(output.getInputValues());
        }
 
-       public Bit getValue()
+       public BitVector getValues()
        {
-               return isOn ? Bit.ONE : Bit.ZERO;
+               return inputValues;
        }
 
        @Override