-package era.mi.logic.wires;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-
-import era.mi.logic.Bit;
-import era.mi.logic.Simulation;
-
-/**
- * Represents an array of wires that can store n bits of information.
- * @author Fabian Stemmler
- *
- */
-public class WireArray
-{
- private Bit[] values;
- public final int travelTime;
- private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
- public final int length;
- private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
-
- public WireArray(int length, int travelTime)
- {
- if(length < 1)
- throw new IllegalArgumentException("Tried to create an array of wires with length " + length + ", but a length of less than 1 makes no sense.");
- this.length = length;
- this.travelTime = travelTime;
- initValues();
- }
-
- private void initValues()
- {
- values = new Bit[length];
- for(int i = 0; i < length; i++)
- values[i] = Bit.Z;
- }
-
- private void recalculateSingleInput()
- {
- WireArrayInput input = inputs.get(0);
- if(!Arrays.equals(input.getValues(), values))
- {
- System.arraycopy(input.getValues(), 0, values, 0, length);
- notifyObservers();
- }
- }
-
- private void recalculateMultipleInputs()
- {
- Iterator<WireArrayInput> it = inputs.iterator();
- Bit[] newValues = it.next().values.clone();
-
- while(it.hasNext())
- {
- WireArrayInput input = it.next();
- Bit[] bits = input.getValues();
- for(int i = 0; i < length; i++)
- {
- if(Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
- continue;
- else if(Bit.Z.equals(newValues[i]))
- newValues[i] = bits[i];
- else
- newValues[i] = Bit.X;
- }
- }
-
- if(!Arrays.equals(newValues, values))
- {
- notifyObservers();
- values = newValues;
- }
- }
-
- private void recalculate()
- {
- switch(inputs.size())
- {
- case 0:
- return;
- case 1:
- recalculateSingleInput();
- break;
- default:
- recalculateMultipleInputs();
- }
- }
-
- /**
- * The WireArray 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 WireArray is interpreted as an unsigned integer with n bits.
- * @return The unsigned value of the {@link WireArray}'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(int i = 0; i < length; i++)
- {
- switch(values[i])
- {
- default:
- case Z:
- case X:
- return 0; //TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; Random number?
- case ONE:
- val |= mask;
- break;
- case ZERO:
- }
- mask = mask << 1;
- }
- return val;
- }
-
- /**
- * The WireArray is interpreted as a signed integer with n bits.
- * @return The signed value of the {@link WireArray}'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;
- }
-
- /**
- * Included for convenient use on {@link WireArray}s of length 1.
- * @return The value of bit 0.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue()
- {
- return getValue(0);
- }
-
- /**
- *
- * @param index Index of the requested bit.
- * @return The value of the indexed bit.
- *
- * @author Fabian Stemmler
- */
- public Bit getValue(int index)
- {
- return values[index];
- }
-
- public Bit[] getValues(int start, int end)
- {
- int length = end - start;
- Bit[] bits = new Bit[length];
- System.arraycopy(values, start, bits, 0, length);
- return bits;
- }
-
-
- /**
- * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
- *
- * @author Fabian Stemmler
- */
- public Bit[] getValues()
- {
- return values.clone();
- }
-
- /**
- * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.
- * @param ob The {@link WireArrayObserver} to be notified of changes.
- * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
- *
- * @author Fabian Stemmler
- */
- public boolean addObserver(WireArrayObserver ob)
- {
- return observers.add(ob);
- }
-
- private void notifyObservers()
- {
- for(WireArrayObserver o : observers)
- o.update(this);
- }
-
- public WireArrayInput createInput()
- {
- return new WireArrayInput(this);
- }
-
- private void registerInput(WireArrayInput toRegister)
- {
- inputs.add(toRegister);
- }
-
- public class WireArrayInput {
- public final WireArray owner;
- private Bit[] values;
-
- private WireArrayInput(WireArray owner) {
- super();
- this.owner = owner;
- initValues();
- owner.registerInput(this);
- }
-
- private void initValues()
- {
- values = new Bit[length];
- for(int i = 0; i < length; i++)
- values[i] = Bit.Z;
- }
-
- /**
- * 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)
- {
- feedSignals(0, newValues);
- }
- else
- throw new IllegalArgumentException("Attempted to input " + newValues.length + " bits instead of " + length + " bits.");
- }
-
- /**
- * 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)
- {
- 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("Attempted to input bits from index " + startingBit + " to "
- + exclLastIndex + " when there are only " + length + "wires.");
- if(!Arrays.equals(values, startingBit, exclLastIndex, newValues, 0, newValues.length))
- {
- System.arraycopy(newValues, 0, values, startingBit, newValues.length);
- owner.recalculate();
- }
- }
-
- public Bit[] getValues()
- {
- return values.clone();
- }
-
- public void clearSignals()
- {
- Bit[] bits = new Bit[length];
- for(int i = 0; i < length; i++)
- bits[i] = Bit.Z;
- feedSignals(bits);
- }
-
- @Override
- public String toString()
- {
- return Arrays.toString(values);
- }
- }
-
- @Override
- public String toString()
- {
- return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
- }
-}
+package era.mi.logic.wires;\r
+\r
+import java.io.Closeable;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+\r
+import era.mi.logic.Bit;\r
+import era.mi.logic.Simulation;\r
+import era.mi.logic.Util;\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 WireArray\r
+{\r
+ private Bit[] values;\r
+ public final int travelTime;\r
+ private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();\r
+ public final int length;\r
+ List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();\r
+\r
+ public WireArray(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.length = length;\r
+ this.travelTime = travelTime;\r
+ initValues();\r
+ }\r
+\r
+ private void initValues()\r
+ {\r
+ values = Bit.U.makeArray(length);\r
+ }\r
+\r
+ private void recalculateSingleInput()\r
+ {\r
+ WireArrayEnd input = inputs.get(0);\r
+ if (!Arrays.equals(input.getValues(), values))\r
+ {\r
+ Bit[] oldValues = values.clone();\r
+ System.arraycopy(input.getValues(), 0, values, 0, length);\r
+ notifyObservers(oldValues);\r
+ }\r
+ }\r
+\r
+ private void recalculateMultipleInputs()\r
+ {\r
+ Iterator<WireArrayEnd> it = inputs.iterator();\r
+ Bit[] newValues = it.next().inputValues.clone();\r
+\r
+ while (it.hasNext())\r
+ {\r
+ WireArrayEnd input = it.next();\r
+ Bit[] bits = input.getValues();\r
+ for (int i = 0; i < length; i++)\r
+ {\r
+ newValues[i] = newValues[i].combineWith(bits[i]);\r
+ }\r
+ }\r
+\r
+ if (!Arrays.equals(newValues, values))\r
+ {\r
+ Bit[] oldValues = values;\r
+ values = newValues;\r
+ notifyObservers(oldValues);\r
+ }\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 WireArray 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 WireArray is interpreted as an unsigned integer with n bits.\r
+ * \r
+ * @return The unsigned value of the {@link WireArray}'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 (int i = 0; i < length; i++)\r
+ {\r
+ switch (values[i])\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
+ // Random number?\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 WireArray is interpreted as a signed integer with n bits.\r
+ * \r
+ * @return The signed value of the {@link WireArray}'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
+ /**\r
+ * Included for convenient use on {@link WireArray}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 getValue(0);\r
+ }\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 values[index];\r
+ }\r
+\r
+ public Bit[] getValues(int start, int end)\r
+ {\r
+ int length = end - start;\r
+ Bit[] bits = new Bit[length];\r
+ System.arraycopy(values, start, bits, 0, length);\r
+ return bits;\r
+ }\r
+\r
+ /**\r
+ * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public Bit[] getValues()\r
+ {\r
+ return values.clone();\r
+ }\r
+\r
+ /**\r
+ * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.\r
+ * \r
+ * @param ob The {@link WireArrayObserver} to be notified of changes.\r
+ * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public boolean addObserver(WireArrayObserver ob)\r
+ {\r
+ return observers.add(ob);\r
+ }\r
+\r
+ private void notifyObservers(Bit[] oldValues)\r
+ {\r
+ for (WireArrayObserver o : observers)\r
+ o.update(this, oldValues);\r
+ }\r
+\r
+ /**\r
+ * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}.\r
+ */\r
+ public WireArrayEnd createInput()\r
+ {\r
+ return new WireArrayEnd(this);\r
+ }\r
+\r
+ void registerInput(WireArrayEnd toRegister)\r
+ {\r
+ inputs.add(toRegister);\r
+ }\r
+\r
+ /**\r
+ * A {@link WireArrayEnd} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines the\r
+ * {@link WireArray}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than\r
+ * Z 0 and 1 turn into X when they are mixed\r
+ * \r
+ * @author Fabian Stemmler\r
+ */\r
+ public class WireArrayEnd implements Closeable\r
+ {\r
+ public final WireArray owner;\r
+ private boolean open;\r
+ Bit[] inputValues;\r
+\r
+ WireArrayEnd(WireArray owner)\r
+ {\r
+ super();\r
+ this.owner = owner;\r
+ open = true;\r
+ initValues();\r
+ owner.registerInput(this);\r
+ }\r
+\r
+ private void initValues()\r
+ {\r
+ inputValues = Bit.U.makeArray(length);\r
+ }\r
+\r
+ /**\r
+ * Sets the wires values. This takes up time, as specified by the {@link WireArray}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
+ if (newValues.length != length)\r
+ throw new IllegalArgumentException(\r
+ String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));\r
+ feedSignals(0, newValues);\r
+\r
+ }\r
+\r
+ /**\r
+ * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.\r
+ * \r
+ * @param newValues 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, Bit... newValues)\r
+ {\r
+ if (!open)\r
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");\r
+ Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);\r
+ }\r
+\r
+ private void setValues(int startingBit, Bit... newValues)\r
+ {\r
+ int exclLastIndex = startingBit + newValues.length;\r
+ if (length < exclLastIndex)\r
+ throw new ArrayIndexOutOfBoundsException(\r
+ String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,\r
+ exclLastIndex - 1, length));\r
+ if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))\r
+ {\r
+ System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);\r
+ owner.recalculate();\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated\r
+ * {@link WireArray}.\r
+ */\r
+ public Bit[] getValues()\r
+ {\r
+ return inputValues.clone();\r
+ }\r
+\r
+ /**\r
+ * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}.\r
+ */\r
+ public void clearSignals()\r
+ {\r
+ feedSignals(Bit.Z.makeArray(length));\r
+ }\r
+\r
+ public Bit[] wireValuesExcludingMe()\r
+ {\r
+ Bit[] bits = Bit.Z.makeArray(length);\r
+ for (WireArrayEnd wai : inputs)\r
+ {\r
+ if (wai == this)\r
+ continue;\r
+ Util.combineInto(bits, wai.getValues());\r
+ }\r
+ return bits;\r
+ }\r
+\r
+ public Bit getWireValue()\r
+ {\r
+ return owner.getValue();\r
+ }\r
+\r
+ public Bit[] getWireValues()\r
+ {\r
+ return owner.getValues();\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ return Arrays.toString(inputValues);\r
+ }\r
+\r
+ @Override\r
+ public void close()\r
+ {\r
+ inputs.remove(this);\r
+ open = false;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);\r
+ }\r
+\r
+ public static WireArrayEnd[] extractInputs(WireArray[] w)\r
+ {\r
+ WireArrayEnd[] inputs = new WireArrayEnd[w.length];\r
+ for (int i = 0; i < w.length; i++)\r
+ inputs[i] = w[i].createInput();\r
+ return inputs;\r
+ }\r
+}
\ No newline at end of file