-package era.mi.logic.components;
-
-import era.mi.logic.Bit;
-import era.mi.logic.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.timeline.Timeline;\r
+import era.mi.logic.wires.Wire;\r
+import era.mi.logic.wires.Wire.ReadEnd;\r
+import era.mi.logic.wires.Wire.ReadWriteEnd;\r
+\r
+/**\r
+ * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}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 ReadEnd select;\r
+ private ReadWriteEnd out;\r
+ private ReadEnd[] inputs;\r
+ private final int outputSize;\r
+\r
+ /**\r
+ * Input {@link Wire}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(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs)\r
+ {\r
+ super(timeline, 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
+ this.out = out;\r
+ }\r
+\r
+ public ReadEnd getOut()\r
+ {\r
+ return out;\r
+ }\r
+\r
+ public ReadEnd 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
+ out.clearSignals();\r
+ return;\r
+ }\r
+\r
+ ReadEnd active = inputs[selectValue];\r
+ out.feedSignals(active.getValues());\r
+ }\r
+\r
+ @Override\r
+ public List<ReadEnd> getAllInputs()\r
+ {\r
+ ArrayList<ReadEnd> wires = new ArrayList<ReadEnd>(Arrays.asList(inputs));\r
+ wires.add(select);\r
+ return Collections.unmodifiableList(wires);\r
+ }\r
+\r
+ @Override\r
+ public List<ReadWriteEnd> getAllOutputs()\r
+ {\r
+ return List.of(out);\r
+ }\r
+}\r