Reformatted everything. Eclipse built-in Linewrapping/Comments 140 chars
[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.WireArray;
8 import era.mi.logic.wires.WireArray.WireArrayInput;
9
10 /**
11  * Models a multiplexer. Takes an arbitrary amount of input {@link WireArray}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         private final WireArray select, in;
19         private final WireArray[] outputs;
20         private final WireArrayInput[] outputsI;
21         private final int outputSize;
22         private int selected = -1;
23
24         /**
25          * Input {@link WireArray}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, WireArray in, WireArray select, WireArray... outputs) {
32                 super(processTime);
33                 outputSize = in.length;
34
35                 this.in = in;
36                 this.outputs = outputs;
37                 this.outputsI = new WireArrayInput[outputs.length];
38                 for (int i = 0; i < this.outputsI.length; i++) {
39                         if (outputs[i].length != outputSize)
40                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
41                         this.outputsI[i] = outputs[i].createInput();
42                 }
43
44                 this.select = select;
45                 select.addObserver(this);
46
47                 int maxInputs = 1 << select.length;
48                 if (this.outputsI.length > maxInputs)
49                         throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "
50                                         + select.length + " select bits (" + maxInputs + ").");
51                 in.addObserver(this);
52         }
53
54         @Override
55         public void compute() {
56                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
57                 if (selectValue >= outputsI.length)
58                         selectValue = -1;
59
60                 if (selected != selectValue && selected != -1)
61                         outputsI[selected].clearSignals();
62
63                 selected = selectValue;
64
65                 if (selectValue != -1)
66                         outputsI[selectValue].feedSignals(in.getValues());
67         }
68
69         @Override
70         public List<WireArray> getAllInputs() {
71                 return Collections.unmodifiableList(Arrays.asList(in, select));
72         }
73
74         @Override
75         public List<WireArray> getAllOutputs() {
76                 return Collections.unmodifiableList(Arrays.asList(outputs));
77         }
78 }