WireEnd functionality split into ReadEnd and ReadWriteEnd
[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.Wire;
6 import era.mi.logic.wires.Wire.ReadEnd;
7 import era.mi.logic.wires.Wire.ReadWriteEnd;
8
9 /**
10  * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the
11  * output.
12  * 
13  * @author Fabian Stemmler
14  *
15  */
16 public class Demux extends BasicComponent
17 {
18         private final ReadEnd select, in;
19         private final ReadWriteEnd[] outputs;
20         private final int outputSize;
21         private int selected = -1;
22
23         /**
24          * Output {@link Wire}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, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs)
31         {
32                 super(processTime);
33                 outputSize = in.length();
34
35                 this.in = in;
36                 this.outputs = outputs;
37                 for (int i = 0; i < this.outputs.length; i++)
38                 {
39                         if (outputs[i].length() != outputSize)
40                                 throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!");
41                         this.outputs[i] = outputs[i];
42                 }
43
44                 this.select = select;
45                 select.addObserver(this);
46
47                 int maxInputs = 1 << select.length();
48                 if (this.outputs.length > maxInputs)
49                         throw new IllegalArgumentException("There are more outputs (" + this.outputs.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         {
57                 int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1;
58                 if (selectValue >= outputs.length)
59                         selectValue = -1;
60
61                 if (selected != selectValue && selected != -1)
62                         outputs[selected].clearSignals();
63
64                 selected = selectValue;
65
66                 if (selectValue != -1)
67                         outputs[selectValue].feedSignals(in.getValues());
68         }
69
70         @Override
71         public List<ReadEnd> getAllInputs()
72         {
73                 return List.of(in, select);
74         }
75
76         @Override
77         public List<ReadWriteEnd> getAllOutputs()
78         {
79                 return List.of(outputs);
80         }
81 }