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
21 public final int travelTime;
22 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
23 public final int length;
24 private List<WireEnd> inputs = new ArrayList<WireEnd>();
26 public Wire(int length, int travelTime)
29 throw new IllegalArgumentException(
30 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
32 this.travelTime = travelTime;
36 private void initValues()
38 values = Bit.U.makeArray(length);
41 private void recalculateSingleInput()
43 WireEnd input = inputs.get(0);
44 if (!Arrays.equals(input.getInputValues(), values))
46 Bit[] oldValues = values.clone();
47 System.arraycopy(input.getInputValues(), 0, values, 0, length);
48 notifyObservers(oldValues);
52 private void recalculateMultipleInputs()
54 Iterator<WireEnd> it = inputs.iterator();
55 Bit[] newValues = it.next().inputValues.clone();
59 WireEnd input = it.next();
60 Bit[] bits = input.getInputValues();
61 for (int i = 0; i < length; i++)
63 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
65 else if (Bit.Z.equals(newValues[i]))
66 newValues[i] = bits[i];
72 if (!Arrays.equals(newValues, values))
74 Bit[] oldValues = values;
76 notifyObservers(oldValues);
80 private void recalculate()
82 switch (inputs.size())
87 recalculateSingleInput();
90 recalculateMultipleInputs();
95 * The {@link Wire} is interpreted as an unsigned integer with n bits.
97 * @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
98 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
100 * @author Fabian Stemmler
102 public boolean hasNumericValue()
106 if (b != Bit.ZERO && b != Bit.ONE)
113 * The {@link Wire} is interpreted as an unsigned integer with n bits.
115 * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
117 * @author Fabian Stemmler
119 public long getUnsignedValue()
123 for (int i = 0; i < length; i++)
130 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
143 * The {@link Wire} is interpreted as a signed integer with n bits.
145 * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
147 * @author Fabian Stemmler
149 public long getSignedValue()
151 long val = getUnsignedValue();
152 long mask = 1 << (length - 1);
153 if ((mask & val) != 0)
155 int shifts = 64 - length;
156 return (val << shifts) >> shifts;
161 public Bit getValue()
166 public Bit getValue(int index)
168 return values[index];
171 public Bit[] getValues(int start, int end)
173 int length = end - start;
174 Bit[] bits = new Bit[length];
175 System.arraycopy(values, start, bits, 0, length);
179 public Bit[] getValues()
181 return values.clone();
185 * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link Wire} is updated.
187 * @param ob The {@link WireArrayObserver} to be notified of changes.
188 * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
190 * @author Fabian Stemmler
192 public boolean addObserver(WireArrayObserver ob)
194 return observers.add(ob);
197 private void notifyObservers(Bit[] oldValues)
199 for (WireArrayObserver o : observers)
200 o.update(this, oldValues);
204 * Create and register a {@link WireEnd} object, which is tied to this {@link Wire}.
206 public WireEnd createEnd()
208 return new WireEnd();
211 private void registerInput(WireEnd toRegister)
213 inputs.add(toRegister);
217 * A {@link WireEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
218 * {@link Wire}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than
219 * Z 0 and 1 turn into X when they are mixed
221 * @author Fabian Stemmler
225 private boolean open;
226 private Bit[] inputValues;
236 private void initValues()
238 inputValues = Bit.Z.makeArray(length);
242 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
244 * @param newValues The new values the wires should take on.
246 * @author Fabian Stemmler
248 public void feedSignals(Bit... newValues)
250 if (newValues.length == length)
252 feedSignals(0, newValues);
254 throw new IllegalArgumentException(
255 String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
259 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
261 * @param newValues The new values the wires should take on.
262 * @param startingBit The first index of the subarray of wires.
264 * @author Fabian Stemmler
266 public void feedSignals(int startingBit, Bit... newValues)
269 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
270 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
273 private void setValues(int startingBit, Bit... newValues)
275 int exclLastIndex = startingBit + newValues.length;
276 if (length < exclLastIndex)
277 throw new ArrayIndexOutOfBoundsException(
278 String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,
279 exclLastIndex - 1, length));
280 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
282 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
283 Wire.this.recalculate();
288 * @return The value (of bit 0) the {@link WireEnd} is currently feeding into the associated {@link Wire}.
290 public Bit getInputValue()
292 return getInputValue(0);
296 * @return The value which the {@link WireEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
298 public Bit getInputValue(int index)
300 return inputValues[index];
304 * @return A copy (safe to modify) of the values the {@link WireEnd} is currently feeding into the associated
307 public Bit[] getInputValues()
309 return getInputValues(0, length);
312 public Bit[] getInputValues(int start, int end)
314 int length = end - start;
315 Bit[] bits = new Bit[length];
316 System.arraycopy(inputValues, start, bits, 0, length);
321 * {@link WireEnd} now feeds Z into the associated {@link Wire}.
323 public void clearSignals()
325 feedSignals(Bit.Z.makeArray(length));
328 public Bit[] wireValuesExcludingMe()
330 Bit[] bits = Bit.Z.makeArray(length);
331 for (WireEnd wai : inputs)
335 Util.combineInto(bits, wai.getInputValues());
341 * Included for convenient use on {@link Wire}s of length 1.
343 * @return The value of bit 0.
345 * @author Fabian Stemmler
347 public Bit getValue()
349 return Wire.this.getValue();
353 * @param index Index of the requested bit.
354 * @return The value of the indexed bit.
356 * @author Fabian Stemmler
358 public Bit getValue(int index)
360 return Wire.this.getValue(index);
364 * @param index Index of the requested bit.
365 * @return The value of the indexed bit.
367 * @author Fabian Stemmler
369 public Bit[] getValues()
371 return Wire.this.getValues();
375 * @param start Start of the wanted segment. (inclusive)
376 * @param end End of the wanted segment. (exclusive)
377 * @return The values of the segment of {@link Bit}s indexed.
379 * @author Fabian Stemmler
381 public Bit[] getValues(int start, int end)
383 return Wire.this.getValues(start, end);
388 * The {@link Wire} is interpreted as an unsigned integer with n bits.
390 * @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
391 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
393 * @author Fabian Stemmler
395 public boolean hasNumericValue()
397 return Wire.this.hasNumericValue();
401 * The {@link Wire} is interpreted as an unsigned integer with n bits.
403 * @return The unsigned value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
405 * @author Fabian Stemmler
407 public long getUnsignedValue()
409 return Wire.this.getUnsignedValue();
413 * The {@link Wire} is interpreted as a signed integer with n bits.
415 * @return The signed value of the {@link Wire}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
417 * @author Fabian Stemmler
419 public long getSignedValue()
421 return Wire.this.getSignedValue();
425 public String toString()
427 return Arrays.toString(values);
428 //return String.format("%s \nFeeding: %s", WireArray.this.toString(), Arrays.toString(inputValues));
431 public void disconnect()
442 public boolean addObserver(WireArrayObserver ob)
444 return Wire.this.addObserver(ob);
447 public Wire getWire()
454 public String toString()
456 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
457 //Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
460 public static WireEnd[] extractEnds(Wire[] w)
462 WireEnd[] inputs = new WireEnd[w.length];
463 for (int i = 0; i < w.length; i++)
464 inputs[i] = w[i].createEnd();