Refactored BitVector methods to resolve ambiguity
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / wires / Wire.java
index 479054a..b0e96f1 100644 (file)
@@ -51,19 +51,6 @@ public class Wire
                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))
@@ -75,19 +62,28 @@ public class Wire
 
        void recalculate()
        {
-               switch (inputs.size())
-               {
-               case 0:
+               if (inputs.size() == 0)
                        setNewValues(BitVector.of(Bit.U, length));
-                       break;
-               case 1:
-                       recalculateSingleInput();
-                       break;
-               default:
-                       recalculateMultipleInputs();
+               else
+               {
+                       BitVectorMutator mutator = BitVectorMutator.empty();
+                       for (ReadWriteEnd wireArrayEnd : inputs)
+                               mutator.join(wireArrayEnd.getInputValues());
+                       setNewValues(mutator.toBitVector());
                }
        }
 
+       /**
+        * Forces a Wire to take on specific values. If the new values differ from the old ones, the observers of the Wire will be notified.
+        * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)
+        * 
+        * @param values The values the <code>Wire</code> will have immediately after this method is called
+        */
+       public void forceValues(BitVector values)
+       {
+               setNewValues(values);
+       }
+
        /**
         * The {@link Wire} is interpreted as an unsigned integer with n bits.
         * 
@@ -154,14 +150,20 @@ public class Wire
                return val;
        }
 
+       /**
+        * Returns the least significant bit (LSB)
+        */
        public Bit getValue()
        {
                return getValue(0);
        }
 
+       /**
+        * Returns the least significant bit (LSB) of the given index
+        */
        public Bit getValue(int index)
        {
-               return values.getBit(index);
+               return values.getLSBit(index);
        }
 
        public BitVector getValues(int start, int end)
@@ -450,7 +452,8 @@ public class Wire
                }
 
                /**
-                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
+                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
+                *         significant bit (LSB)
                 */
                public Bit getInputValue()
                {
@@ -459,10 +462,12 @@ public class Wire
 
                /**
                 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
+                *         Returns the least significant bit (LSB)
+                * 
                 */
                public Bit getInputValue(int index)
                {
-                       return inputValues.getBit(index);
+                       return inputValues.getLSBit(index);
                }
 
                /**
@@ -470,7 +475,7 @@ public class Wire
                 */
                public BitVector getInputValues()
                {
-                       return getInputValues(0, length);
+                       return inputValues;
                }
 
                public BitVector getInputValues(int start, int end)
@@ -495,7 +500,7 @@ public class Wire
                                        continue;
                                mutator.join(wireEnd.inputValues);
                        }
-                       return mutator.get();
+                       return mutator.toBitVector();
                }
 
                @Override
@@ -556,7 +561,7 @@ public class Wire
         * @param fromB  The first bit of {@link Wire} b to be fused
         * @param length The amount of bits to fuse
         */
-       private static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
+       public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
        {
                ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
                rA.setWriting(false);