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 width;
29 List<ReadWriteEnd> inputs = new ArrayList<>();
32 public Wire(Timeline timeline, int width, int travelTime)
34 this(timeline, width, travelTime, null);
37 public Wire(Timeline timeline, int width, int travelTime, String name)
40 throw new IllegalArgumentException(
41 String.format("Tried to create an array of wires with width %d, but a width of less than 1 makes no sense.", width));
43 this.timeline = timeline;
45 this.travelTime = travelTime;
49 private void initValues()
51 values = U.toVector(width);
54 private void setNewValues(BitVector newValues)
56 if (values.equals(newValues))
65 setNewValues(U.toVector(width));
68 BitVectorMutator mutator = BitVectorMutator.empty();
69 for (ReadWriteEnd wireArrayEnd : inputs)
70 mutator.join(wireArrayEnd.getInputValues());
71 setNewValues(mutator.toBitVector());
76 * 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.
77 * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)
79 * @param values The values the <code>Wire</code> will have immediately after this method is called
81 public void forceValues(BitVector values)
87 * The {@link Wire} is interpreted as an unsigned integer with n bits.
89 * @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
90 * value), not <code>Bit.U</code>, <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
92 * @author Fabian Stemmler
94 public boolean hasNumericValue()
96 return values.isBinary();
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 << (width - 1);
139 if ((mask & val) != 0)
141 int shifts = 64 - width;
142 return (val << shifts) >> shifts;
148 * Returns the least significant bit (LSB)
150 public Bit getValue()
156 * Returns the least significant bit (LSB) of the given index
158 public Bit getValue(int index)
160 return values.getLSBit(index);
163 public BitVector getValues(int start, int end)
165 return values.subVector(start, end);
168 public BitVector getValues()
174 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
176 * @param ob The {@link LogicObserver} to be notified of changes.
177 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
179 * @author Fabian Stemmler
181 boolean attachEnd(ReadEnd end)
183 return attached.add(end);
186 void detachEnd(ReadEnd end)
188 attached.remove(end);
191 private void notifyObservers()
193 attached.forEach(ReadEnd::update);
197 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
199 public ReadWriteEnd createReadWriteEnd()
201 return new ReadWriteEnd();
205 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
207 public ReadEnd createReadOnlyEnd()
209 return new ReadEnd();
212 void registerInput(ReadWriteEnd toRegister)
214 inputs.add(toRegister);
218 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
219 * {@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
220 * and 1 turn into X when they are mixed
222 * @author Fabian Stemmler
224 public class ReadEnd implements LogicObservable
226 private List<LogicObserver> observers = new ArrayList<>();
231 Wire.this.attachEnd(this);
240 * Included for convenient use on {@link Wire}s of width 1.
242 * @return The value of bit 0.
244 * @author Fabian Stemmler
246 public Bit getValue()
248 return Wire.this.getValue();
252 * @param index Index of the requested bit.
253 * @return The value of the indexed bit.
255 * @author Fabian Stemmler
257 public Bit getValue(int index)
259 return Wire.this.getValue(index);
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 Wire getWire()
340 public void registerObserver(LogicObserver ob)
346 public void deregisterObserver(LogicObserver ob)
348 observers.remove(ob);
352 public void notifyObservers()
354 observers.forEach(ob -> ob.update(this));
358 public class ReadWriteEnd extends ReadEnd
360 private boolean open;
361 private boolean isWriting;
362 private BitVector inputValues;
373 private void initValues()
375 inputValues = U.toVector(width);
379 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
381 * @param newValues The new values the wires should take on.
383 * @author Fabian Stemmler
385 public void feedSignals(Bit... newValues)
387 feedSignals(BitVector.of(newValues));
390 public void feedSignals(BitVector newValues)
392 if (newValues.length() != width)
393 throw new IllegalArgumentException(
394 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), width));
396 throw new IllegalStateException("Attempted to write to closed WireArrayEnd.");
397 timeline.addEvent(e -> setValues(newValues), travelTime);
401 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
403 * @param bitVector The new values the wires should take on.
404 * @param startingBit The first index of the subarray of wires.
406 * @author Fabian Stemmler
408 public void feedSignals(int startingBit, BitVector bitVector)
411 throw new IllegalStateException("Attempted to write to closed WireArrayEnd.");
412 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
416 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
417 * feedValues(...) with a delay.
419 void setValues(int startingBit, BitVector newValues)
421 // index check covered in equals
422 if (!inputValues.equalsWithOffset(newValues, startingBit))
424 Bit[] vals = inputValues.getBits();
425 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
426 inputValues = BitVector.of(vals);
427 Wire.this.recalculate();
432 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
433 * feedValues(...) with a delay.
435 void setValues(BitVector newValues)
437 if (inputValues.equals(newValues))
439 inputValues = newValues;
440 Wire.this.recalculate();
444 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
445 * significant bit (LSB)
447 public Bit getInputValue()
449 return getInputValue(0);
453 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
454 * Returns the least significant bit (LSB)
457 public Bit getInputValue(int index)
459 return inputValues.getLSBit(index);
463 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
465 public BitVector getInputValues()
470 public BitVector getInputValues(int start, int end)
472 return inputValues.subVector(start, end);
476 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
478 public void clearSignals()
480 feedSignals(Z.toVector(width));
483 public BitVector wireValuesExcludingMe()
485 BitVectorMutator mutator = BitVectorMutator.empty();
486 for (ReadWriteEnd wireEnd : inputs)
490 mutator.join(wireEnd.inputValues);
492 return mutator.toBitVector();
496 public String toString()
498 return inputValues.toString();
508 void setWriting(boolean isWriting)
510 if (this.isWriting != isWriting)
512 this.isWriting = isWriting;
517 Wire.this.recalculate();
528 public String toString()
530 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
531 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
534 public static ReadEnd[] extractEnds(Wire[] w)
536 ReadEnd[] inputs = new ReadEnd[w.length];
537 for (int i = 0; i < w.length; i++)
538 inputs[i] = w[i].createReadWriteEnd();
542 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
544 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
545 * The bits are permanently fused together.
547 * @param a The {@link Wire} to be (partially) fused with b
548 * @param b The {@link Wire} to be (partially) fused with a
549 * @param fromA The first bit of {@link Wire} a to be fused
550 * @param fromB The first bit of {@link Wire} b to be fused
551 * @param length The amount of bits to fuse
553 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
555 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
556 rA.setWriting(false);
557 rB.setWriting(false);
558 rA.setValues(BitVector.of(Bit.Z, a.width));
559 rB.setValues(BitVector.of(Bit.Z, b.width));
560 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
561 rA.registerObserver(aF);
562 rB.registerObserver(bF);
569 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
570 * permanently fused together.
572 * @param a The {@link Wire} to be fused with b
573 * @param b The {@link Wire} to be fused with a
575 public static void fuse(Wire a, Wire b)
577 fuse(a, b, 0, 0, a.width);
580 private static class Fusion implements LogicObserver
582 private ReadWriteEnd target;
583 int fromSource, fromTarget, length;
585 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
587 this.target = target;
588 this.fromSource = fromSource;
589 this.fromTarget = fromTarget;
590 this.length = length;
594 public void update(LogicObservable initiator)
596 ReadWriteEnd source = (ReadWriteEnd) initiator;
597 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
598 target.setWriting(false);
601 target.setWriting(true);
602 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
603 target.setValues(fromTarget, targetInput);