1 package era.mi.logic.wires;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Arrays;
\r
5 import java.util.Iterator;
\r
6 import java.util.List;
\r
8 import era.mi.logic.Bit;
\r
9 import era.mi.logic.Simulation;
\r
10 import era.mi.logic.Util;
\r
13 * Represents an array of wires that can store n bits of information.
\r
15 * @author Fabian Stemmler
\r
18 public class WireArray
\r
20 private Bit[] values;
\r
21 public final int travelTime;
\r
22 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
\r
23 public final int length;
\r
24 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
\r
26 public WireArray(int length, int travelTime)
\r
29 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));
\r
30 this.length = length;
\r
31 this.travelTime = travelTime;
\r
35 private void initValues()
\r
37 values = Bit.Z.makeArray(length);
\r
40 private void recalculateSingleInput()
\r
42 WireArrayInput input = inputs.get(0);
\r
43 if (!Arrays.equals(input.getValues(), values))
\r
45 Bit[] oldValues = values.clone();
\r
46 System.arraycopy(input.getValues(), 0, values, 0, length);
\r
47 notifyObservers(oldValues);
\r
51 private void recalculateMultipleInputs()
\r
53 Iterator<WireArrayInput> it = inputs.iterator();
\r
54 Bit[] newValues = it.next().inputValues.clone();
\r
56 while (it.hasNext())
\r
58 WireArrayInput input = it.next();
\r
59 Bit[] bits = input.getValues();
\r
60 for (int i = 0; i < length; i++)
\r
62 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
\r
64 else if (Bit.Z.equals(newValues[i]))
\r
65 newValues[i] = bits[i];
\r
67 newValues[i] = Bit.X;
\r
71 if (!Arrays.equals(newValues, values))
\r
73 Bit[] oldValues = values;
\r
75 notifyObservers(oldValues);
\r
79 private void recalculate()
\r
81 switch (inputs.size())
\r
86 recalculateSingleInput();
\r
89 recalculateMultipleInputs();
\r
94 * The WireArray is interpreted as an unsigned integer with n bits.
\r
96 * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or
\r
97 * <code>Bit.ZERO</code> (they do not all have to have the same value),
\r
98 * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is
\r
99 * returned otherwise.
\r
101 * @author Fabian Stemmler
\r
103 public boolean hasNumericValue()
\r
105 for (Bit b : values)
\r
107 if (b != Bit.ZERO && b != Bit.ONE)
\r
114 * The WireArray is interpreted as an unsigned integer with n bits.
\r
116 * @return The unsigned value of the {@link WireArray}'s bits, where value 0
\r
117 * corresponds with 2^0, value 1 is 2^1 and so on.
\r
119 * @author Fabian Stemmler
\r
121 public long getUnsignedValue()
\r
125 for (int i = 0; i < length; i++)
\r
132 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
145 * The WireArray is interpreted as a signed integer with n bits.
\r
147 * @return The signed value of the {@link WireArray}'s bits, where value 0
\r
148 * corresponds with 2^0, value 1 is 2^1 and so on.
\r
150 * @author Fabian Stemmler
\r
152 public long getSignedValue()
\r
154 long val = getUnsignedValue();
\r
155 long mask = 1 << (length - 1);
\r
156 if ((mask & val) != 0)
\r
158 int shifts = 64 - length;
\r
159 return (val << shifts) >> shifts;
\r
165 * Included for convenient use on {@link WireArray}s of length 1.
\r
167 * @return The value of bit 0.
\r
169 * @author Fabian Stemmler
\r
171 public Bit getValue()
\r
173 return getValue(0);
\r
178 * @param index Index of the requested bit.
\r
179 * @return The value of the indexed bit.
\r
181 * @author Fabian Stemmler
\r
183 public Bit getValue(int index)
\r
185 return values[index];
\r
188 public Bit[] getValues(int start, int end)
\r
190 int length = end - start;
\r
191 Bit[] bits = new Bit[length];
\r
192 System.arraycopy(values, start, bits, 0, length);
\r
197 * @return An array of length n containing the values of the n bits in the
\r
198 * {@link WireArray}. Can be safely modified.
\r
200 * @author Fabian Stemmler
\r
202 public Bit[] getValues()
\r
204 return values.clone();
\r
208 * Adds an {@link WireArrayObserver}, who will be notified when the value of the
\r
209 * {@link WireArray} is updated.
\r
211 * @param ob The {@link WireArrayObserver} to be notified of changes.
\r
212 * @return true if the given {@link WireArrayObserver} was not already
\r
213 * registered, false otherwise
\r
215 * @author Fabian Stemmler
\r
217 public boolean addObserver(WireArrayObserver ob)
\r
219 return observers.add(ob);
\r
222 private void notifyObservers(Bit[] oldValues)
\r
224 for (WireArrayObserver o : observers)
\r
225 o.update(this, oldValues);
\r
229 * Create and register a {@link WireArrayInput} object, which is tied to this
\r
230 * {@link WireArray}.
\r
232 public WireArrayInput createInput()
\r
234 return new WireArrayInput(this);
\r
237 private void registerInput(WireArrayInput toRegister)
\r
239 inputs.add(toRegister);
\r
243 * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
\r
244 * it is tied to. The combination of all inputs determines the
\r
245 * {@link WireArray}s final value. X dominates all other inputs Z does not
\r
246 * affect the final value, unless there are no other inputs than Z 0 and 1 turn
\r
247 * into X when they are mixed
\r
249 * @author Fabian Stemmler
\r
251 public class WireArrayInput
\r
253 public final WireArray owner;
\r
254 private Bit[] inputValues;
\r
256 private WireArrayInput(WireArray owner)
\r
259 this.owner = owner;
\r
261 owner.registerInput(this);
\r
264 private void initValues()
\r
266 inputValues = Bit.Z.makeArray(length);
\r
270 * Sets the wires values. This takes up time, as specified by the
\r
271 * {@link WireArray}s travel time.
\r
273 * @param newValues The new values the wires should take on.
\r
275 * @author Fabian Stemmler
\r
277 public void feedSignals(Bit... newValues)
\r
279 if (newValues.length == length)
\r
281 feedSignals(0, newValues);
\r
283 throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));
\r
287 * Sets values of a subarray of wires. This takes up time, as specified by the
\r
288 * {@link WireArray}s travel time.
\r
290 * @param newValues The new values the wires should take on.
\r
291 * @param startingBit The first index of the subarray of wires.
\r
293 * @author Fabian Stemmler
\r
295 public void feedSignals(int startingBit, Bit... newValues)
\r
297 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
\r
300 private void setValues(int startingBit, Bit... newValues)
\r
302 int exclLastIndex = startingBit + newValues.length;
\r
303 if (length < exclLastIndex)
\r
304 throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));
\r
305 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
\r
307 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
\r
308 owner.recalculate();
\r
313 * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.
\r
315 public Bit[] getValues()
\r
317 return inputValues.clone();
\r
321 * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.
\r
323 public void clearSignals()
\r
325 feedSignals(Bit.Z.makeArray(length));
\r
328 public Bit[] wireValuesExcludingMe()
\r
330 Bit[] bits = Bit.Z.makeArray(length);
\r
331 for (WireArrayInput wai : inputs)
\r
335 Util.combineInto(bits, wai.getValues());
\r
341 public String toString()
\r
343 return Arrays.toString(inputValues);
\r
348 public String toString()
\r
350 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
\r
353 public static WireArrayInput[] extractInputs(WireArray[] w)
\r
355 WireArrayInput[] inputs = new WireArrayInput[w.length];
\r
356 for (int i = 0; i < w.length; i++)
\r
357 inputs[i] = w[i].createInput();
\r