3 import java.util.ArrayList;
4 import java.util.Arrays;
8 * Represents an array of wires that can store n bits of information.
9 * @author Fabian Stemmler
12 public class WireArray
15 private final int travelTime;
16 private List<WireArrayObserver> observers = new ArrayList<>();//<WireArrayObserver>();
17 private final int length;
19 public WireArray(int length, int travelTime)
22 throw new IllegalArgumentException("Tried to create an array of wires with length " + length + ", but a length of less than 1 makes no sense.");
24 this.travelTime = travelTime;
28 private void initValues()
30 values = new Bit[length];
31 for(int i = 0; i < length; i++)
36 * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.
37 * @param newValues The new values the wires should take on.
39 * @author Fabian Stemmler
41 public void feedSignals(Bit... newValues)
43 Simulation.TIMELINE.addEvent((e) -> setValues(newValues), travelTime);
46 private void setValues(Bit... newValues)
48 if(length != newValues.length)
49 throw new IllegalArgumentException(String.format("Unexpected length for input array. Length was %d but expected %d", newValues.length, length)); //TODO: Proper handling
50 if(!Arrays.equals(values, newValues))
52 values = newValues.clone();
58 * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.
59 * @param newValues The new values the wires should take on.
60 * @param startingBit The first index of the subarray of wires.
62 * @author Fabian Stemmler
64 public void feedSignals(int startingBit, Bit... newValues)
66 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
69 private void setValues(int startingBit, Bit... newValues)
71 if(length < startingBit + newValues.length)
72 throw new IllegalArgumentException(); //TODO: Proper handling
73 if(!Arrays.equals(values, startingBit, startingBit + newValues.length, newValues, 0, newValues.length))
75 System.arraycopy(newValues, 0, values, startingBit, newValues.length);
81 * The WireArray is interpreted as an unsigned integer with n bits.
82 * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or <code>Bit.ZERO</code> (they do not all have to have the same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
84 * @author Fabian Stemmler
86 public boolean hasNumericValue()
90 if(b != Bit.ZERO && b != Bit.ONE)
97 * The WireArray is interpreted as an unsigned integer with n bits.
98 * @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.
100 * @author Fabian Stemmler
102 public int getUnsignedValue()
106 for(int i = 0; i < length; i++)
113 return 0; //TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; Random number?
125 * The WireArray is interpreted as a signed integer with n bits.
126 * @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.
128 * @author Fabian Stemmler
130 public int getSignedValue()
132 int val = getUnsignedValue();
133 int mask = 1 << (length - 1);
134 if((mask & val) != 0)
136 int shifts = 32 - length;
137 return (val << shifts) >> shifts;
143 * Included for convenient use on {@link WireArray}s of length 1.
144 * @return The value of bit 0.
146 * @author Fabian Stemmler
148 public Bit getValue()
155 * @param index Index of the requested bit.
156 * @return The value of the indexed bit.
158 * @author Fabian Stemmler
160 public Bit getValue(int index)
162 //TODO: ArrayIndexOutOfBoundsException handling for accessing single bit in WireArray
163 return values[index];
166 public Bit[] getValues(int start, int end)
168 int length = end - start;
169 Bit[] bits = new Bit[length];
170 System.arraycopy(values, start, bits, 0, length);
176 * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
178 * @author Fabian Stemmler
180 public Bit[] getValues()
182 return values.clone();
192 * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.
193 * @param ob The {@link WireArrayObserver} to be notified of changes.
194 * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
196 * @author Fabian Stemmler
198 public boolean addObserver(WireArrayObserver ob)
200 return observers.add(ob);
203 private void notifyObservers()
205 for(WireArrayObserver o : observers)