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<WireObserver> observers = new ArrayList<WireObserver>();
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 WireObserver}, who will be notified when the value of the {@link Wire} is updated.
187 * @param ob The {@link WireObserver} to be notified of changes.
188 * @return true if the given {@link WireObserver} was not already registered, false otherwise
190 * @author Fabian Stemmler
192 public boolean addObserver(WireObserver ob)
194 return observers.add(ob);
197 private void notifyObservers(Bit[] oldValues)
199 for (WireObserver 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 Z 0
219 * 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 {@link Wire}.
306 public Bit[] getInputValues()
308 return getInputValues(0, length);
311 public Bit[] getInputValues(int start, int end)
313 int length = end - start;
314 Bit[] bits = new Bit[length];
315 System.arraycopy(inputValues, start, bits, 0, length);
320 * {@link WireEnd} now feeds Z into the associated {@link Wire}.
322 public void clearSignals()
324 feedSignals(Bit.Z.makeArray(length));
327 public Bit[] wireValuesExcludingMe()
329 Bit[] bits = Bit.Z.makeArray(length);
330 for (WireEnd wai : inputs)
334 Util.combineInto(bits, wai.getInputValues());
340 * Included for convenient use on {@link Wire}s of length 1.
342 * @return The value of bit 0.
344 * @author Fabian Stemmler
346 public Bit getValue()
348 return Wire.this.getValue();
352 * @param index Index of the requested bit.
353 * @return The value of the indexed bit.
355 * @author Fabian Stemmler
357 public Bit getValue(int index)
359 return Wire.this.getValue(index);
363 * @param index Index of the requested bit.
364 * @return The value of the indexed bit.
366 * @author Fabian Stemmler
368 public Bit[] getValues()
370 return Wire.this.getValues();
374 * @param start Start of the wanted segment. (inclusive)
375 * @param end End of the wanted segment. (exclusive)
376 * @return The values of the segment of {@link Bit}s indexed.
378 * @author Fabian Stemmler
380 public Bit[] getValues(int start, int end)
382 return Wire.this.getValues(start, end);
386 * The {@link Wire} is interpreted as an unsigned integer with n bits.
388 * @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
389 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
391 * @author Fabian Stemmler
393 public boolean hasNumericValue()
395 return Wire.this.hasNumericValue();
399 * The {@link Wire} is interpreted as an unsigned integer with n bits.
401 * @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.
403 * @author Fabian Stemmler
405 public long getUnsignedValue()
407 return Wire.this.getUnsignedValue();
411 * The {@link Wire} is interpreted as a signed integer with n bits.
413 * @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.
415 * @author Fabian Stemmler
417 public long getSignedValue()
419 return Wire.this.getSignedValue();
423 public String toString()
425 return Arrays.toString(values);
426 // return String.format("%s \nFeeding: %s", WireArray.this.toString(), Arrays.toString(inputValues));
429 public void disconnect()
440 public boolean addObserver(WireObserver ob)
442 return Wire.this.addObserver(ob);
445 public Wire getWire()
452 public String toString()
454 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
455 // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
458 public static WireEnd[] extractEnds(Wire[] w)
460 WireEnd[] inputs = new WireEnd[w.length];
461 for (int i = 0; i < w.length; i++)
462 inputs[i] = w[i].createEnd();