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 setNewValues(BitVector newValues)
56 if (values.equals(newValues))
58 // BitVector oldValues = values;
65 if (inputs.size() == 0)
66 setNewValues(BitVector.of(Bit.U, length));
69 BitVectorMutator mutator = BitVectorMutator.empty();
70 for (ReadWriteEnd wireArrayEnd : inputs)
71 mutator.join(wireArrayEnd.getInputValues());
72 setNewValues(mutator.toBitVector());
77 * Forces a Wire to take on specific values. If the new values differ from the old ones, the observers of the Wire will be notified.
78 * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)
80 * @param values The values the <code>Wire</code> will have immediately after this method is called
82 public void forceValues(BitVector values)
88 * The {@link Wire} is interpreted as an unsigned integer with n bits.
90 * @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
91 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
93 * @author Fabian Stemmler
95 public boolean hasNumericValue()
99 if (b != Bit.ZERO && b != Bit.ONE)
106 * The {@link Wire} is interpreted as an unsigned integer with n bits.
108 * @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.
110 * @author Fabian Stemmler
112 public long getUnsignedValue()
116 for (Bit bit : values)
123 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
135 * The {@link Wire} is interpreted as a signed integer with n bits.
137 * @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.
139 * @author Fabian Stemmler
141 public long getSignedValue()
143 long val = getUnsignedValue();
144 long mask = 1 << (length - 1);
145 if ((mask & val) != 0)
147 int shifts = 64 - length;
148 return (val << shifts) >> shifts;
154 * Returns the least significant bit (LSB)
156 public Bit getValue()
162 * Returns the least significant bit (LSB) of the given index
164 public Bit getValue(int index)
166 return values.getLSBit(index);
169 public BitVector getValues(int start, int end)
171 return values.subVector(start, end);
174 public BitVector getValues()
180 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
182 * @param ob The {@link LogicObserver} to be notified of changes.
183 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
185 * @author Fabian Stemmler
187 void attachEnd(ReadEnd end)
192 void detachEnd(ReadEnd end)
194 attached.remove(end);
197 private void notifyObservers()
199 attached.forEach(r -> r.update());
203 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
205 public ReadWriteEnd createReadWriteEnd()
207 return new ReadWriteEnd();
211 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
213 public ReadEnd createReadOnlyEnd()
215 return new ReadEnd();
218 void registerInput(ReadWriteEnd toRegister)
220 inputs.add(toRegister);
224 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
225 * {@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
226 * and 1 turn into X when they are mixed
228 * @author Fabian Stemmler
230 public class ReadEnd implements LogicObservable
232 private List<LogicObserver> observers = new ArrayList<>();
237 Wire.this.attachEnd(this);
246 * Included for convenient use on {@link Wire}s of length 1.
248 * @return The value of bit 0.
250 * @author Fabian Stemmler
252 public Bit getValue()
254 return Wire.this.getValue();
258 * @param index Index of the requested bit.
259 * @return The value of the indexed bit.
261 * @author Fabian Stemmler
263 public Bit getValue(int index)
265 return Wire.this.getValue(index);
269 * @param index Index of the requested bit.
270 * @return The value of the indexed bit.
272 * @author Fabian Stemmler
274 public BitVector getValues()
276 return Wire.this.getValues();
280 * @param start Start of the wanted segment. (inclusive)
281 * @param end End of the wanted segment. (exclusive)
282 * @return The values of the segment of {@link Bit}s indexed.
284 * @author Fabian Stemmler
286 public BitVector getValues(int start, int end)
288 return Wire.this.getValues(start, end);
292 * The {@link Wire} is interpreted as an unsigned integer with n bits.
294 * @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
295 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
297 * @author Fabian Stemmler
299 public boolean hasNumericValue()
301 return Wire.this.hasNumericValue();
305 * The {@link Wire} is interpreted as an unsigned integer with n bits.
307 * @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.
309 * @author Fabian Stemmler
311 public long getUnsignedValue()
313 return Wire.this.getUnsignedValue();
317 * The {@link Wire} is interpreted as a signed integer with n bits.
319 * @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.
321 * @author Fabian Stemmler
323 public long getSignedValue()
325 return Wire.this.getSignedValue();
329 public String toString()
331 return Wire.this.toString();
346 public Wire getWire()
352 public void registerObserver(LogicObserver ob)
358 public void deregisterObserver(LogicObserver ob)
360 observers.remove(ob);
364 public void notifyObservers()
366 observers.forEach(ob -> ob.update(this));
370 public class ReadWriteEnd extends ReadEnd
372 private boolean open, isWriting;
373 private BitVector inputValues;
384 private void initValues()
386 inputValues = U.toVector(length);
390 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
392 * @param newValues The new values the wires should take on.
394 * @author Fabian Stemmler
396 public void feedSignals(Bit... newValues)
398 feedSignals(BitVector.of(newValues));
401 public void feedSignals(BitVector newValues)
403 if (newValues.length() != length)
404 throw new IllegalArgumentException(
405 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
407 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
408 timeline.addEvent(e -> setValues(newValues), travelTime);
412 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
414 * @param bitVector The new values the wires should take on.
415 * @param startingBit The first index of the subarray of wires.
417 * @author Fabian Stemmler
419 public void feedSignals(int startingBit, BitVector bitVector)
422 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
423 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
427 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
428 * feedValues(...) with a delay.
430 void setValues(int startingBit, BitVector newValues)
432 // index check covered in equals
433 if (!inputValues.equalsWithOffset(newValues, startingBit))
435 Bit[] vals = inputValues.getBits();
436 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
437 inputValues = BitVector.of(vals);
438 Wire.this.recalculate();
443 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
444 * feedValues(...) with a delay.
446 void setValues(BitVector newValues)
448 if (inputValues.equals(newValues))
450 inputValues = newValues;
451 Wire.this.recalculate();
455 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
456 * significant bit (LSB)
458 public Bit getInputValue()
460 return getInputValue(0);
464 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
465 * Returns the least significant bit (LSB)
468 public Bit getInputValue(int index)
470 return inputValues.getLSBit(index);
474 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
476 public BitVector getInputValues()
481 public BitVector getInputValues(int start, int end)
483 return inputValues.subVector(start, end);
487 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
489 public void clearSignals()
491 feedSignals(Z.toVector(length));
494 public BitVector wireValuesExcludingMe()
496 BitVectorMutator mutator = BitVectorMutator.empty();
497 for (ReadWriteEnd wireEnd : inputs)
501 mutator.join(wireEnd.inputValues);
503 return mutator.toBitVector();
507 public String toString()
509 return inputValues.toString();
519 void setWriting(boolean isWriting)
521 if (this.isWriting != isWriting)
523 this.isWriting = isWriting;
528 Wire.this.recalculate();
539 public String toString()
541 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
542 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
545 public static ReadEnd[] extractEnds(Wire[] w)
547 ReadEnd[] inputs = new ReadEnd[w.length];
548 for (int i = 0; i < w.length; i++)
549 inputs[i] = w[i].createReadWriteEnd();
553 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
555 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
556 * The bits are permanently fused together.
558 * @param a The {@link Wire} to be (partially) fused with b
559 * @param b The {@link Wire} to be (partially) fused with a
560 * @param fromA The first bit of {@link Wire} a to be fused
561 * @param fromB The first bit of {@link Wire} b to be fused
562 * @param length The amount of bits to fuse
564 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
566 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
567 rA.setWriting(false);
568 rB.setWriting(false);
569 rA.setValues(BitVector.of(Bit.Z, a.length));
570 rB.setValues(BitVector.of(Bit.Z, b.length));
571 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
572 rA.registerObserver(aF);
573 rB.registerObserver(bF);
580 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
581 * permanently fused together.
583 * @param a The {@link Wire} to be fused with b
584 * @param b The {@link Wire} to be fused with a
586 public static void fuse(Wire a, Wire b)
588 fuse(a, b, 0, 0, a.length);
591 private static class Fusion implements LogicObserver
593 private ReadWriteEnd target;
594 int fromSource, fromTarget, length;
596 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
598 this.target = target;
599 this.fromSource = fromSource;
600 this.fromTarget = fromTarget;
601 this.length = length;
605 public void update(LogicObservable initiator)
607 ReadWriteEnd source = (ReadWriteEnd) initiator;
608 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
609 target.setWriting(false);
612 target.setWriting(true);
613 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
614 target.setValues(fromTarget, targetInput);