+ public final WireArray owner;
+ private boolean open;
+ Bit[] inputValues;
+
+ WireArrayEnd(WireArray owner)
+ {
+ super();
+ this.owner = owner;
+ open = true;
+ initValues();
+ owner.registerInput(this);
+ }
+
+ private void initValues()
+ {
+ inputValues = Bit.U.makeArray(length);
+ }
+
+ /**
+ * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.
+ *
+ * @param newValues The new values the wires should take on.
+ *
+ * @author Fabian Stemmler
+ */
+ public void feedSignals(Bit... newValues)
+ {
+ if (newValues.length != length)
+ throw new IllegalArgumentException(
+ String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
+ feedSignals(0, newValues);
+
+ }
+
+ /**
+ * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.
+ *
+ * @param newValues 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, Bit... newValues)
+ {
+ if (!open)
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
+ Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
+ }
+
+ private void setValues(int startingBit, Bit... newValues)
+ {
+ int exclLastIndex = startingBit + newValues.length;
+ if (length < exclLastIndex)
+ throw new ArrayIndexOutOfBoundsException(
+ String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,
+ exclLastIndex - 1, length));
+ if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
+ {
+ System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
+ owner.recalculate();
+ }
+ }
+
+ /**
+ * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated
+ * {@link WireArray}.
+ */
+ public Bit[] getValues()
+ {
+ return inputValues.clone();
+ }
+
+ /**
+ * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}.
+ */
+ public void clearSignals()
+ {
+ feedSignals(Bit.Z.makeArray(length));
+ }
+
+ public Bit[] wireValuesExcludingMe()
+ {
+ Bit[] bits = Bit.Z.makeArray(length);
+ for (WireArrayEnd wai : inputs)
+ {
+ if (wai == this)
+ continue;
+ Util.combineInto(bits, wai.getValues());
+ }
+ return bits;
+ }
+
+ public Bit getWireValue()
+ {
+ return owner.getValue();
+ }
+
+ public Bit[] getWireValues()
+ {
+ return owner.getValues();
+ }
+
+ @Override
+ public String toString()
+ {
+ return Arrays.toString(inputValues);
+ }
+
+ @Override
+ public void close()
+ {
+ inputs.remove(this);
+ open = false;
+ }