From: Fabian Stemmler Date: Sun, 2 Jun 2019 10:01:56 +0000 (+0200) Subject: Cleanup; Cleared warnings in the logic core X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=218ed3e44595ad3534c33e05b43a55cc1a67e851;p=Mograsim.git Cleanup; Cleared warnings in the logic core --- 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 index ddfa070a..501d4b08 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/LogicObservable.java @@ -4,7 +4,8 @@ public interface LogicObservable { public void registerObserver(LogicObserver ob); + public void deregisterObserver(LogicObserver ob); + public void notifyObservers(); -// public InnerState getInnerState(); } 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 5e75f96d..4980d777 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 @@ -1,95 +1,95 @@ -package net.mograsim.logic.core.components; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -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 Mux extends BasicComponent -{ - private ReadEnd select; - private ReadWriteEnd out; - private ReadEnd[] inputs; - private final int outputSize; - - /** - * Input {@link Wire}s and out must be of uniform length - * - * @param out Must be of uniform length with all inputs. - * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs. - * @param inputs One of these inputs is mapped to the output, depending on the select bits - */ - public Mux(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs) - { - super(timeline, processTime); - outputSize = out.length(); - - this.inputs = inputs.clone(); - for (int i = 0; i < this.inputs.length; i++) - { - if (inputs[i].length() != outputSize) - throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!"); - inputs[i].registerObserver(this); - } - - this.select = select; - select.registerObserver(this); - - int maxInputs = 1 << select.length(); - if (this.inputs.length > maxInputs) - throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by " - + select.length() + " select bits (" + maxInputs + ")."); - - this.out = out; - } - - public ReadEnd getOut() - { - return out; - } - - public ReadEnd getSelect() - { - return select; - } - - @Override - public void compute() - { - int selectValue; - if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length) - { - out.clearSignals(); - return; - } - - ReadEnd active = inputs[selectValue]; - out.feedSignals(active.getValues()); - } - - @Override - public List getAllInputs() - { - ArrayList wires = new ArrayList(Arrays.asList(inputs)); - wires.add(select); - return Collections.unmodifiableList(wires); - } - - @Override - public List getAllOutputs() - { - return List.of(out); - } -} +package net.mograsim.logic.core.components; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +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 Mux extends BasicComponent +{ + private ReadEnd select; + private ReadWriteEnd out; + private ReadEnd[] inputs; + private final int outputSize; + + /** + * Input {@link Wire}s and out must be of uniform length + * + * @param out Must be of uniform length with all inputs. + * @param select Indexes the input array which is to be mapped to the output. Must have enough bits to index all inputs. + * @param inputs One of these inputs is mapped to the output, depending on the select bits + */ + public Mux(Timeline timeline, int processTime, ReadWriteEnd out, ReadEnd select, ReadEnd... inputs) + { + super(timeline, processTime); + outputSize = out.length(); + + this.inputs = inputs.clone(); + for (int i = 0; i < this.inputs.length; i++) + { + if (inputs[i].length() != outputSize) + throw new IllegalArgumentException("All MUX wire arrays must be of uniform length!"); + inputs[i].registerObserver(this); + } + + this.select = select; + select.registerObserver(this); + + int maxInputs = 1 << select.length(); + if (this.inputs.length > maxInputs) + throw new IllegalArgumentException("There are more inputs (" + this.inputs.length + ") to the MUX than supported by " + + select.length() + " select bits (" + maxInputs + ")."); + + this.out = out; + } + + public ReadEnd getOut() + { + return out; + } + + public ReadEnd getSelect() + { + return select; + } + + @Override + public void compute() + { + int selectValue; + if (!select.hasNumericValue() || (selectValue = (int) select.getUnsignedValue()) >= inputs.length) + { + out.clearSignals(); + return; + } + + ReadEnd active = inputs[selectValue]; + out.feedSignals(active.getValues()); + } + + @Override + public List getAllInputs() + { + ArrayList wires = new ArrayList<>(Arrays.asList(inputs)); + wires.add(select); + return Collections.unmodifiableList(wires); + } + + @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 e97836b1..c1bdba54 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,6 +1,5 @@ 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; @@ -26,6 +25,7 @@ import net.mograsim.logic.core.wires.Wire; import net.mograsim.logic.core.wires.Wire.ReadEnd; import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; +@SuppressWarnings("unused") class ComponentTest { private Timeline t = new Timeline(11); diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/GUITest.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/GUITest.java index 7d066219..544b064f 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/GUITest.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/tests/GUITest.java @@ -114,7 +114,7 @@ public class GUITest extends JPanel public Timeline getTimeline() { return t; - }; + } @Override public void paint(Graphics some_g) @@ -177,6 +177,7 @@ public class GUITest extends JPanel g.setFont(g.getFont().deriveFont(Math.min(height, width) / 40f)); } + @SuppressWarnings("static-method") private void drawString(Graphics g, String s, int x, int y, double anchorX, double anchorY) { int h = g.getFontMetrics().getAscent(); diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java index fb639bed..80a65f46 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/timeline/Timeline.java @@ -1,203 +1,203 @@ -package net.mograsim.logic.core.timeline; - -import java.util.ArrayList; -import java.util.List; -import java.util.PriorityQueue; -import java.util.function.BooleanSupplier; -import java.util.function.Consumer; -import java.util.function.LongSupplier; - -/** - * Orders Events by the time they are due to be executed. Can execute Events individually. - * - * @author Fabian Stemmler - * - */ -public class Timeline -{ - private PriorityQueue events; - private LongSupplier time; - private long lastTimeUpdated = 0; - - private final List> eventAddedListener; - - public Timeline(int initCapacity) - { - events = new PriorityQueue(initCapacity); - - eventAddedListener = new ArrayList<>(); - time = () -> lastTimeUpdated; - } - - /** - * @param timestamp exclusive - * @return true if the first event is later than the timestamp - */ - public BooleanSupplier laterThan(long timestamp) - { - return () -> timeCmp(events.peek().getTiming(), timestamp) > 0; - } - - public boolean hasNext() - { - return !events.isEmpty(); - } - - /** - * Executes all events at the next timestamp, at which there are any - */ - public void executeNext() - { - InnerEvent first = events.peek(); - if (first != null) - executeUntil(laterThan(first.getTiming()), -1); - } - - public void executeAll() - { - while (hasNext()) - executeNext(); - } - - /** - * Executes all events until a given condition is met. The simulation process can be constrained by a real world timestamp. - * - * @param condition the condition until which the events are be processed - * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout. - * @return State of the event execution - * @formatter:off - * NOTHING_DONE if the {@link Timeline} was already empty - * EXEC_OUT_OF_TIME if the given maximum time was reached - * EXEC_UNTIL_CONDITION if the condition was met - * EXEC_UNTIL_EMPTY if events were executed until the {@link Timeline} was empty - * @formatter:on - * @author Christian Femers, Fabian Stemmler - */ - public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis) - { - if (events.isEmpty()) - { - lastTimeUpdated = getSimulationTime(); - return ExecutionResult.NOTHING_DONE; - } - int checkStop = 0; - InnerEvent first = events.peek(); - while (hasNext() && !condition.getAsBoolean()) - { - events.remove(); - lastTimeUpdated = first.getTiming(); - first.run(); - // Don't check after every run - checkStop = (checkStop + 1) % 10; - if (checkStop == 0 && System.currentTimeMillis() >= stopMillis) - return ExecutionResult.EXEC_OUT_OF_TIME; - first = events.peek(); - } - lastTimeUpdated = getSimulationTime(); - return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION; - } - - public void setTimeFunction(LongSupplier time) - { - this.time = time; - } - - public long getSimulationTime() - { - return time.getAsLong(); - } - - public long nextEventTime() - { - if (!hasNext()) - return -1; - return events.peek().getTiming(); - } - - public void reset() - { - events.clear(); - lastTimeUpdated = 0; - } - - public void addEventAddedListener(Consumer listener) - { - eventAddedListener.add(listener); - } - - public void removeEventAddedListener(Consumer listener) - { - eventAddedListener.remove(listener); - } - - /** - * Adds an Event to the {@link Timeline} - * - * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline. - * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time. - */ - public void addEvent(TimelineEventHandler function, int relativeTiming) - { - long timing = getSimulationTime() + relativeTiming; - TimelineEvent event = new TimelineEvent(timing); - events.add(new InnerEvent(function, event)); - eventAddedListener.forEach(l -> l.accept(event)); - } - - private class InnerEvent implements Runnable, Comparable - { - private final TimelineEventHandler function; - private final TimelineEvent event; - - /** - * Creates an {@link InnerEvent} - * - * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs - * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed; - */ - InnerEvent(TimelineEventHandler function, TimelineEvent event) - { - this.function = function; - this.event = event; - } - - public long getTiming() - { - return event.getTiming(); - } - - @Override - public void run() - { - function.handle(event); - } - - @Override - public String toString() - { - return event.toString(); - } - - @Override - public int compareTo(InnerEvent o) - { - return timeCmp(getTiming(), o.getTiming()); - } - } - - public static int timeCmp(long a, long b) - { - return Long.signum(a - b); - } - - @Override - public String toString() - { - return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, events.toString()); - } - - public enum ExecutionResult - { - NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME - } +package net.mograsim.logic.core.timeline; + +import java.util.ArrayList; +import java.util.List; +import java.util.PriorityQueue; +import java.util.function.BooleanSupplier; +import java.util.function.Consumer; +import java.util.function.LongSupplier; + +/** + * Orders Events by the time they are due to be executed. Can execute Events individually. + * + * @author Fabian Stemmler + * + */ +public class Timeline +{ + private PriorityQueue events; + private LongSupplier time; + private long lastTimeUpdated = 0; + + private final List> eventAddedListener; + + public Timeline(int initCapacity) + { + events = new PriorityQueue<>(initCapacity); + + eventAddedListener = new ArrayList<>(); + time = () -> lastTimeUpdated; + } + + /** + * @param timestamp exclusive + * @return true if the first event is later than the timestamp + */ + public BooleanSupplier laterThan(long timestamp) + { + return () -> timeCmp(events.peek().getTiming(), timestamp) > 0; + } + + public boolean hasNext() + { + return !events.isEmpty(); + } + + /** + * Executes all events at the next timestamp, at which there are any + */ + public void executeNext() + { + InnerEvent first = events.peek(); + if (first != null) + executeUntil(laterThan(first.getTiming()), -1); + } + + public void executeAll() + { + while (hasNext()) + executeNext(); + } + + /** + * Executes all events until a given condition is met. The simulation process can be constrained by a real world timestamp. + * + * @param condition the condition until which the events are be processed + * @param stopMillis the System.currentTimeMillis() when simulation definitely needs to stop. A value of -1 means no timeout. + * @return State of the event execution + * @formatter:off + * NOTHING_DONE if the {@link Timeline} was already empty + * EXEC_OUT_OF_TIME if the given maximum time was reached + * EXEC_UNTIL_CONDITION if the condition was met + * EXEC_UNTIL_EMPTY if events were executed until the {@link Timeline} was empty + * @formatter:on + * @author Christian Femers, Fabian Stemmler + */ + public ExecutionResult executeUntil(BooleanSupplier condition, long stopMillis) + { + if (events.isEmpty()) + { + lastTimeUpdated = getSimulationTime(); + return ExecutionResult.NOTHING_DONE; + } + int checkStop = 0; + InnerEvent first = events.peek(); + while (hasNext() && !condition.getAsBoolean()) + { + events.remove(); + lastTimeUpdated = first.getTiming(); + first.run(); + // Don't check after every run + checkStop = (checkStop + 1) % 10; + if (checkStop == 0 && System.currentTimeMillis() >= stopMillis) + return ExecutionResult.EXEC_OUT_OF_TIME; + first = events.peek(); + } + lastTimeUpdated = getSimulationTime(); + return hasNext() ? ExecutionResult.EXEC_UNTIL_EMPTY : ExecutionResult.EXEC_UNTIL_CONDITION; + } + + public void setTimeFunction(LongSupplier time) + { + this.time = time; + } + + public long getSimulationTime() + { + return time.getAsLong(); + } + + public long nextEventTime() + { + if (!hasNext()) + return -1; + return events.peek().getTiming(); + } + + public void reset() + { + events.clear(); + lastTimeUpdated = 0; + } + + public void addEventAddedListener(Consumer listener) + { + eventAddedListener.add(listener); + } + + public void removeEventAddedListener(Consumer listener) + { + eventAddedListener.remove(listener); + } + + /** + * Adds an Event to the {@link Timeline} + * + * @param function The {@link TimelineEventHandler} that will be executed, when the {@link InnerEvent} occurs on the timeline. + * @param relativeTiming The amount of MI ticks in which the {@link InnerEvent} is called, starting from the current time. + */ + public void addEvent(TimelineEventHandler function, int relativeTiming) + { + long timing = getSimulationTime() + relativeTiming; + TimelineEvent event = new TimelineEvent(timing); + events.add(new InnerEvent(function, event)); + eventAddedListener.forEach(l -> l.accept(event)); + } + + private class InnerEvent implements Runnable, Comparable + { + private final TimelineEventHandler function; + private final TimelineEvent event; + + /** + * Creates an {@link InnerEvent} + * + * @param function {@link TimelineEventHandler} to be executed when the {@link InnerEvent} occurs + * @param timing Point in the MI simulation {@link Timeline}, at which the {@link InnerEvent} is executed; + */ + InnerEvent(TimelineEventHandler function, TimelineEvent event) + { + this.function = function; + this.event = event; + } + + public long getTiming() + { + return event.getTiming(); + } + + @Override + public void run() + { + function.handle(event); + } + + @Override + public String toString() + { + return event.toString(); + } + + @Override + public int compareTo(InnerEvent o) + { + return timeCmp(getTiming(), o.getTiming()); + } + } + + public static int timeCmp(long a, long b) + { + return Long.signum(a - b); + } + + @Override + public String toString() + { + return String.format("Simulation time: %s, Last update: %d, Events: %s", getSimulationTime(), lastTimeUpdated, events.toString()); + } + + public enum ExecutionResult + { + NOTHING_DONE, EXEC_UNTIL_EMPTY, EXEC_UNTIL_CONDITION, EXEC_OUT_OF_TIME + } } \ No newline at end of file diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java index a7985319..eb4e45ff 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java @@ -1,53 +1,51 @@ -package net.mograsim.logic.core.types; - -import net.mograsim.logic.core.types.ColorDefinition.BuiltInColor; -import net.mograsim.logic.core.wires.Wire.ReadEnd; - -public class BitVectorFormatter -{ - public static String formatValueAsString(ReadEnd end) - { - return formatAsString(end == null ? null : end.getValues()); - } - - public static String formatAsString(BitVector bitVector) - { - if (bitVector == null) - return "null"; - else - return bitVector.toString(); - } - - public static ColorDefinition formatAsColor(ReadEnd end) - { - return formatAsColor(end == null ? null : end.getValues()); - } - - public static ColorDefinition formatAsColor(BitVector bitVector) - { - // TODO maybe find a color assignment for multiple-bit bit vectors? - if (bitVector == null || bitVector.length() != 1) - return new ColorDefinition(BuiltInColor.COLOR_BLACK); - else - switch (bitVector.getBit(0)) - { - case ONE: - return new ColorDefinition(BuiltInColor.COLOR_GREEN); - case U: - return new ColorDefinition(BuiltInColor.COLOR_CYAN); - case X: - return new ColorDefinition(BuiltInColor.COLOR_RED); - case Z: - return new ColorDefinition(BuiltInColor.COLOR_YELLOW); - case ZERO: - return new ColorDefinition(BuiltInColor.COLOR_GRAY); - default: - throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0)); - } - } - - private BitVectorFormatter() - { - throw new UnsupportedOperationException("No BitVectorFormatter instances"); - } +package net.mograsim.logic.core.types; + +import net.mograsim.logic.core.types.ColorDefinition.BuiltInColor; +import net.mograsim.logic.core.wires.Wire.ReadEnd; + +public class BitVectorFormatter +{ + public static String formatValueAsString(ReadEnd end) + { + return formatAsString(end == null ? null : end.getValues()); + } + + public static String formatAsString(BitVector bitVector) + { + if (bitVector == null) + return "null"; + return bitVector.toString(); + } + + public static ColorDefinition formatAsColor(ReadEnd end) + { + return formatAsColor(end == null ? null : end.getValues()); + } + + public static ColorDefinition formatAsColor(BitVector bitVector) + { + // TODO maybe find a color assignment for multiple-bit bit vectors? + if (bitVector == null || bitVector.length() != 1) + return new ColorDefinition(BuiltInColor.COLOR_BLACK); + switch (bitVector.getBit(0)) + { + case ONE: + return new ColorDefinition(BuiltInColor.COLOR_GREEN); + case U: + return new ColorDefinition(BuiltInColor.COLOR_CYAN); + case X: + return new ColorDefinition(BuiltInColor.COLOR_RED); + case Z: + return new ColorDefinition(BuiltInColor.COLOR_YELLOW); + case ZERO: + return new ColorDefinition(BuiltInColor.COLOR_GRAY); + default: + throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0)); + } + } + + private BitVectorFormatter() + { + throw new UnsupportedOperationException("No BitVectorFormatter instances"); + } } \ No newline at end of file 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 404aacd3..ffbbd328 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 @@ -23,10 +23,10 @@ public class Wire { private BitVector values; public final int travelTime; - private List attached = new ArrayList(); + private List attached = new ArrayList<>(); public final int length; - private List inputs = new ArrayList(); - private Timeline timeline; + List inputs = new ArrayList<>(); + Timeline timeline; public Wire(Timeline timeline, int length, int travelTime) { @@ -61,12 +61,12 @@ public class Wire { if (values.equals(newValues)) return; - BitVector oldValues = values; +// BitVector oldValues = values; values = newValues; - notifyObservers(oldValues); + notifyObservers(); } - private void recalculate() + void recalculate() { switch (inputs.size()) { @@ -174,20 +174,20 @@ public class Wire * * @author Fabian Stemmler */ - private void attachEnd(ReadEnd end) + void attachEnd(ReadEnd end) { attached.add(end); } - private void detachEnd(ReadEnd end) + void detachEnd(ReadEnd end) { attached.remove(end); } - private void notifyObservers(BitVector oldValues) + private void notifyObservers() { for (ReadEnd o : attached) - o.update(oldValues); + o.update(); } /** @@ -206,7 +206,7 @@ public class Wire return new ReadEnd(); } - private void registerInput(ReadWriteEnd toRegister) + void registerInput(ReadWriteEnd toRegister) { inputs.add(toRegister); } @@ -220,15 +220,15 @@ public class Wire */ public class ReadEnd implements LogicObservable { - private List observers = new ArrayList(); + private List observers = new ArrayList<>(); - private ReadEnd() + ReadEnd() { super(); Wire.this.attachEnd(this); } - public void update(BitVector oldValues) + public void update() { notifyObservers(); } @@ -345,6 +345,12 @@ public class Wire observers.add(ob); } + @Override + public void deregisterObserver(LogicObserver ob) + { + observers.remove(ob); + } + @Override public void notifyObservers() { @@ -358,7 +364,7 @@ public class Wire private boolean open; private BitVector inputValues; - private ReadWriteEnd() + ReadWriteEnd() { super(); open = true;