X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fwires%2FWire.java;h=b0e96f1db97b1fd834a0c5fc4f27b1bf4a3191fb;hb=2f2269e36940705063adba3ff89ed7830c0b2edf;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..b0e96f1d 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 @@ -51,19 +51,6 @@ public class Wire 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()); - } - private void setNewValues(BitVector newValues) { if (values.equals(newValues)) @@ -75,18 +62,28 @@ public class Wire void recalculate() { - switch (inputs.size()) + if (inputs.size() == 0) + setNewValues(BitVector.of(Bit.U, length)); + 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. * @@ -153,14 +150,20 @@ public class Wire 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) @@ -366,13 +369,14 @@ public class Wire public class ReadWriteEnd extends ReadEnd { - private boolean open; + private boolean open, isWriting; private BitVector inputValues; ReadWriteEnd() { super(); open = true; + isWriting = true; initValues(); registerInput(this); } @@ -419,7 +423,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)) @@ -431,7 +439,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 +452,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 +462,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 +475,7 @@ public class Wire */ public BitVector getInputValues() { - return getInputValues(0, length); + return inputValues; } public BitVector getInputValues(int start, int end) @@ -485,7 +500,7 @@ public class Wire continue; mutator.join(wireEnd.inputValues); } - return mutator.get(); + return mutator.toBitVector(); } @Override @@ -493,6 +508,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 +549,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