From: Fabian Stemmler Date: Sun, 12 May 2019 12:27:09 +0000 (+0200) Subject: added more doc to Timeline; added functionality to Bit X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=7f37c7b2431309e49a0ee116d1ee6c173272e926;p=Mograsim.git added more doc to Timeline; added functionality to Bit --- diff --git a/era.mi/src/era/mi/logic/Bit.java b/era.mi/src/era/mi/logic/Bit.java index e7c93968..804ecc94 100644 --- a/era.mi/src/era/mi/logic/Bit.java +++ b/era.mi/src/era/mi/logic/Bit.java @@ -1,5 +1,7 @@ package era.mi.logic; +import java.util.Arrays; + public enum Bit { ONE, ZERO, Z, X; @@ -41,9 +43,11 @@ public enum Bit public Bit xor(Bit other) { - // I'm uncertain how this should behave for cases where one value is neither 1 nor 0. - // TODO: Implement xor - return Bit.X; + if(this == Bit.X || this == Bit.Z + || other == Bit.X || other == Bit.Z) + return Bit.X; + else + return this == other ? Bit.ZERO : Bit.ONE; } public Bit not() @@ -58,6 +62,13 @@ public enum Bit return Bit.X; } } + + public Bit[] makeArray(int length) + { + Bit[] bits = new Bit[length]; + Arrays.fill(bits, this); + return bits; + } /** * Rules for two bits that get directly connected
diff --git a/era.mi/src/era/mi/logic/Util.java b/era.mi/src/era/mi/logic/Util.java index 6aec8844..3ca16b7a 100644 --- a/era.mi/src/era/mi/logic/Util.java +++ b/era.mi/src/era/mi/logic/Util.java @@ -85,16 +85,9 @@ public final class Util return out; } - public static Bit[] arrayOfZ(int length) - { - Bit[] out = new Bit[length]; - Arrays.fill(out, Bit.Z); - return out; - } - /** * uses the {@link Bit#combineWith(Bit)} method, does not create a new array, - * the result is stored in the fist array. + * the result is stored in the first array. * * @author Christian Femers */ diff --git a/era.mi/src/era/mi/logic/tests/ComponentTest.java b/era.mi/src/era/mi/logic/tests/ComponentTest.java index a407b8df..f3858f52 100644 --- a/era.mi/src/era/mi/logic/tests/ComponentTest.java +++ b/era.mi/src/era/mi/logic/tests/ComponentTest.java @@ -56,9 +56,9 @@ class ComponentTest Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO }, a.getValues()); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO }, b.getValues()); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE }, c.getValues()); + 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 @@ -126,16 +126,16 @@ class ComponentTest new Mux(1, out, select, a, b, c); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO }, out.getValues()); + assertBitArrayEquals(out.getValues(), Bit.ONE, Bit.ZERO, Bit.ONE, Bit.ZERO); selectIn.feedSignals(Bit.ZERO, Bit.ONE); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE }, out.getValues()); + assertBitArrayEquals(out.getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); selectIn.feedSignals(Bit.ONE, Bit.ONE); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.Z, Bit.Z, Bit.Z, Bit.Z }, out.getValues()); + assertBitArrayEquals(out.getValues(), Bit.Z, Bit.Z, Bit.Z, Bit.Z); } @@ -148,7 +148,7 @@ class ComponentTest gate.getB().createInput().feedSignals(Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ONE); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO }, gate.getOut().getValues()); + assertBitArrayEquals(gate.getOut().getValues(), Bit.ZERO, Bit.ONE, Bit.ZERO, Bit.ZERO); } @Test @@ -161,7 +161,7 @@ class ComponentTest Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE }, gate.getOut().getValues()); + assertBitArrayEquals(gate.getOut().getValues(), Bit.ONE, Bit.ONE, Bit.ZERO, Bit.ONE); } @Test @@ -223,20 +223,20 @@ class ComponentTest wI1.feedSignals(Bit.ONE, Bit.Z); wI2.feedSignals(Bit.Z, Bit.X); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.X }, w.getValues()); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.X); wI2.feedSignals(Bit.ZERO, Bit.Z); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.X, Bit.Z }, w.getValues()); + assertBitArrayEquals(w.getValues(), Bit.X, Bit.Z); wI2.feedSignals(Bit.Z, Bit.Z); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues()); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); wI2.feedSignals(Bit.ONE, Bit.Z); w.addObserver((i) -> fail("WireArray notified observer, although value did not change.")); Simulation.TIMELINE.executeAll(); - assertArrayEquals(new Bit[] { Bit.ONE, Bit.Z }, w.getValues()); + assertBitArrayEquals(w.getValues(), Bit.ONE, Bit.Z); } @Test @@ -315,6 +315,6 @@ class ComponentTest private static void assertBitArrayEquals(Bit[] actual, Bit... expected) { - assertArrayEquals(expected, actual); + assertArrayEquals(expected, actual); } } diff --git a/era.mi/src/era/mi/logic/tests/Connector.java b/era.mi/src/era/mi/logic/tests/Connector.java index 23ec1bca..fcdf6e8b 100644 --- a/era.mi/src/era/mi/logic/tests/Connector.java +++ b/era.mi/src/era/mi/logic/tests/Connector.java @@ -15,7 +15,7 @@ public class Connector implements WireArrayObserver public Connector(WireArray a, WireArray b) { if (a.length != b.length) - throw new IllegalArgumentException("WireArray width does not match: " + a.length + ", " + b.length); + throw new IllegalArgumentException(String.format("WireArray width does not match: %o, %o", a.length, b.length)); this.a = a; this.b = b; a.addObserver(this); diff --git a/era.mi/src/era/mi/logic/timeline/Timeline.java b/era.mi/src/era/mi/logic/timeline/Timeline.java index 2392f4c2..5c88deae 100644 --- a/era.mi/src/era/mi/logic/timeline/Timeline.java +++ b/era.mi/src/era/mi/logic/timeline/Timeline.java @@ -91,5 +91,16 @@ public class Timeline function.handle(event); } + @Override + public String toString() + { + return event.toString(); + } + } + + @Override + public String toString() + { + return "simulation time: " + currentTime + ", " + events.toString(); } } \ No newline at end of file diff --git a/era.mi/src/era/mi/logic/timeline/TimelineEvent.java b/era.mi/src/era/mi/logic/timeline/TimelineEvent.java index 6cec9079..c5fe16cc 100644 --- a/era.mi/src/era/mi/logic/timeline/TimelineEvent.java +++ b/era.mi/src/era/mi/logic/timeline/TimelineEvent.java @@ -1,5 +1,10 @@ package era.mi.logic.timeline; +/** + * A class that stores all relevant information about an event in the {@link Timeline}. Currently, there is not much relevant information to store. + * @author Fabian Stemmler + * + */ public class TimelineEvent { private final long timing; @@ -14,4 +19,9 @@ public class TimelineEvent { return timing; } + + public String toString() + { + return "timestamp: " + timing; + } } \ No newline at end of file diff --git a/era.mi/src/era/mi/logic/wires/WireArray.java b/era.mi/src/era/mi/logic/wires/WireArray.java index 370cbbd5..add55f71 100644 --- a/era.mi/src/era/mi/logic/wires/WireArray.java +++ b/era.mi/src/era/mi/logic/wires/WireArray.java @@ -17,346 +17,342 @@ import era.mi.logic.Util; */ public class WireArray { - private Bit[] values; - public final int travelTime; - private List observers = new ArrayList(); - public final int length; - private List inputs = new ArrayList(); - - public WireArray(int length, int travelTime) - { - if (length < 1) - throw new IllegalArgumentException("Tried to create an array of wires with length " + length - + ", but a length of less than 1 makes no sense."); - this.length = length; - this.travelTime = travelTime; - initValues(); - } - - private void initValues() - { - values = new Bit[length]; - for (int i = 0; i < length; i++) - values[i] = Bit.Z; - } - - private void recalculateSingleInput() - { - WireArrayInput input = inputs.get(0); - if (!Arrays.equals(input.getValues(), values)) + private Bit[] values; + public final int travelTime; + private List observers = new ArrayList(); + public final int length; + private List inputs = new ArrayList(); + + public WireArray(int length, int travelTime) { - System.arraycopy(input.getValues(), 0, values, 0, length); - notifyObservers(); + if (length < 1) + throw new IllegalArgumentException(String.format("Tried to create an array of wires with length %o, but a length of less than 1 makes no sense.", length)); + this.length = length; + this.travelTime = travelTime; + initValues(); } - } - private void recalculateMultipleInputs() - { - Iterator it = inputs.iterator(); - Bit[] newValues = it.next().values.clone(); - - while (it.hasNext()) + private void initValues() { - WireArrayInput input = it.next(); - Bit[] bits = input.getValues(); - for (int i = 0; i < length; i++) - { - if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i])) - continue; - else if (Bit.Z.equals(newValues[i])) - newValues[i] = bits[i]; - else - newValues[i] = Bit.X; - } + values = Bit.Z.makeArray(length); } - if (!Arrays.equals(newValues, values)) + private void recalculateSingleInput() { - notifyObservers(); - values = newValues; + WireArrayInput input = inputs.get(0); + if (!Arrays.equals(input.getValues(), values)) + { + System.arraycopy(input.getValues(), 0, values, 0, length); + notifyObservers(); + } } - } - private void recalculate() - { - switch (inputs.size()) + private void recalculateMultipleInputs() { - case 0: - return; - case 1: - recalculateSingleInput(); - break; - default: - recalculateMultipleInputs(); + Iterator it = inputs.iterator(); + Bit[] newValues = it.next().inputValues.clone(); + + while (it.hasNext()) + { + WireArrayInput input = it.next(); + Bit[] bits = input.getValues(); + for (int i = 0; i < length; i++) + { + if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i])) + continue; + else if (Bit.Z.equals(newValues[i])) + newValues[i] = bits[i]; + else + newValues[i] = Bit.X; + } + } + + if (!Arrays.equals(newValues, values)) + { + notifyObservers(); + values = newValues; + } } - } - - /** - * The WireArray 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) + + private void recalculate() { - if (b != Bit.ZERO && b != Bit.ONE) - return false; + switch (inputs.size()) + { + case 0: + return; + case 1: + recalculateSingleInput(); + break; + default: + recalculateMultipleInputs(); + } } - return true; - } - - /** - * The WireArray is interpreted as an unsigned integer with n bits. - * - * @return The unsigned value of the {@link WireArray}'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 (int i = 0; i < length; i++) + + /** + * The WireArray 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() { - switch (values[i]) - { - default: - case Z: - case X: - return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; - // Random number? - case ONE: - val |= mask; - break; - case ZERO: - } - mask = mask << 1; + for (Bit b : values) + { + if (b != Bit.ZERO && b != Bit.ONE) + return false; + } + return true; } - return val; - } - - /** - * The WireArray is interpreted as a signed integer with n bits. - * - * @return The signed value of the {@link WireArray}'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) + + /** + * The WireArray is interpreted as an unsigned integer with n bits. + * + * @return The unsigned value of the {@link WireArray}'s bits, where value 0 + * corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getUnsignedValue() { - int shifts = 64 - length; - return (val << shifts) >> shifts; + long val = 0; + long mask = 1; + for (int i = 0; i < length; i++) + { + switch (values[i]) + { + default: + case Z: + case X: + return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; + // Random number? + case ONE: + val |= mask; + break; + case ZERO: + } + mask = mask << 1; + } + return val; } - return val; - } - - /** - * Included for convenient use on {@link WireArray}s of length 1. - * - * @return The value of bit 0. - * - * @author Fabian Stemmler - */ - public Bit getValue() - { - return getValue(0); - } - - /** - * - * @param index Index of the requested bit. - * @return The value of the indexed bit. - * - * @author Fabian Stemmler - */ - public Bit getValue(int index) - { - return values[index]; - } - - public Bit[] getValues(int start, int end) - { - int length = end - start; - Bit[] bits = new Bit[length]; - System.arraycopy(values, start, bits, 0, length); - return bits; - } - - /** - * @return An array of length n containing the values of the n bits in the - * {@link WireArray}. Can be safely modified. - * - * @author Fabian Stemmler - */ - public Bit[] getValues() - { - return values.clone(); - } - - /** - * Adds an {@link WireArrayObserver}, who will be notified when the value of the - * {@link WireArray} is updated. - * - * @param ob The {@link WireArrayObserver} to be notified of changes. - * @return true if the given {@link WireArrayObserver} was not already - * registered, false otherwise - * - * @author Fabian Stemmler - */ - public boolean addObserver(WireArrayObserver ob) - { - return observers.add(ob); - } - - private void notifyObservers() - { - for (WireArrayObserver o : observers) - o.update(this); - } - - /** - * Create and register a {@link WireArrayInput} object, which is tied to this - * {@link WireArray}. - */ - public WireArrayInput createInput() - { - return new WireArrayInput(this); - } - - private void registerInput(WireArrayInput toRegister) - { - inputs.add(toRegister); - } - - /** - * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray} - * it is tied to. The combination of all inputs determines the - * {@link WireArray}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 WireArrayInput - { - public final WireArray owner; - private Bit[] values; - private WireArrayInput(WireArray owner) + /** + * The WireArray is interpreted as a signed integer with n bits. + * + * @return The signed value of the {@link WireArray}'s bits, where value 0 + * corresponds with 2^0, value 1 is 2^1 and so on. + * + * @author Fabian Stemmler + */ + public long getSignedValue() { - super(); - this.owner = owner; - initValues(); - owner.registerInput(this); + long val = getUnsignedValue(); + long mask = 1 << (length - 1); + if ((mask & val) != 0) + { + int shifts = 64 - length; + return (val << shifts) >> shifts; + } + return val; } - private void initValues() + /** + * Included for convenient use on {@link WireArray}s of length 1. + * + * @return The value of bit 0. + * + * @author Fabian Stemmler + */ + public Bit getValue() { - values = new Bit[length]; - for (int i = 0; i < length; i++) - values[i] = Bit.Z; + return getValue(0); } /** - * Sets the wires values. This takes up time, as specified by the - * {@link WireArray}s travel time. * - * @param newValues The new values the wires should take on. + * @param index Index of the requested bit. + * @return The value of the indexed bit. * * @author Fabian Stemmler */ - public void feedSignals(Bit... newValues) + public Bit getValue(int index) + { + return values[index]; + } + + public Bit[] getValues(int start, int end) { - if (newValues.length == length) - { - feedSignals(0, newValues); - } else - throw new IllegalArgumentException( - "Attempted to input " + newValues.length + " bits instead of " + length + " bits."); + int length = end - start; + Bit[] bits = new Bit[length]; + System.arraycopy(values, start, bits, 0, length); + return bits; } /** - * Sets values of a subarray of wires. This takes up time, as specified by the - * {@link WireArray}s travel time. + * @return An array of length n containing the values of the n bits in the + * {@link WireArray}. Can be safely modified. * - * @param newValues The new values the wires should take on. - * @param startingBit The first index of the subarray of wires. + * @author Fabian Stemmler + */ + public Bit[] getValues() + { + return values.clone(); + } + + /** + * Adds an {@link WireArrayObserver}, who will be notified when the value of the + * {@link WireArray} is updated. + * + * @param ob The {@link WireArrayObserver} to be notified of changes. + * @return true if the given {@link WireArrayObserver} was not already + * registered, false otherwise * * @author Fabian Stemmler */ - public void feedSignals(int startingBit, Bit... newValues) + public boolean addObserver(WireArrayObserver ob) { - Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime); + return observers.add(ob); } - private void setValues(int startingBit, Bit... newValues) + private void notifyObservers() { - int exclLastIndex = startingBit + newValues.length; - if (length < exclLastIndex) - throw new ArrayIndexOutOfBoundsException("Attempted to input bits from index " + startingBit + " to " - + exclLastIndex + " when there are only " + length + "wires."); - if (!Arrays.equals(values, startingBit, exclLastIndex, newValues, 0, newValues.length)) - { - System.arraycopy(newValues, 0, values, startingBit, newValues.length); - owner.recalculate(); - } + for (WireArrayObserver o : observers) + o.update(this); } - public Bit[] getValues() + /** + * Create and register a {@link WireArrayInput} object, which is tied to this + * {@link WireArray}. + */ + public WireArrayInput createInput() + { + return new WireArrayInput(this); + } + + private void registerInput(WireArrayInput toRegister) { - return values.clone(); + inputs.add(toRegister); } - - public Bit[] wireValuesExcludingMe() + + /** + * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray} + * it is tied to. The combination of all inputs determines the + * {@link WireArray}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 WireArrayInput { - Bit[] bits = Util.arrayOfZ(length); - for (WireArrayInput wai : inputs) + public final WireArray owner; + private Bit[] inputValues; + + private WireArrayInput(WireArray owner) { - if(wai == this) - continue; - Util.combineInto(bits, wai.getValues()); + super(); + this.owner = owner; + initValues(); + owner.registerInput(this); } - return bits; - } - public void clearSignals() - { - Bit[] bits = new Bit[length]; - for (int i = 0; i < length; i++) - bits[i] = Bit.Z; - feedSignals(bits); + private void initValues() + { + inputValues = Bit.Z.makeArray(length); + } + + /** + * Sets the wires values. This takes up time, as specified by the + * {@link WireArray}s travel time. + * + * @param newValues The new values the wires should take on. + * + * @author Fabian Stemmler + */ + public void feedSignals(Bit... newValues) + { + if (newValues.length == length) + { + feedSignals(0, newValues); + } else + throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length)); + } + + /** + * Sets values of a subarray of wires. This takes up time, as specified by the + * {@link WireArray}s travel time. + * + * @param newValues 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, Bit... newValues) + { + Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime); + } + + private void setValues(int startingBit, Bit... newValues) + { + int exclLastIndex = startingBit + newValues.length; + if (length < exclLastIndex) + throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length)); + if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length)) + { + System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length); + owner.recalculate(); + } + } + + /** + * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}. + */ + public Bit[] getValues() + { + return inputValues.clone(); + } + + /** + * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}. + */ + public void clearSignals() + { + feedSignals(Bit.Z.makeArray(length)); + } + + public Bit[] wireValuesExcludingMe() + { + Bit[] bits = Bit.Z.makeArray(length); + for (WireArrayInput wai : inputs) + { + if(wai == this) + continue; + Util.combineInto(bits, wai.getValues()); + } + return bits; + } + + @Override + public String toString() + { + return Arrays.toString(inputValues); + } } @Override public String toString() { - return Arrays.toString(values); + return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs); + } + + public static WireArrayInput[] extractInputs(WireArray[] w) + { + WireArrayInput[] inputs = new WireArrayInput[w.length]; + for (int i = 0; i < w.length; i++) + inputs[i] = w[i].createInput(); + return inputs; } - } - - @Override - public String toString() - { - return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs); - } - - public static WireArrayInput[] extractInputs(WireArray[] w) - { - WireArrayInput[] inputs = new WireArrayInput[w.length]; - for(int i = 0; i < w.length; i++) - inputs[i] = w[i].createInput(); - return inputs; - } } \ No newline at end of file