1 package net.mograsim.logic.core.wires;
\r
3 import static net.mograsim.logic.core.types.Bit.U;
\r
4 import static net.mograsim.logic.core.types.Bit.Z;
\r
6 import java.util.ArrayList;
\r
7 import java.util.List;
\r
9 import net.mograsim.logic.core.LogicObservable;
\r
10 import net.mograsim.logic.core.LogicObserver;
\r
11 import net.mograsim.logic.core.timeline.Timeline;
\r
12 import net.mograsim.logic.core.types.Bit;
\r
13 import net.mograsim.logic.core.types.BitVector;
\r
14 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
\r
17 * Represents an array of wires that can store n bits of information.
\r
19 * @author Fabian Stemmler
\r
24 public final String name;
\r
25 private BitVector values;
\r
26 public final int travelTime;
\r
27 private List<ReadEnd> attached = new ArrayList<>();
\r
28 public final int length;
\r
29 List<ReadWriteEnd> inputs = new ArrayList<>();
\r
32 public Wire(Timeline timeline, int length, int travelTime)
\r
34 this(timeline, length, travelTime, null);
\r
37 public Wire(Timeline timeline, int length, int travelTime, String name)
\r
40 throw new IllegalArgumentException(
\r
41 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
43 this.timeline = timeline;
\r
44 this.length = length;
\r
45 this.travelTime = travelTime;
\r
49 private void initValues()
\r
51 values = U.toVector(length);
\r
54 private void setNewValues(BitVector newValues)
\r
56 if (values.equals(newValues))
\r
58 // BitVector oldValues = values;
\r
65 if (inputs.size() == 0)
\r
66 setNewValues(BitVector.of(Bit.U, length));
\r
69 BitVectorMutator mutator = BitVectorMutator.empty();
\r
70 for (ReadWriteEnd wireArrayEnd : inputs)
\r
71 mutator.join(wireArrayEnd.getInputValues());
\r
72 setNewValues(mutator.toBitVector());
\r
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.
\r
78 * WARNING! Use this with care! The preferred way of writing the values is ReadWriteEnd.feedSignals(BitVector)
\r
80 * @param values The values the <code>Wire</code> will have immediately after this method is called
\r
82 public void forceValues(BitVector values)
\r
84 setNewValues(values);
\r
88 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
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
\r
91 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
93 * @author Fabian Stemmler
\r
95 public boolean hasNumericValue()
\r
97 for (Bit b : values)
\r
99 if (b != Bit.ZERO && b != Bit.ONE)
\r
106 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
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.
\r
110 * @author Fabian Stemmler
\r
112 public long getUnsignedValue()
\r
116 for (Bit bit : values)
\r
123 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
135 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
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.
\r
139 * @author Fabian Stemmler
\r
141 public long getSignedValue()
\r
143 long val = getUnsignedValue();
\r
144 long mask = 1 << (length - 1);
\r
145 if ((mask & val) != 0)
\r
147 int shifts = 64 - length;
\r
148 return (val << shifts) >> shifts;
\r
154 * Returns the least significant bit (LSB)
\r
156 public Bit getValue()
\r
158 return getValue(0);
\r
162 * Returns the least significant bit (LSB) of the given index
\r
164 public Bit getValue(int index)
\r
166 return values.getLSBit(index);
\r
169 public BitVector getValues(int start, int end)
\r
171 return values.subVector(start, end);
\r
174 public BitVector getValues()
\r
180 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
\r
182 * @param ob The {@link LogicObserver} to be notified of changes.
\r
183 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
\r
185 * @author Fabian Stemmler
\r
187 void attachEnd(ReadEnd end)
\r
192 void detachEnd(ReadEnd end)
\r
194 attached.remove(end);
\r
197 private void notifyObservers()
\r
199 attached.forEach(r -> r.update());
\r
203 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
\r
205 public ReadWriteEnd createReadWriteEnd()
\r
207 return new ReadWriteEnd();
\r
211 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
\r
213 public ReadEnd createReadOnlyEnd()
\r
215 return new ReadEnd();
\r
218 void registerInput(ReadWriteEnd toRegister)
\r
220 inputs.add(toRegister);
\r
224 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
\r
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
\r
226 * and 1 turn into X when they are mixed
\r
228 * @author Fabian Stemmler
\r
230 public class ReadEnd implements LogicObservable
\r
232 private List<LogicObserver> observers = new ArrayList<>();
\r
237 Wire.this.attachEnd(this);
\r
240 public void update()
\r
246 * Included for convenient use on {@link Wire}s of length 1.
\r
248 * @return The value of bit 0.
\r
250 * @author Fabian Stemmler
\r
252 public Bit getValue()
\r
254 return Wire.this.getValue();
\r
258 * @param index Index of the requested bit.
\r
259 * @return The value of the indexed bit.
\r
261 * @author Fabian Stemmler
\r
263 public Bit getValue(int index)
\r
265 return Wire.this.getValue(index);
\r
268 public BitVector getValues()
\r
270 return Wire.this.getValues();
\r
274 * @param start Start of the wanted segment. (inclusive)
\r
275 * @param end End of the wanted segment. (exclusive)
\r
276 * @return The values of the segment of {@link Bit}s indexed.
\r
278 * @author Fabian Stemmler
\r
280 public BitVector getValues(int start, int end)
\r
282 return Wire.this.getValues(start, end);
\r
286 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
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
\r
289 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
291 * @author Fabian Stemmler
\r
293 public boolean hasNumericValue()
\r
295 return Wire.this.hasNumericValue();
\r
299 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
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.
\r
303 * @author Fabian Stemmler
\r
305 public long getUnsignedValue()
\r
307 return Wire.this.getUnsignedValue();
\r
311 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
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.
\r
315 * @author Fabian Stemmler
\r
317 public long getSignedValue()
\r
319 return Wire.this.getSignedValue();
\r
323 public String toString()
\r
325 return Wire.this.toString();
\r
328 public void close()
\r
330 inputs.remove(this);
\r
335 public int length()
\r
340 public Wire getWire()
\r
346 public void registerObserver(LogicObserver ob)
\r
352 public void deregisterObserver(LogicObserver ob)
\r
354 observers.remove(ob);
\r
358 public void notifyObservers()
\r
360 observers.forEach(ob -> ob.update(this));
\r
364 public class ReadWriteEnd extends ReadEnd
\r
366 private boolean open, isWriting;
\r
367 private BitVector inputValues;
\r
375 registerInput(this);
\r
378 private void initValues()
\r
380 inputValues = U.toVector(length);
\r
384 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
\r
386 * @param newValues The new values the wires should take on.
\r
388 * @author Fabian Stemmler
\r
390 public void feedSignals(Bit... newValues)
\r
392 feedSignals(BitVector.of(newValues));
\r
395 public void feedSignals(BitVector newValues)
\r
397 if (newValues.length() != length)
\r
398 throw new IllegalArgumentException(
\r
399 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
\r
401 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
402 timeline.addEvent(e -> setValues(newValues), travelTime);
\r
406 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
\r
408 * @param bitVector The new values the wires should take on.
\r
409 * @param startingBit The first index of the subarray of wires.
\r
411 * @author Fabian Stemmler
\r
413 public void feedSignals(int startingBit, BitVector bitVector)
\r
416 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
417 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
\r
421 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
422 * feedValues(...) with a delay.
\r
424 void setValues(int startingBit, BitVector newValues)
\r
426 // index check covered in equals
\r
427 if (!inputValues.equalsWithOffset(newValues, startingBit))
\r
429 Bit[] vals = inputValues.getBits();
\r
430 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
\r
431 inputValues = BitVector.of(vals);
\r
432 Wire.this.recalculate();
\r
437 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
438 * feedValues(...) with a delay.
\r
440 void setValues(BitVector newValues)
\r
442 if (inputValues.equals(newValues))
\r
444 inputValues = newValues;
\r
445 Wire.this.recalculate();
\r
449 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
\r
450 * significant bit (LSB)
\r
452 public Bit getInputValue()
\r
454 return getInputValue(0);
\r
458 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
459 * Returns the least significant bit (LSB)
\r
462 public Bit getInputValue(int index)
\r
464 return inputValues.getLSBit(index);
\r
468 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
470 public BitVector getInputValues()
\r
472 return inputValues;
\r
475 public BitVector getInputValues(int start, int end)
\r
477 return inputValues.subVector(start, end);
\r
481 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
\r
483 public void clearSignals()
\r
485 feedSignals(Z.toVector(length));
\r
488 public BitVector wireValuesExcludingMe()
\r
490 BitVectorMutator mutator = BitVectorMutator.empty();
\r
491 for (ReadWriteEnd wireEnd : inputs)
\r
493 if (wireEnd == this)
\r
495 mutator.join(wireEnd.inputValues);
\r
497 return mutator.toBitVector();
\r
501 public String toString()
\r
503 return inputValues.toString();
\r
507 public void close()
\r
513 void setWriting(boolean isWriting)
\r
515 if (this.isWriting != isWriting)
\r
517 this.isWriting = isWriting;
\r
521 inputs.remove(this);
\r
522 Wire.this.recalculate();
\r
526 boolean isWriting()
\r
533 public String toString()
\r
535 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
\r
536 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
\r
539 public static ReadEnd[] extractEnds(Wire[] w)
\r
541 ReadEnd[] inputs = new ReadEnd[w.length];
\r
542 for (int i = 0; i < w.length; i++)
\r
543 inputs[i] = w[i].createReadWriteEnd();
\r
547 // TODO Fix ReadWriteEnd feeding signals to entire Wire (Z) instead of only selected Bits
\r
549 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning:
\r
550 * The bits are permanently fused together.
\r
552 * @param a The {@link Wire} to be (partially) fused with b
\r
553 * @param b The {@link Wire} to be (partially) fused with a
\r
554 * @param fromA The first bit of {@link Wire} a to be fused
\r
555 * @param fromB The first bit of {@link Wire} b to be fused
\r
556 * @param length The amount of bits to fuse
\r
558 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
\r
560 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
\r
561 rA.setWriting(false);
\r
562 rB.setWriting(false);
\r
563 rA.setValues(BitVector.of(Bit.Z, a.length));
\r
564 rB.setValues(BitVector.of(Bit.Z, b.length));
\r
565 Fusion aF = new Fusion(rB, fromA, fromB, length), bF = new Fusion(rA, fromB, fromA, length);
\r
566 rA.registerObserver(aF);
\r
567 rB.registerObserver(bF);
\r
574 * Fuses two wires together. If the bits change in one Wire, the other is changed accordingly immediately. Warning: The bits are
\r
575 * permanently fused together.
\r
577 * @param a The {@link Wire} to be fused with b
\r
578 * @param b The {@link Wire} to be fused with a
\r
580 public static void fuse(Wire a, Wire b)
\r
582 fuse(a, b, 0, 0, a.length);
\r
585 private static class Fusion implements LogicObserver
\r
587 private ReadWriteEnd target;
\r
588 int fromSource, fromTarget, length;
\r
590 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
\r
592 this.target = target;
\r
593 this.fromSource = fromSource;
\r
594 this.fromTarget = fromTarget;
\r
595 this.length = length;
\r
599 public void update(LogicObservable initiator)
\r
601 ReadWriteEnd source = (ReadWriteEnd) initiator;
\r
602 if (source.getWire().inputs.size() - (source.isWriting() ? 1 : 0) == 0)
\r
603 target.setWriting(false);
\r
606 target.setWriting(true);
\r
607 BitVector targetInput = source.wireValuesExcludingMe().subVector(fromSource, fromSource + length);
\r
608 target.setValues(fromTarget, targetInput);
\r