X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=era.mi%2Fsrc%2Fera%2Fmi%2Flogic%2Fwires%2FWireArray.java;h=a0bd7f8b00cf8a53a3487bac3c6a17ab6c1170ad;hb=18cf2f85ed378005aa93c8b88fe5fa055a108fad;hp=8cceebefe8a13e3bd297649dfc55c32a18d53d8d;hpb=33d4533c5e48fbb5d1d0057f2b08d3d6f8e29a87;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 8cceebef..a0bd7f8b 100644 --- a/era.mi/src/era/mi/logic/wires/WireArray.java +++ b/era.mi/src/era/mi/logic/wires/WireArray.java @@ -1,5 +1,6 @@ package era.mi.logic.wires; +import java.io.Closeable; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -7,9 +8,11 @@ import java.util.List; import era.mi.logic.Bit; import era.mi.logic.Simulation; +import era.mi.logic.Util; /** * Represents an array of wires that can store n bits of information. + * * @author Fabian Stemmler * */ @@ -19,64 +22,60 @@ public class WireArray public final int travelTime; private List observers = new ArrayList(); public final int length; - private List inputs = new ArrayList(); - + 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."); + 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.length = length; this.travelTime = travelTime; initValues(); } - + private void initValues() { - values = new Bit[length]; - for(int i = 0; i < length; i++) - values[i] = Bit.Z; + values = Bit.U.makeArray(length); } - + private void recalculateSingleInput() { - WireArrayInput input = inputs.get(0); - if(!Arrays.equals(input.getValues(), values)) + WireArrayEnd input = inputs.get(0); + if (!Arrays.equals(input.getValues(), values)) { + Bit[] oldValues = values.clone(); System.arraycopy(input.getValues(), 0, values, 0, length); - notifyObservers(); + notifyObservers(oldValues); } } - + private void recalculateMultipleInputs() { - Iterator it = inputs.iterator(); - Bit[] newValues = it.next().values.clone(); - - while(it.hasNext()) + Iterator it = inputs.iterator(); + Bit[] newValues = it.next().inputValues.clone(); + + while (it.hasNext()) { - WireArrayInput input = it.next(); + WireArrayEnd 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])) - newValues[i] = bits[i]; - else - newValues[i] = Bit.X; + newValues[i] = newValues[i].combineWith(bits[i]); } } - - if(!Arrays.equals(newValues, values)) + + if (!Arrays.equals(newValues, values)) { - notifyObservers(); + Bit[] oldValues = values; values = newValues; + notifyObservers(oldValues); } } private void recalculate() { - switch(inputs.size()) + switch (inputs.size()) { case 0: return; @@ -87,25 +86,28 @@ public class WireArray recalculateMultipleInputs(); } } - + /** * 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) + for (Bit b : values) { - if(b != Bit.ZERO && b != Bit.ONE) + if (b != Bit.ZERO && b != Bit.ONE) return false; } 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 @@ -114,14 +116,15 @@ public class WireArray { long val = 0; long mask = 1; - for(int i = 0; i < length; i++) + for (int i = 0; i < length; i++) { - switch(values[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? + return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; + // Random number? case ONE: val |= mask; break; @@ -131,9 +134,10 @@ public class WireArray } 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 @@ -142,16 +146,17 @@ public class WireArray { long val = getUnsignedValue(); long mask = 1 << (length - 1); - if((mask & val) != 0) + if ((mask & val) != 0) { int shifts = 64 - length; return (val << shifts) >> shifts; } return val; } - + /** * Included for convenient use on {@link WireArray}s of length 1. + * * @return The value of bit 0. * * @author Fabian Stemmler @@ -160,7 +165,7 @@ public class WireArray { return getValue(0); } - + /** * * @param index Index of the requested bit. @@ -172,16 +177,15 @@ public class WireArray { 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); + 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. * @@ -191,9 +195,10 @@ public class WireArray { 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 * @@ -203,99 +208,149 @@ public class WireArray { return observers.add(ob); } - - private void notifyObservers() + + private void notifyObservers(Bit[] oldValues) { - for(WireArrayObserver o : observers) - o.update(this); + for (WireArrayObserver o : observers) + o.update(this, oldValues); } - - public WireArrayInput createInput() + + /** + * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}. + */ + public WireArrayEnd createInput() { - return new WireArrayInput(this); + return new WireArrayEnd(this); } - - private void registerInput(WireArrayInput toRegister) + + private void registerInput(WireArrayEnd toRegister) { inputs.add(toRegister); } - - public class WireArrayInput { + + /** + * A {@link WireArrayEnd} 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 WireArrayEnd implements Closeable + { public final WireArray owner; - private Bit[] values; - - private WireArrayInput(WireArray owner) { + private boolean open; + private Bit[] inputValues; + + private WireArrayEnd(WireArray owner) + { super(); this.owner = owner; + open = true; initValues(); owner.registerInput(this); } - + private void initValues() { - values = new Bit[length]; - for(int i = 0; i < length; i++) - values[i] = Bit.Z; + inputValues = Bit.U.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) + if (newValues.length == length) { feedSignals(0, newValues); - } - else - throw new IllegalArgumentException("Attempted to input " + newValues.length + " bits instead of " + length + " bits."); + } else + throw new IllegalArgumentException( + String.format("Attempted to input %d bits instead of %d 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 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) { + if (!open) + throw new RuntimeException("Attempted to write to closed WireArrayEnd."); 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("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)) + if (length < exclLastIndex) + throw new ArrayIndexOutOfBoundsException( + String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit, + exclLastIndex - 1, length)); + if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length)) { - System.arraycopy(newValues, 0, values, startingBit, newValues.length); + System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length); owner.recalculate(); } } - + + /** + * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated + * {@link WireArray}. + */ public Bit[] getValues() { - return values.clone(); + return inputValues.clone(); } - + + /** + * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}. + */ public void clearSignals() { - Bit[] bits = new Bit[length]; - for(int i = 0; i < length; i++) - bits[i] = Bit.Z; - feedSignals(bits); + feedSignals(Bit.Z.makeArray(length)); } - + + public Bit[] wireValuesExcludingMe() + { + Bit[] bits = Bit.Z.makeArray(length); + for (WireArrayEnd wai : inputs) + { + if (wai == this) + continue; + Util.combineInto(bits, wai.getValues()); + } + return bits; + } + + public Bit getWireValue() + { + return owner.getValue(); + } + + public Bit[] getWireValues() + { + return owner.getValues(); + } + @Override public String toString() { - return Arrays.toString(values); + return Arrays.toString(inputValues); + } + + @Override + public void close() + { + inputs.remove(this); + open = false; } } @@ -304,4 +359,12 @@ public class WireArray { return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs); } -} + + public static WireArrayEnd[] extractInputs(WireArray[] w) + { + WireArrayEnd[] inputs = new WireArrayEnd[w.length]; + for (int i = 0; i < w.length; i++) + inputs[i] = w[i].createInput(); + return inputs; + } +} \ No newline at end of file