1 package era.mi.logic.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import era.mi.logic.Bit;
9 import era.mi.logic.Simulation;
12 * Represents an array of wires that can store n bits of information.
13 * @author Fabian Stemmler
16 public class WireArray
19 public final int travelTime;
20 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
21 public final int length;
22 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
24 public WireArray(int length, int travelTime)
27 throw new IllegalArgumentException("Tried to create an array of wires with length " + length + ", but a length of less than 1 makes no sense.");
29 this.travelTime = travelTime;
33 private void initValues()
35 values = new Bit[length];
36 for(int i = 0; i < length; i++)
40 private void recalculateSingleInput()
42 WireArrayInput input = inputs.get(0);
43 if(!Arrays.equals(input.getValues(), values))
45 System.arraycopy(input.getValues(), 0, values, 0, length);
50 private void recalculateMultipleInputs()
52 Iterator<WireArrayInput> it = inputs.iterator();
53 Bit[] newValues = it.next().values.clone();
57 WireArrayInput input = it.next();
58 Bit[] bits = input.getValues();
59 for(int i = 0; i < length; i++)
61 if(Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
63 else if(Bit.Z.equals(newValues[i]))
64 newValues[i] = bits[i];
70 if(!Arrays.equals(newValues, values))
77 private void recalculate()
84 recalculateSingleInput();
87 recalculateMultipleInputs();
92 * The WireArray is interpreted as an unsigned integer with n bits.
93 * @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.
95 * @author Fabian Stemmler
97 public boolean hasNumericValue()
101 if(b != Bit.ZERO && b != Bit.ONE)
108 * The WireArray is interpreted as an unsigned integer with n bits.
109 * @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.
111 * @author Fabian Stemmler
113 public long getUnsignedValue()
117 for(int i = 0; i < length; i++)
124 return 0; //TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0; Random number?
136 * The WireArray is interpreted as a signed integer with n bits.
137 * @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.
139 * @author Fabian Stemmler
141 public long getSignedValue()
143 long val = getUnsignedValue();
144 long mask = 1 << (length - 1);
145 if((mask & val) != 0)
147 int shifts = 64 - length;
148 return (val << shifts) >> shifts;
154 * Included for convenient use on {@link WireArray}s of length 1.
155 * @return The value of bit 0.
157 * @author Fabian Stemmler
159 public Bit getValue()
166 * @param index Index of the requested bit.
167 * @return The value of the indexed bit.
169 * @author Fabian Stemmler
171 public Bit getValue(int index)
173 return values[index];
176 public Bit[] getValues(int start, int end)
178 int length = end - start;
179 Bit[] bits = new Bit[length];
180 System.arraycopy(values, start, bits, 0, length);
186 * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
188 * @author Fabian Stemmler
190 public Bit[] getValues()
192 return values.clone();
196 * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.
197 * @param ob The {@link WireArrayObserver} to be notified of changes.
198 * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
200 * @author Fabian Stemmler
202 public boolean addObserver(WireArrayObserver ob)
204 return observers.add(ob);
207 private void notifyObservers()
209 for(WireArrayObserver o : observers)
213 public WireArrayInput createInput()
215 return new WireArrayInput(this);
218 private void registerInput(WireArrayInput toRegister)
220 inputs.add(toRegister);
223 public class WireArrayInput {
224 public final WireArray owner;
225 private Bit[] values;
227 private WireArrayInput(WireArray owner) {
231 owner.registerInput(this);
234 private void initValues()
236 values = new Bit[length];
237 for(int i = 0; i < length; i++)
242 * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.
243 * @param newValues The new values the wires should take on.
245 * @author Fabian Stemmler
247 public void feedSignals(Bit... newValues)
249 if(newValues.length == length)
251 feedSignals(0, newValues);
254 throw new IllegalArgumentException("Attempted to input " + newValues.length + " bits instead of " + length + " bits.");
258 * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.
259 * @param newValues The new values the wires should take on.
260 * @param startingBit The first index of the subarray of wires.
262 * @author Fabian Stemmler
264 public void feedSignals(int startingBit, Bit... newValues)
266 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
269 private void setValues(int startingBit, Bit... newValues)
271 int exclLastIndex = startingBit + newValues.length;
272 if(length < exclLastIndex)
273 throw new ArrayIndexOutOfBoundsException("Attempted to input bits from index " + startingBit + " to "
274 + exclLastIndex + " when there are only " + length + "wires.");
275 if(!Arrays.equals(values, startingBit, exclLastIndex, newValues, 0, newValues.length))
277 System.arraycopy(newValues, 0, values, startingBit, newValues.length);
282 public Bit[] getValues()
284 return values.clone();
287 public void clearSignals()
289 Bit[] bits = new Bit[length];
290 for(int i = 0; i < length; i++)
296 public String toString()
298 return Arrays.toString(values);
303 public String toString()
305 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);