Did some clean up
[Mograsim.git] / era.mi / src / era / mi / logic / components / Demux.java
1 package era.mi.logic.components;\r
2 \r
3 import java.util.List;\r
4 \r
5 import era.mi.logic.wires.WireArray;\r
6 import era.mi.logic.wires.WireArray.WireArrayEnd;\r
7 \r
8 /**\r
9  * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input\r
10  * signal.\r
11  * \r
12  * @author Fabian Stemmler\r
13  *\r
14  */\r
15 public class Demux extends BasicComponent\r
16 {\r
17         private final WireArray select, in;\r
18         private final WireArray[] outputs;\r
19         private final WireArrayEnd[] outputsI;\r
20         private final int outputSize;\r
21         private int selected = -1;\r
22 \r
23         /**\r
24          * Output {@link WireArray}s and in must be of uniform length\r
25          * \r
26          * @param in      Must be of uniform length with all outputs.\r
27          * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.\r
28          * @param outputs One of these outputs receives the input signal, depending on the select bits\r
29          */\r
30         public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)\r
31         {\r
32                 super(processTime);\r
33                 outputSize = in.length;\r
34 \r
35                 this.in = in;\r
36                 this.outputs = outputs;\r
37                 this.outputsI = new WireArrayEnd[outputs.length];\r
38                 for (int i = 0; i < this.outputsI.length; i++)\r
39                 {\r
40                         if (outputs[i].length != outputSize)\r
41                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");\r
42                         this.outputsI[i] = outputs[i].createInput();\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.outputsI.length > maxInputs)\r
50                         throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "\r
51                                         + select.length + " select bits (" + maxInputs + ").");\r
52                 in.addObserver(this);\r
53         }\r
54 \r
55         @Override\r
56         public void compute()\r
57         {\r
58                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;\r
59                 if (selectValue >= outputsI.length)\r
60                         selectValue = -1;\r
61 \r
62                 if (selected != selectValue && selected != -1)\r
63                         outputsI[selected].clearSignals();\r
64 \r
65                 selected = selectValue;\r
66 \r
67                 if (selectValue != -1)\r
68                         outputsI[selectValue].feedSignals(in.getValues());\r
69         }\r
70 \r
71         @Override\r
72         public List<WireArray> getAllInputs()\r
73         {\r
74                 return List.of(in, select);\r
75         }\r
76 \r
77         @Override\r
78         public List<WireArray> getAllOutputs()\r
79         {\r
80                 return List.of(outputs);\r
81         }\r
82 }\r