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;
10 import era.mi.logic.Util;
13 * Represents an array of wires that can store n bits of information.
15 * @author Fabian Stemmler
18 public class WireArray
21 public final int travelTime;
22 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
23 public final int length;
24 private List<WireArrayInput> inputs = new ArrayList<WireArrayInput>();
26 public WireArray(int length, int travelTime)
29 throw new IllegalArgumentException(String.format("Tried to create an array of wires with length %o, but a length of less than 1 makes no sense.", length));
31 this.travelTime = travelTime;
35 private void initValues()
37 values = Bit.Z.makeArray(length);
40 private void recalculateSingleInput()
42 WireArrayInput input = inputs.get(0);
43 if (!Arrays.equals(input.getValues(), values))
45 System.arraycopy(input.getValues(), 0, values, 0, length);
50 private void recalculateMultipleInputs()
52 Iterator<WireArrayInput> it = inputs.iterator();
53 Bit[] newValues = it.next().inputValues.clone();
57 WireArrayInput input = it.next();
58 Bit[] bits = input.getValues();
59 for (int i = 0; i < length; i++)
61 if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
63 else if (Bit.Z.equals(newValues[i]))
64 newValues[i] = bits[i];
70 if (!Arrays.equals(newValues, values))
77 private void recalculate()
79 switch (inputs.size())
84 recalculateSingleInput();
87 recalculateMultipleInputs();
92 * The WireArray is interpreted as an unsigned integer with n bits.
94 * @return <code>true</code> if all bits are either <code>Bit.ONE</code> or
95 * <code>Bit.ZERO</code> (they do not all have to have the same value),
96 * not <code>Bit.X</code> or <code>Bit.Z</code>. <code>false</code> is
99 * @author Fabian Stemmler
101 public boolean hasNumericValue()
105 if (b != Bit.ZERO && b != Bit.ONE)
112 * The WireArray is interpreted as an unsigned integer with n bits.
114 * @return The unsigned value of the {@link WireArray}'s bits, where value 0
115 * corresponds with 2^0, value 1 is 2^1 and so on.
117 * @author Fabian Stemmler
119 public long getUnsignedValue()
123 for (int i = 0; i < length; i++)
130 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
143 * The WireArray is interpreted as a signed integer with n bits.
145 * @return The signed value of the {@link WireArray}'s bits, where value 0
146 * corresponds with 2^0, value 1 is 2^1 and so on.
148 * @author Fabian Stemmler
150 public long getSignedValue()
152 long val = getUnsignedValue();
153 long mask = 1 << (length - 1);
154 if ((mask & val) != 0)
156 int shifts = 64 - length;
157 return (val << shifts) >> shifts;
163 * Included for convenient use on {@link WireArray}s of length 1.
165 * @return The value of bit 0.
167 * @author Fabian Stemmler
169 public Bit getValue()
176 * @param index Index of the requested bit.
177 * @return The value of the indexed bit.
179 * @author Fabian Stemmler
181 public Bit getValue(int index)
183 return values[index];
186 public Bit[] getValues(int start, int end)
188 int length = end - start;
189 Bit[] bits = new Bit[length];
190 System.arraycopy(values, start, bits, 0, length);
195 * @return An array of length n containing the values of the n bits in the
196 * {@link WireArray}. Can be safely modified.
198 * @author Fabian Stemmler
200 public Bit[] getValues()
202 return values.clone();
206 * Adds an {@link WireArrayObserver}, who will be notified when the value of the
207 * {@link WireArray} is updated.
209 * @param ob The {@link WireArrayObserver} to be notified of changes.
210 * @return true if the given {@link WireArrayObserver} was not already
211 * registered, false otherwise
213 * @author Fabian Stemmler
215 public boolean addObserver(WireArrayObserver ob)
217 return observers.add(ob);
220 private void notifyObservers()
222 for (WireArrayObserver o : observers)
227 * Create and register a {@link WireArrayInput} object, which is tied to this
230 public WireArrayInput createInput()
232 return new WireArrayInput(this);
235 private void registerInput(WireArrayInput toRegister)
237 inputs.add(toRegister);
241 * A {@link WireArrayInput} feeds a constant signal into the {@link WireArray}
242 * it is tied to. The combination of all inputs determines the
243 * {@link WireArray}s final value. X dominates all other inputs Z does not
244 * affect the final value, unless there are no other inputs than Z 0 and 1 turn
245 * into X when they are mixed
247 * @author Fabian Stemmler
249 public class WireArrayInput
251 public final WireArray owner;
252 private Bit[] inputValues;
254 private WireArrayInput(WireArray owner)
259 owner.registerInput(this);
262 private void initValues()
264 inputValues = Bit.Z.makeArray(length);
268 * Sets the wires values. This takes up time, as specified by the
269 * {@link WireArray}s travel time.
271 * @param newValues The new values the wires should take on.
273 * @author Fabian Stemmler
275 public void feedSignals(Bit... newValues)
277 if (newValues.length == length)
279 feedSignals(0, newValues);
281 throw new IllegalArgumentException(String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));
285 * Sets values of a subarray of wires. This takes up time, as specified by the
286 * {@link WireArray}s travel time.
288 * @param newValues The new values the wires should take on.
289 * @param startingBit The first index of the subarray of wires.
291 * @author Fabian Stemmler
293 public void feedSignals(int startingBit, Bit... newValues)
295 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
298 private void setValues(int startingBit, Bit... newValues)
300 int exclLastIndex = startingBit + newValues.length;
301 if (length < exclLastIndex)
302 throw new ArrayIndexOutOfBoundsException(String.format("Attempted to input bits from index %o to %o when there are only %o wires.", startingBit, exclLastIndex - 1, length));
303 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
305 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
311 * Returns a copy (safe to modify) of the values the {@link WireArrayInput} is currently feeding into the associated {@link WireArray}.
313 public Bit[] getValues()
315 return inputValues.clone();
319 * {@link WireArrayInput} now feeds Z into the associated {@link WireArray}.
321 public void clearSignals()
323 feedSignals(Bit.Z.makeArray(length));
326 public Bit[] wireValuesExcludingMe()
328 Bit[] bits = Bit.Z.makeArray(length);
329 for (WireArrayInput wai : inputs)
333 Util.combineInto(bits, wai.getValues());
339 public String toString()
341 return Arrays.toString(inputValues);
346 public String toString()
348 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
351 public static WireArrayInput[] extractInputs(WireArray[] w)
353 WireArrayInput[] inputs = new WireArrayInput[w.length];
354 for (int i = 0; i < w.length; i++)
355 inputs[i] = w[i].createInput();