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 recalculateSingleInput()
\r
56 setNewValues(inputs.get(0).getInputValues());
\r
59 private void recalculateMultipleInputs()
\r
61 BitVectorMutator mutator = BitVectorMutator.empty();
\r
62 for (ReadWriteEnd wireArrayEnd : inputs)
\r
63 mutator.join(wireArrayEnd.getInputValues());
\r
64 setNewValues(mutator.get());
\r
67 private void setNewValues(BitVector newValues)
\r
69 if (values.equals(newValues))
\r
71 // BitVector oldValues = values;
\r
78 switch (inputs.size())
\r
83 recalculateSingleInput();
\r
86 recalculateMultipleInputs();
\r
91 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
93 * @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
94 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
96 * @author Fabian Stemmler
\r
98 public boolean hasNumericValue()
\r
100 for (Bit b : values)
\r
102 if (b != Bit.ZERO && b != Bit.ONE)
\r
109 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
111 * @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
113 * @author Fabian Stemmler
\r
115 public long getUnsignedValue()
\r
119 for (Bit bit : values)
\r
126 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
138 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
140 * @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
142 * @author Fabian Stemmler
\r
144 public long getSignedValue()
\r
146 long val = getUnsignedValue();
\r
147 long mask = 1 << (length - 1);
\r
148 if ((mask & val) != 0)
\r
150 int shifts = 64 - length;
\r
151 return (val << shifts) >> shifts;
\r
156 public Bit getValue()
\r
158 return getValue(0);
\r
161 public Bit getValue(int index)
\r
163 return values.getBit(index);
\r
166 public BitVector getValues(int start, int end)
\r
168 return values.subVector(start, end);
\r
171 public BitVector getValues()
\r
177 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
\r
179 * @param ob The {@link LogicObserver} to be notified of changes.
\r
180 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
\r
182 * @author Fabian Stemmler
\r
184 void attachEnd(ReadEnd end)
\r
189 void detachEnd(ReadEnd end)
\r
191 attached.remove(end);
\r
194 private void notifyObservers()
\r
196 attached.forEach(r -> r.update());
\r
200 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
\r
202 public ReadWriteEnd createReadWriteEnd()
\r
204 return new ReadWriteEnd();
\r
208 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
\r
210 public ReadEnd createReadOnlyEnd()
\r
212 return new ReadEnd();
\r
215 void registerInput(ReadWriteEnd toRegister)
\r
217 inputs.add(toRegister);
\r
221 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
\r
222 * {@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
223 * and 1 turn into X when they are mixed
\r
225 * @author Fabian Stemmler
\r
227 public class ReadEnd implements LogicObservable
\r
229 private List<LogicObserver> observers = new ArrayList<>();
\r
234 Wire.this.attachEnd(this);
\r
237 public void update()
\r
243 * Included for convenient use on {@link Wire}s of length 1.
\r
245 * @return The value of bit 0.
\r
247 * @author Fabian Stemmler
\r
249 public Bit getValue()
\r
251 return Wire.this.getValue();
\r
255 * @param index Index of the requested bit.
\r
256 * @return The value of the indexed bit.
\r
258 * @author Fabian Stemmler
\r
260 public Bit getValue(int index)
\r
262 return Wire.this.getValue(index);
\r
266 * @param index Index of the requested bit.
\r
267 * @return The value of the indexed bit.
\r
269 * @author Fabian Stemmler
\r
271 public BitVector getValues()
\r
273 return Wire.this.getValues();
\r
277 * @param start Start of the wanted segment. (inclusive)
\r
278 * @param end End of the wanted segment. (exclusive)
\r
279 * @return The values of the segment of {@link Bit}s indexed.
\r
281 * @author Fabian Stemmler
\r
283 public BitVector getValues(int start, int end)
\r
285 return Wire.this.getValues(start, end);
\r
289 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
291 * @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
292 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
294 * @author Fabian Stemmler
\r
296 public boolean hasNumericValue()
\r
298 return Wire.this.hasNumericValue();
\r
302 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
304 * @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
306 * @author Fabian Stemmler
\r
308 public long getUnsignedValue()
\r
310 return Wire.this.getUnsignedValue();
\r
314 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
316 * @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
318 * @author Fabian Stemmler
\r
320 public long getSignedValue()
\r
322 return Wire.this.getSignedValue();
\r
326 public String toString()
\r
328 return Wire.this.toString();
\r
331 public void close()
\r
333 inputs.remove(this);
\r
338 public int length()
\r
343 public Wire getWire()
\r
349 public void registerObserver(LogicObserver ob)
\r
355 public void deregisterObserver(LogicObserver ob)
\r
357 observers.remove(ob);
\r
361 public void notifyObservers()
\r
363 observers.forEach(ob -> ob.update(this));
\r
367 public class ReadWriteEnd extends ReadEnd
\r
369 private boolean open;
\r
370 private BitVector inputValues;
\r
377 registerInput(this);
\r
380 private void initValues()
\r
382 inputValues = U.toVector(length);
\r
386 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
\r
388 * @param newValues The new values the wires should take on.
\r
390 * @author Fabian Stemmler
\r
392 public void feedSignals(Bit... newValues)
\r
394 feedSignals(BitVector.of(newValues));
\r
397 public void feedSignals(BitVector newValues)
\r
399 if (newValues.length() != length)
\r
400 throw new IllegalArgumentException(
\r
401 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
\r
403 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
404 timeline.addEvent(e -> setValues(newValues), travelTime);
\r
408 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
\r
410 * @param bitVector The new values the wires should take on.
\r
411 * @param startingBit The first index of the subarray of wires.
\r
413 * @author Fabian Stemmler
\r
415 public void feedSignals(int startingBit, BitVector bitVector)
\r
418 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
419 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
\r
423 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
424 * feedValues(...) with a delay.
\r
426 void setValues(int startingBit, BitVector newValues)
\r
428 // index check covered in equals
\r
429 if (!inputValues.equalsWithOffset(newValues, startingBit))
\r
431 Bit[] vals = inputValues.getBits();
\r
432 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
\r
433 inputValues = BitVector.of(vals);
\r
434 Wire.this.recalculate();
\r
439 * Sets the values that are being fed into the {@link Wire}. The preferred way of setting {@link ReadWriteEnd} values is via
\r
440 * feedValues(...) with a delay.
\r
442 void setValues(BitVector newValues)
\r
444 if (inputValues.equals(newValues))
\r
446 inputValues = newValues;
\r
447 Wire.this.recalculate();
\r
451 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
453 public Bit getInputValue()
\r
455 return getInputValue(0);
\r
459 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
461 public Bit getInputValue(int index)
\r
463 return inputValues.getBit(index);
\r
467 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
469 public BitVector getInputValues()
\r
471 return getInputValues(0, length);
\r
474 public BitVector getInputValues(int start, int end)
\r
476 return inputValues.subVector(start, end);
\r
480 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
\r
482 public void clearSignals()
\r
484 feedSignals(Z.toVector(length));
\r
487 public BitVector wireValuesExcludingMe()
\r
489 BitVectorMutator mutator = BitVectorMutator.empty();
\r
490 for (ReadWriteEnd wireEnd : inputs)
\r
492 if (wireEnd == this)
\r
494 mutator.join(wireEnd.inputValues);
\r
496 return mutator.get();
\r
500 public String toString()
\r
502 return inputValues.toString();
\r
507 public String toString()
\r
509 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
\r
510 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
\r
513 public static ReadEnd[] extractEnds(Wire[] w)
\r
515 ReadEnd[] inputs = new ReadEnd[w.length];
\r
516 for (int i = 0; i < w.length; i++)
\r
517 inputs[i] = w[i].createReadWriteEnd();
\r
523 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.
\r
524 * Warning: The bits are permanently fused together.
\r
526 * @param a The {@link Wire} to be (partially) fused with b
\r
527 * @param b The {@link Wire} to be (partially) fused with a
\r
528 * @param fromA The first bit of {@link Wire} a to be fused
\r
529 * @param fromB The first bit of {@link Wire} b to be fused
\r
530 * @param length The amount of bits to fuse
\r
532 public static void fuse(Wire a, Wire b, int fromA, int fromB, int length)
\r
534 ReadWriteEnd rA = a.createReadWriteEnd(), rB = b.createReadWriteEnd();
\r
535 rA.setValues(BitVector.of(Bit.Z, a.length));
\r
536 rB.setValues(BitVector.of(Bit.Z, b.length));
\r
537 rA.registerObserver(new Fusion(rB, fromA, fromB, length));
\r
538 rB.registerObserver(new Fusion(rA, fromB, fromA, length));
\r
543 * Fuses the selected bits of two wires together. If the bits change in one Wire, the other is changed accordingly immediately.
\r
544 * Warning: The bits are permanently fused together.
\r
546 * @param a The {@link Wire} to be fused with b
\r
547 * @param b The {@link Wire} to be fused with a
\r
549 public static void fuse(Wire a, Wire b)
\r
551 fuse(a, b, 0, 0, a.length);
\r
554 private static class Fusion implements LogicObserver
\r
556 private ReadWriteEnd target;
\r
557 int fromSource, fromTarget, length;
\r
559 public Fusion(ReadWriteEnd target, int fromSource, int fromTarget, int length)
\r
561 this.target = target;
\r
562 this.fromSource = fromSource;
\r
563 this.fromTarget = fromTarget;
\r
564 this.length = length;
\r
568 public void update(LogicObservable initiator)
\r
570 ReadWriteEnd read = (ReadWriteEnd) initiator;
\r
571 target.setValues(fromTarget, read.wireValuesExcludingMe().subVector(fromSource, fromSource + length));
\r