From ecc651406020231ff0ebc8d1bfc6f916693f69c6 Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Sun, 2 Jun 2019 11:33:57 +0200 Subject: [PATCH] Generalized WireObserver to LogicObserver --- .../mograsim/logic/core/LogicObservable.java | 10 + .../mograsim/logic/core/LogicObserver.java | 6 + .../logic/core/components/BasicComponent.java | 71 +- .../logic/core/components/BitDisplay.java | 102 +- .../logic/core/components/Connector.java | 152 +-- .../mograsim/logic/core/components/Demux.java | 164 +-- .../logic/core/components/Merger.java | 170 +-- .../mograsim/logic/core/components/Mux.java | 4 +- .../logic/core/components/Splitter.java | 119 +-- .../logic/core/components/TriStateBuffer.java | 102 +- .../core/components/gates/MultiInputGate.java | 112 +- .../logic/core/components/gates/NotGate.java | 100 +- .../logic/core/tests/ComponentTest.java | 866 +++++++-------- .../net/mograsim/logic/core/wires/Wire.java | 990 +++++++++--------- .../logic/core/wires/WireObserver.java | 9 - 15 files changed, 1496 insertions(+), 1481 deletions(-) create mode 100644 net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java create mode 100644 net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObserver.java delete mode 100644 net.mograsim.logic.core/src/net/mograsim/logic/core/wires/WireObserver.java diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java new file mode 100644 index 00000000..ddfa070a --- /dev/null +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java @@ -0,0 +1,10 @@ +package net.mograsim.logic.core; + +public interface LogicObservable +{ + public void registerObserver(LogicObserver ob); + + public void notifyObservers(); + +// public InnerState getInnerState(); +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObserver.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObserver.java new file mode 100644 index 00000000..9375004a --- /dev/null +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObserver.java @@ -0,0 +1,6 @@ +package net.mograsim.logic.core; + +public interface LogicObserver +{ + public void update(LogicObservable initiator); +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BasicComponent.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BasicComponent.java index 426c6a11..e2d2d654 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BasicComponent.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BasicComponent.java @@ -1,36 +1,35 @@ -package net.mograsim.logic.core.components; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.WireObserver; -import net.mograsim.logic.core.wires.Wire.ReadEnd; - -/** - * A basic component that recomputes all outputs (with a delay), when it is updated. - * - * @author Fabian Stemmler - */ -public abstract class BasicComponent extends Component implements WireObserver -{ - private int processTime; - - /** - * - * @param processTime Amount of time this component takes to update its outputs. Must be more than 0, otherwise 1 is assumed. - * - * @author Fabian Stemmler - */ - public BasicComponent(Timeline timeline, int processTime) - { - super(timeline); - this.processTime = processTime > 0 ? processTime : 1; - } - - @Override - public void update(ReadEnd initiator, BitVector oldValues) - { - timeline.addEvent(e -> compute(), processTime); - } - - protected abstract void compute(); -} +package net.mograsim.logic.core.components; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; + +/** + * A basic component that recomputes all outputs (with a delay), when it is updated. + * + * @author Fabian Stemmler + */ +public abstract class BasicComponent extends Component implements LogicObserver +{ + private int processTime; + + /** + * + * @param processTime Amount of time this component takes to update its outputs. Must be more than 0, otherwise 1 is assumed. + * + * @author Fabian Stemmler + */ + public BasicComponent(Timeline timeline, int processTime) + { + super(timeline); + this.processTime = processTime > 0 ? processTime : 1; + } + + @Override + public void update(LogicObservable initiator) + { + timeline.addEvent(e -> compute(), processTime); + } + + protected abstract void compute(); +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BitDisplay.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BitDisplay.java index 268d1572..af2fbc00 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BitDisplay.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/BitDisplay.java @@ -1,51 +1,51 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.Bit; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class BitDisplay extends BasicComponent -{ - private final ReadEnd in; - private BitVector displayedValue; - - public BitDisplay(Timeline timeline, ReadEnd in) - { - super(timeline, 1); - this.in = in; - in.addObserver(this); - compute(); - } - - @Override - protected void compute() - { - displayedValue = in.getValues(); - } - - public BitVector getDisplayedValue() - { - return displayedValue; - } - - public boolean isDisplaying(Bit... values) - { - return displayedValue.equals(BitVector.of(values)); - } - - @Override - public List getAllInputs() - { - return List.of(in); - } - - @Override - public List getAllOutputs() - { - return List.of(); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class BitDisplay extends BasicComponent +{ + private final ReadEnd in; + private BitVector displayedValue; + + public BitDisplay(Timeline timeline, ReadEnd in) + { + super(timeline, 1); + this.in = in; + in.registerObserver(this); + compute(); + } + + @Override + protected void compute() + { + displayedValue = in.getValues(); + } + + public BitVector getDisplayedValue() + { + return displayedValue; + } + + public boolean isDisplaying(Bit... values) + { + return displayedValue.equals(BitVector.of(values)); + } + + @Override + public List getAllInputs() + { + return List.of(in); + } + + @Override + public List getAllOutputs() + { + return List.of(); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Connector.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Connector.java index 16b8e709..dc8490a7 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Connector.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Connector.java @@ -1,76 +1,76 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.WireObserver; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class Connector extends Component implements WireObserver -{ - private boolean connected; - private final ReadWriteEnd a; - private final ReadWriteEnd b; - - public Connector(Timeline timeline, ReadWriteEnd a, ReadWriteEnd b) - { - super(timeline); - if (a.length() != b.length()) - throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length(), b.length())); - this.a = a; - this.b = b; - a.addObserver(this); - b.addObserver(this); - } - - public void connect() - { - connected = true; - update(a); - update(b); - } - - public void disconnect() - { - connected = false; - a.clearSignals(); - b.clearSignals(); - } - - public void setConnection(boolean connected) - { - if (connected) - connect(); - else - disconnect(); - } - - @Override - public void update(ReadEnd initiator, BitVector oldValues) - { - if (connected) - timeline.addEvent(e -> update(initiator), 1); - } - - private void update(ReadEnd initiator) - { - if (initiator == a) - b.feedSignals(a.wireValuesExcludingMe()); - else - a.feedSignals(b.wireValuesExcludingMe()); - } - - @Override - public List getAllInputs() - { - return List.of(a, b); - } - - @Override - public List getAllOutputs() - { - return List.of(a, b); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class Connector extends Component implements LogicObserver +{ + private boolean connected; + private final ReadWriteEnd a; + private final ReadWriteEnd b; + + public Connector(Timeline timeline, ReadWriteEnd a, ReadWriteEnd b) + { + super(timeline); + if (a.length() != b.length()) + throw new IllegalArgumentException(String.format("WireArray width does not match: %d, %d", a.length(), b.length())); + this.a = a; + this.b = b; + a.registerObserver(this); + b.registerObserver(this); + } + + public void connect() + { + connected = true; + update(a); + update(b); + } + + public void disconnect() + { + connected = false; + a.clearSignals(); + b.clearSignals(); + } + + public void setConnection(boolean connected) + { + if (connected) + connect(); + else + disconnect(); + } + + @Override + public void update(LogicObservable initiator) + { + if (connected) + timeline.addEvent(e -> innerUpdate(initiator), 1); + } + + private void innerUpdate(LogicObservable initiator) + { + if (initiator == a) + b.feedSignals(a.wireValuesExcludingMe()); + else + a.feedSignals(b.wireValuesExcludingMe()); + } + + @Override + public List getAllInputs() + { + return List.of(a, b); + } + + @Override + public List getAllOutputs() + { + return List.of(a, b); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Demux.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Demux.java index 5da1bf6a..e5b228d1 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Demux.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Demux.java @@ -1,82 +1,82 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.wires.Wire; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -/** - * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the - * output. - * - * @author Fabian Stemmler - * - */ -public class Demux extends BasicComponent -{ - private final ReadEnd select, in; - private final ReadWriteEnd[] outputs; - private final int outputSize; - private int selected = -1; - - /** - * Output {@link Wire}s and in must be of uniform length - * - * @param in Must be of uniform length with all outputs. - * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs. - * @param outputs One of these outputs receives the input signal, depending on the select bits - */ - public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs) - { - super(timeline, processTime); - outputSize = in.length(); - - this.in = in; - this.outputs = outputs; - for (int i = 0; i < this.outputs.length; i++) - { - if (outputs[i].length() != outputSize) - throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!"); - this.outputs[i] = outputs[i]; - } - - this.select = select; - select.addObserver(this); - - int maxInputs = 1 << select.length(); - if (this.outputs.length > maxInputs) - throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by " - + select.length() + " select bits (" + maxInputs + ")."); - in.addObserver(this); - } - - @Override - public void compute() - { - int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1; - if (selectValue >= outputs.length) - selectValue = -1; - - if (selected != selectValue && selected != -1) - outputs[selected].clearSignals(); - - selected = selectValue; - - if (selectValue != -1) - outputs[selectValue].feedSignals(in.getValues()); - } - - @Override - public List getAllInputs() - { - return List.of(in, select); - } - - @Override - public List getAllOutputs() - { - return List.of(outputs); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +/** + * Models a multiplexer. Takes an arbitrary amount of input {@link Wire}s, one of which, as determined by select, is put through to the + * output. + * + * @author Fabian Stemmler + * + */ +public class Demux extends BasicComponent +{ + private final ReadEnd select, in; + private final ReadWriteEnd[] outputs; + private final int outputSize; + private int selected = -1; + + /** + * Output {@link Wire}s and in must be of uniform length + * + * @param in Must be of uniform length with all outputs. + * @param select Indexes the output array to which the input is mapped. Must have enough bits to index all outputs. + * @param outputs One of these outputs receives the input signal, depending on the select bits + */ + public Demux(Timeline timeline, int processTime, ReadEnd in, ReadEnd select, ReadWriteEnd... outputs) + { + super(timeline, processTime); + outputSize = in.length(); + + this.in = in; + this.outputs = outputs; + for (int i = 0; i < this.outputs.length; i++) + { + if (outputs[i].length() != outputSize) + throw new IllegalArgumentException("All DEMUX wire arrays must be of uniform length!"); + this.outputs[i] = outputs[i]; + } + + this.select = select; + select.registerObserver(this); + + int maxInputs = 1 << select.length(); + if (this.outputs.length > maxInputs) + throw new IllegalArgumentException("There are more outputs (" + this.outputs.length + ") to the DEMUX than supported by " + + select.length() + " select bits (" + maxInputs + ")."); + in.registerObserver(this); + } + + @Override + public void compute() + { + int selectValue = select.hasNumericValue() ? (int) select.getUnsignedValue() : -1; + if (selectValue >= outputs.length) + selectValue = -1; + + if (selected != selectValue && selected != -1) + outputs[selected].clearSignals(); + + selected = selectValue; + + if (selectValue != -1) + outputs[selectValue].feedSignals(in.getValues()); + } + + @Override + public List getAllInputs() + { + return List.of(in, select); + } + + @Override + public List getAllOutputs() + { + return List.of(outputs); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java index c4c71701..06e064c9 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Merger.java @@ -1,85 +1,85 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.Wire; -import net.mograsim.logic.core.wires.WireObserver; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class Merger extends Component implements WireObserver -{ - private ReadWriteEnd out; - private ReadEnd[] inputs; - private int[] beginningIndex; - - /** - * - * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length(). - * @param inputs The inputs to be merged into the union - */ - public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs) - { - super(timeline); - this.inputs = inputs; - this.out = union; - this.beginningIndex = new int[inputs.length]; - - int length = 0; - for (int i = 0; i < inputs.length; i++) - { - beginningIndex[i] = length; - length += inputs[i].length(); - inputs[i].addObserver(this); - } - - if (length != union.length()) - throw new IllegalArgumentException( - "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length()."); - } - - public ReadEnd getInput(int index) - { - return inputs[index]; - } - - public ReadEnd getUnion() - { - return out; - } - - @Override - public void update(ReadEnd initiator, BitVector oldValues) - { - int index = find(initiator); - int beginning = beginningIndex[index]; - out.feedSignals(beginning, inputs[index].getValues()); - } - - private int find(ReadEnd r) - { - for (int i = 0; i < inputs.length; i++) - if (inputs[i] == r) - return i; - return -1; - } - - public ReadEnd[] getInputs() - { - return inputs.clone(); - } - - @Override - public List getAllInputs() - { - return List.of(inputs); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class Merger extends Component implements LogicObserver +{ + private ReadWriteEnd out; + private ReadEnd[] inputs; + private int[] beginningIndex; + + /** + * + * @param union The output of merging n {@link Wire}s into one. Must have length = a1.length() + a2.length() + ... + an.length(). + * @param inputs The inputs to be merged into the union + */ + public Merger(Timeline timeline, ReadWriteEnd union, ReadEnd... inputs) + { + super(timeline); + this.inputs = inputs; + this.out = union; + this.beginningIndex = new int[inputs.length]; + + int length = 0; + for (int i = 0; i < inputs.length; i++) + { + beginningIndex[i] = length; + length += inputs[i].length(); + inputs[i].registerObserver(this); + } + + if (length != union.length()) + throw new IllegalArgumentException( + "The output of merging n WireArrays into one must have length = a1.length() + a2.length() + ... + an.length()."); + } + + public ReadEnd getInput(int index) + { + return inputs[index]; + } + + public ReadEnd getUnion() + { + return out; + } + + @Override + public void update(LogicObservable initiator) + { + int index = find(initiator); + int beginning = beginningIndex[index]; + out.feedSignals(beginning, inputs[index].getValues()); + } + + private int find(LogicObservable r) + { + for (int i = 0; i < inputs.length; i++) + if (inputs[i] == r) + return i; + return -1; + } + + public ReadEnd[] getInputs() + { + return inputs.clone(); + } + + @Override + public List getAllInputs() + { + return List.of(inputs); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Mux.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Mux.java index bbb10f5e..5e75f96d 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Mux.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Mux.java @@ -41,11 +41,11 @@ public class Mux extends BasicComponent { if (inputs[i].length() != outputSize) throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!"); - inputs[i].addObserver(this); + inputs[i].registerObserver(this); } this.select = select; - select.addObserver(this); + select.registerObserver(this); int maxInputs = 1 << select.length(); if (this.inputs.length > maxInputs) diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Splitter.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Splitter.java index 9eb5b2c0..a6165d9d 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Splitter.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/Splitter.java @@ -1,59 +1,60 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.WireObserver; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -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); - } -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class Splitter extends Component implements LogicObserver +{ + private ReadEnd input; + private ReadWriteEnd[] outputs; + + public Splitter(Timeline timeline, ReadEnd input, ReadWriteEnd... outputs) + { + super(timeline); + this.input = input; + this.outputs = outputs; + input.registerObserver(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(LogicObservable initiator) + { + compute(); + } + + @Override + public List getAllInputs() + { + return List.of(input); + } + + @Override + public List getAllOutputs() + { + return List.of(outputs); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/TriStateBuffer.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/TriStateBuffer.java index f4f1b491..1f8f85a5 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/TriStateBuffer.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/TriStateBuffer.java @@ -1,51 +1,51 @@ -package net.mograsim.logic.core.components; - -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.Bit; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class TriStateBuffer extends BasicComponent -{ - ReadEnd in, enable; - ReadWriteEnd out; - - public TriStateBuffer(Timeline timeline, int processTime, ReadEnd in, ReadWriteEnd out, ReadEnd enable) - { - super(timeline, processTime); - if (in.length() != out.length()) - throw new IllegalArgumentException( - "Tri-state output must have the same amount of bits as the input. Input: " + in.length() + " Output: " + out.length()); - if (enable.length() != 1) - throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length() + "."); - this.in = in; - in.addObserver(this); - this.enable = enable; - enable.addObserver(this); - this.out = out; - } - - @Override - protected void compute() - { - if (enable.getValue() == Bit.ONE) - out.feedSignals(in.getValues()); - else - out.clearSignals(); - } - - @Override - public List getAllInputs() - { - return List.of(in, enable); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } - -} +package net.mograsim.logic.core.components; + +import java.util.List; + +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class TriStateBuffer extends BasicComponent +{ + ReadEnd in, enable; + ReadWriteEnd out; + + public TriStateBuffer(Timeline timeline, int processTime, ReadEnd in, ReadWriteEnd out, ReadEnd enable) + { + super(timeline, processTime); + if (in.length() != out.length()) + throw new IllegalArgumentException( + "Tri-state output must have the same amount of bits as the input. Input: " + in.length() + " Output: " + out.length()); + if (enable.length() != 1) + throw new IllegalArgumentException("Tri-state enable must have exactly one bit, not " + enable.length() + "."); + this.in = in; + in.registerObserver(this); + this.enable = enable; + enable.registerObserver(this); + this.out = out; + } + + @Override + protected void compute() + { + if (enable.getValue() == Bit.ONE) + out.feedSignals(in.getValues()); + else + out.clearSignals(); + } + + @Override + public List getAllInputs() + { + return List.of(in, enable); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } + +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/MultiInputGate.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/MultiInputGate.java index afc89511..79a1df50 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/MultiInputGate.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/MultiInputGate.java @@ -1,56 +1,56 @@ -package net.mograsim.logic.core.components.gates; - -import java.util.List; - -import net.mograsim.logic.core.components.BasicComponent; -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.MutationOperation; -import net.mograsim.logic.core.types.BitVector.BitVectorMutator; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public abstract class MultiInputGate extends BasicComponent -{ - protected ReadEnd[] in; - protected ReadWriteEnd out; - protected final int length; - protected MutationOperation op; - - protected MultiInputGate(Timeline timeline, int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in) - { - super(timeline, processTime); - this.op = op; - length = out.length(); - this.in = in.clone(); - if (in.length < 1) - throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length)); - for (ReadEnd w : in) - { - if (w.length() != length) - throw new IllegalArgumentException("All wires connected to the gate must be of uniform length."); - w.addObserver(this); - } - this.out = out; - } - - @Override - public List getAllInputs() - { - return List.of(in); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } - - @Override - protected void compute() - { - BitVectorMutator mutator = BitVectorMutator.empty(); - for (ReadEnd w : in) - op.apply(mutator, w.getValues()); - out.feedSignals(mutator.get()); - } -} +package net.mograsim.logic.core.components.gates; + +import java.util.List; + +import net.mograsim.logic.core.components.BasicComponent; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.MutationOperation; +import net.mograsim.logic.core.types.BitVector.BitVectorMutator; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public abstract class MultiInputGate extends BasicComponent +{ + protected ReadEnd[] in; + protected ReadWriteEnd out; + protected final int length; + protected MutationOperation op; + + protected MultiInputGate(Timeline timeline, int processTime, MutationOperation op, ReadWriteEnd out, ReadEnd... in) + { + super(timeline, processTime); + this.op = op; + length = out.length(); + this.in = in.clone(); + if (in.length < 1) + throw new IllegalArgumentException(String.format("Cannot create gate with %d wires.", in.length)); + for (ReadEnd w : in) + { + if (w.length() != length) + throw new IllegalArgumentException("All wires connected to the gate must be of uniform length."); + w.registerObserver(this); + } + this.out = out; + } + + @Override + public List getAllInputs() + { + return List.of(in); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } + + @Override + protected void compute() + { + BitVectorMutator mutator = BitVectorMutator.empty(); + for (ReadEnd w : in) + op.apply(mutator, w.getValues()); + out.feedSignals(mutator.get()); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/NotGate.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/NotGate.java index b1cfefee..e1c0e607 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/NotGate.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/gates/NotGate.java @@ -1,50 +1,50 @@ -package net.mograsim.logic.core.components.gates; - -import java.util.List; - -import net.mograsim.logic.core.components.BasicComponent; -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -public class NotGate extends BasicComponent -{ - private ReadEnd in; - private ReadWriteEnd out; - - public NotGate(Timeline timeline, int processTime, ReadEnd in, ReadWriteEnd out) - { - super(timeline, processTime); - this.in = in; - in.addObserver(this); - this.out = out; - } - - @Override - protected void compute() - { - out.feedSignals(in.getValues().not()); - } - - public ReadEnd getIn() - { - return in; - } - - public ReadEnd getOut() - { - return out; - } - - @Override - public List getAllInputs() - { - return List.of(in); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } -} +package net.mograsim.logic.core.components.gates; + +import java.util.List; + +import net.mograsim.logic.core.components.BasicComponent; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +public class NotGate extends BasicComponent +{ + private ReadEnd in; + private ReadWriteEnd out; + + public NotGate(Timeline timeline, int processTime, ReadEnd in, ReadWriteEnd out) + { + super(timeline, processTime); + this.in = in; + in.registerObserver(this); + this.out = out; + } + + @Override + protected void compute() + { + out.feedSignals(in.getValues().not()); + } + + public ReadEnd getIn() + { + return in; + } + + public ReadEnd getOut() + { + return out; + } + + @Override + public List getAllInputs() + { + return List.of(in); + } + + @Override + public List getAllOutputs() + { + return List.of(out); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/ComponentTest.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/ComponentTest.java index a47f7921..e97836b1 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/ComponentTest.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/ComponentTest.java @@ -1,433 +1,433 @@ -package net.mograsim.logic.core.tests; - -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.util.function.LongConsumer; - -import org.junit.jupiter.api.Test; - -import net.mograsim.logic.core.components.Connector; -import net.mograsim.logic.core.components.Demux; -import net.mograsim.logic.core.components.Merger; -import net.mograsim.logic.core.components.Mux; -import net.mograsim.logic.core.components.Splitter; -import net.mograsim.logic.core.components.TriStateBuffer; -import net.mograsim.logic.core.components.gates.AndGate; -import net.mograsim.logic.core.components.gates.NotGate; -import net.mograsim.logic.core.components.gates.OrGate; -import net.mograsim.logic.core.components.gates.XorGate; -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.Bit; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.Wire; -import net.mograsim.logic.core.wires.Wire.ReadEnd; -import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; - -class ComponentTest -{ - private Timeline t = new Timeline(11); - - @Test - void circuitExampleTest() - { - Wire a = new Wire(t, 1, 1), b = new Wire(t, 1, 1), c = new Wire(t, 1, 10), d = new Wire(t, 2, 1), e = new Wire(t, 1, 1), - f = new Wire(t, 1, 1), g = new Wire(t, 1, 1), h = new Wire(t, 2, 1), i = new Wire(t, 2, 1), j = new Wire(t, 1, 1), - k = new Wire(t, 1, 1); - new AndGate(t, 1, f.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); - new NotGate(t, 1, f.createReadOnlyEnd(), g.createReadWriteEnd()); - new Merger(t, h.createReadWriteEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd()); - new Mux(t, 1, i.createReadWriteEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd()); - new Splitter(t, i.createReadOnlyEnd(), k.createReadWriteEnd(), j.createReadWriteEnd()); - - a.createReadWriteEnd().feedSignals(Bit.ZERO); - b.createReadWriteEnd().feedSignals(Bit.ONE); - c.createReadWriteEnd().feedSignals(Bit.ZERO); - d.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE); - e.createReadWriteEnd().feedSignals(Bit.ZERO); - - t.executeAll(); - - assertEquals(Bit.ONE, j.getValue()); - assertEquals(Bit.ZERO, k.getValue()); - } - - @Test - void splitterTest() - { - t.reset(); - Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), in = new Wire(t, 8, 1); - in.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - new Splitter(t, in.createReadOnlyEnd(), a.createReadWriteEnd(), b.createReadWriteEnd(), c.createReadWriteEnd()); - - t.executeAll(); - - assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO); - assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO); - assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE); - } - - @Test - void mergerTest() - { - t.reset(); - Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), out = new Wire(t, 8, 1); - a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO); - b.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO); - c.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); - - new Merger(t, out.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); - - t.executeAll(); - - assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - } - - @Test - void triStateBufferTest() - { - Wire a = new Wire(t, 1, 1), b = new Wire(t, 1, 1), en = new Wire(t, 1, 1), notEn = new Wire(t, 1, 1); - new NotGate(t, 1, en.createReadOnlyEnd(), notEn.createReadWriteEnd()); - new TriStateBuffer(t, 1, a.createReadOnlyEnd(), b.createReadWriteEnd(), en.createReadOnlyEnd()); - new TriStateBuffer(t, 1, b.createReadOnlyEnd(), a.createReadWriteEnd(), notEn.createReadOnlyEnd()); - - ReadWriteEnd enI = en.createReadWriteEnd(), aI = a.createReadWriteEnd(), bI = b.createReadWriteEnd(); - enI.feedSignals(Bit.ONE); - aI.feedSignals(Bit.ONE); - bI.feedSignals(Bit.Z); - - t.executeAll(); - - assertEquals(Bit.ONE, b.getValue()); - - bI.feedSignals(Bit.ZERO); - - t.executeAll(); - - assertEquals(Bit.X, b.getValue()); - assertEquals(Bit.ONE, a.getValue()); - - aI.clearSignals(); - enI.feedSignals(Bit.ZERO); - - t.executeAll(); - - assertEquals(Bit.ZERO, a.getValue()); - - } - - @Test - void muxTest() - { - t.reset(); - Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), out = new Wire(t, 4, 1); - ReadWriteEnd selectIn = select.createReadWriteEnd(); - - selectIn.feedSignals(Bit.ZERO, Bit.ZERO); - a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); - c.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - - new Mux(t, 1, out.createReadWriteEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), - c.createReadOnlyEnd()); - t.executeAll(); - - assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); - selectIn.feedSignals(Bit.ZERO, Bit.ONE); - t.executeAll(); - - assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - - selectIn.feedSignals(Bit.ONE, Bit.ONE); - t.executeAll(); - - assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); - - } - - @Test - void demuxTest() - { - t.reset(); - Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), in = new Wire(t, 4, 1); - ReadWriteEnd selectIn = select.createReadWriteEnd(); - - selectIn.feedSignals(Bit.ZERO, Bit.ZERO); - in.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); - - new Demux(t, 1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createReadWriteEnd(), b.createReadWriteEnd(), - c.createReadWriteEnd()); - t.executeAll(); - - assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); - assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); - assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); - selectIn.feedSignals(Bit.ZERO, Bit.ONE); - t.executeAll(); - - assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); - assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); - assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); - - selectIn.feedSignals(Bit.ONE, Bit.ONE); - t.executeAll(); - - assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); - assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); - assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); - - } - - @Test - void andTest() - { - t.reset(); - Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); - new AndGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); - a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); - b.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - - t.executeAll(); - - assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO); - } - - @Test - void orTest() - { - t.reset(); - Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); - new OrGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); - a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); - b.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); - - t.executeAll(); - - assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE); - } - - @Test - void xorTest() - { - t.reset(); - Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2), c = new Wire(t, 3, 1), d = new Wire(t, 3, 1); - new XorGate(t, 1, d.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); - a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); - b.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); - c.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); - - t.executeAll(); - - assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE); - } - - @Test - void notTest() - { - t.reset(); - Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2); - new NotGate(t, 1, a.createReadOnlyEnd(), b.createReadWriteEnd()); - a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); - - t.executeAll(); - - assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO); - } - - @Test - void rsLatchCircuitTest() - { - t.reset(); - Wire r = new Wire(t, 1, 1), s = new Wire(t, 1, 1), t1 = new Wire(t, 1, 15), t2 = new Wire(t, 1, 1), q = new Wire(t, 1, 1), - nq = new Wire(t, 1, 1); - - new OrGate(t, 1, t2.createReadWriteEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd()); - new OrGate(t, 1, t1.createReadWriteEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd()); - new NotGate(t, 1, t2.createReadOnlyEnd(), q.createReadWriteEnd()); - new NotGate(t, 1, t1.createReadOnlyEnd(), nq.createReadWriteEnd()); - - ReadWriteEnd sIn = s.createReadWriteEnd(), rIn = r.createReadWriteEnd(); - - sIn.feedSignals(Bit.ONE); - rIn.feedSignals(Bit.ZERO); - - t.executeAll(); - - assertEquals(Bit.ONE, q.getValue()); - assertEquals(Bit.ZERO, nq.getValue()); - - sIn.feedSignals(Bit.ZERO); - - t.executeAll(); - assertEquals(Bit.ONE, q.getValue()); - assertEquals(Bit.ZERO, nq.getValue()); - - rIn.feedSignals(Bit.ONE); - - t.executeAll(); - - assertEquals(Bit.ZERO, q.getValue()); - assertEquals(Bit.ONE, nq.getValue()); - } - - @Test - void numericValueTest() - { - t.reset(); - - Wire a = new Wire(t, 4, 1); - a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE); - - t.executeAll(); - - assertEquals(15, a.getUnsignedValue()); - assertEquals(-1, a.getSignedValue()); - } - - boolean flag = false; - - @Test - void simpleTimelineTest() - { - Timeline t = new Timeline(3); - flag = false; - t.addEvent((e) -> - { - if (!flag) - fail(); - flag = false; - }, 15); - t.addEvent((e) -> - { - if (flag) - fail(); - flag = true; - }, 10); - t.addEvent((e) -> - { - if (flag) - fail(); - flag = true; - }, 20); - t.addEvent((e) -> - { - fail("Only supposed to execute until timestamp 20, not 25"); - }, 25); - - t.executeUntil(t.laterThan(20), 100); - - if (!flag) - fail(); - } - - @Test - void multipleInputs() - { - t.reset(); - Wire w = new Wire(t, 2, 1); - ReadWriteEnd wI1 = w.createReadWriteEnd(), wI2 = w.createReadWriteEnd(); - wI1.feedSignals(Bit.ONE, Bit.Z); - wI2.feedSignals(Bit.Z, Bit.X); - t.executeAll(); - assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X); - - wI2.feedSignals(Bit.ZERO, Bit.Z); - t.executeAll(); - assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z); - - wI2.feedSignals(Bit.Z, Bit.Z); - t.executeAll(); - assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); - - wI2.feedSignals(Bit.ONE, Bit.Z); - ReadEnd rE = w.createReadOnlyEnd(); - rE.addObserver((i, oldValues) -> fail("WireEnd notified observer, although value did not change.")); - t.executeAll(); - rE.close(); - wI1.feedSignals(Bit.X, Bit.X); - t.executeAll(); - wI1.addObserver((i, oldValues) -> fail("WireEnd notified observer, although it was closed.")); - wI1.close(); - assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); - } - - @Test - void wireConnections() - { - // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde - - t.reset(); - - Wire a = new Wire(t, 1, 2); - Wire b = new Wire(t, 1, 2); - Wire c = new Wire(t, 1, 2); - ReadWriteEnd aI = a.createReadWriteEnd(); - ReadWriteEnd bI = b.createReadWriteEnd(); - ReadWriteEnd cI = c.createReadWriteEnd(); - - TestBitDisplay test = new TestBitDisplay(t, c.createReadOnlyEnd()); - TestBitDisplay test2 = new TestBitDisplay(t, a.createReadOnlyEnd()); - LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c); - - cI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - - cI.feedSignals(Bit.X); - test.assertAfterSimulationIs(print, Bit.X); - - cI.feedSignals(Bit.X); - cI.feedSignals(Bit.Z); - test.assertAfterSimulationIs(print, Bit.Z); - - new Connector(t, b.createReadWriteEnd(), c.createReadWriteEnd()).connect(); - test.assertAfterSimulationIs(print, Bit.Z); - System.err.println("ONE"); - bI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - System.err.println("ZERO"); - bI.feedSignals(Bit.ZERO); - test.assertAfterSimulationIs(print, Bit.ZERO); - System.err.println("Z"); - bI.feedSignals(Bit.Z); - test.assertAfterSimulationIs(print, Bit.Z); - - new Connector(t, a.createReadWriteEnd(), b.createReadWriteEnd()).connect(); - System.err.println("Z 2"); - aI.feedSignals(Bit.Z); - test.assertAfterSimulationIs(print, Bit.Z); - test2.assertAfterSimulationIs(Bit.Z); - System.err.println("ONE 2"); - aI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - test2.assertAfterSimulationIs(Bit.ONE); - System.err.println("ZERO 2"); - aI.feedSignals(Bit.ZERO); - test.assertAfterSimulationIs(print, Bit.ZERO); - test2.assertAfterSimulationIs(Bit.ZERO); - System.err.println("Z 2 II"); - aI.feedSignals(Bit.Z); - test.assertAfterSimulationIs(print, Bit.Z); - test2.assertAfterSimulationIs(Bit.Z); - - System.err.println("No Conflict yet"); - bI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - test2.assertAfterSimulationIs(Bit.ONE); - aI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - test2.assertAfterSimulationIs(Bit.ONE); - System.err.println("Conflict"); - aI.feedSignals(Bit.ZERO); - test.assertAfterSimulationIs(print, Bit.X); - test2.assertAfterSimulationIs(Bit.X); - aI.feedSignals(Bit.ONE); - test.assertAfterSimulationIs(print, Bit.ONE); - test2.assertAfterSimulationIs(Bit.ONE); - } - - private static void assertBitArrayEquals(BitVector actual, Bit... expected) - { - assertArrayEquals(expected, actual.getBits()); - } -} +package net.mograsim.logic.core.tests; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.function.LongConsumer; + +import org.junit.jupiter.api.Test; + +import net.mograsim.logic.core.components.Connector; +import net.mograsim.logic.core.components.Demux; +import net.mograsim.logic.core.components.Merger; +import net.mograsim.logic.core.components.Mux; +import net.mograsim.logic.core.components.Splitter; +import net.mograsim.logic.core.components.TriStateBuffer; +import net.mograsim.logic.core.components.gates.AndGate; +import net.mograsim.logic.core.components.gates.NotGate; +import net.mograsim.logic.core.components.gates.OrGate; +import net.mograsim.logic.core.components.gates.XorGate; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.wires.Wire; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +class ComponentTest +{ + private Timeline t = new Timeline(11); + + @Test + void circuitExampleTest() + { + Wire a = new Wire(t, 1, 1), b = new Wire(t, 1, 1), c = new Wire(t, 1, 10), d = new Wire(t, 2, 1), e = new Wire(t, 1, 1), + f = new Wire(t, 1, 1), g = new Wire(t, 1, 1), h = new Wire(t, 2, 1), i = new Wire(t, 2, 1), j = new Wire(t, 1, 1), + k = new Wire(t, 1, 1); + new AndGate(t, 1, f.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); + new NotGate(t, 1, f.createReadOnlyEnd(), g.createReadWriteEnd()); + new Merger(t, h.createReadWriteEnd(), c.createReadOnlyEnd(), g.createReadOnlyEnd()); + new Mux(t, 1, i.createReadWriteEnd(), e.createReadOnlyEnd(), h.createReadOnlyEnd(), d.createReadOnlyEnd()); + new Splitter(t, i.createReadOnlyEnd(), k.createReadWriteEnd(), j.createReadWriteEnd()); + + a.createReadWriteEnd().feedSignals(Bit.ZERO); + b.createReadWriteEnd().feedSignals(Bit.ONE); + c.createReadWriteEnd().feedSignals(Bit.ZERO); + d.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE); + e.createReadWriteEnd().feedSignals(Bit.ZERO); + + t.executeAll(); + + assertEquals(Bit.ONE, j.getValue()); + assertEquals(Bit.ZERO, k.getValue()); + } + + @Test + void splitterTest() + { + t.reset(); + Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), in = new Wire(t, 8, 1); + in.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + new Splitter(t, in.createReadOnlyEnd(), a.createReadWriteEnd(), b.createReadWriteEnd(), c.createReadWriteEnd()); + + t.executeAll(); + + assertBitArrayEquals(a.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO); + assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO); + assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE); + } + + @Test + void mergerTest() + { + t.reset(); + Wire a = new Wire(t, 3, 1), b = new Wire(t, 2, 1), c = new Wire(t, 3, 1), out = new Wire(t, 8, 1); + a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO); + b.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO); + c.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); + + new Merger(t, out.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); + + t.executeAll(); + + assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + } + + @Test + void triStateBufferTest() + { + Wire a = new Wire(t, 1, 1), b = new Wire(t, 1, 1), en = new Wire(t, 1, 1), notEn = new Wire(t, 1, 1); + new NotGate(t, 1, en.createReadOnlyEnd(), notEn.createReadWriteEnd()); + new TriStateBuffer(t, 1, a.createReadOnlyEnd(), b.createReadWriteEnd(), en.createReadOnlyEnd()); + new TriStateBuffer(t, 1, b.createReadOnlyEnd(), a.createReadWriteEnd(), notEn.createReadOnlyEnd()); + + ReadWriteEnd enI = en.createReadWriteEnd(), aI = a.createReadWriteEnd(), bI = b.createReadWriteEnd(); + enI.feedSignals(Bit.ONE); + aI.feedSignals(Bit.ONE); + bI.feedSignals(Bit.Z); + + t.executeAll(); + + assertEquals(Bit.ONE, b.getValue()); + + bI.feedSignals(Bit.ZERO); + + t.executeAll(); + + assertEquals(Bit.X, b.getValue()); + assertEquals(Bit.ONE, a.getValue()); + + aI.clearSignals(); + enI.feedSignals(Bit.ZERO); + + t.executeAll(); + + assertEquals(Bit.ZERO, a.getValue()); + + } + + @Test + void muxTest() + { + t.reset(); + Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), out = new Wire(t, 4, 1); + ReadWriteEnd selectIn = select.createReadWriteEnd(); + + selectIn.feedSignals(Bit.ZERO, Bit.ZERO); + a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); + c.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + + new Mux(t, 1, out.createReadWriteEnd(), select.createReadOnlyEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), + c.createReadOnlyEnd()); + t.executeAll(); + + assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); + selectIn.feedSignals(Bit.ZERO, Bit.ONE); + t.executeAll(); + + assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + + selectIn.feedSignals(Bit.ONE, Bit.ONE); + t.executeAll(); + + assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); + + } + + @Test + void demuxTest() + { + t.reset(); + Wire a = new Wire(t, 4, 3), b = new Wire(t, 4, 6), c = new Wire(t, 4, 4), select = new Wire(t, 2, 5), in = new Wire(t, 4, 1); + ReadWriteEnd selectIn = select.createReadWriteEnd(); + + selectIn.feedSignals(Bit.ZERO, Bit.ZERO); + in.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); + + new Demux(t, 1, in.createReadOnlyEnd(), select.createReadOnlyEnd(), a.createReadWriteEnd(), b.createReadWriteEnd(), + c.createReadWriteEnd()); + t.executeAll(); + + assertBitArrayEquals(a.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); + assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); + assertBitArrayEquals(c.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); + selectIn.feedSignals(Bit.ZERO, Bit.ONE); + t.executeAll(); + + assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); + assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); + assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); + + selectIn.feedSignals(Bit.ONE, Bit.ONE); + t.executeAll(); + + assertBitArrayEquals(a.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); + assertBitArrayEquals(b.getValues(), Bit.U, Bit.U, Bit.U, Bit.U); + assertBitArrayEquals(c.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); + + } + + @Test + void andTest() + { + t.reset(); + Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); + new AndGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); + a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); + b.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + + t.executeAll(); + + assertBitArrayEquals(c.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO); + } + + @Test + void orTest() + { + t.reset(); + Wire a = new Wire(t, 4, 1), b = new Wire(t, 4, 3), c = new Wire(t, 4, 1); + new OrGate(t, 1, c.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd()); + a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ZERO); + b.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); + + t.executeAll(); + + assertBitArrayEquals(c.getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE); + } + + @Test + void xorTest() + { + t.reset(); + Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2), c = new Wire(t, 3, 1), d = new Wire(t, 3, 1); + new XorGate(t, 1, d.createReadWriteEnd(), a.createReadOnlyEnd(), b.createReadOnlyEnd(), c.createReadOnlyEnd()); + a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); + b.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); + c.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ZERO, Bit.ONE); + + t.executeAll(); + + assertBitArrayEquals(d.getValues(), Bit.ZERO, Bit.ONE, Bit.ONE); + } + + @Test + void notTest() + { + t.reset(); + Wire a = new Wire(t, 3, 1), b = new Wire(t, 3, 2); + new NotGate(t, 1, a.createReadOnlyEnd(), b.createReadWriteEnd()); + a.createReadWriteEnd().feedSignals(Bit.ZERO, Bit.ONE, Bit.ONE); + + t.executeAll(); + + assertBitArrayEquals(b.getValues(), Bit.ONE, Bit.ZERO, Bit.ZERO); + } + + @Test + void rsLatchCircuitTest() + { + t.reset(); + Wire r = new Wire(t, 1, 1), s = new Wire(t, 1, 1), t1 = new Wire(t, 1, 15), t2 = new Wire(t, 1, 1), q = new Wire(t, 1, 1), + nq = new Wire(t, 1, 1); + + new OrGate(t, 1, t2.createReadWriteEnd(), r.createReadOnlyEnd(), nq.createReadOnlyEnd()); + new OrGate(t, 1, t1.createReadWriteEnd(), s.createReadOnlyEnd(), q.createReadOnlyEnd()); + new NotGate(t, 1, t2.createReadOnlyEnd(), q.createReadWriteEnd()); + new NotGate(t, 1, t1.createReadOnlyEnd(), nq.createReadWriteEnd()); + + ReadWriteEnd sIn = s.createReadWriteEnd(), rIn = r.createReadWriteEnd(); + + sIn.feedSignals(Bit.ONE); + rIn.feedSignals(Bit.ZERO); + + t.executeAll(); + + assertEquals(Bit.ONE, q.getValue()); + assertEquals(Bit.ZERO, nq.getValue()); + + sIn.feedSignals(Bit.ZERO); + + t.executeAll(); + assertEquals(Bit.ONE, q.getValue()); + assertEquals(Bit.ZERO, nq.getValue()); + + rIn.feedSignals(Bit.ONE); + + t.executeAll(); + + assertEquals(Bit.ZERO, q.getValue()); + assertEquals(Bit.ONE, nq.getValue()); + } + + @Test + void numericValueTest() + { + t.reset(); + + Wire a = new Wire(t, 4, 1); + a.createReadWriteEnd().feedSignals(Bit.ONE, Bit.ONE, Bit.ONE, Bit.ONE); + + t.executeAll(); + + assertEquals(15, a.getUnsignedValue()); + assertEquals(-1, a.getSignedValue()); + } + + boolean flag = false; + + @Test + void simpleTimelineTest() + { + Timeline t = new Timeline(3); + flag = false; + t.addEvent((e) -> + { + if (!flag) + fail("Events executed out of order!"); + flag = false; + }, 15); + t.addEvent((e) -> + { + if (flag) + fail("Events executed out of order!"); + flag = true; + }, 10); + t.addEvent((e) -> + { + if (flag) + fail("Events executed out of order!"); + flag = true; + }, 20); + t.addEvent((e) -> + { + fail("Only supposed to execute until timestamp 20, not 25"); + }, 25); + + t.executeUntil(t.laterThan(20), 100); + + if (!flag) + fail("Not all events were executed in order!"); + } + + @Test + void multipleInputs() + { + t.reset(); + Wire w = new Wire(t, 2, 1); + ReadWriteEnd wI1 = w.createReadWriteEnd(), wI2 = w.createReadWriteEnd(); + wI1.feedSignals(Bit.ONE, Bit.Z); + wI2.feedSignals(Bit.Z, Bit.X); + t.executeAll(); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X); + + wI2.feedSignals(Bit.ZERO, Bit.Z); + t.executeAll(); + assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z); + + wI2.feedSignals(Bit.Z, Bit.Z); + t.executeAll(); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); + + wI2.feedSignals(Bit.ONE, Bit.Z); + ReadEnd rE = w.createReadOnlyEnd(); + rE.registerObserver((i) -> fail("WireEnd notified observer, although value did not change.")); + t.executeAll(); + rE.close(); + wI1.feedSignals(Bit.X, Bit.X); + t.executeAll(); + wI1.registerObserver((i) -> fail("WireEnd notified observer, although it was closed.")); + wI1.close(); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); + } + + @Test + void wireConnections() + { + // Nur ein Experiment, was über mehrere 'passive' Bausteine hinweg passieren würde + + t.reset(); + + Wire a = new Wire(t, 1, 2); + Wire b = new Wire(t, 1, 2); + Wire c = new Wire(t, 1, 2); + ReadWriteEnd aI = a.createReadWriteEnd(); + ReadWriteEnd bI = b.createReadWriteEnd(); + ReadWriteEnd cI = c.createReadWriteEnd(); + + TestBitDisplay test = new TestBitDisplay(t, c.createReadOnlyEnd()); + TestBitDisplay test2 = new TestBitDisplay(t, a.createReadOnlyEnd()); + LongConsumer print = time -> System.out.format("Time %2d\n a: %s\n b: %s\n c: %s\n", time, a, b, c); + + cI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + + cI.feedSignals(Bit.X); + test.assertAfterSimulationIs(print, Bit.X); + + cI.feedSignals(Bit.X); + cI.feedSignals(Bit.Z); + test.assertAfterSimulationIs(print, Bit.Z); + + new Connector(t, b.createReadWriteEnd(), c.createReadWriteEnd()).connect(); + test.assertAfterSimulationIs(print, Bit.Z); + System.err.println("ONE"); + bI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + System.err.println("ZERO"); + bI.feedSignals(Bit.ZERO); + test.assertAfterSimulationIs(print, Bit.ZERO); + System.err.println("Z"); + bI.feedSignals(Bit.Z); + test.assertAfterSimulationIs(print, Bit.Z); + + new Connector(t, a.createReadWriteEnd(), b.createReadWriteEnd()).connect(); + System.err.println("Z 2"); + aI.feedSignals(Bit.Z); + test.assertAfterSimulationIs(print, Bit.Z); + test2.assertAfterSimulationIs(Bit.Z); + System.err.println("ONE 2"); + aI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + test2.assertAfterSimulationIs(Bit.ONE); + System.err.println("ZERO 2"); + aI.feedSignals(Bit.ZERO); + test.assertAfterSimulationIs(print, Bit.ZERO); + test2.assertAfterSimulationIs(Bit.ZERO); + System.err.println("Z 2 II"); + aI.feedSignals(Bit.Z); + test.assertAfterSimulationIs(print, Bit.Z); + test2.assertAfterSimulationIs(Bit.Z); + + System.err.println("No Conflict yet"); + bI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + test2.assertAfterSimulationIs(Bit.ONE); + aI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + test2.assertAfterSimulationIs(Bit.ONE); + System.err.println("Conflict"); + aI.feedSignals(Bit.ZERO); + test.assertAfterSimulationIs(print, Bit.X); + test2.assertAfterSimulationIs(Bit.X); + aI.feedSignals(Bit.ONE); + test.assertAfterSimulationIs(print, Bit.ONE); + test2.assertAfterSimulationIs(Bit.ONE); + } + + private static void assertBitArrayEquals(BitVector actual, Bit... expected) + { + assertArrayEquals(expected, actual.getBits()); + } +} diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java index d73ee61c..404aacd3 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java @@ -1,492 +1,500 @@ -package net.mograsim.logic.core.wires; - -import static net.mograsim.logic.core.types.Bit.U; -import static net.mograsim.logic.core.types.Bit.Z; - -import java.util.ArrayList; -import java.util.List; - -import net.mograsim.logic.core.timeline.Timeline; -import net.mograsim.logic.core.types.Bit; -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.types.BitVector.BitVectorMutator; - -/** - * Represents an array of wires that can store n bits of information. - * - * @author Fabian Stemmler - * - */ -public class Wire -{ - private BitVector values; - public final int travelTime; - private List attached = new ArrayList(); - public final int length; - private List inputs = new ArrayList(); - private Timeline timeline; - - public Wire(Timeline timeline, int length, int travelTime) - { - if (length < 1) - throw new IllegalArgumentException( - String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length)); - this.timeline = timeline; - this.length = length; - this.travelTime = travelTime; - initValues(); - } - - private void initValues() - { - values = U.toVector(length); - } - - private void recalculateSingleInput() - { - setNewValues(inputs.get(0).getInputValues()); - } - - private void recalculateMultipleInputs() - { - BitVectorMutator mutator = BitVectorMutator.empty(); - for (ReadWriteEnd wireArrayEnd : inputs) - mutator.join(wireArrayEnd.getInputValues()); - setNewValues(mutator.get()); - } - - private void setNewValues(BitVector newValues) - { - if (values.equals(newValues)) - return; - BitVector oldValues = values; - values = newValues; - notifyObservers(oldValues); - } - - private void recalculate() - { - switch (inputs.size()) - { - case 0: - return; - case 1: - recalculateSingleInput(); - break; - default: - recalculateMultipleInputs(); - } - } - - /** - * The {@link Wire} is interpreted as an unsigned integer with n bits. - * - * @return true if all bits are either Bit.ONE or Bit.ZERO (they do not all have to have the same - * value), not Bit.X or Bit.Z. false is returned otherwise. - * - * @author Fabian Stemmler - */ - public boolean hasNumericValue() - { - for (Bit b : values) - { - if (b != Bit.ZERO && b != Bit.ONE) - return false; - } - return true; - } - - /** - * The {@link Wire} is interpreted as an unsigned integer with n bits. - * - * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. - * - * @author Fabian Stemmler - */ - public long getUnsignedValue() - { - long val = 0; - long mask = 1; - for (Bit bit : values) - { - switch (bit) - { - default: - case Z: - case X: - return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; - case ONE: - val |= mask; - break; - case ZERO: - } - mask = mask << 1; - } - return val; - } - - /** - * The {@link Wire} is interpreted as a signed integer with n bits. - * - * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. - * - * @author Fabian Stemmler - */ - public long getSignedValue() - { - long val = getUnsignedValue(); - long mask = 1 << (length - 1); - if ((mask & val) != 0) - { - int shifts = 64 - length; - return (val << shifts) >> shifts; - } - return val; - } - - public Bit getValue() - { - return getValue(0); - } - - public Bit getValue(int index) - { - return values.getBit(index); - } - - public BitVector getValues(int start, int end) - { - return values.subVector(start, end); - } - - public BitVector getValues() - { - return values; - } - - /** - * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated. - * - * @param ob The {@link WireObserver} to be notified of changes. - * @return true if the given {@link WireObserver} was not already registered, false otherwise - * - * @author Fabian Stemmler - */ - private void attachEnd(ReadEnd end) - { - attached.add(end); - } - - private void detachEnd(ReadEnd end) - { - attached.remove(end); - } - - private void notifyObservers(BitVector oldValues) - { - for (ReadEnd o : attached) - o.update(oldValues); - } - - /** - * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to. - */ - public ReadWriteEnd createReadWriteEnd() - { - return new ReadWriteEnd(); - } - - /** - * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to. - */ - public ReadEnd createReadOnlyEnd() - { - return new ReadEnd(); - } - - private void registerInput(ReadWriteEnd toRegister) - { - inputs.add(toRegister); - } - - /** - * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the - * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0 - * and 1 turn into X when they are mixed - * - * @author Fabian Stemmler - */ - public class ReadEnd - { - private List observers = new ArrayList(); - - private ReadEnd() - { - super(); - Wire.this.attachEnd(this); - } - - public void update(BitVector oldValues) - { - for (WireObserver ob : observers) - ob.update(this, oldValues); - } - - /** - * Included for convenient use on {@link Wire}s of length 1. - * - * @return The value of bit 0. - * - * @author Fabian Stemmler - */ - public Bit getValue() - { - return Wire.this.getValue(); - } - - /** - * @param index Index of the requested bit. - * @return The value of the indexed bit. - * - * @author Fabian Stemmler - */ - public Bit getValue(int index) - { - return Wire.this.getValue(index); - } - - /** - * @param index Index of the requested bit. - * @return The value of the indexed bit. - * - * @author Fabian Stemmler - */ - public BitVector getValues() - { - return Wire.this.getValues(); - } - - /** - * @param start Start of the wanted segment. (inclusive) - * @param end End of the wanted segment. (exclusive) - * @return The values of the segment of {@link Bit}s indexed. - * - * @author Fabian Stemmler - */ - public BitVector getValues(int start, int end) - { - return Wire.this.getValues(start, end); - } - - /** - * The {@link Wire} is interpreted as an unsigned integer with n bits. - * - * @return true if all bits are either Bit.ONE or Bit.ZERO (they do not all have to have the - * same value), not Bit.X or Bit.Z. false is returned otherwise. - * - * @author Fabian Stemmler - */ - public boolean hasNumericValue() - { - return Wire.this.hasNumericValue(); - } - - /** - * The {@link Wire} is interpreted as an unsigned integer with n bits. - * - * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. - * - * @author Fabian Stemmler - */ - public long getUnsignedValue() - { - return Wire.this.getUnsignedValue(); - } - - /** - * The {@link Wire} is interpreted as a signed integer with n bits. - * - * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. - * - * @author Fabian Stemmler - */ - public long getSignedValue() - { - return Wire.this.getSignedValue(); - } - - @Override - public String toString() - { - return Wire.this.toString(); - } - - public void close() - { - inputs.remove(this); - detachEnd(this); - recalculate(); - } - - public int length() - { - return length; - } - - public boolean addObserver(WireObserver ob) - { - return observers.add(ob); - } - - public Wire getWire() - { - return Wire.this; - } - } - - public class ReadWriteEnd extends ReadEnd - { - private boolean open; - private BitVector inputValues; - - private ReadWriteEnd() - { - super(); - open = true; - initValues(); - registerInput(this); - } - - private void initValues() - { - inputValues = U.toVector(length); - } - - /** - * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time. - * - * @param newValues The new values the wires should take on. - * - * @author Fabian Stemmler - */ - public void feedSignals(Bit... newValues) - { - feedSignals(BitVector.of(newValues)); - } - - public void feedSignals(BitVector newValues) - { - if (newValues.length() != length) - throw new IllegalArgumentException( - String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length)); - if (!open) - throw new RuntimeException("Attempted to write to closed WireArrayEnd."); - timeline.addEvent(e -> setValues(newValues), travelTime); - } - - /** - * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time. - * - * @param bitVector The new values the wires should take on. - * @param startingBit The first index of the subarray of wires. - * - * @author Fabian Stemmler - */ - public void feedSignals(int startingBit, BitVector bitVector) - { - if (!open) - throw new RuntimeException("Attempted to write to closed WireArrayEnd."); - timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime); - } - - private void setValues(int startingBit, BitVector newValues) - { - // index check covered in equals - if (!inputValues.equalsWithOffset(newValues, startingBit)) - { - Bit[] vals = inputValues.getBits(); - System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length()); - inputValues = BitVector.of(vals); - Wire.this.recalculate(); - } - } - - private void setValues(BitVector newValues) - { - if (inputValues.equals(newValues)) - return; - inputValues = newValues; - Wire.this.recalculate(); - } - - /** - * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}. - */ - public Bit getInputValue() - { - return getInputValue(0); - } - - /** - * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}. - */ - public Bit getInputValue(int index) - { - return inputValues.getBit(index); - } - - /** - * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}. - */ - public BitVector getInputValues() - { - return getInputValues(0, length); - } - - public BitVector getInputValues(int start, int end) - { - return inputValues.subVector(start, end); - } - - /** - * {@link ReadEnd} now feeds Z into the associated {@link Wire}. - */ - public void clearSignals() - { - feedSignals(Z.toVector(length)); - } - - public BitVector wireValuesExcludingMe() - { - BitVectorMutator mutator = BitVectorMutator.empty(); - for (ReadWriteEnd wireEnd : inputs) - { - if (wireEnd == this) - continue; - mutator.join(wireEnd.inputValues); - } - return mutator.get(); - } - - @Override - public String toString() - { - return inputValues.toString(); - } - } - - @Override - public String toString() - { - return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs); - // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2) - } - - public static ReadEnd[] extractEnds(Wire[] w) - { - ReadEnd[] inputs = new ReadEnd[w.length]; - for (int i = 0; i < w.length; i++) - inputs[i] = w[i].createReadWriteEnd(); - return inputs; - } +package net.mograsim.logic.core.wires; + +import static net.mograsim.logic.core.types.Bit.U; +import static net.mograsim.logic.core.types.Bit.Z; + +import java.util.ArrayList; +import java.util.List; + +import net.mograsim.logic.core.LogicObservable; +import net.mograsim.logic.core.LogicObserver; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.types.BitVector.BitVectorMutator; + +/** + * Represents an array of wires that can store n bits of information. + * + * @author Fabian Stemmler + * + */ +public class Wire +{ + private BitVector values; + public final int travelTime; + private List attached = new ArrayList(); + public final int length; + private List inputs = new ArrayList(); + private Timeline timeline; + + public Wire(Timeline timeline, int length, int travelTime) + { + if (length < 1) + throw new IllegalArgumentException( + String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length)); + this.timeline = timeline; + this.length = length; + this.travelTime = travelTime; + initValues(); + } + + private void initValues() + { + values = U.toVector(length); + } + + private void recalculateSingleInput() + { + setNewValues(inputs.get(0).getInputValues()); + } + + private void recalculateMultipleInputs() + { + BitVectorMutator mutator = BitVectorMutator.empty(); + for (ReadWriteEnd wireArrayEnd : inputs) + mutator.join(wireArrayEnd.getInputValues()); + setNewValues(mutator.get()); + } + + private void setNewValues(BitVector newValues) + { + if (values.equals(newValues)) + return; + BitVector oldValues = values; + values = newValues; + notifyObservers(oldValues); + } + + private void recalculate() + { + switch (inputs.size()) + { + case 0: + return; + case 1: + recalculateSingleInput(); + break; + default: + recalculateMultipleInputs(); + } + } + + /** + * The {@link Wire} is interpreted as an unsigned integer with n bits. + * + * @return true if all bits are either Bit.ONE or Bit.ZERO (they do not all have to have the same + * value), not Bit.X or Bit.Z. false is returned otherwise. + * + * @author Fabian Stemmler + */ + public boolean hasNumericValue() + { + for (Bit b : values) + { + if (b != Bit.ZERO && b != Bit.ONE) + return false; + } + return true; + } + + /** + * The {@link Wire} is interpreted as an unsigned integer with n bits. + * + * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getUnsignedValue() + { + long val = 0; + long mask = 1; + for (Bit bit : values) + { + switch (bit) + { + default: + case Z: + case X: + return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; + case ONE: + val |= mask; + break; + case ZERO: + } + mask = mask << 1; + } + return val; + } + + /** + * The {@link Wire} is interpreted as a signed integer with n bits. + * + * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getSignedValue() + { + long val = getUnsignedValue(); + long mask = 1 << (length - 1); + if ((mask & val) != 0) + { + int shifts = 64 - length; + return (val << shifts) >> shifts; + } + return val; + } + + public Bit getValue() + { + return getValue(0); + } + + public Bit getValue(int index) + { + return values.getBit(index); + } + + public BitVector getValues(int start, int end) + { + return values.subVector(start, end); + } + + public BitVector getValues() + { + return values; + } + + /** + * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated. + * + * @param ob The {@link LogicObserver} to be notified of changes. + * @return true if the given {@link LogicObserver} was not already registered, false otherwise + * + * @author Fabian Stemmler + */ + private void attachEnd(ReadEnd end) + { + attached.add(end); + } + + private void detachEnd(ReadEnd end) + { + attached.remove(end); + } + + private void notifyObservers(BitVector oldValues) + { + for (ReadEnd o : attached) + o.update(oldValues); + } + + /** + * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to. + */ + public ReadWriteEnd createReadWriteEnd() + { + return new ReadWriteEnd(); + } + + /** + * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to. + */ + public ReadEnd createReadOnlyEnd() + { + return new ReadEnd(); + } + + private void registerInput(ReadWriteEnd toRegister) + { + inputs.add(toRegister); + } + + /** + * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the + * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than Z 0 + * and 1 turn into X when they are mixed + * + * @author Fabian Stemmler + */ + public class ReadEnd implements LogicObservable + { + private List observers = new ArrayList(); + + private ReadEnd() + { + super(); + Wire.this.attachEnd(this); + } + + public void update(BitVector oldValues) + { + notifyObservers(); + } + + /** + * Included for convenient use on {@link Wire}s of length 1. + * + * @return The value of bit 0. + * + * @author Fabian Stemmler + */ + public Bit getValue() + { + return Wire.this.getValue(); + } + + /** + * @param index Index of the requested bit. + * @return The value of the indexed bit. + * + * @author Fabian Stemmler + */ + public Bit getValue(int index) + { + return Wire.this.getValue(index); + } + + /** + * @param index Index of the requested bit. + * @return The value of the indexed bit. + * + * @author Fabian Stemmler + */ + public BitVector getValues() + { + return Wire.this.getValues(); + } + + /** + * @param start Start of the wanted segment. (inclusive) + * @param end End of the wanted segment. (exclusive) + * @return The values of the segment of {@link Bit}s indexed. + * + * @author Fabian Stemmler + */ + public BitVector getValues(int start, int end) + { + return Wire.this.getValues(start, end); + } + + /** + * The {@link Wire} is interpreted as an unsigned integer with n bits. + * + * @return true if all bits are either Bit.ONE or Bit.ZERO (they do not all have to have the + * same value), not Bit.X or Bit.Z. false is returned otherwise. + * + * @author Fabian Stemmler + */ + public boolean hasNumericValue() + { + return Wire.this.hasNumericValue(); + } + + /** + * The {@link Wire} is interpreted as an unsigned integer with n bits. + * + * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getUnsignedValue() + { + return Wire.this.getUnsignedValue(); + } + + /** + * The {@link Wire} is interpreted as a signed integer with n bits. + * + * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getSignedValue() + { + return Wire.this.getSignedValue(); + } + + @Override + public String toString() + { + return Wire.this.toString(); + } + + public void close() + { + inputs.remove(this); + detachEnd(this); + recalculate(); + } + + public int length() + { + return length; + } + + public Wire getWire() + { + return Wire.this; + } + + @Override + public void registerObserver(LogicObserver ob) + { + observers.add(ob); + } + + @Override + public void notifyObservers() + { + for (LogicObserver ob : observers) + ob.update(this); + } + } + + public class ReadWriteEnd extends ReadEnd + { + private boolean open; + private BitVector inputValues; + + private ReadWriteEnd() + { + super(); + open = true; + initValues(); + registerInput(this); + } + + private void initValues() + { + inputValues = U.toVector(length); + } + + /** + * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time. + * + * @param newValues The new values the wires should take on. + * + * @author Fabian Stemmler + */ + public void feedSignals(Bit... newValues) + { + feedSignals(BitVector.of(newValues)); + } + + public void feedSignals(BitVector newValues) + { + if (newValues.length() != length) + throw new IllegalArgumentException( + String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length)); + if (!open) + throw new RuntimeException("Attempted to write to closed WireArrayEnd."); + timeline.addEvent(e -> setValues(newValues), travelTime); + } + + /** + * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time. + * + * @param bitVector The new values the wires should take on. + * @param startingBit The first index of the subarray of wires. + * + * @author Fabian Stemmler + */ + public void feedSignals(int startingBit, BitVector bitVector) + { + if (!open) + throw new RuntimeException("Attempted to write to closed WireArrayEnd."); + timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime); + } + + private void setValues(int startingBit, BitVector newValues) + { + // index check covered in equals + if (!inputValues.equalsWithOffset(newValues, startingBit)) + { + Bit[] vals = inputValues.getBits(); + System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length()); + inputValues = BitVector.of(vals); + Wire.this.recalculate(); + } + } + + private void setValues(BitVector newValues) + { + if (inputValues.equals(newValues)) + return; + inputValues = newValues; + Wire.this.recalculate(); + } + + /** + * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}. + */ + public Bit getInputValue() + { + return getInputValue(0); + } + + /** + * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}. + */ + public Bit getInputValue(int index) + { + return inputValues.getBit(index); + } + + /** + * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}. + */ + public BitVector getInputValues() + { + return getInputValues(0, length); + } + + public BitVector getInputValues(int start, int end) + { + return inputValues.subVector(start, end); + } + + /** + * {@link ReadEnd} now feeds Z into the associated {@link Wire}. + */ + public void clearSignals() + { + feedSignals(Z.toVector(length)); + } + + public BitVector wireValuesExcludingMe() + { + BitVectorMutator mutator = BitVectorMutator.empty(); + for (ReadWriteEnd wireEnd : inputs) + { + if (wireEnd == this) + continue; + mutator.join(wireEnd.inputValues); + } + return mutator.get(); + } + + @Override + public String toString() + { + return inputValues.toString(); + } + } + + @Override + public String toString() + { + return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs); + } + + public static ReadEnd[] extractEnds(Wire[] w) + { + ReadEnd[] inputs = new ReadEnd[w.length]; + for (int i = 0; i < w.length; i++) + inputs[i] = w[i].createReadWriteEnd(); + return inputs; + } } \ No newline at end of file diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/WireObserver.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/WireObserver.java deleted file mode 100644 index 66914b48..00000000 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/WireObserver.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.mograsim.logic.core.wires; - -import net.mograsim.logic.core.types.BitVector; -import net.mograsim.logic.core.wires.Wire.ReadEnd; - -public interface WireObserver -{ - public void update(ReadEnd initiator, BitVector oldValues); -} -- 2.17.1