Added era.mi; Project containing provisional simulator core
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mux.java
diff --git a/era.mi/src/era/mi/logic/components/Mux.java b/era.mi/src/era/mi/logic/components/Mux.java
new file mode 100644 (file)
index 0000000..29c4814
--- /dev/null
@@ -0,0 +1,77 @@
+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;
+       }
+}