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