Added project specific format; Default values in WireArray are now U
[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.WireArrayEnd;
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 {
19         private final WireArray select, in;
20         private final WireArray[] outputs;
21         private final WireArrayEnd[] outputsI;
22         private final int outputSize;
23         private int selected = -1;
24
25         /**
26          * Input {@link WireArray}s and out must be of uniform length
27          * 
28          * @param out     Must be of uniform length with all inputs.
29          * @param select  Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs.
30          * @param outputs One of these inputs is mapped to the output, depending on the select bits
31          */
32         public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
33         {
34                 super(processTime);
35                 outputSize = in.length;
36
37                 this.in = in;
38                 this.outputs = outputs;
39                 this.outputsI = new WireArrayEnd[outputs.length];
40                 for (int i = 0; i < this.outputsI.length; i++)
41                 {
42                         if (outputs[i].length != outputSize)
43                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
44                         this.outputsI[i] = outputs[i].createInput();
45                 }
46
47                 this.select = select;
48                 select.addObserver(this);
49
50                 int maxInputs = 1 << select.length;
51                 if (this.outputsI.length > maxInputs)
52                         throw new IllegalArgumentException("There are more outputs (" + this.outputsI.length + ") to the DEMUX than supported by "
53                                         + select.length + " select bits (" + maxInputs + ").");
54                 in.addObserver(this);
55         }
56
57         @Override
58         public void compute()
59         {
60                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
61                 if (selectValue >= outputsI.length)
62                         selectValue = -1;
63
64                 if (selected != selectValue && selected != -1)
65                         outputsI[selected].clearSignals();
66
67                 selected = selectValue;
68
69                 if (selectValue != -1)
70                         outputsI[selectValue].feedSignals(in.getValues());
71         }
72
73         @Override
74         public List<WireArray> getAllInputs()
75         {
76                 return Collections.unmodifiableList(Arrays.asList(in, select));
77         }
78
79         @Override
80         public List<WireArray> getAllOutputs()
81         {
82                 return Collections.unmodifiableList(Arrays.asList(outputs));
83         }
84 }