Merge logic of origin into logic
[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.Wire;
9 import era.mi.logic.wires.Wire.WireEnd;
10
11 /**
12  * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}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 WireEnd select;
21         private WireEnd out;
22         private WireEnd[] inputs;
23         private final int outputSize;
24
25         /**
26          * Input {@link Wire}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, WireEnd out, WireEnd select, WireEnd... 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                 this.out = out;
54         }
55
56         public WireEnd getOut()
57         {
58                 return out;
59         }
60
61         public WireEnd 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                         out.clearSignals();
73                         return;
74                 }
75
76                 WireEnd active = inputs[selectValue];
77                 out.feedSignals(active.getValues());
78         }
79
80         @Override
81         public List<WireEnd> getAllInputs()
82         {
83                 ArrayList<WireEnd> wires = new ArrayList<WireEnd>(Arrays.asList(inputs));
84                 wires.add(select);
85                 return Collections.unmodifiableList(wires);
86         }
87
88         @Override
89         public List<WireEnd> getAllOutputs()
90         {
91                 return List.of(out);
92         }
93 }