1 package era.mi.logic.wires;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Iterator;
8 import era.mi.logic.Bit;
9 import era.mi.logic.Simulation;
12 * Represents an array of wires that can store n bits of information.
14 * @author Fabian Stemmler
17 public class WireArray
20 public final int travelTime;
21 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
22 public final int length;
23 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
25 public WireArray(int length, int travelTime)
28 throw new IllegalArgumentException("Tried to create an array of wires with length " + length
29 + ", but a length of less than 1 makes no sense.");
31 this.travelTime = travelTime;
35 private void initValues()
37 values = new Bit[length];
38 for (int i = 0; i < length; i++)
42 private void recalculateSingleInput()
44 WireArrayInput input = inputs.get(0);
45 if (!Arrays.equals(input.getValues(), values))
47 System.arraycopy(input.getValues(), 0, values, 0, length);
52 private void recalculateMultipleInputs()
54 Iterator<WireArrayInput> it = inputs.iterator();
55 Bit[] newValues = it.next().values.clone();
59 WireArrayInput input = it.next();
60 Bit[] bits = input.getValues();
61 for (int i = 0; i < length; i++)
63 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
65 else if (Bit.Z.equals(newValues[i]))
66 newValues[i] = bits[i];
72 if (!Arrays.equals(newValues, values))
79 private void recalculate()
81 switch (inputs.size())
86 recalculateSingleInput();
89 recalculateMultipleInputs();
94 * The WireArray is interpreted as an unsigned integer with n bits.
96 * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or
97 * <code>Bit.ZERO</code> (they do not all have to have the same value),
98 * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is
101 * @author Fabian Stemmler
103 public boolean hasNumericValue()
107 if (b != Bit.ZERO && b != Bit.ONE)
114 * The WireArray is interpreted as an unsigned integer with n bits.
116 * @return The unsigned value of the {@link WireArray}'s bits, where value 0
117 * corresponds with 2^0, value 1 is 2^1 and so on.
119 * @author Fabian Stemmler
121 public long getUnsignedValue()
125 for (int i = 0; i < length; i++)
132 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
145 * The WireArray is interpreted as a signed integer with n bits.
147 * @return The signed value of the {@link WireArray}'s bits, where value 0
148 * corresponds with 2^0, value 1 is 2^1 and so on.
150 * @author Fabian Stemmler
152 public long getSignedValue()
154 long val = getUnsignedValue();
155 long mask = 1 << (length - 1);
156 if ((mask & val) != 0)
158 int shifts = 64 - length;
159 return (val << shifts) >> shifts;
165 * Included for convenient use on {@link WireArray}s of length 1.
167 * @return The value of bit 0.
169 * @author Fabian Stemmler
171 public Bit getValue()
178 * @param index Index of the requested bit.
179 * @return The value of the indexed bit.
181 * @author Fabian Stemmler
183 public Bit getValue(int index)
185 return values[index];
188 public Bit[] getValues(int start, int end)
190 int length = end - start;
191 Bit[] bits = new Bit[length];
192 System.arraycopy(values, start, bits, 0, length);
197 * @return An array of length n containing the values of the n bits in the
198 * {@link WireArray}. Can be safely modified.
200 * @author Fabian Stemmler
202 public Bit[] getValues()
204 return values.clone();
208 * Adds an {@link WireArrayObserver}, who will be notified when the value of the
209 * {@link WireArray} is updated.
211 * @param ob The {@link WireArrayObserver} to be notified of changes.
212 * @return true if the given {@link WireArrayObserver} was not already
213 * registered, false otherwise
215 * @author Fabian Stemmler
217 public boolean addObserver(WireArrayObserver ob)
219 return observers.add(ob);
222 private void notifyObservers()
224 for (WireArrayObserver o : observers)
229 * Create and register a {@link WireArrayInput} object, which is tied to this
232 public WireArrayInput createInput()
234 return new WireArrayInput(this);
237 private void registerInput(WireArrayInput toRegister)
239 inputs.add(toRegister);
243 * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
244 * it is tied to. The combination of all inputs determines the
245 * {@link WireArray}s final value. X dominates all other inputs Z does not
246 * affect the final value, unless there are no other inputs than Z 0 and 1 turn
247 * into X when they are mixed
249 * @author Fabian Stemmler
251 public class WireArrayInput
253 public final WireArray owner;
254 private Bit[] values;
256 private WireArrayInput(WireArray owner)
261 owner.registerInput(this);
264 private void initValues()
266 values = new Bit[length];
267 for (int i = 0; i < length; i++)
272 * Sets the wires values. This takes up time, as specified by the
273 * {@link WireArray}s travel time.
275 * @param newValues The new values the wires should take on.
277 * @author Fabian Stemmler
279 public void feedSignals(Bit... newValues)
281 if (newValues.length == length)
283 feedSignals(0, newValues);
285 throw new IllegalArgumentException(
286 "Attempted to input " + newValues.length + " bits instead of " + length + " bits.");
290 * Sets values of a subarray of wires. This takes up time, as specified by the
291 * {@link WireArray}s travel time.
293 * @param newValues The new values the wires should take on.
294 * @param startingBit The first index of the subarray of wires.
296 * @author Fabian Stemmler
298 public void feedSignals(int startingBit, Bit... newValues)
300 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
303 private void setValues(int startingBit, Bit... newValues)
305 int exclLastIndex = startingBit + newValues.length;
306 if (length < exclLastIndex)
307 throw new ArrayIndexOutOfBoundsException("Attempted to input bits from index " + startingBit + " to "
308 + exclLastIndex + " when there are only " + length + "wires.");
309 if (!Arrays.equals(values, startingBit, exclLastIndex, newValues, 0, newValues.length))
311 System.arraycopy(newValues, 0, values, startingBit, newValues.length);
316 public Bit[] getValues()
318 return values.clone();
321 public void clearSignals()
323 Bit[] bits = new Bit[length];
324 for (int i = 0; i < length; i++)
330 public String toString()
332 return Arrays.toString(values);
337 public String toString()
339 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
342 public static WireArrayInput[] extractInputs(WireArray[] w)
344 WireArrayInput[] inputs = new WireArrayInput[w.length];
345 for(int i = 0; i < w.length; i++)
346 inputs[i] = w[i].createInput();