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