Did some clean up
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mux.java
index 0b19e7c..6ac32c6 100644 (file)
@@ -1,77 +1,93 @@
-package era.mi.logic.components;
-
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-
-/**
- * Models a Multiplexer. A is selected when select bit is 1, B when select bit is 0. Outputs X otherwise.
- * @author Fabian
- *
- */
-public class Mux extends BasicComponent
-{
-       private WireArray a, b, out;
-       private WireArray select;
-       private final int size;
-       
-       /**
-        * {@link WireArray}s a, b and out must be of uniform length, select
-        * @param a Must be of uniform length with b and out.
-        * @param b Must be of uniform length with a and out.
-        * @param select C
-        * @param out Must be of uniform length with a and b.
-        */
-       public Mux(int processTime, WireArray a, WireArray b, WireArray select, WireArray out)
-       {
-               super(processTime);
-               size = a.length;
-               if(b.length != out.length || b.length != size)
-                       throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
-               this.a = a;
-               a.addObserver(this);
-               this.b = b;
-               b.addObserver(this);
-               this.select = select;
-               select.addObserver(this);
-               this.out = out;
-       }
-
-       @Override
-       protected void compute()
-       {
-               WireArray active = b;
-               switch(select.getValue())
-               {
-               case ONE:
-                       active = a;
-               case ZERO:
-                       out.feedSignals(active.getValues());
-                       break;
-               default:
-                       Bit[] newValues = new Bit[size];
-                       for(int i = 0; i < size; i++)
-                               newValues[i] = Bit.X;
-                       out.feedSignals(newValues);
-               }
-       }
-
-       public WireArray getA()
-       {
-               return a;
-       }
-
-       public WireArray getB()
-       {
-               return b;
-       }
-
-       public WireArray getOut()
-       {
-               return out;
-       }
-
-       public WireArray getSelect()
-       {
-               return select;
-       }
-}
+package era.mi.logic.components;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import era.mi.logic.wires.WireArray;\r
+import era.mi.logic.wires.WireArray.WireArrayEnd;\r
+\r
+/**\r
+ * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which, as determined by select, is put through to the\r
+ * output.\r
+ * \r
+ * @author Fabian Stemmler\r
+ *\r
+ */\r
+public class Mux extends BasicComponent\r
+{\r
+       private WireArray select;\r
+       private WireArrayEnd outI;\r
+       private WireArray[] inputs;\r
+       private final int outputSize;\r
+\r
+       /**\r
+        * Input {@link WireArray}s and out must be of uniform length\r
+        * \r
+        * @param out    Must be of uniform length with all inputs.\r
+        * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.\r
+        * @param inputs One of these inputs is mapped to the output, depending on the select bits\r
+        */\r
+       public Mux(int processTime, WireArray out, WireArray select, WireArray... inputs)\r
+       {\r
+               super(processTime);\r
+               outputSize = out.length;\r
+\r
+               this.inputs = inputs.clone();\r
+               for (int i = 0; i < this.inputs.length; i++)\r
+               {\r
+                       if (inputs[i].length != outputSize)\r
+                               throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");\r
+                       inputs[i].addObserver(this);\r
+               }\r
+\r
+               this.select = select;\r
+               select.addObserver(this);\r
+\r
+               int maxInputs = 1 << select.length;\r
+               if (this.inputs.length > maxInputs)\r
+                       throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by "\r
+                                       + select.length + " select bits (" + maxInputs + ").");\r
+\r
+               outI = out.createInput();\r
+       }\r
+\r
+       public WireArray getOut()\r
+       {\r
+               return outI.owner;\r
+       }\r
+\r
+       public WireArray getSelect()\r
+       {\r
+               return select;\r
+       }\r
+\r
+       @Override\r
+       public void compute()\r
+       {\r
+               int selectValue;\r
+               if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)\r
+               {\r
+                       outI.clearSignals();\r
+                       return;\r
+               }\r
+\r
+               WireArray active = inputs[selectValue];\r
+               outI.feedSignals(active.getValues());\r
+       }\r
+\r
+       @Override\r
+       public List<WireArray> getAllInputs()\r
+       {\r
+               ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));\r
+               wires.add(select);\r
+               return Collections.unmodifiableList(wires);\r
+       }\r
+\r
+       @Override\r
+       public List<WireArray> getAllOutputs()\r
+       {\r
+               return List.of(outI.owner);\r
+       }\r
+}\r