64f5d26447dbb22f09c58c8dbcda8eded8f89740
[Mograsim.git] / era.mi / src / era / mi / logic / components / Demux.java
1 package era.mi.logic.components;\r
2 \r
3 import java.util.Arrays;\r
4 import java.util.Collections;\r
5 import java.util.List;\r
6 \r
7 import era.mi.logic.wires.WireArray;\r
8 import era.mi.logic.wires.WireArray.WireArrayEnd;\r
9 \r
10 /**\r
11  * 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
12  * output.\r
13  * \r
14  * @author Fabian Stemmler\r
15  *\r
16  */\r
17 public class Demux extends BasicComponent\r
18 {\r
19         private final WireArray select, in;\r
20         private final WireArray[] outputs;\r
21         private final WireArrayEnd[] outputsI;\r
22         private final int outputSize;\r
23         private int selected = -1;\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 outputs One of these inputs is mapped to the output, depending on the select bits\r
31          */\r
32         public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)\r
33         {\r
34                 super(processTime);\r
35                 outputSize = in.length;\r
36 \r
37                 this.in = in;\r
38                 this.outputs = outputs;\r
39                 this.outputsI = new WireArrayEnd[outputs.length];\r
40                 for (int i = 0; i < this.outputsI.length; i++)\r
41                 {\r
42                         if (outputs[i].length != outputSize)\r
43                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");\r
44                         this.outputsI[i] = outputs[i].createInput();\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.outputsI.length > maxInputs)\r
52                         throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "\r
53                                         + select.length + " select bits (" + maxInputs + ").");\r
54                 in.addObserver(this);\r
55         }\r
56 \r
57         @Override\r
58         public void compute()\r
59         {\r
60                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;\r
61                 if (selectValue >= outputsI.length)\r
62                         selectValue = -1;\r
63 \r
64                 if (selected != selectValue && selected != -1)\r
65                         outputsI[selected].clearSignals();\r
66 \r
67                 selected = selectValue;\r
68 \r
69                 if (selectValue != -1)\r
70                         outputsI[selectValue].feedSignals(in.getValues());\r
71         }\r
72 \r
73         @Override\r
74         public List<WireArray> getAllInputs()\r
75         {\r
76                 return Collections.unmodifiableList(Arrays.asList(in, select));\r
77         }\r
78 \r
79         @Override\r
80         public List<WireArray> getAllOutputs()\r
81         {\r
82                 return Collections.unmodifiableList(Arrays.asList(outputs));\r
83         }\r
84 }\r