X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2FManualSwitch.java;h=e5cdf1be639bdd839c0e2b4ee91f030629833d1e;hb=6345e8d7c88792fb1f92eb490e43b0decc15bf0f;hp=fc6e9432271c96fc69dd183e6b17fb762b4a447b;hpb=67c1d352795802dae0c045cedeed82c883819d4e;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 fc6e9432..e5cdf1be 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 @@ -1,74 +1,106 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.Bit; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -/** - * This class models a simple on/off (ONE/ZERO) switch for user interaction. - * - * @author Christian Femers - * - */ -public class ManualSwitch extends Component -{ - private ReadWriteEnd output; - private boolean isOn; - - public ManualSwitch(Timeline timeline, ReadWriteEnd output) - { - super(timeline); - if (output.length() != 1) - throw new IllegalArgumentException("Switch output can be only a single wire"); - this.output = output; - } - - public void switchOn() - { - setState(true); - } - - public void switchOff() - { - setState(false); - } - - public void toggle() - { - setState(!isOn); - } - - public void setState(boolean isOn) - { - if (this.isOn == isOn) - return; - this.isOn = isOn; - output.feedSignals(getValue()); - } - - public boolean isOn() - { - return isOn; - } - - public Bit getValue() - { - return isOn ? Bit.ONE : Bit.ZERO; - } - - @Override - public List getAllInputs() - { - return List.of(); - } - - @Override - public List getAllOutputs() - { - return List.of(output); - } - -} +package net.mograsim.logic.core.components; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +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; + +/** + * This class models a simple on/off (ONE/ZERO) switch for user interaction. + * + * @author Christian Femers + * + */ +public class ManualSwitch extends Component implements LogicObservable +{ + private Collection observers; + private ReadWriteEnd output; + + public ManualSwitch(Timeline timeline, ReadWriteEnd output) + { + super(timeline); + observers = new ArrayList<>(); + this.output = output; + } + + public void switchFullOn() + { + setState(BitVector.of(Bit.ONE, output.length())); + } + + public void switchFullOff() + { + setState(BitVector.of(Bit.ZERO, output.length())); + } + + public void toggle() + { + if (isFullOn()) + switchFullOff(); + else + switchFullOn(); + } + + public void setState(Bit bit) + { + setState(BitVector.of(bit)); + } + + public void setState(BitVector bits) + { + if (bits.length() != output.length()) + throw new IllegalArgumentException("Incorrect bit vector length"); + if (bits.equals(output.getInputValues())) + return; + output.feedSignals(bits); + notifyObservers(); + } + + public boolean isFullOn() + { + return BitVector.of(Bit.ONE, output.length()).equals(output.getInputValues()); + } + + public BitVector getValues() + { + return output.getInputValues(); + } + + @Override + public List getAllInputs() + { + return List.of(); + } + + @Override + public List getAllOutputs() + { + return List.of(output); + } + + @Override + public void registerObserver(LogicObserver ob) + { + observers.add(ob); + } + + @Override + public void deregisterObserver(LogicObserver ob) + { + observers.remove(ob); + } + + @Override + public void notifyObservers() + { + observers.forEach(ob -> ob.update(this)); + } + +}