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 private BitVector values;
\r
25 public final int travelTime;
\r
26 private List<ReadEnd> attached = new ArrayList<ReadEnd>();
\r
27 public final int length;
\r
28 private List<ReadWriteEnd> inputs = new ArrayList<ReadWriteEnd>();
\r
29 private Timeline timeline;
\r
31 public Wire(Timeline timeline, int length, int travelTime)
\r
34 throw new IllegalArgumentException(
\r
35 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
36 this.timeline = timeline;
\r
37 this.length = length;
\r
38 this.travelTime = travelTime;
\r
42 private void initValues()
\r
44 values = U.toVector(length);
\r
47 private void recalculateSingleInput()
\r
49 setNewValues(inputs.get(0).getInputValues());
\r
52 private void recalculateMultipleInputs()
\r
54 BitVectorMutator mutator = BitVectorMutator.empty();
\r
55 for (ReadWriteEnd wireArrayEnd : inputs)
\r
56 mutator.join(wireArrayEnd.getInputValues());
\r
57 setNewValues(mutator.get());
\r
60 private void setNewValues(BitVector newValues)
\r
62 if (values.equals(newValues))
\r
64 BitVector oldValues = values;
\r
66 notifyObservers(oldValues);
\r
69 private void recalculate()
\r
71 switch (inputs.size())
\r
76 recalculateSingleInput();
\r
79 recalculateMultipleInputs();
\r
84 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
86 * @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
87 * value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
89 * @author Fabian Stemmler
\r
91 public boolean hasNumericValue()
\r
93 for (Bit b : values)
\r
95 if (b != Bit.ZERO && b != Bit.ONE)
\r
102 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
104 * @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
106 * @author Fabian Stemmler
\r
108 public long getUnsignedValue()
\r
112 for (Bit bit : values)
\r
119 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
131 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
133 * @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
135 * @author Fabian Stemmler
\r
137 public long getSignedValue()
\r
139 long val = getUnsignedValue();
\r
140 long mask = 1 << (length - 1);
\r
141 if ((mask & val) != 0)
\r
143 int shifts = 64 - length;
\r
144 return (val << shifts) >> shifts;
\r
149 public Bit getValue()
\r
151 return getValue(0);
\r
154 public Bit getValue(int index)
\r
156 return values.getBit(index);
\r
159 public BitVector getValues(int start, int end)
\r
161 return values.subVector(start, end);
\r
164 public BitVector getValues()
\r
170 * Adds an {@link LogicObserver}, who will be notified when the value of the {@link Wire} is updated.
\r
172 * @param ob The {@link LogicObserver} to be notified of changes.
\r
173 * @return true if the given {@link LogicObserver} was not already registered, false otherwise
\r
175 * @author Fabian Stemmler
\r
177 private void attachEnd(ReadEnd end)
\r
182 private void detachEnd(ReadEnd end)
\r
184 attached.remove(end);
\r
187 private void notifyObservers(BitVector oldValues)
\r
189 for (ReadEnd o : attached)
\r
190 o.update(oldValues);
\r
194 * Create and register a {@link ReadWriteEnd} object, which is tied to this {@link Wire}. This {@link ReadWriteEnd} can be written to.
\r
196 public ReadWriteEnd createReadWriteEnd()
\r
198 return new ReadWriteEnd();
\r
202 * Create a {@link ReadEnd} object, which is tied to this {@link Wire}. This {@link ReadEnd} cannot be written to.
\r
204 public ReadEnd createReadOnlyEnd()
\r
206 return new ReadEnd();
\r
209 private void registerInput(ReadWriteEnd toRegister)
\r
211 inputs.add(toRegister);
\r
215 * A {@link ReadEnd} feeds a constant signal into the {@link Wire} it is tied to. The combination of all inputs determines the
\r
216 * {@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
217 * and 1 turn into X when they are mixed
\r
219 * @author Fabian Stemmler
\r
221 public class ReadEnd implements LogicObservable
\r
223 private List<LogicObserver> observers = new ArrayList<LogicObserver>();
\r
228 Wire.this.attachEnd(this);
\r
231 public void update(BitVector oldValues)
\r
237 * Included for convenient use on {@link Wire}s of length 1.
\r
239 * @return The value of bit 0.
\r
241 * @author Fabian Stemmler
\r
243 public Bit getValue()
\r
245 return Wire.this.getValue();
\r
249 * @param index Index of the requested bit.
\r
250 * @return The value of the indexed bit.
\r
252 * @author Fabian Stemmler
\r
254 public Bit getValue(int index)
\r
256 return Wire.this.getValue(index);
\r
260 * @param index Index of the requested bit.
\r
261 * @return The value of the indexed bit.
\r
263 * @author Fabian Stemmler
\r
265 public BitVector getValues()
\r
267 return Wire.this.getValues();
\r
271 * @param start Start of the wanted segment. (inclusive)
\r
272 * @param end End of the wanted segment. (exclusive)
\r
273 * @return The values of the segment of {@link Bit}s indexed.
\r
275 * @author Fabian Stemmler
\r
277 public BitVector getValues(int start, int end)
\r
279 return Wire.this.getValues(start, end);
\r
283 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
285 * @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
286 * same value), not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is returned otherwise.
\r
288 * @author Fabian Stemmler
\r
290 public boolean hasNumericValue()
\r
292 return Wire.this.hasNumericValue();
\r
296 * The {@link Wire} is interpreted as an unsigned integer with n bits.
\r
298 * @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
300 * @author Fabian Stemmler
\r
302 public long getUnsignedValue()
\r
304 return Wire.this.getUnsignedValue();
\r
308 * The {@link Wire} is interpreted as a signed integer with n bits.
\r
310 * @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
312 * @author Fabian Stemmler
\r
314 public long getSignedValue()
\r
316 return Wire.this.getSignedValue();
\r
320 public String toString()
\r
322 return Wire.this.toString();
\r
325 public void close()
\r
327 inputs.remove(this);
\r
332 public int length()
\r
337 public Wire getWire()
\r
343 public void registerObserver(LogicObserver ob)
\r
349 public void notifyObservers()
\r
351 for (LogicObserver ob : observers)
\r
356 public class ReadWriteEnd extends ReadEnd
\r
358 private boolean open;
\r
359 private BitVector inputValues;
\r
361 private ReadWriteEnd()
\r
366 registerInput(this);
\r
369 private void initValues()
\r
371 inputValues = U.toVector(length);
\r
375 * Sets the wires values. This takes up time, as specified by the {@link Wire}s travel time.
\r
377 * @param newValues The new values the wires should take on.
\r
379 * @author Fabian Stemmler
\r
381 public void feedSignals(Bit... newValues)
\r
383 feedSignals(BitVector.of(newValues));
\r
386 public void feedSignals(BitVector newValues)
\r
388 if (newValues.length() != length)
\r
389 throw new IllegalArgumentException(
\r
390 String.format("Attempted to input %d bits instead of %d bits.", newValues.length(), length));
\r
392 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
393 timeline.addEvent(e -> setValues(newValues), travelTime);
\r
397 * Sets values of a subarray of wires. This takes up time, as specified by the {@link Wire}s travel time.
\r
399 * @param bitVector The new values the wires should take on.
\r
400 * @param startingBit The first index of the subarray of wires.
\r
402 * @author Fabian Stemmler
\r
404 public void feedSignals(int startingBit, BitVector bitVector)
\r
407 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
408 timeline.addEvent(e -> setValues(startingBit, bitVector), travelTime);
\r
411 private void setValues(int startingBit, BitVector newValues)
\r
413 // index check covered in equals
\r
414 if (!inputValues.equalsWithOffset(newValues, startingBit))
\r
416 Bit[] vals = inputValues.getBits();
\r
417 System.arraycopy(newValues.getBits(), 0, vals, startingBit, newValues.length());
\r
418 inputValues = BitVector.of(vals);
\r
419 Wire.this.recalculate();
\r
423 private void setValues(BitVector newValues)
\r
425 if (inputValues.equals(newValues))
\r
427 inputValues = newValues;
\r
428 Wire.this.recalculate();
\r
432 * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
434 public Bit getInputValue()
\r
436 return getInputValue(0);
\r
440 * @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
442 public Bit getInputValue(int index)
\r
444 return inputValues.getBit(index);
\r
448 * @return A copy (safe to modify) of the values the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
450 public BitVector getInputValues()
\r
452 return getInputValues(0, length);
\r
455 public BitVector getInputValues(int start, int end)
\r
457 return inputValues.subVector(start, end);
\r
461 * {@link ReadEnd} now feeds Z into the associated {@link Wire}.
\r
463 public void clearSignals()
\r
465 feedSignals(Z.toVector(length));
\r
468 public BitVector wireValuesExcludingMe()
\r
470 BitVectorMutator mutator = BitVectorMutator.empty();
\r
471 for (ReadWriteEnd wireEnd : inputs)
\r
473 if (wireEnd == this)
\r
475 mutator.join(wireEnd.inputValues);
\r
477 return mutator.get();
\r
481 public String toString()
\r
483 return inputValues.toString();
\r
488 public String toString()
\r
490 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), values, inputs);
\r
493 public static ReadEnd[] extractEnds(Wire[] w)
\r
495 ReadEnd[] inputs = new ReadEnd[w.length];
\r
496 for (int i = 0; i < w.length; i++)
\r
497 inputs[i] = w[i].createReadWriteEnd();
\r