37e940157090ce93ff62679a8180c7e37baf21b0
[Mograsim.git] / era.mi / src / era / mi / logic / components / Splitter.java
1 package era.mi.logic.components;
2
3 import era.mi.logic.Bit;
4 import era.mi.logic.wires.WireArray;
5 import era.mi.logic.wires.WireArray.WireArrayEnd;
6 import era.mi.logic.wires.WireArrayObserver;
7
8 public class Splitter implements WireArrayObserver
9 {
10         private WireArray input;
11         private WireArrayEnd[] outputs;
12
13         public Splitter(WireArray input, WireArray... outputs)
14         {
15                 this.input = input;
16                 this.outputs = WireArray.extractInputs(outputs);
17                 input.addObserver(this);
18                 int length = 0;
19                 for (WireArray out : outputs)
20                         length += out.length;
21
22                 if (input.length != length)
23                         throw new IllegalArgumentException(
24                                         "The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");
25         }
26
27         protected void compute()
28         {
29                 int startIndex = 0;
30                 Bit[] inputBits = input.getValues();
31                 for (int i = 0; i < outputs.length; i++)
32                 {
33                         Bit[] outputBits = new Bit[outputs[i].owner.length];
34                         System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].owner.length);
35                         outputs[i].feedSignals(outputBits);
36                         startIndex += outputs[i].owner.length;
37                 }
38         }
39
40         @Override
41         public void update(WireArray initiator, Bit[] oldValues)
42         {
43                 compute();
44         }
45 }