Integrated new types, tests still work, not used yet
[Mograsim.git] / era.mi / src / era / mi / logic / components / Merger.java
index eb910a4..ca441ba 100644 (file)
@@ -1,61 +1,82 @@
 package era.mi.logic.components;
 
-import era.mi.logic.Util;
-import era.mi.logic.Bit;
-import era.mi.logic.WireArray;
-import era.mi.logic.WireArrayObserver;
+import java.util.List;
 
-@Deprecated
-public class Merger implements WireArrayObserver
+import era.mi.logic.types.Bit;
+import era.mi.logic.wires.Wire;
+import era.mi.logic.wires.Wire.WireEnd;
+import era.mi.logic.wires.WireObserver;
+
+public class Merger implements WireObserver, Component
 {
-       private WireArray out;
-       private WireArray[] inputs;
-       
-       //TODO: General problem with this concept; New inputs coming in at the same time override each other
-       
+       private WireEnd out;
+       private WireEnd[] inputs;
+       private int[] beginningIndex;
+
        /**
         * 
-        * @param union The output of merging n {@link WireArray}s into one. Must have length = a1.length() + a2.length() + ... + an.length().
+        * @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(WireArray union, WireArray... inputs)
+       public Merger(WireEnd union, WireEnd... inputs)
        {
                this.inputs = inputs;
                this.out = union;
-               
+               this.beginningIndex = new int[inputs.length];
+
                int length = 0;
-               for(WireArray input : inputs)
+               for (int i = 0; i < inputs.length; i++)
                {
-                       length += input.length();
-                       input.addObserver(this);
+                       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().");
-       }
 
-       protected void compute()
-       {
-               Bit[][] bits = new Bit[inputs.length][];
-               for(int i = 0; i < inputs.length; i++)
-                       bits[i] = inputs[i].getValues();
-               Bit[] newOut = Util.concat(bits);
-               out.feedSignals(newOut);
+               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 WireArray getInput(int index)
+       public WireEnd getInput(int index)
        {
                return inputs[index];
        }
-       
-       public WireArray getUnion()
+
+       public WireEnd getUnion()
        {
                return out;
        }
-       
+
+       @Override
+       public void update(Wire initiator, Bit[] oldValues)
+       {
+               int index = find(initiator);
+               int beginning = beginningIndex[index];
+               out.feedSignals(beginning, inputs[index].getValues());
+       }
+
+       private int find(Wire w)
+       {
+               for (int i = 0; i < inputs.length; i++)
+                       if (inputs[i].getWire() == w)
+                               return i;
+               return -1;
+       }
+
+       public WireEnd[] getInputs()
+       {
+               return inputs.clone();
+       }
+
+       @Override
+       public List<WireEnd> getAllInputs()
+       {
+               return List.of(inputs);
+       }
+
        @Override
-       public void update(WireArray initiator)
+       public List<WireEnd> getAllOutputs()
        {
-               compute(); //No inner delay
+               return List.of(out);
        }
 }