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