X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FManualSwitch.java;h=26238fe656503624fdb26c562221d7be81e38eca;hb=026db85f46b6ea58e765ecff069545728eebdcac;hp=83842ddae1909768736880c42d7194313d62101d;hpb=b37ba7609a925cc945bbac0f6ead619d07912238;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java index 83842dda..26238fe6 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/ManualSwitch.java @@ -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,55 @@ public class ManualSwitch extends Component implements LogicObservable { private Collection observers; private ReadWriteEnd output; - private boolean isOn; 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; } - 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) { - 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.length()) + throw new IllegalArgumentException("Incorrect bit vector length"); + if (bits.equals(output.getInputValues())) + return; + output.feedSignals(bits); + notifyObservers(); } - public boolean isOn() + public boolean isFullOn() { - return isOn; + return BitVector.of(Bit.ONE, output.length()).equals(output.getInputValues()); } - public Bit getValue() + public BitVector getValues() { - return isOn ? Bit.ONE : Bit.ZERO; + return output.getInputValues(); } @Override