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