WireEnd functionality split into ReadEnd and ReadWriteEnd
[Mograsim.git] / era.mi / src / era / mi / logic / components / gates / MultiInputGate.java
index fac267a..dd8a6d6 100644 (file)
@@ -2,45 +2,44 @@ package era.mi.logic.components.gates;
 
 import java.util.List;
 
-import era.mi.logic.Bit;
 import era.mi.logic.components.BasicComponent;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArray.WireArrayEnd;
+import era.mi.logic.types.BitVector.BitVectorMutator;
+import era.mi.logic.types.MutationOperation;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
 
 public abstract class MultiInputGate extends BasicComponent
 {
-       protected WireArray[] in;
-       protected WireArray out;
-       protected WireArrayEnd outI;
+       protected ReadEnd[] in;
+       protected ReadWriteEnd out;
        protected final int length;
-       protected Operation op;
+       protected MutationOperation op;
 
-       protected MultiInputGate(int processTime, Operation op, WireArray out, WireArray... in)
+       protected MultiInputGate(int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in)
        {
                super(processTime);
                this.op = op;
-               length = out.length;
+               length = out.length();
                this.in = in.clone();
                if (in.length < 1)
                        throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length));
-               for (WireArray w : in)
+               for (ReadEnd w : in)
                {
-                       if (w.length != length)
+                       if (w.length() != length)
                                throw new IllegalArgumentException("All wires connected to the gate must be of uniform length.");
                        w.addObserver(this);
                }
                this.out = out;
-               outI = out.createInput();
        }
 
        @Override
-       public List<WireArray> getAllInputs()
+       public List<ReadEnd> getAllInputs()
        {
                return List.of(in);
        }
 
        @Override
-       public List<WireArray> getAllOutputs()
+       public List<ReadWriteEnd> getAllOutputs()
        {
                return List.of(out);
        }
@@ -48,14 +47,9 @@ public abstract class MultiInputGate extends BasicComponent
        @Override
        protected void compute()
        {
-               Bit[] result = in[0].getValues();
-               for (int i = 1; i < in.length; i++)
-                       result = op.execute(result, in[i].getValues());
-               outI.feedSignals(result);
-       }
-
-       protected interface Operation
-       {
-               public Bit[] execute(Bit[] a, Bit[] b);
+               BitVectorMutator mutator = BitVectorMutator.empty();
+               for (ReadEnd w : in)
+                       op.apply(mutator, w.getValues());
+               out.feedSignals(mutator.get());
        }
 }