1 package era.mi.logic.wires;
3 import static era.mi.logic.types.Bit.U;
4 import static era.mi.logic.types.Bit.Z;
6 import java.util.ArrayList;
9 import era.mi.logic.timeline.Timeline;
10 import era.mi.logic.types.Bit;
11 import era.mi.logic.types.BitVector;
12 import era.mi.logic.types.BitVector.BitVectorMutator;
15 * Represents an array of wires that can store n bits of information.
17 * @author Fabian Stemmler
22 private BitVector values;
23 public final int travelTime;
24 private List<ReadEnd> attached = new ArrayList<ReadEnd>();
25 public final int length;
26 private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
27 private Timeline timeline;
29 public Wire(Timeline timeline, int length, int travelTime)
32 throw new IllegalArgumentException(
33 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
34 this.timeline = timeline;
36 this.travelTime = travelTime;
40 private void initValues()
42 values = U.toVector(length);
45 private void recalculateSingleInput()
47 setNewValues(inputs.get(0).getInputValues());
50 private void recalculateMultipleInputs()
52 BitVectorMutator mutator = BitVectorMutator.empty();
53 for (ReadWriteEnd wireArrayEnd : inputs)
54 mutator.join(wireArrayEnd.getInputValues());
55 setNewValues(mutator.get());
58 private void setNewValues(BitVector newValues)
60 if (values.equals(newValues))
62 BitVector oldValues = values;
64 notifyObservers(oldValues);
67 private void recalculate()
69 switch (inputs.size())
74 recalculateSingleInput();
77 recalculateMultipleInputs();
82 * The {@link Wire} is interpreted as an unsigned integer with n bits.
84 * @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
85 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
87 * @author Fabian Stemmler
89 public boolean hasNumericValue()
93 if (b != Bit.ZERO && b != Bit.ONE)
100 * The {@link Wire} is interpreted as an unsigned integer with n bits.
102 * @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.
104 * @author Fabian Stemmler
106 public long getUnsignedValue()
110 for (Bit bit : values)
117 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
129 * The {@link Wire} is interpreted as a signed integer with n bits.
131 * @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.
133 * @author Fabian Stemmler
135 public long getSignedValue()
137 long val = getUnsignedValue();
138 long mask = 1 << (length - 1);
139 if ((mask & val) != 0)
141 int shifts = 64 - length;
142 return (val << shifts) >> shifts;
147 public Bit getValue()
152 public Bit getValue(int index)
154 return values.getBit(index);
157 public BitVector getValues(int start, int end)
159 return values.subVector(start, end);
162 public BitVector getValues()
168 * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated.
170 * @param ob The {@link WireObserver} to be notified of changes.
171 * @return true if the given {@link WireObserver} was not already registered, false otherwise
173 * @author Fabian Stemmler
175 private void attachEnd(ReadEnd end)
180 private void detachEnd(ReadEnd end)
182 attached.remove(end);
185 private void notifyObservers(BitVector oldValues)
187 for (ReadEnd o : attached)
192 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
194 public ReadWriteEnd createReadWriteEnd()
196 return new ReadWriteEnd();
200 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
202 public ReadEnd createReadOnlyEnd()
204 return new ReadEnd();
207 private void registerInput(ReadWriteEnd toRegister)
209 inputs.add(toRegister);
213 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
214 * {@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
215 * and 1 turn into X when they are mixed
217 * @author Fabian Stemmler
221 private List<WireObserver> observers = new ArrayList<WireObserver>();
226 Wire.this.attachEnd(this);
229 public void update(BitVector oldValues)
231 for (WireObserver ob : observers)
232 ob.update(this, oldValues);
236 * Included for convenient use on {@link Wire}s of length 1.
238 * @return The value of bit 0.
240 * @author Fabian Stemmler
242 public Bit getValue()
244 return Wire.this.getValue();
248 * @param index Index of the requested bit.
249 * @return The value of the indexed bit.
251 * @author Fabian Stemmler
253 public Bit getValue(int index)
255 return Wire.this.getValue(index);
259 * @param index Index of the requested bit.
260 * @return The value of the indexed bit.
262 * @author Fabian Stemmler
264 public BitVector getValues()
266 return Wire.this.getValues();
270 * @param start Start of the wanted segment. (inclusive)
271 * @param end End of the wanted segment. (exclusive)
272 * @return The values of the segment of {@link Bit}s indexed.
274 * @author Fabian Stemmler
276 public BitVector getValues(int start, int end)
278 return Wire.this.getValues(start, end);
282 * The {@link Wire} is interpreted as an unsigned integer with n bits.
284 * @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
285 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
287 * @author Fabian Stemmler
289 public boolean hasNumericValue()
291 return Wire.this.hasNumericValue();
295 * The {@link Wire} is interpreted as an unsigned integer with n bits.
297 * @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.
299 * @author Fabian Stemmler
301 public long getUnsignedValue()
303 return Wire.this.getUnsignedValue();
307 * The {@link Wire} is interpreted as a signed integer with n bits.
309 * @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.
311 * @author Fabian Stemmler
313 public long getSignedValue()
315 return Wire.this.getSignedValue();
319 public String toString()
321 return Wire.this.toString();
336 public boolean addObserver(WireObserver ob)
338 return observers.add(ob);
341 public Wire getWire()
347 public class ReadWriteEnd extends ReadEnd
349 private boolean open;
350 private BitVector inputValues;
352 private ReadWriteEnd()
360 private void initValues()
362 inputValues = U.toVector(length);
366 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
368 * @param newValues The new values the wires should take on.
370 * @author Fabian Stemmler
372 public void feedSignals(Bit... newValues)
374 feedSignals(BitVector.of(newValues));
377 public void feedSignals(BitVector newValues)
379 if (newValues.length() != length)
380 throw new IllegalArgumentException(
381 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
383 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
384 timeline.addEvent(e -> setValues(newValues), travelTime);
388 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
390 * @param bitVector The new values the wires should take on.
391 * @param startingBit The first index of the subarray of wires.
393 * @author Fabian Stemmler
395 public void feedSignals(int startingBit, BitVector bitVector)
398 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
399 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
402 private void setValues(int startingBit, BitVector newValues)
404 // index check covered in equals
405 if (!inputValues.equalsWithOffset(newValues, startingBit))
407 Bit[] vals = inputValues.getBits();
408 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
409 inputValues = BitVector.of(vals);
410 Wire.this.recalculate();
414 private void setValues(BitVector newValues)
416 if (inputValues.equals(newValues))
418 inputValues = newValues;
419 Wire.this.recalculate();
423 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
425 public Bit getInputValue()
427 return getInputValue(0);
431 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
433 public Bit getInputValue(int index)
435 return inputValues.getBit(index);
439 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
441 public BitVector getInputValues()
443 return getInputValues(0, length);
446 public BitVector getInputValues(int start, int end)
448 return inputValues.subVector(start, end);
452 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
454 public void clearSignals()
456 feedSignals(Z.toVector(length));
459 public BitVector wireValuesExcludingMe()
461 BitVectorMutator mutator = BitVectorMutator.empty();
462 for (ReadWriteEnd wireEnd : inputs)
466 mutator.join(wireEnd.inputValues);
468 return mutator.get();
472 public String toString()
474 return inputValues.toString();
479 public String toString()
481 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
482 // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
485 public static ReadEnd[] extractEnds(Wire[] w)
487 ReadEnd[] inputs = new ReadEnd[w.length];
488 for (int i = 0; i < w.length; i++)
489 inputs[i] = w[i].createReadWriteEnd();