e5f49f419c997251f0283a4f9ff5fb7e6186588c
[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,
12  * as determined by select, is put through to the output.
13  * @author Fabian Stemmler
14  *
15  */
16 public class Demux extends BasicComponent
17 {
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          * @param out Must be of uniform length with all inputs.
27          * @param select Indexes the input array which is to be mapped to the output. Must have enough bits
28          * 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         {
33                 super(processTime);
34                 outputSize = in.length;
35                 
36                 this.in = in;
37                 this.outputs = outputs;
38                 this.outputsI = new WireArrayInput[outputs.length];
39                 for(int i = 0; i < this.outputsI.length; i++)
40                 {
41                         if(outputs[i].length != outputSize)
42                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
43                         this.outputsI[i] = outputs[i].createInput();
44                 }
45                 
46                 this.select = select;
47                 select.addObserver(this);
48                 
49                 int maxInputs = 1 << select.length;
50                 if(this.outputsI.length > maxInputs)
51                         throw new IllegalArgumentException("There are more outputs ("
52                                         + 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                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
60                 if(selectValue >= outputsI.length)
61                         selectValue = -1;
62                 
63                 if(selected != selectValue && selected != -1)
64                         outputsI[selected].clearSignals();
65                 
66                 selected = selectValue;
67                 
68                 if(selectValue != -1)
69                         outputsI[selectValue].feedSignals(in.getValues());
70         }
71
72         @Override
73         public List<WireArray> getAllInputs()
74         {
75                 return Collections.unmodifiableList(Arrays.asList(in, select));
76         }
77
78         @Override
79         public List<WireArray> getAllOutputs()
80         {
81                 return Collections.unmodifiableList(Arrays.asList(outputs));
82         }
83 }