From: Christian Femers Date: Sat, 29 Jun 2019 01:28:14 +0000 (+0200) Subject: Fixed ManualSwitch concerning U and added Bit methods X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=c6ad3a18e8a437d7b131b203267fe2723708641f;p=Mograsim.git Fixed ManualSwitch concerning U and added Bit methods --- 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..9ec98ede 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 @@ -21,7 +21,6 @@ public class ManualSwitch extends Component implements LogicObservable { private Collection 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 diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java index 707c5ad4..4fce8dde 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java @@ -18,6 +18,11 @@ public enum Bit implements StrictLogicType this.symbol = symbol; } + public boolean isBinary() + { + return this == ONE || this == ZERO; + } + @Override public Bit and(Bit other) {