WireEnd functionality split into ReadEnd and ReadWriteEnd
[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.ReadEnd;\r
7 import era.mi.logic.wires.Wire.ReadWriteEnd;\r
8 \r
9 /**\r
10  * 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
11  * output.\r
12  * \r
13  * @author Fabian Stemmler\r
14  *\r
15  */\r
16 public class Demux extends BasicComponent\r
17 {\r
18         private final ReadEnd select, in;\r
19         private final ReadWriteEnd[] outputs;\r
20         private final int outputSize;\r
21         private int selected = -1;\r
22 \r
23         /**\r
24          * Output {@link Wire}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, ReadEnd in, ReadEnd select, ReadWriteEnd... 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                 for (int i = 0; i < this.outputs.length; i++)\r
38                 {\r
39                         if (outputs[i].length() != outputSize)\r
40                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");\r
41                         this.outputs[i] = outputs[i];\r
42                 }\r
43 \r
44                 this.select = select;\r
45                 select.addObserver(this);\r
46 \r
47                 int maxInputs = 1 << select.length();\r
48                 if (this.outputs.length > maxInputs)\r
49                         throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by "\r
50                                         + select.length() + " select bits (" + maxInputs + ").");\r
51                 in.addObserver(this);\r
52         }\r
53 \r
54         @Override\r
55         public void compute()\r
56         {\r
57                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;\r
58                 if (selectValue >= outputs.length)\r
59                         selectValue = -1;\r
60 \r
61                 if (selected != selectValue && selected != -1)\r
62                         outputs[selected].clearSignals();\r
63 \r
64                 selected = selectValue;\r
65 \r
66                 if (selectValue != -1)\r
67                         outputs[selectValue].feedSignals(in.getValues());\r
68         }\r
69 \r
70         @Override\r
71         public List<ReadEnd> getAllInputs()\r
72         {\r
73                 return List.of(in, select);\r
74         }\r
75 \r
76         @Override\r
77         public List<ReadWriteEnd> getAllOutputs()\r
78         {\r
79                 return List.of(outputs);\r
80         }\r
81 }\r