1 package net.mograsim.logic.core.wires;
3 import static net.mograsim.logic.core.types.Bit.U;
4 import static net.mograsim.logic.core.types.Bit.Z;
6 import java.util.ArrayList;
9 import net.mograsim.logic.core.LogicObservable;
10 import net.mograsim.logic.core.LogicObserver;
11 import net.mograsim.logic.core.timeline.Timeline;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
17 * Represents an array of wires that can store n bits of information.
19 * @author Fabian Stemmler
24 public final String name;
25 private BitVector values;
26 public final int travelTime;
27 private List<ReadEnd> attached = new ArrayList<>();
28 public final int length;
29 List<ReadWriteEnd> inputs = new ArrayList<>();
32 public Wire(Timeline timeline, int length, int travelTime)
34 this(timeline, length, travelTime, null);
37 public Wire(Timeline timeline, int length, int travelTime, String name)
40 throw new IllegalArgumentException(
41 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
43 this.timeline = timeline;
45 this.travelTime = travelTime;
49 private void initValues()
51 values = U.toVector(length);
54 private void recalculateSingleInput()
56 setNewValues(inputs.get(0).getInputValues());
59 private void recalculateMultipleInputs()
61 BitVectorMutator mutator = BitVectorMutator.empty();
62 for (ReadWriteEnd wireArrayEnd : inputs)
63 mutator.join(wireArrayEnd.getInputValues());
64 setNewValues(mutator.get());
67 private void setNewValues(BitVector newValues)
69 if (values.equals(newValues))
71 // BitVector oldValues = values;
78 switch (inputs.size())
81 setNewValues(BitVector.of(Bit.U, length));
84 recalculateSingleInput();
87 recalculateMultipleInputs();
92 * The {@link Wire} is interpreted as an unsigned integer with n bits.
94 * @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
95 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
97 * @author Fabian Stemmler
99 public boolean hasNumericValue()
103 if (b != Bit.ZERO && b != Bit.ONE)
110 * The {@link Wire} is interpreted as an unsigned integer with n bits.
112 * @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.
114 * @author Fabian Stemmler
116 public long getUnsignedValue()
120 for (Bit bit : values)
127 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
139 * The {@link Wire} is interpreted as a signed integer with n bits.
141 * @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.
143 * @author Fabian Stemmler
145 public long getSignedValue()
147 long val = getUnsignedValue();
148 long mask = 1 << (length - 1);
149 if ((mask & val) != 0)
151 int shifts = 64 - length;
152 return (val << shifts) >> shifts;
157 public Bit getValue()
162 public Bit getValue(int index)
164 return values.getBit(index);
167 public BitVector getValues(int start, int end)
169 return values.subVector(start, end);
172 public BitVector getValues()
178 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
180 * @param ob The {@link LogicObserver} to be notified of changes.
181 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
183 * @author Fabian Stemmler
185 void attachEnd(ReadEnd end)
190 void detachEnd(ReadEnd end)
192 attached.remove(end);
195 private void notifyObservers()
197 attached.forEach(r -> r.update());
201 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
203 public ReadWriteEnd createReadWriteEnd()
205 return new ReadWriteEnd();
209 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
211 public ReadEnd createReadOnlyEnd()
213 return new ReadEnd();
216 void registerInput(ReadWriteEnd toRegister)
218 inputs.add(toRegister);
222 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
223 * {@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
224 * and 1 turn into X when they are mixed
226 * @author Fabian Stemmler
228 public class ReadEnd implements LogicObservable
230 private List<LogicObserver> observers = new ArrayList<>();
235 Wire.this.attachEnd(this);
244 * Included for convenient use on {@link Wire}s of length 1.
246 * @return The value of bit 0.
248 * @author Fabian Stemmler
250 public Bit getValue()
252 return Wire.this.getValue();
256 * @param index Index of the requested bit.
257 * @return The value of the indexed bit.
259 * @author Fabian Stemmler
261 public Bit getValue(int index)
263 return Wire.this.getValue(index);
267 * @param index Index of the requested bit.
268 * @return The value of the indexed bit.
270 * @author Fabian Stemmler
272 public BitVector getValues()
274 return Wire.this.getValues();
278 * @param start Start of the wanted segment. (inclusive)
279 * @param end End of the wanted segment. (exclusive)
280 * @return The values of the segment of {@link Bit}s indexed.
282 * @author Fabian Stemmler
284 public BitVector getValues(int start, int end)
286 return Wire.this.getValues(start, end);
290 * The {@link Wire} is interpreted as an unsigned integer with n bits.
292 * @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
293 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
295 * @author Fabian Stemmler
297 public boolean hasNumericValue()
299 return Wire.this.hasNumericValue();
303 * The {@link Wire} is interpreted as an unsigned integer with n bits.
305 * @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.
307 * @author Fabian Stemmler
309 public long getUnsignedValue()
311 return Wire.this.getUnsignedValue();
315 * The {@link Wire} is interpreted as a signed integer with n bits.
317 * @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.
319 * @author Fabian Stemmler
321 public long getSignedValue()
323 return Wire.this.getSignedValue();
327 public String toString()
329 return Wire.this.toString();
344 public Wire getWire()
350 public void registerObserver(LogicObserver ob)
356 public void deregisterObserver(LogicObserver ob)
358 observers.remove(ob);
362 public void notifyObservers()
364 observers.forEach(ob -> ob.update(this));
368 public class ReadWriteEnd extends ReadEnd
370 private boolean open, isWriting;
371 private BitVector inputValues;
382 private void initValues()
384 inputValues = U.toVector(length);
388 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
390 * @param newValues The new values the wires should take on.
392 * @author Fabian Stemmler
394 public void feedSignals(Bit... newValues)
396 feedSignals(BitVector.of(newValues));
399 public void feedSignals(BitVector newValues)
401 if (newValues.length() != length)
402 throw new IllegalArgumentException(
403 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
405 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
406 timeline.addEvent(e -> setValues(newValues), travelTime);
410 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
412 * @param bitVector The new values the wires should take on.
413 * @param startingBit The first index of the subarray of wires.
415 * @author Fabian Stemmler
417 public void feedSignals(int startingBit, BitVector bitVector)
420 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
421 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
425 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
426 * feedValues(...) with a delay.
428 void setValues(int startingBit, BitVector newValues)
430 // index check covered in equals
431 if (!inputValues.equalsWithOffset(newValues, startingBit))
433 Bit[] vals = inputValues.getBits();
434 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
435 inputValues = BitVector.of(vals);
436 Wire.this.recalculate();
441 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
442 * feedValues(...) with a delay.
444 void setValues(BitVector newValues)
446 if (inputValues.equals(newValues))
448 inputValues = newValues;
449 Wire.this.recalculate();
453 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
455 public Bit getInputValue()
457 return getInputValue(0);
461 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
463 public Bit getInputValue(int index)
465 return inputValues.getBit(index);
469 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
471 public BitVector getInputValues()
473 return getInputValues(0, length);
476 public BitVector getInputValues(int start, int end)
478 return inputValues.subVector(start, end);
482 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
484 public void clearSignals()
486 feedSignals(Z.toVector(length));
489 public BitVector wireValuesExcludingMe()
491 BitVectorMutator mutator = BitVectorMutator.empty();
492 for (ReadWriteEnd wireEnd : inputs)
496 mutator.join(wireEnd.inputValues);
498 return mutator.get();
502 public String toString()
504 return inputValues.toString();
514 void setWriting(boolean isWriting)
516 if (this.isWriting != isWriting)
518 this.isWriting = isWriting;
523 Wire.this.recalculate();
534 public String toString()
536 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
537 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
540 public static ReadEnd[] extractEnds(Wire[] w)
542 ReadEnd[] inputs = new ReadEnd[w.length];
543 for (int i = 0; i < w.length; i++)
544 inputs[i] = w[i].createReadWriteEnd();
548 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
550 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
551 * The bits are permanently fused together.
553 * @param a The {@link Wire} to be (partially) fused with b
554 * @param b The {@link Wire} to be (partially) fused with a
555 * @param fromA The first bit of {@link Wire} a to be fused
556 * @param fromB The first bit of {@link Wire} b to be fused
557 * @param length The amount of bits to fuse
559 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
561 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
562 rA.setWriting(false);
563 rB.setWriting(false);
564 rA.setValues(BitVector.of(Bit.Z, a.length));
565 rB.setValues(BitVector.of(Bit.Z, b.length));
566 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
567 rA.registerObserver(aF);
568 rB.registerObserver(bF);
575 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
576 * permanently fused together.
578 * @param a The {@link Wire} to be fused with b
579 * @param b The {@link Wire} to be fused with a
581 public static void fuse(Wire a, Wire b)
583 fuse(a, b, 0, 0, a.length);
586 private static class Fusion implements LogicObserver
588 private ReadWriteEnd target;
589 int fromSource, fromTarget, length;
591 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
593 this.target = target;
594 this.fromSource = fromSource;
595 this.fromTarget = fromTarget;
596 this.length = length;
600 public void update(LogicObservable initiator)
602 ReadWriteEnd source = (ReadWriteEnd) initiator;
603 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
604 target.setWriting(false);
607 target.setWriting(true);
608 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
609 target.setValues(fromTarget, targetInput);