Removed unused import
[Mograsim.git] / era.mi / src / era / mi / logic / components / Mux.java
1 package era.mi.logic.components;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
8 import era.mi.logic.wires.WireArray;
9 import era.mi.logic.wires.WireArray.WireArrayInput;
10
11 /**
12  * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}s, one of which,
13  * as determined by select, is put through to the output.
14  * @author Fabian Stemmler
15  *
16  */
17 public class Mux extends BasicComponent
18 {
19         private WireArray select;
20         private WireArrayInput outI;
21         private WireArray[] inputs;
22         private final int outputSize;
23         /**
24          * Input {@link WireArray}s and out must be of uniform length
25          * @param out Must be of uniform length with all inputs.
26          * @param select Indexes the input array which is to be mapped to the output. Must have enough bits
27          * to index all inputs.
28          * @param inputs One of these inputs is mapped to the output, depending on the select bits
29          */
30         public Mux(int processTime, WireArray out, WireArray select, WireArray... inputs)
31         {
32                 super(processTime);
33                 outputSize = out.length;
34                 
35                 this.inputs = inputs.clone();
36                 for(int i = 0; i < this.inputs.length; i++)
37                 {
38                         if(inputs[i].length != outputSize)
39                                 throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!");
40                         inputs[i].addObserver(this);
41                 }
42                 
43                 this.select = select;
44                 select.addObserver(this);
45                 
46                 int maxInputs = 1 << select.length;
47                 if(this.inputs.length > maxInputs)
48                         throw new IllegalArgumentException("There are more inputs ("
49                                         + this.inputs.length + ") to the MUX than supported by "
50                                         + select.length + " select bits (" + maxInputs + ").");
51                 
52                 outI = out.createInput();
53         }
54         
55         public WireArray getOut()
56         {
57                 return outI.owner;
58         }
59
60         public WireArray getSelect()
61         {
62                 return select;
63         }
64
65         @Override
66         public void compute() {
67                 int selectValue;
68                 if(!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length)
69                 {
70                         outI.clearSignals();
71                         return;
72                 }
73                 
74                 WireArray active = inputs[selectValue];
75                 outI.feedSignals(active.getValues());
76         }
77
78         @Override
79         public List<WireArray> getAllInputs()
80         {
81                 ArrayList<WireArray> wires = new ArrayList<WireArray>(Arrays.asList(inputs));
82                 wires.add(select);
83                 return Collections.unmodifiableList(wires);
84         }
85
86         @Override
87         public List<WireArray> getAllOutputs()
88         {
89                 return Collections.unmodifiableList(Arrays.asList(outI.owner));
90         }
91 }