X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fcomponents%2FSplitter.java;h=b26cb3cedf4cfa42fa03557dc5fd5a055ec142c0;hb=daa558fbbd87e334d370f1a9d791adc6a3c14ca6;hp=58d48e7c563861d3555d1d862dabd6dca3f68c2c;hpb=5f74819aeebd1f133d3513bc2db9f2258a76c4ad;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/components/Splitter.java b/era.mi/src/era/mi/logic/components/Splitter.java index 58d48e7c..b26cb3ce 100644 --- a/era.mi/src/era/mi/logic/components/Splitter.java +++ b/era.mi/src/era/mi/logic/components/Splitter.java @@ -1,53 +1,59 @@ -package era.mi.logic.components; - -import era.mi.logic.Bit; -import era.mi.logic.wires.WireArray; -import era.mi.logic.wires.WireArrayObserver; - -public class Splitter implements WireArrayObserver -{ - private WireArray input; - private WireArray[] outputs; - - public Splitter(WireArray input, WireArray... outputs) - { - 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()."); - } - - protected void compute() - { - int startIndex = 0; - Bit[] inputBits = input.getValues(); - 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; - } - } - - public WireArray getInput() - { - return input; - } - - public WireArray[] getOutputs() - { - return outputs.clone(); - } - - @Override - public void update(WireArray initiator) - { - compute(); - } -} +package era.mi.logic.components; + +import java.util.List; + +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 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 (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; + for (int i = 0; i < outputs.length; i++) + { + outputs[i].feedSignals(inputBits.subVector(startIndex, startIndex + outputs[i].length())); + startIndex += outputs[i].length(); + } + } + + @Override + public void update(ReadEnd initiator, BitVector oldValues) + { + compute(); + } + + @Override + public List getAllInputs() + { + return List.of(input); + } + + @Override + public List getAllOutputs() + { + return List.of(outputs); + } +}