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);
268 public BitVector getValues()
270 return Wire.this.getValues();
274 * @param start Start of the wanted segment. (inclusive)
275 * @param end End of the wanted segment. (exclusive)
276 * @return The values of the segment of {@link Bit}s indexed.
278 * @author Fabian Stemmler
280 public BitVector getValues(int start, int end)
282 return Wire.this.getValues(start, end);
286 * The {@link Wire} is interpreted as an unsigned integer with n bits.
288 * @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
289 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
291 * @author Fabian Stemmler
293 public boolean hasNumericValue()
295 return Wire.this.hasNumericValue();
299 * The {@link Wire} is interpreted as an unsigned integer with n bits.
301 * @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.
303 * @author Fabian Stemmler
305 public long getUnsignedValue()
307 return Wire.this.getUnsignedValue();
311 * The {@link Wire} is interpreted as a signed integer with n bits.
313 * @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.
315 * @author Fabian Stemmler
317 public long getSignedValue()
319 return Wire.this.getSignedValue();
323 public String toString()
325 return Wire.this.toString();
340 public Wire getWire()
346 public void registerObserver(LogicObserver ob)
352 public void deregisterObserver(LogicObserver ob)
354 observers.remove(ob);
358 public void notifyObservers()
360 observers.forEach(ob -> ob.update(this));
364 public class ReadWriteEnd extends ReadEnd
366 private boolean open, isWriting;
367 private BitVector inputValues;
378 private void initValues()
380 inputValues = U.toVector(length);
384 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
386 * @param newValues The new values the wires should take on.
388 * @author Fabian Stemmler
390 public void feedSignals(Bit... newValues)
392 feedSignals(BitVector.of(newValues));
395 public void feedSignals(BitVector newValues)
397 if (newValues.length() != length)
398 throw new IllegalArgumentException(
399 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
401 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
402 timeline.addEvent(e -> setValues(newValues), travelTime);
406 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
408 * @param bitVector The new values the wires should take on.
409 * @param startingBit The first index of the subarray of wires.
411 * @author Fabian Stemmler
413 public void feedSignals(int startingBit, BitVector bitVector)
416 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
417 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
421 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
422 * feedValues(...) with a delay.
424 void setValues(int startingBit, BitVector newValues)
426 // index check covered in equals
427 if (!inputValues.equalsWithOffset(newValues, startingBit))
429 Bit[] vals = inputValues.getBits();
430 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
431 inputValues = BitVector.of(vals);
432 Wire.this.recalculate();
437 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
438 * feedValues(...) with a delay.
440 void setValues(BitVector newValues)
442 if (inputValues.equals(newValues))
444 inputValues = newValues;
445 Wire.this.recalculate();
449 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
450 * significant bit (LSB)
452 public Bit getInputValue()
454 return getInputValue(0);
458 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
459 * Returns the least significant bit (LSB)
462 public Bit getInputValue(int index)
464 return inputValues.getLSBit(index);
468 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
470 public BitVector getInputValues()
475 public BitVector getInputValues(int start, int end)
477 return inputValues.subVector(start, end);
481 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
483 public void clearSignals()
485 feedSignals(Z.toVector(length));
488 public BitVector wireValuesExcludingMe()
490 BitVectorMutator mutator = BitVectorMutator.empty();
491 for (ReadWriteEnd wireEnd : inputs)
495 mutator.join(wireEnd.inputValues);
497 return mutator.toBitVector();
501 public String toString()
503 return inputValues.toString();
513 void setWriting(boolean isWriting)
515 if (this.isWriting != isWriting)
517 this.isWriting = isWriting;
522 Wire.this.recalculate();
533 public String toString()
535 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
536 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
539 public static ReadEnd[] extractEnds(Wire[] w)
541 ReadEnd[] inputs = new ReadEnd[w.length];
542 for (int i = 0; i < w.length; i++)
543 inputs[i] = w[i].createReadWriteEnd();
547 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
549 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
550 * The bits are permanently fused together.
552 * @param a The {@link Wire} to be (partially) fused with b
553 * @param b The {@link Wire} to be (partially) fused with a
554 * @param fromA The first bit of {@link Wire} a to be fused
555 * @param fromB The first bit of {@link Wire} b to be fused
556 * @param length The amount of bits to fuse
558 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
560 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
561 rA.setWriting(false);
562 rB.setWriting(false);
563 rA.setValues(BitVector.of(Bit.Z, a.length));
564 rB.setValues(BitVector.of(Bit.Z, b.length));
565 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
566 rA.registerObserver(aF);
567 rB.registerObserver(bF);
574 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
575 * permanently fused together.
577 * @param a The {@link Wire} to be fused with b
578 * @param b The {@link Wire} to be fused with a
580 public static void fuse(Wire a, Wire b)
582 fuse(a, b, 0, 0, a.length);
585 private static class Fusion implements LogicObserver
587 private ReadWriteEnd target;
588 int fromSource, fromTarget, length;
590 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
592 this.target = target;
593 this.fromSource = fromSource;
594 this.fromTarget = fromTarget;
595 this.length = length;
599 public void update(LogicObservable initiator)
601 ReadWriteEnd source = (ReadWriteEnd) initiator;
602 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
603 target.setWriting(false);
606 target.setWriting(true);
607 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
608 target.setValues(fromTarget, targetInput);