Generalized WireObserver to LogicObserver
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / components / Demux.java
index 5da1bf6..e5b228d 100644 (file)
@@ -1,82 +1,82 @@
-package net.mograsim.logic.core.components;
-
-import java.util.List;
-
-import net.mograsim.logic.core.timeline.Timeline;
-import net.mograsim.logic.core.wires.Wire;
-import net.mograsim.logic.core.wires.Wire.ReadEnd;
-import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
-
-/**
- * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
- * output.
- * 
- * @author Fabian Stemmler
- *
- */
-public class Demux extends BasicComponent
-{
-       private final ReadEnd select, in;
-       private final ReadWriteEnd[] outputs;
-       private final int outputSize;
-       private int selected = -1;
-
-       /**
-        * Output {@link Wire}s and in must be of uniform length
-        * 
-        * @param in      Must be of uniform length with all outputs.
-        * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
-        * @param outputs One of these outputs receives the input signal, depending on the select bits
-        */
-       public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
-       {
-               super(timeline, processTime);
-               outputSize = in.length();
-
-               this.in = in;
-               this.outputs = outputs;
-               for (int i = 0; i < this.outputs.length; i++)
-               {
-                       if (outputs[i].length() != outputSize)
-                               throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
-                       this.outputs[i] = outputs[i];
-               }
-
-               this.select = select;
-               select.addObserver(this);
-
-               int maxInputs = 1 << select.length();
-               if (this.outputs.length > maxInputs)
-                       throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "
-                                       + select.length() + " select bits (" + maxInputs + ").");
-               in.addObserver(this);
-       }
-
-       @Override
-       public void compute()
-       {
-               int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
-               if (selectValue >= outputs.length)
-                       selectValue = -1;
-
-               if (selected != selectValue && selected != -1)
-                       outputs[selected].clearSignals();
-
-               selected = selectValue;
-
-               if (selectValue != -1)
-                       outputs[selectValue].feedSignals(in.getValues());
-       }
-
-       @Override
-       public List<ReadEnd> getAllInputs()
-       {
-               return List.of(in, select);
-       }
-
-       @Override
-       public List<ReadWriteEnd> getAllOutputs()
-       {
-               return List.of(outputs);
-       }
-}
+package net.mograsim.logic.core.components;\r
+\r
+import java.util.List;\r
+\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
+/**\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 Demux extends BasicComponent\r
+{\r
+       private final ReadEnd select, in;\r
+       private final ReadWriteEnd[] outputs;\r
+       private final int outputSize;\r
+       private int selected = -1;\r
+\r
+       /**\r
+        * Output {@link Wire}s and in must be of uniform length\r
+        * \r
+        * @param in      Must be of uniform length with all outputs.\r
+        * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.\r
+        * @param outputs One of these outputs receives the input signal, depending on the select bits\r
+        */\r
+       public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)\r
+       {\r
+               super(timeline, processTime);\r
+               outputSize = in.length();\r
+\r
+               this.in = in;\r
+               this.outputs = outputs;\r
+               for (int i = 0; i < this.outputs.length; i++)\r
+               {\r
+                       if (outputs[i].length() != outputSize)\r
+                               throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");\r
+                       this.outputs[i] = outputs[i];\r
+               }\r
+\r
+               this.select = select;\r
+               select.registerObserver(this);\r
+\r
+               int maxInputs = 1 << select.length();\r
+               if (this.outputs.length > maxInputs)\r
+                       throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "\r
+                                       + select.length() + " select bits (" + maxInputs + ").");\r
+               in.registerObserver(this);\r
+       }\r
+\r
+       @Override\r
+       public void compute()\r
+       {\r
+               int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;\r
+               if (selectValue >= outputs.length)\r
+                       selectValue = -1;\r
+\r
+               if (selected != selectValue && selected != -1)\r
+                       outputs[selected].clearSignals();\r
+\r
+               selected = selectValue;\r
+\r
+               if (selectValue != -1)\r
+                       outputs[selectValue].feedSignals(in.getValues());\r
+       }\r
+\r
+       @Override\r
+       public List<ReadEnd> getAllInputs()\r
+       {\r
+               return List.of(in, select);\r
+       }\r
+\r
+       @Override\r
+       public List<ReadWriteEnd> getAllOutputs()\r
+       {\r
+               return List.of(outputs);\r
+       }\r
+}\r