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