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 recalculateSingleInput()
56 setNewValues(inputs.get(0).getInputValues());
59 private void recalculateMultipleInputs()
61 BitVectorMutator mutator = BitVectorMutator.empty();
62 for (ReadWriteEnd wireArrayEnd : inputs)
63 mutator.join(wireArrayEnd.getInputValues());
64 setNewValues(mutator.get());
67 private void setNewValues(BitVector newValues)
69 if (values.equals(newValues))
71 // BitVector oldValues = values;
78 switch (inputs.size())
83 recalculateSingleInput();
86 recalculateMultipleInputs();
91 * The {@link Wire} is interpreted as an unsigned integer with n bits.
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
94 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
96 * @author Fabian Stemmler
98 public boolean hasNumericValue()
102 if (b != Bit.ZERO && b != Bit.ONE)
109 * The {@link Wire} is interpreted as an unsigned integer with n bits.
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.
113 * @author Fabian Stemmler
115 public long getUnsignedValue()
119 for (Bit bit : values)
126 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
138 * The {@link Wire} is interpreted as a signed integer with n bits.
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.
142 * @author Fabian Stemmler
144 public long getSignedValue()
146 long val = getUnsignedValue();
147 long mask = 1 << (length - 1);
148 if ((mask & val) != 0)
150 int shifts = 64 - length;
151 return (val << shifts) >> shifts;
156 public Bit getValue()
161 public Bit getValue(int index)
163 return values.getBit(index);
166 public BitVector getValues(int start, int end)
168 return values.subVector(start, end);
171 public BitVector getValues()
177 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
179 * @param ob The {@link LogicObserver} to be notified of changes.
180 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
182 * @author Fabian Stemmler
184 void attachEnd(ReadEnd end)
189 void detachEnd(ReadEnd end)
191 attached.remove(end);
194 private void notifyObservers()
196 attached.forEach(r -> r.update());
200 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
202 public ReadWriteEnd createReadWriteEnd()
204 return new ReadWriteEnd();
208 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
210 public ReadEnd createReadOnlyEnd()
212 return new ReadEnd();
215 void registerInput(ReadWriteEnd toRegister)
217 inputs.add(toRegister);
221 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
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
223 * and 1 turn into X when they are mixed
225 * @author Fabian Stemmler
227 public class ReadEnd implements LogicObservable
229 private List<LogicObserver> observers = new ArrayList<>();
234 Wire.this.attachEnd(this);
243 * Included for convenient use on {@link Wire}s of length 1.
245 * @return The value of bit 0.
247 * @author Fabian Stemmler
249 public Bit getValue()
251 return Wire.this.getValue();
255 * @param index Index of the requested bit.
256 * @return The value of the indexed bit.
258 * @author Fabian Stemmler
260 public Bit getValue(int index)
262 return Wire.this.getValue(index);
266 * @param index Index of the requested bit.
267 * @return The value of the indexed bit.
269 * @author Fabian Stemmler
271 public BitVector getValues()
273 return Wire.this.getValues();
277 * @param start Start of the wanted segment. (inclusive)
278 * @param end End of the wanted segment. (exclusive)
279 * @return The values of the segment of {@link Bit}s indexed.
281 * @author Fabian Stemmler
283 public BitVector getValues(int start, int end)
285 return Wire.this.getValues(start, end);
289 * The {@link Wire} is interpreted as an unsigned integer with n bits.
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
292 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
294 * @author Fabian Stemmler
296 public boolean hasNumericValue()
298 return Wire.this.hasNumericValue();
302 * The {@link Wire} is interpreted as an unsigned integer with n bits.
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.
306 * @author Fabian Stemmler
308 public long getUnsignedValue()
310 return Wire.this.getUnsignedValue();
314 * The {@link Wire} is interpreted as a signed integer with n bits.
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.
318 * @author Fabian Stemmler
320 public long getSignedValue()
322 return Wire.this.getSignedValue();
326 public String toString()
328 return Wire.this.toString();
343 public Wire getWire()
349 public void registerObserver(LogicObserver ob)
355 public void deregisterObserver(LogicObserver ob)
357 observers.remove(ob);
361 public void notifyObservers()
363 observers.forEach(ob -> ob.update(this));
367 public class ReadWriteEnd extends ReadEnd
369 private boolean open;
370 private BitVector inputValues;
380 private void initValues()
382 inputValues = U.toVector(length);
386 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
388 * @param newValues The new values the wires should take on.
390 * @author Fabian Stemmler
392 public void feedSignals(Bit... newValues)
394 feedSignals(BitVector.of(newValues));
397 public void feedSignals(BitVector newValues)
399 if (newValues.length() != length)
400 throw new IllegalArgumentException(
401 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
403 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
404 timeline.addEvent(e -> setValues(newValues), travelTime);
408 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
410 * @param bitVector The new values the wires should take on.
411 * @param startingBit The first index of the subarray of wires.
413 * @author Fabian Stemmler
415 public void feedSignals(int startingBit, BitVector bitVector)
418 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
419 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
422 private void setValues(int startingBit, BitVector newValues)
424 // index check covered in equals
425 if (!inputValues.equalsWithOffset(newValues, startingBit))
427 Bit[] vals = inputValues.getBits();
428 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
429 inputValues = BitVector.of(vals);
430 Wire.this.recalculate();
434 private void setValues(BitVector newValues)
436 if (inputValues.equals(newValues))
438 inputValues = newValues;
439 Wire.this.recalculate();
443 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
445 public Bit getInputValue()
447 return getInputValue(0);
451 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
453 public Bit getInputValue(int index)
455 return inputValues.getBit(index);
459 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
461 public BitVector getInputValues()
463 return getInputValues(0, length);
466 public BitVector getInputValues(int start, int end)
468 return inputValues.subVector(start, end);
472 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
474 public void clearSignals()
476 feedSignals(Z.toVector(length));
479 public BitVector wireValuesExcludingMe()
481 BitVectorMutator mutator = BitVectorMutator.empty();
482 for (ReadWriteEnd wireEnd : inputs)
486 mutator.join(wireEnd.inputValues);
488 return mutator.get();
492 public String toString()
494 return inputValues.toString();
499 public String toString()
501 String name = this.name == null ? String.format("0x%08x", hashCode()) : this.name;
502 return String.format("wire %s value: %s inputs: %s", name, values, inputs);
505 public static ReadEnd[] extractEnds(Wire[] w)
507 ReadEnd[] inputs = new ReadEnd[w.length];
508 for (int i = 0; i < w.length; i++)
509 inputs[i] = w[i].createReadWriteEnd();