X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fwires%2FWire.java;h=bae4cb417ca4e970fb5d8f7eb1756471b4a83a25;hb=0a7480e192f9710432f4945923cc739ec51be9b8;hp=d73ee61c8f4ddf0b6a4469db237cdce99fb5e68a;hpb=07faf07e3acb8b2afdc2bf65a46bc868faaed0f8;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java index d73ee61c..bae4cb41 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java @@ -6,6 +6,8 @@ import static net.mograsim.logic.core.types.Bit.Z; import java.util.ArrayList; 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; @@ -19,18 +21,25 @@ import net.mograsim.logic.core.types.BitVector.BitVectorMutator; */ public class Wire { + public final String name; private BitVector values; public final int travelTime; - private List attached = new ArrayList(); + private List attached = new ArrayList<>(); public final int length; - private List inputs = new ArrayList(); - private Timeline timeline; + List inputs = new ArrayList<>(); + Timeline timeline; public Wire(Timeline timeline, int length, int travelTime) + { + this(timeline, length, travelTime, null); + } + + public Wire(Timeline timeline, int length, int travelTime, String name) { if (length < 1) throw new IllegalArgumentException( String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length)); + this.name = name; this.timeline = timeline; this.length = length; this.travelTime = travelTime; @@ -59,12 +68,12 @@ public class Wire { if (values.equals(newValues)) return; - BitVector oldValues = values; +// BitVector oldValues = values; values = newValues; - notifyObservers(oldValues); + notifyObservers(); } - private void recalculate() + void recalculate() { switch (inputs.size()) { @@ -165,27 +174,26 @@ public class Wire } /** - * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated. + * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated. * - * @param ob The {@link WireObserver} to be notified of changes. - * @return true if the given {@link WireObserver} was not already registered, false otherwise + * @param ob The {@link LogicObserver} to be notified of changes. + * @return true if the given {@link LogicObserver} was not already registered, false otherwise * * @author Fabian Stemmler */ - private void attachEnd(ReadEnd end) + void attachEnd(ReadEnd end) { attached.add(end); } - private void detachEnd(ReadEnd end) + void detachEnd(ReadEnd end) { attached.remove(end); } - private void notifyObservers(BitVector oldValues) + private void notifyObservers() { - for (ReadEnd o : attached) - o.update(oldValues); + attached.forEach(r -> r.update()); } /** @@ -204,7 +212,7 @@ public class Wire return new ReadEnd(); } - private void registerInput(ReadWriteEnd toRegister) + void registerInput(ReadWriteEnd toRegister) { inputs.add(toRegister); } @@ -216,20 +224,19 @@ public class Wire * * @author Fabian Stemmler */ - public class ReadEnd + public class ReadEnd implements LogicObservable { - private List observers = new ArrayList(); + private List observers = new ArrayList<>(); - private ReadEnd() + ReadEnd() { super(); Wire.this.attachEnd(this); } - public void update(BitVector oldValues) + public void update() { - for (WireObserver ob : observers) - ob.update(this, oldValues); + notifyObservers(); } /** @@ -333,14 +340,27 @@ public class Wire return length; } - public boolean addObserver(WireObserver ob) + public Wire getWire() { - return observers.add(ob); + return Wire.this; } - public Wire getWire() + @Override + public void registerObserver(LogicObserver ob) { - return Wire.this; + observers.add(ob); + } + + @Override + public void deregisterObserver(LogicObserver ob) + { + observers.remove(ob); + } + + @Override + public void notifyObservers() + { + observers.forEach(ob -> ob.update(this)); } } @@ -349,7 +369,7 @@ public class Wire private boolean open; private BitVector inputValues; - private ReadWriteEnd() + ReadWriteEnd() { super(); open = true; @@ -478,8 +498,8 @@ public class Wire @Override public String toString() { - return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs); - // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2) + String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name; + return String.format("wire %s value: %s inputs: %s", name, values, inputs); } public static ReadEnd[] extractEnds(Wire[] w)