1 package era.mi.logic.wires;
\r
3 import java.io.Closeable;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Arrays;
\r
6 import java.util.Iterator;
\r
7 import java.util.List;
\r
9 import era.mi.logic.Bit;
\r
10 import era.mi.logic.Simulation;
\r
11 import era.mi.logic.Util;
\r
14 * Represents an array of wires that can store n bits of information.
\r
16 * @author Fabian Stemmler
\r
19 public class WireArray
\r
21 private Bit[] values;
\r
22 public final int travelTime;
\r
23 private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
\r
24 public final int length;
\r
25 private List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd>();
\r
27 public WireArray(int length, int travelTime)
\r
30 throw new IllegalArgumentException(
\r
31 String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
32 this.length = length;
\r
33 this.travelTime = travelTime;
\r
37 private void initValues()
\r
39 values = Bit.U.makeArray(length);
\r
42 private void recalculateSingleInput()
\r
44 WireArrayEnd input = inputs.get(0);
\r
45 if (!Arrays.equals(input.getValues(), values))
\r
47 Bit[] oldValues = values.clone();
\r
48 System.arraycopy(input.getValues(), 0, values, 0, length);
\r
49 notifyObservers(oldValues);
\r
53 private void recalculateMultipleInputs()
\r
55 Iterator<WireArrayEnd> it = inputs.iterator();
\r
56 Bit[] newValues = it.next().inputValues.clone();
\r
58 while (it.hasNext())
\r
60 WireArrayEnd input = it.next();
\r
61 Bit[] bits = input.getValues();
\r
62 for (int i = 0; i < length; i++)
\r
64 newValues[i] = newValues[i].combineWith(bits[i]);
\r
68 if (!Arrays.equals(newValues, values))
\r
70 Bit[] oldValues = values;
\r
72 notifyObservers(oldValues);
\r
76 private void recalculate()
\r
78 switch (inputs.size())
\r
83 recalculateSingleInput();
\r
86 recalculateMultipleInputs();
\r
91 * The WireArray 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 WireArray is interpreted as an unsigned integer with n bits.
\r
111 * @return The unsigned value of the {@link WireArray}'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 (int i = 0; i < length; i++)
\r
126 return 0; // TODO: Proper handling for getUnsignedValue(), if not all bits are 1 or 0;
\r
139 * The WireArray is interpreted as a signed integer with n bits.
\r
141 * @return The signed value of the {@link WireArray}'s bits, where value 0 corresponds with 2^0, value 1 is 2^1 and so on.
\r
143 * @author Fabian Stemmler
\r
145 public long getSignedValue()
\r
147 long val = getUnsignedValue();
\r
148 long mask = 1 << (length - 1);
\r
149 if ((mask & val) != 0)
\r
151 int shifts = 64 - length;
\r
152 return (val << shifts) >> shifts;
\r
158 * Included for convenient use on {@link WireArray}s of length 1.
\r
160 * @return The value of bit 0.
\r
162 * @author Fabian Stemmler
\r
164 public Bit getValue()
\r
166 return getValue(0);
\r
171 * @param index Index of the requested bit.
\r
172 * @return The value of the indexed bit.
\r
174 * @author Fabian Stemmler
\r
176 public Bit getValue(int index)
\r
178 return values[index];
\r
181 public Bit[] getValues(int start, int end)
\r
183 int length = end - start;
\r
184 Bit[] bits = new Bit[length];
\r
185 System.arraycopy(values, start, bits, 0, length);
\r
190 * @return An array of length n containing the values of the n bits in the {@link WireArray}. Can be safely modified.
\r
192 * @author Fabian Stemmler
\r
194 public Bit[] getValues()
\r
196 return values.clone();
\r
200 * Adds an {@link WireArrayObserver}, who will be notified when the value of the {@link WireArray} is updated.
\r
202 * @param ob The {@link WireArrayObserver} to be notified of changes.
\r
203 * @return true if the given {@link WireArrayObserver} was not already registered, false otherwise
\r
205 * @author Fabian Stemmler
\r
207 public boolean addObserver(WireArrayObserver ob)
\r
209 return observers.add(ob);
\r
212 private void notifyObservers(Bit[] oldValues)
\r
214 for (WireArrayObserver o : observers)
\r
215 o.update(this, oldValues);
\r
219 * Create and register a {@link WireArrayEnd} object, which is tied to this {@link WireArray}.
\r
221 public WireArrayEnd createInput()
\r
223 return new WireArrayEnd(this);
\r
226 private void registerInput(WireArrayEnd toRegister)
\r
228 inputs.add(toRegister);
\r
232 * A {@link WireArrayEnd} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines the
\r
233 * {@link WireArray}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs than
\r
234 * Z 0 and 1 turn into X when they are mixed
\r
236 * @author Fabian Stemmler
\r
238 public class WireArrayEnd implements Closeable
\r
240 public final WireArray owner;
\r
241 private boolean open;
\r
242 private Bit[] inputValues;
\r
244 private WireArrayEnd(WireArray owner)
\r
247 this.owner = owner;
\r
250 owner.registerInput(this);
\r
253 private void initValues()
\r
255 inputValues = Bit.U.makeArray(length);
\r
259 * Sets the wires values. This takes up time, as specified by the {@link WireArray}s travel time.
\r
261 * @param newValues The new values the wires should take on.
\r
263 * @author Fabian Stemmler
\r
265 public void feedSignals(Bit... newValues)
\r
267 if (newValues.length == length)
\r
269 feedSignals(0, newValues);
\r
271 throw new IllegalArgumentException(
\r
272 String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
\r
276 * Sets values of a subarray of wires. This takes up time, as specified by the {@link WireArray}s travel time.
\r
278 * @param newValues The new values the wires should take on.
\r
279 * @param startingBit The first index of the subarray of wires.
\r
281 * @author Fabian Stemmler
\r
283 public void feedSignals(int startingBit, Bit... newValues)
\r
286 throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
287 Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
\r
290 private void setValues(int startingBit, Bit... newValues)
\r
292 int exclLastIndex = startingBit + newValues.length;
\r
293 if (length < exclLastIndex)
\r
294 throw new ArrayIndexOutOfBoundsException(
\r
295 String.format("Attempted to input bits from index %d to %d when there are only %d wires.", startingBit,
\r
296 exclLastIndex - 1, length));
\r
297 if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
\r
299 System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
\r
300 owner.recalculate();
\r
305 * Returns a copy (safe to modify) of the values the {@link WireArrayEnd} is currently feeding into the associated
\r
306 * {@link WireArray}.
\r
308 public Bit[] getValues()
\r
310 return inputValues.clone();
\r
314 * {@link WireArrayEnd} now feeds Z into the associated {@link WireArray}.
\r
316 public void clearSignals()
\r
318 feedSignals(Bit.Z.makeArray(length));
\r
321 public Bit[] wireValuesExcludingMe()
\r
323 Bit[] bits = Bit.Z.makeArray(length);
\r
324 for (WireArrayEnd wai : inputs)
\r
328 Util.combineInto(bits, wai.getValues());
\r
333 public Bit getWireValue()
\r
335 return owner.getValue();
\r
338 public Bit[] getWireValues()
\r
340 return owner.getValues();
\r
344 public String toString()
\r
346 return Arrays.toString(inputValues);
\r
350 public void close()
\r
352 inputs.remove(this);
\r
358 public String toString()
\r
360 return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
\r
363 public static WireArrayEnd[] extractInputs(WireArray[] w)
\r
365 WireArrayEnd[] inputs = new WireArrayEnd[w.length];
\r
366 for (int i = 0; i < w.length; i++)
\r
367 inputs[i] = w[i].createInput();
\r