Refactored BitVector methods to resolve ambiguity
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / wires / Wire.java
index 404aacd..6310d5b 100644 (file)
@@ -21,18 +21,25 @@ import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
  */\r
 public class Wire\r
 {\r
+       public final String name;\r
        private BitVector values;\r
        public final int travelTime;\r
-       private List<ReadEnd> attached = new ArrayList<ReadEnd>();\r
+       private List<ReadEnd> attached = new ArrayList<>();\r
        public final int length;\r
-       private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();\r
-       private Timeline timeline;\r
+       List<ReadWriteEnd> inputs = new ArrayList<>();\r
+       Timeline timeline;\r
 \r
        public Wire(Timeline timeline, int length, int travelTime)\r
+       {\r
+               this(timeline, length, travelTime, null);\r
+       }\r
+\r
+       public Wire(Timeline timeline, int length, int travelTime, String name)\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.name = name;\r
                this.timeline = timeline;\r
                this.length = length;\r
                this.travelTime = travelTime;\r
@@ -44,42 +51,39 @@ public class Wire
                values = U.toVector(length);\r
        }\r
 \r
-       private void recalculateSingleInput()\r
-       {\r
-               setNewValues(inputs.get(0).getInputValues());\r
-       }\r
-\r
-       private void recalculateMultipleInputs()\r
-       {\r
-               BitVectorMutator mutator = BitVectorMutator.empty();\r
-               for (ReadWriteEnd wireArrayEnd : inputs)\r
-                       mutator.join(wireArrayEnd.getInputValues());\r
-               setNewValues(mutator.get());\r
-       }\r
-\r
        private void setNewValues(BitVector newValues)\r
        {\r
                if (values.equals(newValues))\r
                        return;\r
-               BitVector oldValues = values;\r
+//             BitVector oldValues = values;\r
                values = newValues;\r
-               notifyObservers(oldValues);\r
+               notifyObservers();\r
        }\r
 \r
-       private void recalculate()\r
+       void recalculate()\r
        {\r
-               switch (inputs.size())\r
+               if (inputs.size() == 0)\r
+                       setNewValues(BitVector.of(Bit.U, length));\r
+               else\r
                {\r
-               case 0:\r
-                       return;\r
-               case 1:\r
-                       recalculateSingleInput();\r
-                       break;\r
-               default:\r
-                       recalculateMultipleInputs();\r
+                       BitVectorMutator mutator = BitVectorMutator.empty();\r
+                       for (ReadWriteEnd wireArrayEnd : inputs)\r
+                               mutator.join(wireArrayEnd.getInputValues());\r
+                       setNewValues(mutator.toBitVector());\r
                }\r
        }\r
 \r
+       /**\r
+        * 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.\r
+        * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)\r
+        * \r
+        * @param values The values the <code>Wire</code> will have immediately after this method is called\r
+        */\r
+       public void forceValues(BitVector values)\r
+       {\r
+               setNewValues(values);\r
+       }\r
+\r
        /**\r
         * The {@link Wire} is interpreted as an unsigned integer with n bits.\r
         * \r
@@ -146,14 +150,20 @@ public class Wire
                return val;\r
        }\r
 \r
+       /**\r
+        * Returns the least significant bit (LSB)\r
+        */\r
        public Bit getValue()\r
        {\r
                return getValue(0);\r
        }\r
 \r
+       /**\r
+        * Returns the least significant bit (LSB) of the given index\r
+        */\r
        public Bit getValue(int index)\r
        {\r
-               return values.getBit(index);\r
+               return values.getLSBit(index);\r
        }\r
 \r
        public BitVector getValues(int start, int end)\r
@@ -174,20 +184,19 @@ public class Wire
         * \r
         * @author Fabian Stemmler\r
         */\r
-       private void attachEnd(ReadEnd end)\r
+       void attachEnd(ReadEnd end)\r
        {\r
                attached.add(end);\r
        }\r
 \r
-       private void detachEnd(ReadEnd end)\r
+       void detachEnd(ReadEnd end)\r
        {\r
                attached.remove(end);\r
        }\r
 \r
-       private void notifyObservers(BitVector oldValues)\r
+       private void notifyObservers()\r
        {\r
-               for (ReadEnd o : attached)\r
-                       o.update(oldValues);\r
+               attached.forEach(r -> r.update());\r
        }\r
 \r
        /**\r
@@ -206,7 +215,7 @@ public class Wire
                return new ReadEnd();\r
        }\r
 \r
-       private void registerInput(ReadWriteEnd toRegister)\r
+       void registerInput(ReadWriteEnd toRegister)\r
        {\r
                inputs.add(toRegister);\r
        }\r
@@ -220,15 +229,15 @@ public class Wire
         */\r
        public class ReadEnd implements LogicObservable\r
        {\r
-               private List<LogicObserver> observers = new ArrayList<LogicObserver>();\r
+               private List<LogicObserver> observers = new ArrayList<>();\r
 \r
-               private ReadEnd()\r
+               ReadEnd()\r
                {\r
                        super();\r
                        Wire.this.attachEnd(this);\r
                }\r
 \r
-               public void update(BitVector oldValues)\r
+               public void update()\r
                {\r
                        notifyObservers();\r
                }\r
@@ -345,23 +354,29 @@ public class Wire
                        observers.add(ob);\r
                }\r
 \r
+               @Override\r
+               public void deregisterObserver(LogicObserver ob)\r
+               {\r
+                       observers.remove(ob);\r
+               }\r
+\r
                @Override\r
                public void notifyObservers()\r
                {\r
-                       for (LogicObserver ob : observers)\r
-                               ob.update(this);\r
+                       observers.forEach(ob -> ob.update(this));\r
                }\r
        }\r
 \r
        public class ReadWriteEnd extends ReadEnd\r
        {\r
-               private boolean open;\r
+               private boolean open, isWriting;\r
                private BitVector inputValues;\r
 \r
-               private ReadWriteEnd()\r
+               ReadWriteEnd()\r
                {\r
                        super();\r
                        open = true;\r
+                       isWriting = true;\r
                        initValues();\r
                        registerInput(this);\r
                }\r
@@ -408,7 +423,11 @@ public class Wire
                        timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);\r
                }\r
 \r
-               private void setValues(int startingBit, BitVector newValues)\r
+               /**\r
+                * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via\r
+                * feedValues(...) with a delay.\r
+                */\r
+               void setValues(int startingBit, BitVector newValues)\r
                {\r
                        // index check covered in equals\r
                        if (!inputValues.equalsWithOffset(newValues, startingBit))\r
@@ -420,7 +439,11 @@ public class Wire
                        }\r
                }\r
 \r
-               private void setValues(BitVector newValues)\r
+               /**\r
+                * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via\r
+                * feedValues(...) with a delay.\r
+                */\r
+               void setValues(BitVector newValues)\r
                {\r
                        if (inputValues.equals(newValues))\r
                                return;\r
@@ -429,7 +452,8 @@ public class Wire
                }\r
 \r
                /**\r
-                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.\r
+                * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least\r
+                *         significant bit (LSB)\r
                 */\r
                public Bit getInputValue()\r
                {\r
@@ -438,10 +462,12 @@ public class Wire
 \r
                /**\r
                 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.\r
+                *         Returns the least significant bit (LSB)\r
+                * \r
                 */\r
                public Bit getInputValue(int index)\r
                {\r
-                       return inputValues.getBit(index);\r
+                       return inputValues.getLSBit(index);\r
                }\r
 \r
                /**\r
@@ -449,7 +475,7 @@ public class Wire
                 */\r
                public BitVector getInputValues()\r
                {\r
-                       return getInputValues(0, length);\r
+                       return inputValues;\r
                }\r
 \r
                public BitVector getInputValues(int start, int end)\r
@@ -474,7 +500,7 @@ public class Wire
                                        continue;\r
                                mutator.join(wireEnd.inputValues);\r
                        }\r
-                       return mutator.get();\r
+                       return mutator.toBitVector();\r
                }\r
 \r
                @Override\r
@@ -482,12 +508,38 @@ public class Wire
                {\r
                        return inputValues.toString();\r
                }\r
+\r
+               @Override\r
+               public void close()\r
+               {\r
+                       super.close();\r
+                       open = false;\r
+               }\r
+\r
+               void setWriting(boolean isWriting)\r
+               {\r
+                       if (this.isWriting != isWriting)\r
+                       {\r
+                               this.isWriting = isWriting;\r
+                               if (isWriting)\r
+                                       inputs.add(this);\r
+                               else\r
+                                       inputs.remove(this);\r
+                               Wire.this.recalculate();\r
+                       }\r
+               }\r
+\r
+               boolean isWriting()\r
+               {\r
+                       return isWriting;\r
+               }\r
        }\r
 \r
        @Override\r
        public String toString()\r
        {\r
-               return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);\r
+               String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;\r
+               return String.format("wire %s value: %s inputs: %s", name, values, inputs);\r
        }\r
 \r
        public static ReadEnd[] extractEnds(Wire[] w)\r
@@ -497,4 +549,70 @@ public class Wire
                        inputs[i] = w[i].createReadWriteEnd();\r
                return inputs;\r
        }\r
+\r
+       // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits\r
+       /**\r
+        * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:\r
+        * The bits are permanently fused together.\r
+        * \r
+        * @param a      The {@link Wire} to be (partially) fused with b\r
+        * @param b      The {@link Wire} to be (partially) fused with a\r
+        * @param fromA  The first bit of {@link Wire} a to be fused\r
+        * @param fromB  The first bit of {@link Wire} b to be fused\r
+        * @param length The amount of bits to fuse\r
+        */\r
+       public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)\r
+       {\r
+               ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();\r
+               rA.setWriting(false);\r
+               rB.setWriting(false);\r
+               rA.setValues(BitVector.of(Bit.Z, a.length));\r
+               rB.setValues(BitVector.of(Bit.Z, b.length));\r
+               Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);\r
+               rA.registerObserver(aF);\r
+               rB.registerObserver(bF);\r
+               aF.update(rA);\r
+               bF.update(rB);\r
+       }\r
+\r
+       /**\r
+        * \r
+        * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are\r
+        * permanently fused together.\r
+        * \r
+        * @param a The {@link Wire} to be fused with b\r
+        * @param b The {@link Wire} to be fused with a\r
+        */\r
+       public static void fuse(Wire a, Wire b)\r
+       {\r
+               fuse(a, b, 0, 0, a.length);\r
+       }\r
+\r
+       private static class Fusion implements LogicObserver\r
+       {\r
+               private ReadWriteEnd target;\r
+               int fromSource, fromTarget, length;\r
+\r
+               public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)\r
+               {\r
+                       this.target = target;\r
+                       this.fromSource = fromSource;\r
+                       this.fromTarget = fromTarget;\r
+                       this.length = length;\r
+               }\r
+\r
+               @Override\r
+               public void update(LogicObservable initiator)\r
+               {\r
+                       ReadWriteEnd source = (ReadWriteEnd) initiator;\r
+                       if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)\r
+                               target.setWriting(false);\r
+                       else\r
+                       {\r
+                               target.setWriting(true);\r
+                               BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);\r
+                               target.setValues(fromTarget, targetInput);\r
+                       }\r
+               }\r
+       }\r
 }
\ No newline at end of file