-package net.mograsim.logic.core.wires;
-
-import static net.mograsim.logic.core.types.Bit.U;
-import static net.mograsim.logic.core.types.Bit.Z;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import net.mograsim.logic.core.timeline.Timeline;
-import net.mograsim.logic.core.types.Bit;
-import net.mograsim.logic.core.types.BitVector;
-import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
-
-/**
- * Represents an array of wires that can store n bits of information.
- *
- * @author Fabian Stemmler
- *
- */
-public class Wire
-{
- private BitVector values;
- public final int travelTime;
- private List<ReadEnd> attached = new ArrayList<ReadEnd>();
- public final int length;
- private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
- private Timeline timeline;
-
- public Wire(Timeline timeline, int length, int travelTime)
- {
- 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.timeline = timeline;
- this.length = length;
- 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());
- }
-
- private void setNewValues(BitVector newValues)
- {
- if (values.equals(newValues))
- return;
- BitVector oldValues = values;
- values = newValues;
- notifyObservers(oldValues);
- }
-
- private void recalculate()
- {
- switch (inputs.size())
- {
- case 0:
- return;
- case 1:
- recalculateSingleInput();
- break;
- default:
- recalculateMultipleInputs();
- }
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same
- * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
- *
- * @author Fabian Stemmler
- */
- public boolean hasNumericValue()
- {
- for (Bit b : values)
- {
- if (b != Bit.ZERO && b != Bit.ONE)
- return false;
- }
- return true;
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getUnsignedValue()
- {
- long val = 0;
- long mask = 1;
- for (Bit bit : values)
- {
- switch (bit)
- {
- default:
- case Z:
- case X:
- return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
- case ONE:
- val |= mask;
- break;
- case ZERO:
- }
- mask = mask << 1;
- }
- return val;
- }
-
- /**
- * The {@link Wire} is interpreted as a signed integer with n bits.
- *
- * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getSignedValue()
- {
- long val = getUnsignedValue();
- long mask = 1 << (length - 1);
- if ((mask & val) != 0)
- {
- int shifts = 64 - length;
- return (val << shifts) >> shifts;
- }
- return val;
- }
-
- public Bit getValue()
- {
- return getValue(0);
- }
-
- public Bit getValue(int index)
- {
- return values.getBit(index);
- }
-
- public BitVector getValues(int start, int end)
- {
- return values.subVector(start, end);
- }
-
- public BitVector getValues()
- {
- return values;
- }
-
- /**
- * Adds an {@link WireObserver}, 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
- *
- * @author Fabian Stemmler
- */
- private void attachEnd(ReadEnd end)
- {
- attached.add(end);
- }
-
- private void detachEnd(ReadEnd end)
- {
- attached.remove(end);
- }
-
- private void notifyObservers(BitVector oldValues)
- {
- for (ReadEnd o : attached)
- o.update(oldValues);
- }
-
- /**
- * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
- */
- public ReadWriteEnd createReadWriteEnd()
- {
- return new ReadWriteEnd();
- }
-
- /**
- * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
- */
- public ReadEnd createReadOnlyEnd()
- {
- return new ReadEnd();
- }
-
- private void registerInput(ReadWriteEnd toRegister)
- {
- inputs.add(toRegister);
- }
-
- /**
- * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
- * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0
- * and 1 turn into X when they are mixed
- *
- * @author Fabian Stemmler
- */
- public class ReadEnd
- {
- private List<WireObserver> observers = new ArrayList<WireObserver>();
-
- private ReadEnd()
- {
- super();
- Wire.this.attachEnd(this);
- }
-
- public void update(BitVector oldValues)
- {
- for (WireObserver ob : observers)
- ob.update(this, oldValues);
- }
-
- /**
- * Included for convenient use on {@link Wire}s of length 1.
- *
- * @return The value of bit 0.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue()
- {
- return Wire.this.getValue();
- }
-
- /**
- * @param index Index of the requested bit.
- * @return The value of the indexed bit.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue(int index)
- {
- 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();
- }
-
- /**
- * @param start Start of the wanted segment. (inclusive)
- * @param end End of the wanted segment. (exclusive)
- * @return The values of the segment of {@link Bit}s indexed.
- *
- * @author Fabian Stemmler
- */
- public BitVector getValues(int start, int end)
- {
- return Wire.this.getValues(start, end);
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the
- * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
- *
- * @author Fabian Stemmler
- */
- public boolean hasNumericValue()
- {
- return Wire.this.hasNumericValue();
- }
-
- /**
- * The {@link Wire} is interpreted as an unsigned integer with n bits.
- *
- * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getUnsignedValue()
- {
- return Wire.this.getUnsignedValue();
- }
-
- /**
- * The {@link Wire} is interpreted as a signed integer with n bits.
- *
- * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
- *
- * @author Fabian Stemmler
- */
- public long getSignedValue()
- {
- return Wire.this.getSignedValue();
- }
-
- @Override
- public String toString()
- {
- return Wire.this.toString();
- }
-
- public void close()
- {
- inputs.remove(this);
- detachEnd(this);
- recalculate();
- }
-
- public int length()
- {
- return length;
- }
-
- public boolean addObserver(WireObserver ob)
- {
- return observers.add(ob);
- }
-
- public Wire getWire()
- {
- return Wire.this;
- }
- }
-
- public class ReadWriteEnd extends ReadEnd
- {
- private boolean open;
- private BitVector inputValues;
-
- private ReadWriteEnd()
- {
- super();
- open = true;
- initValues();
- registerInput(this);
- }
-
- private void initValues()
- {
- inputValues = U.toVector(length);
- }
-
- /**
- * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
- *
- * @param newValues The new values the wires should take on.
- *
- * @author Fabian Stemmler
- */
- public void feedSignals(Bit... newValues)
- {
- feedSignals(BitVector.of(newValues));
- }
-
- public void feedSignals(BitVector newValues)
- {
- if (newValues.length() != length)
- throw new IllegalArgumentException(
- String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
- if (!open)
- throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
- timeline.addEvent(e -> setValues(newValues), travelTime);
- }
-
- /**
- * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
- *
- * @param bitVector The new values the wires should take on.
- * @param startingBit The first index of the subarray of wires.
- *
- * @author Fabian Stemmler
- */
- public void feedSignals(int startingBit, BitVector bitVector)
- {
- if (!open)
- throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
- timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
- }
-
- private void setValues(int startingBit, BitVector newValues)
- {
- // index check covered in equals
- if (!inputValues.equalsWithOffset(newValues, startingBit))
- {
- Bit[] vals = inputValues.getBits();
- System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
- inputValues = BitVector.of(vals);
- Wire.this.recalculate();
- }
- }
-
- private void setValues(BitVector newValues)
- {
- if (inputValues.equals(newValues))
- return;
- inputValues = newValues;
- Wire.this.recalculate();
- }
-
- /**
- * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
- */
- public Bit getInputValue()
- {
- return getInputValue(0);
- }
-
- /**
- * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
- */
- public Bit getInputValue(int index)
- {
- return inputValues.getBit(index);
- }
-
- /**
- * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
- */
- public BitVector getInputValues()
- {
- return getInputValues(0, length);
- }
-
- public BitVector getInputValues(int start, int end)
- {
- return inputValues.subVector(start, end);
- }
-
- /**
- * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
- */
- public void clearSignals()
- {
- feedSignals(Z.toVector(length));
- }
-
- public BitVector wireValuesExcludingMe()
- {
- BitVectorMutator mutator = BitVectorMutator.empty();
- for (ReadWriteEnd wireEnd : inputs)
- {
- if (wireEnd == this)
- continue;
- mutator.join(wireEnd.inputValues);
- }
- return mutator.get();
- }
-
- @Override
- public String toString()
- {
- return inputValues.toString();
- }
- }
-
- @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)
- }
-
- public static ReadEnd[] extractEnds(Wire[] w)
- {
- ReadEnd[] inputs = new ReadEnd[w.length];
- for (int i = 0; i < w.length; i++)
- inputs[i] = w[i].createReadWriteEnd();
- return inputs;
- }
+package net.mograsim.logic.core.wires;\r
+\r
+import static net.mograsim.logic.core.types.Bit.U;\r
+import static net.mograsim.logic.core.types.Bit.Z;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import net.mograsim.logic.core.LogicObservable;\r
+import net.mograsim.logic.core.LogicObserver;\r
+import net.mograsim.logic.core.timeline.Timeline;\r
+import net.mograsim.logic.core.types.Bit;\r
+import net.mograsim.logic.core.types.BitVector;\r
+import net.mograsim.logic.core.types.BitVector.BitVectorMutator;\r
+\r
+/**\r
+ * Represents an array of wires that can store n bits of information.\r
+ * \r
+ * @author Fabian Stemmler\r
+ *\r
+ */\r
+public class Wire\r
+{\r
+ private BitVector values;\r
+ public final int travelTime;\r
+ private List<ReadEnd> attached = new ArrayList<>();\r
+ public final int length;\r
+ List<ReadWriteEnd> inputs = new ArrayList<>();\r
+ Timeline timeline;\r
+\r
+ public Wire(Timeline timeline, int length, int travelTime)\r
+ {\r
+ if (length < 1)\r
+ throw new IllegalArgumentException(\r
+ String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));\r
+ this.timeline = timeline;\r
+ this.length = length;\r
+ this.travelTime = travelTime;\r
+ initValues();\r
+ }\r
+\r
+ private void initValues()\r
+ {\r
+ values = U.toVector(length);\r
+ }\r
+\r
+ private void recalculateSingleInput()\r
+ {\r
+ setNewValues(inputs.get(0).getInputValues());\r
+ }\r
+\r
+ private void recalculateMultipleInputs()\r
+ {\r
+ BitVectorMutator mutator = BitVectorMutator.empty();\r
+ for (ReadWriteEnd wireArrayEnd : inputs)\r
+ mutator.join(wireArrayEnd.getInputValues());\r
+ setNewValues(mutator.get());\r
+ }\r
+\r
+ private void setNewValues(BitVector newValues)\r
+ {\r
+ if (values.equals(newValues))\r
+ return;\r
+// BitVector oldValues = values;\r
+ values = newValues;\r
+ notifyObservers();\r
+ }\r
+\r
+ void recalculate()\r
+ {\r
+ switch (inputs.size())\r
+ {\r
+ case 0:\r
+ return;\r
+ case 1:\r
+ recalculateSingleInput();\r
+ break;\r
+ default:\r
+ recalculateMultipleInputs();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.\r
+ * \r
+ * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same\r
+ * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public boolean hasNumericValue()\r
+ {\r
+ for (Bit b : values)\r
+ {\r
+ if (b != Bit.ZERO && b != Bit.ONE)\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.\r
+ * \r
+ * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public long getUnsignedValue()\r
+ {\r
+ long val = 0;\r
+ long mask = 1;\r
+ for (Bit bit : values)\r
+ {\r
+ switch (bit)\r
+ {\r
+ default:\r
+ case Z:\r
+ case X:\r
+ return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;\r
+ case ONE:\r
+ val |= mask;\r
+ break;\r
+ case ZERO:\r
+ }\r
+ mask = mask << 1;\r
+ }\r
+ return val;\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as a signed integer with n bits.\r
+ * \r
+ * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public long getSignedValue()\r
+ {\r
+ long val = getUnsignedValue();\r
+ long mask = 1 << (length - 1);\r
+ if ((mask & val) != 0)\r
+ {\r
+ int shifts = 64 - length;\r
+ return (val << shifts) >> shifts;\r
+ }\r
+ return val;\r
+ }\r
+\r
+ public Bit getValue()\r
+ {\r
+ return getValue(0);\r
+ }\r
+\r
+ public Bit getValue(int index)\r
+ {\r
+ return values.getBit(index);\r
+ }\r
+\r
+ public BitVector getValues(int start, int end)\r
+ {\r
+ return values.subVector(start, end);\r
+ }\r
+\r
+ public BitVector getValues()\r
+ {\r
+ return values;\r
+ }\r
+\r
+ /**\r
+ * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.\r
+ * \r
+ * @param ob The {@link LogicObserver} to be notified of changes.\r
+ * @return true if the given {@link LogicObserver} was not already registered, false otherwise\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ void attachEnd(ReadEnd end)\r
+ {\r
+ attached.add(end);\r
+ }\r
+\r
+ void detachEnd(ReadEnd end)\r
+ {\r
+ attached.remove(end);\r
+ }\r
+\r
+ private void notifyObservers()\r
+ {\r
+ attached.forEach(r -> r.update());\r
+ }\r
+\r
+ /**\r
+ * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.\r
+ */\r
+ public ReadWriteEnd createReadWriteEnd()\r
+ {\r
+ return new ReadWriteEnd();\r
+ }\r
+\r
+ /**\r
+ * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.\r
+ */\r
+ public ReadEnd createReadOnlyEnd()\r
+ {\r
+ return new ReadEnd();\r
+ }\r
+\r
+ void registerInput(ReadWriteEnd toRegister)\r
+ {\r
+ inputs.add(toRegister);\r
+ }\r
+\r
+ /**\r
+ * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the\r
+ * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0\r
+ * and 1 turn into X when they are mixed\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public class ReadEnd implements LogicObservable\r
+ {\r
+ private List<LogicObserver> observers = new ArrayList<>();\r
+\r
+ ReadEnd()\r
+ {\r
+ super();\r
+ Wire.this.attachEnd(this);\r
+ }\r
+\r
+ public void update()\r
+ {\r
+ notifyObservers();\r
+ }\r
+\r
+ /**\r
+ * Included for convenient use on {@link Wire}s of length 1.\r
+ * \r
+ * @return The value of bit 0.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public Bit getValue()\r
+ {\r
+ return Wire.this.getValue();\r
+ }\r
+\r
+ /**\r
+ * @param index Index of the requested bit.\r
+ * @return The value of the indexed bit.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public Bit getValue(int index)\r
+ {\r
+ return Wire.this.getValue(index);\r
+ }\r
+\r
+ /**\r
+ * @param index Index of the requested bit.\r
+ * @return The value of the indexed bit.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public BitVector getValues()\r
+ {\r
+ return Wire.this.getValues();\r
+ }\r
+\r
+ /**\r
+ * @param start Start of the wanted segment. (inclusive)\r
+ * @param end End of the wanted segment. (exclusive)\r
+ * @return The values of the segment of {@link Bit}s indexed.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public BitVector getValues(int start, int end)\r
+ {\r
+ return Wire.this.getValues(start, end);\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.\r
+ * \r
+ * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the\r
+ * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public boolean hasNumericValue()\r
+ {\r
+ return Wire.this.hasNumericValue();\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as an unsigned integer with n bits.\r
+ * \r
+ * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public long getUnsignedValue()\r
+ {\r
+ return Wire.this.getUnsignedValue();\r
+ }\r
+\r
+ /**\r
+ * The {@link Wire} is interpreted as a signed integer with n bits.\r
+ * \r
+ * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public long getSignedValue()\r
+ {\r
+ return Wire.this.getSignedValue();\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ return Wire.this.toString();\r
+ }\r
+\r
+ public void close()\r
+ {\r
+ inputs.remove(this);\r
+ detachEnd(this);\r
+ recalculate();\r
+ }\r
+\r
+ public int length()\r
+ {\r
+ return length;\r
+ }\r
+\r
+ public Wire getWire()\r
+ {\r
+ return Wire.this;\r
+ }\r
+\r
+ @Override\r
+ public void registerObserver(LogicObserver ob)\r
+ {\r
+ observers.add(ob);\r
+ }\r
+\r
+ @Override\r
+ public void deregisterObserver(LogicObserver ob)\r
+ {\r
+ observers.remove(ob);\r
+ }\r
+\r
+ @Override\r
+ public void notifyObservers()\r
+ {\r
+ observers.forEach(ob -> ob.update(this));\r
+ }\r
+ }\r
+\r
+ public class ReadWriteEnd extends ReadEnd\r
+ {\r
+ private boolean open;\r
+ private BitVector inputValues;\r
+\r
+ ReadWriteEnd()\r
+ {\r
+ super();\r
+ open = true;\r
+ initValues();\r
+ registerInput(this);\r
+ }\r
+\r
+ private void initValues()\r
+ {\r
+ inputValues = U.toVector(length);\r
+ }\r
+\r
+ /**\r
+ * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.\r
+ * \r
+ * @param newValues The new values the wires should take on.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public void feedSignals(Bit... newValues)\r
+ {\r
+ feedSignals(BitVector.of(newValues));\r
+ }\r
+\r
+ public void feedSignals(BitVector newValues)\r
+ {\r
+ if (newValues.length() != length)\r
+ throw new IllegalArgumentException(\r
+ String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));\r
+ if (!open)\r
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");\r
+ timeline.addEvent(e -> setValues(newValues), travelTime);\r
+ }\r
+\r
+ /**\r
+ * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.\r
+ * \r
+ * @param bitVector The new values the wires should take on.\r
+ * @param startingBit The first index of the subarray of wires.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public void feedSignals(int startingBit, BitVector bitVector)\r
+ {\r
+ if (!open)\r
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");\r
+ timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);\r
+ }\r
+\r
+ private void setValues(int startingBit, BitVector newValues)\r
+ {\r
+ // index check covered in equals\r
+ if (!inputValues.equalsWithOffset(newValues, startingBit))\r
+ {\r
+ Bit[] vals = inputValues.getBits();\r
+ System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());\r
+ inputValues = BitVector.of(vals);\r
+ Wire.this.recalculate();\r
+ }\r
+ }\r
+\r
+ private void setValues(BitVector newValues)\r
+ {\r
+ if (inputValues.equals(newValues))\r
+ return;\r
+ inputValues = newValues;\r
+ Wire.this.recalculate();\r
+ }\r
+\r
+ /**\r
+ * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.\r
+ */\r
+ public Bit getInputValue()\r
+ {\r
+ return getInputValue(0);\r
+ }\r
+\r
+ /**\r
+ * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.\r
+ */\r
+ public Bit getInputValue(int index)\r
+ {\r
+ return inputValues.getBit(index);\r
+ }\r
+\r
+ /**\r
+ * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.\r
+ */\r
+ public BitVector getInputValues()\r
+ {\r
+ return getInputValues(0, length);\r
+ }\r
+\r
+ public BitVector getInputValues(int start, int end)\r
+ {\r
+ return inputValues.subVector(start, end);\r
+ }\r
+\r
+ /**\r
+ * {@link ReadEnd} now feeds Z into the associated {@link Wire}.\r
+ */\r
+ public void clearSignals()\r
+ {\r
+ feedSignals(Z.toVector(length));\r
+ }\r
+\r
+ public BitVector wireValuesExcludingMe()\r
+ {\r
+ BitVectorMutator mutator = BitVectorMutator.empty();\r
+ for (ReadWriteEnd wireEnd : inputs)\r
+ {\r
+ if (wireEnd == this)\r
+ continue;\r
+ mutator.join(wireEnd.inputValues);\r
+ }\r
+ return mutator.get();\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ return inputValues.toString();\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);\r
+ }\r
+\r
+ public static ReadEnd[] extractEnds(Wire[] w)\r
+ {\r
+ ReadEnd[] inputs = new ReadEnd[w.length];\r
+ for (int i = 0; i < w.length; i++)\r
+ inputs[i] = w[i].createReadWriteEnd();\r
+ return inputs;\r
+ }\r