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