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.Simulation;
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>();
28 public Wire(int length, int travelTime)
31 throw new IllegalArgumentException(
32 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.travelTime = travelTime;
38 private void initValues()
40 values = U.toVector(length);
43 private void recalculateSingleInput()
45 setNewValues(inputs.get(0).getInputValues());
48 private void recalculateMultipleInputs()
50 BitVectorMutator mutator = BitVectorMutator.empty();
51 for (ReadWriteEnd wireArrayEnd : inputs)
52 mutator.join(wireArrayEnd.getInputValues());
53 setNewValues(mutator.get());
56 private void setNewValues(BitVector newValues)
58 if (values.equals(newValues))
60 BitVector oldValues = values;
62 notifyObservers(oldValues);
65 private void recalculate()
67 switch (inputs.size())
72 recalculateSingleInput();
75 recalculateMultipleInputs();
80 * The {@link Wire} is interpreted as an unsigned integer with n bits.
82 * @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
83 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
85 * @author Fabian Stemmler
87 public boolean hasNumericValue()
91 if (b != Bit.ZERO && b != Bit.ONE)
98 * The {@link Wire} is interpreted as an unsigned integer with n bits.
100 * @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.
102 * @author Fabian Stemmler
104 public long getUnsignedValue()
108 for (Bit bit : values)
115 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
127 * The {@link Wire} is interpreted as a signed integer with n bits.
129 * @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.
131 * @author Fabian Stemmler
133 public long getSignedValue()
135 long val = getUnsignedValue();
136 long mask = 1 << (length - 1);
137 if ((mask & val) != 0)
139 int shifts = 64 - length;
140 return (val << shifts) >> shifts;
145 public Bit getValue()
150 public Bit getValue(int index)
152 return values.getBit(index);
155 public BitVector getValues(int start, int end)
157 return values.subVector(start, end);
160 public BitVector getValues()
166 * Adds an {@link WireObserver}, who will be notified when the value of the {@link Wire} is updated.
168 * @param ob The {@link WireObserver} to be notified of changes.
169 * @return true if the given {@link WireObserver} was not already registered, false otherwise
171 * @author Fabian Stemmler
173 private void attachEnd(ReadEnd end)
178 private void detachEnd(ReadEnd end)
180 attached.remove(end);
183 private void notifyObservers(BitVector oldValues)
185 for (ReadEnd o : attached)
190 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
192 public ReadWriteEnd createReadWriteEnd()
194 return new ReadWriteEnd();
198 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
200 public ReadEnd createReadOnlyEnd()
202 return new ReadEnd();
205 private void registerInput(ReadWriteEnd toRegister)
207 inputs.add(toRegister);
211 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
212 * {@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
213 * and 1 turn into X when they are mixed
215 * @author Fabian Stemmler
219 private List<WireObserver> observers = new ArrayList<WireObserver>();
224 Wire.this.attachEnd(this);
227 public void update(BitVector oldValues)
229 for (WireObserver ob : observers)
230 ob.update(this, oldValues);
234 * Included for convenient use on {@link Wire}s of length 1.
236 * @return The value of bit 0.
238 * @author Fabian Stemmler
240 public Bit getValue()
242 return Wire.this.getValue();
246 * @param index Index of the requested bit.
247 * @return The value of the indexed bit.
249 * @author Fabian Stemmler
251 public Bit getValue(int index)
253 return Wire.this.getValue(index);
257 * @param index Index of the requested bit.
258 * @return The value of the indexed bit.
260 * @author Fabian Stemmler
262 public BitVector getValues()
264 return Wire.this.getValues();
268 * @param start Start of the wanted segment. (inclusive)
269 * @param end End of the wanted segment. (exclusive)
270 * @return The values of the segment of {@link Bit}s indexed.
272 * @author Fabian Stemmler
274 public BitVector getValues(int start, int end)
276 return Wire.this.getValues(start, end);
280 * The {@link Wire} is interpreted as an unsigned integer with n bits.
282 * @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
283 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
285 * @author Fabian Stemmler
287 public boolean hasNumericValue()
289 return Wire.this.hasNumericValue();
293 * The {@link Wire} is interpreted as an unsigned integer with n bits.
295 * @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.
297 * @author Fabian Stemmler
299 public long getUnsignedValue()
301 return Wire.this.getUnsignedValue();
305 * The {@link Wire} is interpreted as a signed integer with n bits.
307 * @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.
309 * @author Fabian Stemmler
311 public long getSignedValue()
313 return Wire.this.getSignedValue();
317 public String toString()
319 return Wire.this.toString();
334 public boolean addObserver(WireObserver ob)
336 return observers.add(ob);
339 public Wire getWire()
345 public class ReadWriteEnd extends ReadEnd
347 private boolean open;
348 private BitVector inputValues;
350 private ReadWriteEnd()
358 private void initValues()
360 inputValues = U.toVector(length);
364 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
366 * @param newValues The new values the wires should take on.
368 * @author Fabian Stemmler
370 public void feedSignals(Bit... newValues)
372 feedSignals(BitVector.of(newValues));
375 public void feedSignals(BitVector newValues)
377 if (newValues.length() != length)
378 throw new IllegalArgumentException(
379 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
381 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
382 Simulation.TIMELINE.addEvent(e -> setValues(newValues), travelTime);
386 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
388 * @param bitVector The new values the wires should take on.
389 * @param startingBit The first index of the subarray of wires.
391 * @author Fabian Stemmler
393 public void feedSignals(int startingBit, BitVector bitVector)
396 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
397 Simulation.TIMELINE.addEvent(e -> setValues(startingBit, bitVector), travelTime);
400 private void setValues(int startingBit, BitVector newValues)
402 // index check covered in equals
403 if (!inputValues.equalsWithOffset(newValues, startingBit))
405 Bit[] vals = inputValues.getBits();
406 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
407 inputValues = BitVector.of(vals);
408 Wire.this.recalculate();
412 private void setValues(BitVector newValues)
414 if (inputValues.equals(newValues))
416 inputValues = newValues;
417 Wire.this.recalculate();
421 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
423 public Bit getInputValue()
425 return getInputValue(0);
429 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
431 public Bit getInputValue(int index)
433 return inputValues.getBit(index);
437 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
439 public BitVector getInputValues()
441 return getInputValues(0, length);
444 public BitVector getInputValues(int start, int end)
446 return inputValues.subVector(start, end);
450 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
452 public void clearSignals()
454 feedSignals(Z.toVector(length));
457 public BitVector wireValuesExcludingMe()
459 BitVectorMutator mutator = BitVectorMutator.empty();
460 for (ReadWriteEnd wireEnd : inputs)
464 mutator.join(wireEnd.inputValues);
466 return mutator.get();
470 public String toString()
472 return inputValues.toString();
477 public String toString()
479 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
480 // Arrays.toString(values), inputs.stream().map(i -> Arrays.toString(i.inputValues)).reduce((s1, s2) -> s1 + s2)
483 public static ReadEnd[] extractEnds(Wire[] w)
485 ReadEnd[] inputs = new ReadEnd[w.length];
486 for (int i = 0; i < w.length; i++)
487 inputs[i] = w[i].createReadWriteEnd();