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