X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fwires%2FWire.java;h=fc7d346856deae23397e630282f488116e91dcb8;hb=2bc7d9114dc95b4a2b75c402f3ed63bf8b4a09b2;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..fc7d3468 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,17 +68,18 @@ 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()) { case 0: - return; + setNewValues(BitVector.of(Bit.U, length)); + break; case 1: recalculateSingleInput(); break; @@ -165,27 +175,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 +213,7 @@ public class Wire return new ReadEnd(); } - private void registerInput(ReadWriteEnd toRegister) + void registerInput(ReadWriteEnd toRegister) { inputs.add(toRegister); } @@ -216,20 +225,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,26 +341,40 @@ 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)); } } public class ReadWriteEnd extends ReadEnd { - private boolean open; + private boolean open, isWriting; private BitVector inputValues; - private ReadWriteEnd() + ReadWriteEnd() { super(); open = true; + isWriting = true; initValues(); registerInput(this); } @@ -399,7 +421,11 @@ public class Wire timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime); } - private void setValues(int startingBit, BitVector newValues) + /** + * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via + * feedValues(...) with a delay. + */ + void setValues(int startingBit, BitVector newValues) { // index check covered in equals if (!inputValues.equalsWithOffset(newValues, startingBit)) @@ -411,7 +437,11 @@ public class Wire } } - private void setValues(BitVector newValues) + /** + * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via + * feedValues(...) with a delay. + */ + void setValues(BitVector newValues) { if (inputValues.equals(newValues)) return; @@ -473,13 +503,38 @@ public class Wire { return inputValues.toString(); } + + @Override + public void close() + { + super.close(); + open = false; + } + + void setWriting(boolean isWriting) + { + if (this.isWriting != isWriting) + { + this.isWriting = isWriting; + if (isWriting) + inputs.add(this); + else + inputs.remove(this); + Wire.this.recalculate(); + } + } + + boolean isWriting() + { + return isWriting; + } } @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) @@ -489,4 +544,70 @@ public class Wire inputs[i] = w[i].createReadWriteEnd(); return inputs; } + + // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits + /** + * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: + * The bits are permanently fused together. + * + * @param a The {@link Wire} to be (partially) fused with b + * @param b The {@link Wire} to be (partially) fused with a + * @param fromA The first bit of {@link Wire} a to be fused + * @param fromB The first bit of {@link Wire} b to be fused + * @param length The amount of bits to fuse + */ + public static void fuse(Wire a, Wire b, int fromA, int fromB, int length) + { + ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd(); + rA.setWriting(false); + rB.setWriting(false); + rA.setValues(BitVector.of(Bit.Z, a.length)); + rB.setValues(BitVector.of(Bit.Z, b.length)); + Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length); + rA.registerObserver(aF); + rB.registerObserver(bF); + aF.update(rA); + bF.update(rB); + } + + /** + * + * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are + * permanently fused together. + * + * @param a The {@link Wire} to be fused with b + * @param b The {@link Wire} to be fused with a + */ + public static void fuse(Wire a, Wire b) + { + fuse(a, b, 0, 0, a.length); + } + + private static class Fusion implements LogicObserver + { + private ReadWriteEnd target; + int fromSource, fromTarget, length; + + public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length) + { + this.target = target; + this.fromSource = fromSource; + this.fromTarget = fromTarget; + this.length = length; + } + + @Override + public void update(LogicObservable initiator) + { + ReadWriteEnd source = (ReadWriteEnd) initiator; + if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0) + target.setWriting(false); + else + { + target.setWriting(true); + BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length); + target.setValues(fromTarget, targetInput); + } + } + } } \ No newline at end of file