X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fwires%2FWire.java;h=b8990cc2d5769d57b918b32d7677b60eb20fe0f2;hb=61621670ab3ea56eea6571cc9bba5e015c9f0861;hp=bae4cb417ca4e970fb5d8f7eb1756471b4a83a25;hpb=0a7480e192f9710432f4945923cc739ec51be9b8;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 bae4cb41..b8990cc2 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 @@ -25,84 +25,75 @@ public class Wire private BitVector values; public final int travelTime; private List attached = new ArrayList<>(); - public final int length; + public final int width; List inputs = new ArrayList<>(); Timeline timeline; - public Wire(Timeline timeline, int length, int travelTime) + public Wire(Timeline timeline, int width, int travelTime) { - this(timeline, length, travelTime, null); + this(timeline, width, travelTime, null); } - public Wire(Timeline timeline, int length, int travelTime, String name) + public Wire(Timeline timeline, int width, int travelTime, String name) { - if (length < 1) + if (width < 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)); + String.format("Tried to create an array of wires with width %d, but a width of less than 1 makes no sense.", width)); this.name = name; this.timeline = timeline; - this.length = length; + this.width = width; this.travelTime = travelTime; initValues(); } private void initValues() { - values = U.toVector(length); - } - - private void recalculateSingleInput() - { - setNewValues(inputs.get(0).getInputValues()); - } - - private void recalculateMultipleInputs() - { - BitVectorMutator mutator = BitVectorMutator.empty(); - for (ReadWriteEnd wireArrayEnd : inputs) - mutator.join(wireArrayEnd.getInputValues()); - setNewValues(mutator.get()); + values = U.toVector(width); } private void setNewValues(BitVector newValues) { if (values.equals(newValues)) return; -// BitVector oldValues = values; values = newValues; notifyObservers(); } void recalculate() { - switch (inputs.size()) + if (inputs.isEmpty()) + setNewValues(U.toVector(width)); + else { - case 0: - return; - case 1: - recalculateSingleInput(); - break; - default: - recalculateMultipleInputs(); + BitVectorMutator mutator = BitVectorMutator.empty(); + for (ReadWriteEnd wireArrayEnd : inputs) + mutator.join(wireArrayEnd.getInputValues()); + setNewValues(mutator.toBitVector()); } } + /** + * Forces a Wire to take on specific values. If the new values differ from the old ones, the observers of the Wire will be notified. + * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector) + * + * @param values The values the Wire will have immediately after this method is called + */ + public void forceValues(BitVector values) + { + setNewValues(values); + } + /** * The {@link Wire} is interpreted as an unsigned integer with n bits. * * @return true if all bits are either Bit.ONE or Bit.ZERO (they do not all have to have the same - * value), not Bit.X or Bit.Z. false is returned otherwise. + * value), not Bit.U, Bit.X or Bit.Z. false is returned otherwise. * * @author Fabian Stemmler */ public boolean hasNumericValue() { - for (Bit b : values) - { - if (b != Bit.ZERO && b != Bit.ONE) - return false; - } - return true; + return values.isBinary(); } /** @@ -144,23 +135,29 @@ public class Wire public long getSignedValue() { long val = getUnsignedValue(); - long mask = 1 << (length - 1); + long mask = 1 << (width - 1); if ((mask & val) != 0) { - int shifts = 64 - length; + int shifts = 64 - width; return (val << shifts) >> shifts; } return val; } + /** + * Returns the least significant bit (LSB) + */ public Bit getValue() { return getValue(0); } + /** + * Returns the least significant bit (LSB) of the given index + */ public Bit getValue(int index) { - return values.getBit(index); + return values.getLSBit(index); } public BitVector getValues(int start, int end) @@ -181,9 +178,9 @@ public class Wire * * @author Fabian Stemmler */ - void attachEnd(ReadEnd end) + boolean attachEnd(ReadEnd end) { - attached.add(end); + return attached.add(end); } void detachEnd(ReadEnd end) @@ -193,7 +190,7 @@ public class Wire private void notifyObservers() { - attached.forEach(r -> r.update()); + attached.forEach(ReadEnd::update); } /** @@ -240,7 +237,7 @@ public class Wire } /** - * Included for convenient use on {@link Wire}s of length 1. + * Included for convenient use on {@link Wire}s of width 1. * * @return The value of bit 0. * @@ -262,12 +259,6 @@ public class Wire return Wire.this.getValue(index); } - /** - * @param index Index of the requested bit. - * @return The value of the indexed bit. - * - * @author Fabian Stemmler - */ public BitVector getValues() { return Wire.this.getValues(); @@ -335,9 +326,9 @@ public class Wire recalculate(); } - public int length() + public int width() { - return length; + return width; } public Wire getWire() @@ -367,19 +358,21 @@ public class Wire public class ReadWriteEnd extends ReadEnd { private boolean open; + private boolean isWriting; private BitVector inputValues; ReadWriteEnd() { super(); open = true; + isWriting = true; initValues(); registerInput(this); } private void initValues() { - inputValues = U.toVector(length); + inputValues = U.toVector(width); } /** @@ -396,11 +389,11 @@ public class Wire public void feedSignals(BitVector newValues) { - if (newValues.length() != length) + if (newValues.length() != width) throw new IllegalArgumentException( - String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length)); + String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), width)); if (!open) - throw new RuntimeException("Attempted to write to closed WireArrayEnd."); + throw new IllegalStateException("Attempted to write to closed WireArrayEnd."); timeline.addEvent(e -> setValues(newValues), travelTime); } @@ -415,11 +408,15 @@ public class Wire public void feedSignals(int startingBit, BitVector bitVector) { if (!open) - throw new RuntimeException("Attempted to write to closed WireArrayEnd."); + throw new IllegalStateException("Attempted to write to closed WireArrayEnd."); 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)) @@ -431,7 +428,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; @@ -440,7 +441,8 @@ public class Wire } /** - * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}. + * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least + * significant bit (LSB) */ public Bit getInputValue() { @@ -449,10 +451,12 @@ public class Wire /** * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}. + * Returns the least significant bit (LSB) + * */ public Bit getInputValue(int index) { - return inputValues.getBit(index); + return inputValues.getLSBit(index); } /** @@ -460,7 +464,7 @@ public class Wire */ public BitVector getInputValues() { - return getInputValues(0, length); + return inputValues; } public BitVector getInputValues(int start, int end) @@ -473,7 +477,7 @@ public class Wire */ public void clearSignals() { - feedSignals(Z.toVector(length)); + feedSignals(Z.toVector(width)); } public BitVector wireValuesExcludingMe() @@ -485,7 +489,7 @@ public class Wire continue; mutator.join(wireEnd.inputValues); } - return mutator.get(); + return mutator.toBitVector(); } @Override @@ -493,6 +497,31 @@ 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 @@ -509,4 +538,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 width The amount of bits to fuse + */ + public static void fuse(Wire a, Wire b, int fromA, int fromB, int width) + { + ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd(); + rA.setWriting(false); + rB.setWriting(false); + rA.setValues(BitVector.of(Bit.Z, a.width)); + rB.setValues(BitVector.of(Bit.Z, b.width)); + Fusion aF = new Fusion(rB, fromA, fromB, width), bF = new Fusion(rA, fromB, fromA, width); + 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.width); + } + + private static class Fusion implements LogicObserver + { + private ReadWriteEnd target; + int fromSource, fromTarget, width; + + public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int width) + { + this.target = target; + this.fromSource = fromSource; + this.fromTarget = fromTarget; + this.width = width; + } + + @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 + width); + target.setValues(fromTarget, targetInput); + } + } + } } \ No newline at end of file