Wire concept was changed to accommodate multiple inputs. Not all
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mux.java
1 package era.mi.logic.components;
2
3 import era.mi.logic.Bit;
4 import era.mi.logic.wires.WireArray;
5
6 /**
7  * Models a Multiplexer. A is selected when select bit is 1, B when select bit is 0. Outputs X otherwise.
8  * @author Fabian
9  *
10  */
11 public class Mux extends BasicComponent
12 {
13         private WireArray a, b, out;
14         private WireArray select;
15         private final int size;
16         
17         /**
18          * {@link WireArray}s a, b and out must be of uniform length, select
19          * @param a Must be of uniform length with b and out.
20          * @param b Must be of uniform length with a and out.
21          * @param select C
22          * @param out Must be of uniform length with a and b.
23          */
24         public Mux(int processTime, WireArray a, WireArray b, WireArray select, WireArray out)
25         {
26                 super(processTime);
27                 size = a.length;
28                 if(b.length != out.length || b.length != size)
29                         throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
30                 this.a = a;
31                 a.addObserver(this);
32                 this.b = b;
33                 b.addObserver(this);
34                 this.select = select;
35                 select.addObserver(this);
36                 this.out = out;
37         }
38
39         @Override
40         protected void compute()
41         {
42                 WireArray active = b;
43                 switch(select.getValue())
44                 {
45                 case ONE:
46                         active = a;
47                 case ZERO:
48                         out.feedSignals(active.getValues());
49                         break;
50                 default:
51                         Bit[] newValues = new Bit[size];
52                         for(int i = 0; i < size; i++)
53                                 newValues[i] = Bit.X;
54                         out.feedSignals(newValues);
55                 }
56         }
57
58         public WireArray getA()
59         {
60                 return a;
61         }
62
63         public WireArray getB()
64         {
65                 return b;
66         }
67
68         public WireArray getOut()
69         {
70                 return out;
71         }
72
73         public WireArray getSelect()
74         {
75                 return select;
76         }
77 }