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