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