1 package era.mi.logic.components;
\r
3 import java.util.Arrays;
\r
4 import java.util.Collections;
\r
5 import java.util.List;
\r
7 import era.mi.logic.wires.Wire;
\r
8 import era.mi.logic.wires.Wire.WireEnd;
\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
14 * @author Fabian Stemmler
\r
17 public class Demux extends BasicComponent
\r
19 private final WireEnd select, in;
\r
20 private final WireEnd[] outputs;
\r
21 private final int outputSize;
\r
22 private int selected = -1;
\r
25 * Input {@link Wire}s and out must be of uniform length
\r
27 * @param out Must be of uniform length with all inputs.
\r
28 * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
\r
29 * @param outputs One of these inputs is mapped to the output, depending on the select bits
\r
31 public Demux(int processTime, WireEnd in, WireEnd select, WireEnd... outputs)
\r
34 outputSize = in.length();
\r
37 this.outputs = outputs;
\r
38 for (int i = 0; i < this.outputs.length; i++)
\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
45 this.select = select;
\r
46 select.addObserver(this);
\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
56 public void compute()
\r
58 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
\r
59 if (selectValue >= outputs.length)
\r
62 if (selected != selectValue && selected != -1)
\r
63 outputs[selected].clearSignals();
\r
65 selected = selectValue;
\r
67 if (selectValue != -1)
\r
68 outputs[selectValue].feedSignals(in.getValues());
\r
72 public List<WireEnd> getAllInputs()
\r
74 return Collections.unmodifiableList(Arrays.asList(in, select));
\r
78 public List<WireEnd> getAllOutputs()
\r
80 return Collections.unmodifiableList(Arrays.asList(outputs));
\r