WireArrayEnd now created with U and GUITest supports U
[Mograsim.git] / era.mi / src / era / mi / logic / wires / WireArray.java
index b7a83fa..39af936 100644 (file)
@@ -1,5 +1,6 @@
 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
@@ -21,12 +22,13 @@ public class WireArray
        public final int travelTime;\r
        private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();\r
        public final int length;\r
-       private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();\r
+       private List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();\r
 \r
        public WireArray(int length, int travelTime)\r
        {\r
                if (length < 1)\r
-                       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));\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
@@ -34,12 +36,12 @@ public class WireArray
 \r
        private void initValues()\r
        {\r
-               values = Bit.Z.makeArray(length);\r
+               values = Bit.U.makeArray(length);\r
        }\r
 \r
        private void recalculateSingleInput()\r
        {\r
-               WireArrayInput input = inputs.get(0);\r
+               WireArrayEnd input = inputs.get(0);\r
                if (!Arrays.equals(input.getValues(), values))\r
                {\r
                        Bit[] oldValues = values.clone();\r
@@ -50,12 +52,12 @@ public class WireArray
 \r
        private void recalculateMultipleInputs()\r
        {\r
-               Iterator<WireArrayInput> it = inputs.iterator();\r
+               Iterator<WireArrayEnd> it = inputs.iterator();\r
                Bit[] newValues = it.next().inputValues.clone();\r
 \r
                while (it.hasNext())\r
                {\r
-                       WireArrayInput input = it.next();\r
+                       WireArrayEnd input = it.next();\r
                        Bit[] bits = input.getValues();\r
                        for (int i = 0; i < length; i++)\r
                        {\r
@@ -93,10 +95,8 @@ public class WireArray
        /**\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\r
-        *         <code>Bit.ZERO</code> (they do not all have to have the same value),\r
-        *         not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is\r
-        *         returned otherwise.\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
@@ -113,8 +113,7 @@ public class WireArray
        /**\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\r
-        *         corresponds with 2^0, value 1 is 2^1 and so on.\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
@@ -144,8 +143,7 @@ public class WireArray
        /**\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\r
-        *         corresponds with 2^0, value 1 is 2^1 and so on.\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
@@ -194,8 +192,7 @@ public class WireArray
        }\r
 \r
        /**\r
-        * @return An array of length n containing the values of the n bits in the\r
-        *         {@link WireArray}. Can be safely modified.\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
@@ -205,12 +202,10 @@ public class WireArray
        }\r
 \r
        /**\r
-        * Adds an {@link WireArrayObserver}, who will be notified when the value of the\r
-        * {@link WireArray} is updated.\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\r
-        *         registered, false otherwise\r
+        * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise\r
         * \r
         * @author Fabian Stemmler\r
         */\r
@@ -226,49 +221,47 @@ public class WireArray
        }\r
 \r
        /**\r
-        * Create and register a {@link WireArrayInput} object, which is tied to this\r
-        * {@link WireArray}.\r
+        * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}.\r
         */\r
-       public WireArrayInput createInput()\r
+       public WireArrayEnd createInput()\r
        {\r
-               return new WireArrayInput(this);\r
+               return new WireArrayEnd(this);\r
        }\r
 \r
-       private void registerInput(WireArrayInput toRegister)\r
+       private void registerInput(WireArrayEnd toRegister)\r
        {\r
                inputs.add(toRegister);\r
        }\r
 \r
        /**\r
-        * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}\r
-        * 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\r
-        * affect the final value, unless there are no other inputs than Z 0 and 1 turn\r
-        * into X when they are mixed\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 WireArrayInput\r
+       public class WireArrayEnd implements Closeable\r
        {\r
                public final WireArray owner;\r
+               private boolean open;\r
                private Bit[] inputValues;\r
 \r
-               private WireArrayInput(WireArray owner)\r
+               private 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.Z.makeArray(length);\r
+                       inputValues = Bit.U.makeArray(length);\r
                }\r
 \r
                /**\r
-                * Sets the wires values. This takes up time, as specified by the\r
-                * {@link WireArray}s travel time.\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
@@ -280,12 +273,12 @@ public class WireArray
                        {\r
                                feedSignals(0, newValues);\r
                        } else\r
-                               throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));\r
+                               throw new IllegalArgumentException(\r
+                                               String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));\r
                }\r
 \r
                /**\r
-                * Sets values of a subarray of wires. This takes up time, as specified by the\r
-                * {@link WireArray}s travel time.\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
@@ -294,6 +287,8 @@ public class WireArray
                 */\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
@@ -301,7 +296,9 @@ public class WireArray
                {\r
                        int exclLastIndex = startingBit + newValues.length;\r
                        if (length < exclLastIndex)\r
-                               throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));\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
@@ -310,7 +307,8 @@ public class WireArray
                }\r
 \r
                /**\r
-                * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.\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
@@ -318,30 +316,47 @@ public class WireArray
                }\r
 \r
                /**\r
-                * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.\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
+               public Bit[] wireValuesExcludingMe()\r
                {\r
                        Bit[] bits = Bit.Z.makeArray(length);\r
-                       for (WireArrayInput wai : inputs) \r
+                       for (WireArrayEnd wai : inputs)\r
                        {\r
-                               if(wai == this)\r
+                               if (wai == this)\r
                                        continue;\r
                                Util.combineInto(bits, wai.getValues());\r
                        }\r
                        return bits;\r
                }\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
@@ -350,9 +365,9 @@ public class WireArray
                return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);\r
        }\r
 \r
-       public static WireArrayInput[] extractInputs(WireArray[] w)\r
+       public static WireArrayEnd[] extractInputs(WireArray[] w)\r
        {\r
-               WireArrayInput[] inputs = new WireArrayInput[w.length];\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