Fixed calculations concerning U, tests work now just like before
[Mograsim.git] / era.mi / src / era / mi / logic / wires / WireArray.java
index c2e974a..a0bd7f8 100644 (file)
@@ -1,5 +1,6 @@
 package era.mi.logic.wires;
 
+import java.io.Closeable;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -21,12 +22,13 @@ public class WireArray
        public final int travelTime;
        private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
        public final int length;
-       private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
+       private List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();
 
        public WireArray(int length, int travelTime)
        {
                if (length < 1)
-                       throw new IllegalArgumentException(String.format("Tried to create an array of wires with length %o, but a length of less than 1 makes no sense.", length));
+                       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.length = length;
                this.travelTime = travelTime;
                initValues();
@@ -34,12 +36,12 @@ public class WireArray
 
        private void initValues()
        {
-               values = Bit.Z.makeArray(length);
+               values = Bit.U.makeArray(length);
        }
 
        private void recalculateSingleInput()
        {
-               WireArrayInput input = inputs.get(0);
+               WireArrayEnd input = inputs.get(0);
                if (!Arrays.equals(input.getValues(), values))
                {
                        Bit[] oldValues = values.clone();
@@ -50,21 +52,16 @@ public class WireArray
 
        private void recalculateMultipleInputs()
        {
-               Iterator<WireArrayInput> it = inputs.iterator();
+               Iterator<WireArrayEnd> it = inputs.iterator();
                Bit[] newValues = it.next().inputValues.clone();
 
                while (it.hasNext())
                {
-                       WireArrayInput input = it.next();
+                       WireArrayEnd 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;
+                               newValues[i] = newValues[i].combineWith(bits[i]);
                        }
                }
 
@@ -93,10 +90,8 @@ public class WireArray
        /**
         * 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.
+        * @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
         */
@@ -113,8 +108,7 @@ public class WireArray
        /**
         * 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.
+        * @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
         */
@@ -144,8 +138,7 @@ public class WireArray
        /**
         * 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.
+        * @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
         */
@@ -194,8 +187,7 @@ public class WireArray
        }
 
        /**
-        * @return An array of length n containing the values of the n bits in the
-        *         {@link WireArray}. Can be safely modified.
+        * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
         * 
         * @author Fabian Stemmler
         */
@@ -205,12 +197,10 @@ public class WireArray
        }
 
        /**
-        * Adds an {@link WireArrayObserver}, who will be notified when the value of the
-        * {@link WireArray} is updated.
+        * 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
+        * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
         * 
         * @author Fabian Stemmler
         */
@@ -226,49 +216,47 @@ public class WireArray
        }
 
        /**
-        * Create and register a {@link WireArrayInput} object, which is tied to this
-        * {@link WireArray}.
+        * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}.
         */
-       public WireArrayInput createInput()
+       public WireArrayEnd createInput()
        {
-               return new WireArrayInput(this);
+               return new WireArrayEnd(this);
        }
 
-       private void registerInput(WireArrayInput toRegister)
+       private void registerInput(WireArrayEnd toRegister)
        {
                inputs.add(toRegister);
        }
 
        /**
-        * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
-        * it is tied to. The combination of all inputs determines the
-        * {@link WireArray}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
+        * A {@link WireArrayEnd} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines the
+        * {@link WireArray}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 WireArrayInput
+       public class WireArrayEnd implements Closeable
        {
                public final WireArray owner;
+               private boolean open;
                private Bit[] inputValues;
 
-               private WireArrayInput(WireArray owner)
+               private WireArrayEnd(WireArray owner)
                {
                        super();
                        this.owner = owner;
+                       open = true;
                        initValues();
                        owner.registerInput(this);
                }
 
                private void initValues()
                {
-                       inputValues = Bit.Z.makeArray(length);
+                       inputValues = Bit.U.makeArray(length);
                }
 
                /**
-                * Sets the wires values. This takes up time, as specified by the
-                * {@link WireArray}s travel time.
+                * 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.
                 * 
@@ -280,12 +268,12 @@ public class WireArray
                        {
                                feedSignals(0, newValues);
                        } else
-                               throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));
+                               throw new IllegalArgumentException(
+                                               String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
                }
 
                /**
-                * Sets values of a subarray of wires. This takes up time, as specified by the
-                * {@link WireArray}s travel time.
+                * 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.
@@ -294,6 +282,8 @@ public class WireArray
                 */
                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);
                }
 
@@ -301,7 +291,9 @@ public class WireArray
                {
                        int exclLastIndex = startingBit + newValues.length;
                        if (length < exclLastIndex)
-                               throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));
+                               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);
@@ -310,7 +302,8 @@ public class WireArray
                }
 
                /**
-                * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.
+                * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated
+                * {@link WireArray}.
                 */
                public Bit[] getValues()
                {
@@ -318,30 +311,47 @@ public class WireArray
                }
 
                /**
-                * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.
+                * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}.
                 */
                public void clearSignals()
                {
                        feedSignals(Bit.Z.makeArray(length));
                }
 
-               public Bit[] wireValuesExcludingMe() 
+               public Bit[] wireValuesExcludingMe()
                {
                        Bit[] bits = Bit.Z.makeArray(length);
-                       for (WireArrayInput wai : inputs) 
+                       for (WireArrayEnd wai : inputs)
                        {
-                               if(wai == this)
+                               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;
+               }
        }
 
        @Override
@@ -350,9 +360,9 @@ public class WireArray
                return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
        }
 
-       public static WireArrayInput[] extractInputs(WireArray[] w)
+       public static WireArrayEnd[] extractInputs(WireArray[] w)
        {
-               WireArrayInput[] inputs = new WireArrayInput[w.length];
+               WireArrayEnd[] inputs = new WireArrayEnd[w.length];
                for (int i = 0; i < w.length; i++)
                        inputs[i] = w[i].createInput();
                return inputs;