Generalized WireObserver to LogicObserver
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / Merger.java
index c4c7170..06e064c 100644 (file)
@@ -1,85 +1,85 @@
-package net.mograsim.logic.core.components;
-
-import java.util.List;
-
-import net.mograsim.logic.core.timeline.Timeline;
-import net.mograsim.logic.core.types.BitVector;
-import net.mograsim.logic.core.wires.Wire;
-import net.mograsim.logic.core.wires.WireObserver;
-import net.mograsim.logic.core.wires.Wire.ReadEnd;
-import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
-
-public class Merger extends Component implements WireObserver
-{
-       private ReadWriteEnd out;
-       private ReadEnd[] inputs;
-       private int[] beginningIndex;
-
-       /**
-        * 
-        * @param union  The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
-        * @param inputs The inputs to be merged into the union
-        */
-       public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs)
-       {
-               super(timeline);
-               this.inputs = inputs;
-               this.out = union;
-               this.beginningIndex = new int[inputs.length];
-
-               int length = 0;
-               for (int i = 0; i < inputs.length; i++)
-               {
-                       beginningIndex[i] = length;
-                       length += inputs[i].length();
-                       inputs[i].addObserver(this);
-               }
-
-               if (length != union.length())
-                       throw new IllegalArgumentException(
-                                       "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");
-       }
-
-       public ReadEnd getInput(int index)
-       {
-               return inputs[index];
-       }
-
-       public ReadEnd getUnion()
-       {
-               return out;
-       }
-
-       @Override
-       public void update(ReadEnd initiator, BitVector oldValues)
-       {
-               int index = find(initiator);
-               int beginning = beginningIndex[index];
-               out.feedSignals(beginning, inputs[index].getValues());
-       }
-
-       private int find(ReadEnd r)
-       {
-               for (int i = 0; i < inputs.length; i++)
-                       if (inputs[i] == r)
-                               return i;
-               return -1;
-       }
-
-       public ReadEnd[] getInputs()
-       {
-               return inputs.clone();
-       }
-
-       @Override
-       public List<ReadEnd> getAllInputs()
-       {
-               return List.of(inputs);
-       }
-
-       @Override
-       public List<ReadWriteEnd> getAllOutputs()
-       {
-               return List.of(out);
-       }
-}
+package net.mograsim.logic.core.components;\r
+\r
+import java.util.List;\r
+\r
+import net.mograsim.logic.core.LogicObservable;\r
+import net.mograsim.logic.core.LogicObserver;\r
+import net.mograsim.logic.core.timeline.Timeline;\r
+import net.mograsim.logic.core.wires.Wire;\r
+import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
+import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;\r
+\r
+public class Merger extends Component implements LogicObserver\r
+{\r
+       private ReadWriteEnd out;\r
+       private ReadEnd[] inputs;\r
+       private int[] beginningIndex;\r
+\r
+       /**\r
+        * \r
+        * @param union  The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length().\r
+        * @param inputs The inputs to be merged into the union\r
+        */\r
+       public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs)\r
+       {\r
+               super(timeline);\r
+               this.inputs = inputs;\r
+               this.out = union;\r
+               this.beginningIndex = new int[inputs.length];\r
+\r
+               int length = 0;\r
+               for (int i = 0; i < inputs.length; i++)\r
+               {\r
+                       beginningIndex[i] = length;\r
+                       length += inputs[i].length();\r
+                       inputs[i].registerObserver(this);\r
+               }\r
+\r
+               if (length != union.length())\r
+                       throw new IllegalArgumentException(\r
+                                       "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length().");\r
+       }\r
+\r
+       public ReadEnd getInput(int index)\r
+       {\r
+               return inputs[index];\r
+       }\r
+\r
+       public ReadEnd getUnion()\r
+       {\r
+               return out;\r
+       }\r
+\r
+       @Override\r
+       public void update(LogicObservable initiator)\r
+       {\r
+               int index = find(initiator);\r
+               int beginning = beginningIndex[index];\r
+               out.feedSignals(beginning, inputs[index].getValues());\r
+       }\r
+\r
+       private int find(LogicObservable r)\r
+       {\r
+               for (int i = 0; i < inputs.length; i++)\r
+                       if (inputs[i] == r)\r
+                               return i;\r
+               return -1;\r
+       }\r
+\r
+       public ReadEnd[] getInputs()\r
+       {\r
+               return inputs.clone();\r
+       }\r
+\r
+       @Override\r
+       public List<ReadEnd> getAllInputs()\r
+       {\r
+               return List.of(inputs);\r
+       }\r
+\r
+       @Override\r
+       public List<ReadWriteEnd> getAllOutputs()\r
+       {\r
+               return List.of(out);\r
+       }\r
+}\r