1 package net.mograsim.logic.core.components;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import net.mograsim.logic.core.LogicObservable;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.timeline.Timeline;
10 import net.mograsim.logic.core.types.Bit;
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
13 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
16 * This class models a simple on/off (ONE/ZERO) switch for user interaction.
18 * @author Christian Femers
21 public class CoreManualSwitch extends CoreComponent implements LogicObservable
23 private Collection<LogicObserver> observers;
24 private ReadWriteEnd output;
25 private BitVector inputValues;
27 public CoreManualSwitch(Timeline timeline, ReadWriteEnd output)
30 observers = new ArrayList<>();
32 this.inputValues = output.getInputValues();
35 public void switchFullOn()
37 setState(BitVector.of(Bit.ONE, output.width()));
40 public void switchFullOff()
42 setState(BitVector.of(Bit.ZERO, output.width()));
53 public void setState(Bit bit)
55 setState(BitVector.of(bit));
58 public void setState(BitVector bits)
60 if (bits.length() != output.width())
61 throw new IllegalArgumentException("Incorrect bit vector length");
62 if (bits.equals(inputValues))
65 output.feedSignals(bits);
69 public boolean isFullOn()
71 return BitVector.of(Bit.ONE, output.width()).equals(output.getInputValues());
74 public BitVector getValues()
80 public List<ReadEnd> getAllInputs()
86 public List<ReadWriteEnd> getAllOutputs()
88 return List.of(output);
92 public void registerObserver(LogicObserver ob)
98 public void deregisterObserver(LogicObserver ob)
100 observers.remove(ob);
104 public void notifyObservers()
106 observers.forEach(ob -> ob.update(this));