package era.mi.logic.components;
-import era.mi.logic.Bit;
-import era.mi.logic.wires.WireArray;
-import era.mi.logic.wires.WireArrayObserver;
+import java.util.List;
-public class Splitter implements WireArrayObserver
+import era.mi.logic.timeline.Timeline;
+import era.mi.logic.types.BitVector;
+import era.mi.logic.wires.Wire.ReadEnd;
+import era.mi.logic.wires.Wire.ReadWriteEnd;
+import era.mi.logic.wires.WireObserver;
+
+public class Splitter extends Component implements WireObserver
{
- private WireArray input;
- private WireArray[] outputs;
-
- public Splitter(WireArray input, WireArray... outputs)
+ private ReadEnd input;
+ private ReadWriteEnd[] outputs;
+
+ public Splitter(Timeline timeline, ReadEnd input, ReadWriteEnd... outputs)
{
+ super(timeline);
this.input = input;
this.outputs = outputs;
input.addObserver(this);
int length = 0;
- for(WireArray out : outputs)
- length += out.length;
-
- if(input.length != length)
- throw new IllegalArgumentException("The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");
+ for (ReadEnd out : outputs)
+ length += out.length();
+
+ if (input.length() != length)
+ throw new IllegalArgumentException(
+ "The input of splitting one into n WireArrays must have length = a1.length() + a2.length() + ... + an.length().");
}
protected void compute()
{
+ BitVector inputBits = input.getValues();
int startIndex = 0;
- Bit[] inputBits = input.getValues();
- for(int i = 0; i < outputs.length; i++)
+ for (int i = 0; i < outputs.length; i++)
{
- Bit[] outputBits = new Bit[outputs[i].length];
- System.arraycopy(inputBits, startIndex, outputBits, 0, outputs[i].length);
- outputs[i].feedSignals(outputBits);
- startIndex += outputs[i].length;
+ outputs[i].feedSignals(inputBits.subVector(startIndex, startIndex + outputs[i].length()));
+ startIndex += outputs[i].length();
}
}
- public WireArray getInput()
+ @Override
+ public void update(ReadEnd initiator, BitVector oldValues)
{
- return input;
+ compute();
}
-
- public WireArray[] getOutputs()
+
+ @Override
+ public List<ReadEnd> getAllInputs()
{
- return outputs.clone();
+ return List.of(input);
}
-
+
@Override
- public void update(WireArray initiator)
+ public List<ReadWriteEnd> getAllOutputs()
{
- compute();
+ return List.of(outputs);
}
}