Reformatted everything. Eclipse built-in Linewrapping/Comments 140 chars
[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.WireArrayInput;
6 import era.mi.logic.wires.WireArrayObserver;
7
8 public class Splitter implements WireArrayObserver {
9         private WireArray input;
10         private WireArrayInput[] outputs;
11
12         public Splitter(WireArray input, WireArray... outputs) {
13                 this.input = input;
14                 this.outputs = WireArray.extractInputs(outputs);
15                 input.addObserver(this);
16                 int length = 0;
17                 for (WireArray out : outputs)
18                         length += out.length;
19
20                 if (input.length != length)
21                         throw new IllegalArgumentException(
22                                         "The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");
23         }
24
25         protected void compute() {
26                 int startIndex = 0;
27                 Bit[] inputBits = input.getValues();
28                 for (int i = 0; i < outputs.length; i++) {
29                         Bit[] outputBits = new Bit[outputs[i].owner.length];
30                         System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].owner.length);
31                         outputs[i].feedSignals(outputBits);
32                         startIndex += outputs[i].owner.length;
33                 }
34         }
35
36         @Override
37         public void update(WireArray initiator, Bit[] oldValues) {
38                 compute();
39         }
40 }