X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fwires%2FWireArray.java;h=957f00b196dce3dd9e2d321a8deb8aad7b899546;hb=74aebd92f41d03f4a44c9a455ef8c05465136412;hp=add55f719326acf153694564c41b57b9b840053c;hpb=7f37c7b2431309e49a0ee116d1ee6c173272e926;p=Mograsim.git diff --git a/era.mi/src/era/mi/logic/wires/WireArray.java b/era.mi/src/era/mi/logic/wires/WireArray.java index add55f71..957f00b1 100644 --- a/era.mi/src/era/mi/logic/wires/WireArray.java +++ b/era.mi/src/era/mi/logic/wires/WireArray.java @@ -15,49 +15,43 @@ import era.mi.logic.Util; * @author Fabian Stemmler * */ -public class WireArray -{ +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) - { + public WireArray(int length, int travelTime) { 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)); + 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.length = length; this.travelTime = travelTime; initValues(); } - private void initValues() - { + private void initValues() { values = Bit.Z.makeArray(length); } - private void recalculateSingleInput() - { + private void recalculateSingleInput() { WireArrayInput input = inputs.get(0); - if (!Arrays.equals(input.getValues(), values)) - { + if (!Arrays.equals(input.getValues(), values)) { + Bit[] oldValues = values.clone(); System.arraycopy(input.getValues(), 0, values, 0, length); - notifyObservers(); + notifyObservers(oldValues); } } - private void recalculateMultipleInputs() - { + private void recalculateMultipleInputs() { Iterator it = inputs.iterator(); Bit[] newValues = it.next().inputValues.clone(); - while (it.hasNext()) - { + while (it.hasNext()) { WireArrayInput input = it.next(); Bit[] bits = input.getValues(); - for (int i = 0; i < length; i++) - { + 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])) @@ -67,17 +61,15 @@ public class WireArray } } - if (!Arrays.equals(newValues, values)) - { - notifyObservers(); + if (!Arrays.equals(newValues, values)) { + Bit[] oldValues = values; values = newValues; + notifyObservers(oldValues); } } - private void recalculate() - { - switch (inputs.size()) - { + private void recalculate() { + switch (inputs.size()) { case 0: return; case 1: @@ -91,17 +83,13 @@ public class WireArray /** * 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. + * @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) - { + public boolean hasNumericValue() { + for (Bit b : values) { if (b != Bit.ZERO && b != Bit.ONE) return false; } @@ -111,19 +99,15 @@ public class WireArray /** * 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. + * @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() - { + public long getUnsignedValue() { long val = 0; long mask = 1; - for (int i = 0; i < length; i++) - { - switch (values[i]) - { + for (int i = 0; i < length; i++) { + switch (values[i]) { default: case Z: case X: @@ -142,17 +126,14 @@ public class WireArray /** * 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. + * @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() - { + public long getSignedValue() { long val = getUnsignedValue(); long mask = 1 << (length - 1); - if ((mask & val) != 0) - { + if ((mask & val) != 0) { int shifts = 64 - length; return (val << shifts) >> shifts; } @@ -166,8 +147,7 @@ public class WireArray * * @author Fabian Stemmler */ - public Bit getValue() - { + public Bit getValue() { return getValue(0); } @@ -178,13 +158,11 @@ public class WireArray * * @author Fabian Stemmler */ - public Bit getValue(int index) - { + public Bit getValue(int index) { return values[index]; } - public Bit[] getValues(int start, int end) - { + public Bit[] getValues(int start, int end) { int length = end - start; Bit[] bits = new Bit[length]; System.arraycopy(values, start, bits, 0, length); @@ -192,164 +170,140 @@ public class WireArray } /** - * @return An array of length n containing the values of the n bits in the - * {@link WireArray}. Can be safely modified. + * @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() - { + public Bit[] getValues() { return values.clone(); } /** - * Adds an {@link WireArrayObserver}, who will be notified when the value of the - * {@link WireArray} is updated. + * 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 + * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise * * @author Fabian Stemmler */ - public boolean addObserver(WireArrayObserver ob) - { + public boolean addObserver(WireArrayObserver ob) { return observers.add(ob); } - private void notifyObservers() - { + private void notifyObservers(Bit[] oldValues) { for (WireArrayObserver o : observers) - o.update(this); + o.update(this, oldValues); } /** - * Create and register a {@link WireArrayInput} object, which is tied to this - * {@link WireArray}. + * Create and register a {@link WireArrayInput} object, which is tied to this {@link WireArray}. */ - public WireArrayInput createInput() - { + public WireArrayInput createInput() { return new WireArrayInput(this); } - private void registerInput(WireArrayInput toRegister) - { + 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 + * 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 class WireArrayInput { public final WireArray owner; private Bit[] inputValues; - private WireArrayInput(WireArray owner) - { + private WireArrayInput(WireArray owner) { super(); this.owner = owner; initValues(); owner.registerInput(this); } - private void initValues() - { + 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. + * 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) - { + 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)); + 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. + * 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) - { + public void feedSignals(int startingBit, Bit... newValues) { Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime); } - private void setValues(int startingBit, Bit... newValues) - { + 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)) - { + 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}. + * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated + * {@link WireArray}. */ - public Bit[] getValues() - { + public Bit[] getValues() { return inputValues.clone(); } /** * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}. */ - public void clearSignals() - { + public void clearSignals() { feedSignals(Bit.Z.makeArray(length)); } - public Bit[] wireValuesExcludingMe() - { + public Bit[] wireValuesExcludingMe() { Bit[] bits = Bit.Z.makeArray(length); - for (WireArrayInput wai : inputs) - { - if(wai == this) + for (WireArrayInput wai : inputs) { + if (wai == this) continue; Util.combineInto(bits, wai.getValues()); } return bits; } - + @Override - public String toString() - { + public String toString() { return Arrays.toString(inputValues); } } @Override - public String toString() - { + public String toString() { return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs); } - public static WireArrayInput[] extractInputs(WireArray[] w) - { + 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();