Renamed mograsim to net.mograsim
[Mograsim.git] / era.mi / src / net / mograsim / logic / core / components / Splitter.java
1 package net.mograsim.logic.core.components;\r
2 \r
3 import java.util.List;\r
4 \r
5 import net.mograsim.logic.core.timeline.Timeline;\r
6 import net.mograsim.logic.core.types.BitVector;\r
7 import net.mograsim.logic.core.wires.WireObserver;\r
8 import net.mograsim.logic.core.wires.Wire.ReadEnd;\r
9 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;\r
10 \r
11 public class Splitter extends Component implements WireObserver\r
12 {\r
13         private ReadEnd input;\r
14         private ReadWriteEnd[] outputs;\r
15 \r
16         public Splitter(Timeline timeline, ReadEnd input, ReadWriteEnd... outputs)\r
17         {\r
18                 super(timeline);\r
19                 this.input = input;\r
20                 this.outputs = outputs;\r
21                 input.addObserver(this);\r
22                 int length = 0;\r
23                 for (ReadEnd out : outputs)\r
24                         length += out.length();\r
25 \r
26                 if (input.length() != length)\r
27                         throw new IllegalArgumentException(\r
28                                         "The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");\r
29         }\r
30 \r
31         protected void compute()\r
32         {\r
33                 BitVector inputBits = input.getValues();\r
34                 int startIndex = 0;\r
35                 for (int i = 0; i < outputs.length; i++)\r
36                 {\r
37                         outputs[i].feedSignals(inputBits.subVector(startIndex, startIndex + outputs[i].length()));\r
38                         startIndex += outputs[i].length();\r
39                 }\r
40         }\r
41 \r
42         @Override\r
43         public void update(ReadEnd initiator, BitVector oldValues)\r
44         {\r
45                 compute();\r
46         }\r
47 \r
48         @Override\r
49         public List<ReadEnd> getAllInputs()\r
50         {\r
51                 return List.of(input);\r
52         }\r
53 \r
54         @Override\r
55         public List<ReadWriteEnd> getAllOutputs()\r
56         {\r
57                 return List.of(outputs);\r
58         }\r
59 }\r