a05145781cf22c624164608afd6a8ba29982a471
[Mograsim.git] / era.mi / src / net / mograsim / logic / core / components / Demux.java
1 package net.mograsim.logic.core.components;\r
2 \r
3 import java.util.List;\r
4 \r
5 import net.mograsim.logic.core.timeline.Timeline;\r
6 import net.mograsim.logic.core.wires.Wire;\r
7 import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
8 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;\r
9 \r
10 /**\r
11  * 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
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 ReadEnd select, in;\r
20         private final ReadWriteEnd[] outputs;\r
21         private final int outputSize;\r
22         private int selected = -1;\r
23 \r
24         /**\r
25          * Output {@link Wire}s and in must be of uniform length\r
26          * \r
27          * @param in      Must be of uniform length with all outputs.\r
28          * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.\r
29          * @param outputs One of these outputs receives the input signal, depending on the select bits\r
30          */\r
31         public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)\r
32         {\r
33                 super(timeline, processTime);\r
34                 outputSize = in.length();\r
35 \r
36                 this.in = in;\r
37                 this.outputs = outputs;\r
38                 for (int i = 0; i < this.outputs.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.outputs[i] = outputs[i];\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.outputs.length > maxInputs)\r
50                         throw new IllegalArgumentException("There are more outputs (" + this.outputs.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 >= outputs.length)\r
60                         selectValue = -1;\r
61 \r
62                 if (selected != selectValue && selected != -1)\r
63                         outputs[selected].clearSignals();\r
64 \r
65                 selected = selectValue;\r
66 \r
67                 if (selectValue != -1)\r
68                         outputs[selectValue].feedSignals(in.getValues());\r
69         }\r
70 \r
71         @Override\r
72         public List<ReadEnd> getAllInputs()\r
73         {\r
74                 return List.of(in, select);\r
75         }\r
76 \r
77         @Override\r
78         public List<ReadWriteEnd> getAllOutputs()\r
79         {\r
80                 return List.of(outputs);\r
81         }\r
82 }\r