762d8067bfc8316649b870b2ad50d9ab859e24b7
[Mograsim.git] / era.mi / src / era / mi / logic / components / Demux.java
1 package era.mi.logic.components;
2
3 import java.util.List;
4
5 import era.mi.logic.wires.WireArray;
6 import era.mi.logic.wires.WireArray.WireArrayEnd;
7
8 /**
9  * Models a multiplexer. Takes an arbitrary amount of outputs {@link WireArray}s, one of which, as determined by select, receives the input
10  * signal.
11  * 
12  * @author Fabian Stemmler
13  *
14  */
15 public class Demux extends BasicComponent
16 {
17         private final WireArray select, in;
18         private final WireArray[] outputs;
19         private final WireArrayEnd[] outputsI;
20         private final int outputSize;
21         private int selected = -1;
22
23         /**
24          * Output {@link WireArray}s and in must be of uniform length
25          * 
26          * @param in      Must be of uniform length with all outputs.
27          * @param select  Indexes the output array to which the input is mapped. Must have enough bits to index all outputs.
28          * @param outputs One of these outputs receives the input signal, depending on the select bits
29          */
30         public Demux(int processTime, WireArray in, WireArray select, WireArray... outputs)
31         {
32                 super(processTime);
33                 outputSize = in.length;
34
35                 this.in = in;
36                 this.outputs = outputs;
37                 this.outputsI = new WireArrayEnd[outputs.length];
38                 for (int i = 0; i < this.outputsI.length; i++)
39                 {
40                         if (outputs[i].length != outputSize)
41                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
42                         this.outputsI[i] = outputs[i].createInput();
43                 }
44
45                 this.select = select;
46                 select.addObserver(this);
47
48                 int maxInputs = 1 << select.length;
49                 if (this.outputsI.length > maxInputs)
50                         throw new IllegalArgumentException("There are more outputs (" + this.outputsI.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 >= outputsI.length)
60                         selectValue = -1;
61
62                 if (selected != selectValue && selected != -1)
63                         outputsI[selected].clearSignals();
64
65                 selected = selectValue;
66
67                 if (selectValue != -1)
68                         outputsI[selectValue].feedSignals(in.getValues());
69         }
70
71         @Override
72         public List<WireArray> getAllInputs()
73         {
74                 return List.of(in, select);
75         }
76
77         @Override
78         public List<WireArray> getAllOutputs()
79         {
80                 return List.of(outputs);
81         }
82 }