projects
/
Mograsim.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Did some clean up
[Mograsim.git]
/
era.mi
/
src
/
era
/
mi
/
logic
/
wires
/
WireArray.java
diff --git
a/era.mi/src/era/mi/logic/wires/WireArray.java
b/era.mi/src/era/mi/logic/wires/WireArray.java
index
1e98f70
..
ca93fcb
100644
(file)
--- a/
era.mi/src/era/mi/logic/wires/WireArray.java
+++ b/
era.mi/src/era/mi/logic/wires/WireArray.java
@@
-1,5
+1,6
@@
package era.mi.logic.wires;
\r
\r
package era.mi.logic.wires;
\r
\r
+import java.io.Closeable;
\r
import java.util.ArrayList;
\r
import java.util.Arrays;
\r
import java.util.Iterator;
\r
import java.util.ArrayList;
\r
import java.util.Arrays;
\r
import java.util.Iterator;
\r
@@
-15,14
+16,16
@@
import era.mi.logic.Util;
* @author Fabian Stemmler
\r
*
\r
*/
\r
* @author Fabian Stemmler
\r
*
\r
*/
\r
-public class WireArray {
\r
+public class WireArray
\r
+{
\r
private Bit[] values;
\r
public final int travelTime;
\r
private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
\r
public final int length;
\r
private Bit[] values;
\r
public final int travelTime;
\r
private List<WireArrayObserver> observers = new ArrayList<WireArrayObserver>();
\r
public final int length;
\r
-
private List<WireArrayInput> inputs = new ArrayList<WireArrayInput
>();
\r
+
List<WireArrayEnd> inputs = new ArrayList<WireArrayEnd
>();
\r
\r
\r
- public WireArray(int length, int travelTime) {
\r
+ public WireArray(int length, int travelTime)
\r
+ {
\r
if (length < 1)
\r
throw new IllegalArgumentException(
\r
String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
if (length < 1)
\r
throw new IllegalArgumentException(
\r
String.format("Tried to create an array of wires with length %d, but a length of less than 1 makes no sense.", length));
\r
@@
-31,45
+34,49
@@
public class WireArray {
initValues();
\r
}
\r
\r
initValues();
\r
}
\r
\r
- private void initValues() {
\r
- values = Bit.Z.makeArray(length);
\r
+ private void initValues()
\r
+ {
\r
+ values = Bit.U.makeArray(length);
\r
}
\r
\r
}
\r
\r
- private void recalculateSingleInput() {
\r
- WireArrayInput input = inputs.get(0);
\r
- if (!Arrays.equals(input.getValues(), values)) {
\r
+ private void recalculateSingleInput()
\r
+ {
\r
+ WireArrayEnd input = inputs.get(0);
\r
+ if (!Arrays.equals(input.getValues(), values))
\r
+ {
\r
Bit[] oldValues = values.clone();
\r
System.arraycopy(input.getValues(), 0, values, 0, length);
\r
notifyObservers(oldValues);
\r
}
\r
}
\r
\r
Bit[] oldValues = values.clone();
\r
System.arraycopy(input.getValues(), 0, values, 0, length);
\r
notifyObservers(oldValues);
\r
}
\r
}
\r
\r
- private void recalculateMultipleInputs() {
\r
- Iterator<WireArrayInput> it = inputs.iterator();
\r
+ private void recalculateMultipleInputs()
\r
+ {
\r
+ Iterator<WireArrayEnd> it = inputs.iterator();
\r
Bit[] newValues = it.next().inputValues.clone();
\r
\r
Bit[] newValues = it.next().inputValues.clone();
\r
\r
- while (it.hasNext()) {
\r
- WireArrayInput input = it.next();
\r
+ while (it.hasNext())
\r
+ {
\r
+ WireArrayEnd input = it.next();
\r
Bit[] bits = input.getValues();
\r
Bit[] bits = input.getValues();
\r
- for (int i = 0; i < length; i++) {
\r
- if (Bit.Z.equals(bits[i]) || newValues[i].equals(bits[i]))
\r
- continue;
\r
- else if (Bit.Z.equals(newValues[i]))
\r
- newValues[i] = bits[i];
\r
- else
\r
- newValues[i] = Bit.X;
\r
+ for (int i = 0; i < length; i++)
\r
+ {
\r
+ newValues[i] = newValues[i].combineWith(bits[i]);
\r
}
\r
}
\r
\r
}
\r
}
\r
\r
- if (!Arrays.equals(newValues, values)) {
\r
+ if (!Arrays.equals(newValues, values))
\r
+ {
\r
Bit[] oldValues = values;
\r
values = newValues;
\r
notifyObservers(oldValues);
\r
}
\r
}
\r
\r
Bit[] oldValues = values;
\r
values = newValues;
\r
notifyObservers(oldValues);
\r
}
\r
}
\r
\r
- private void recalculate() {
\r
- switch (inputs.size()) {
\r
+ void recalculate()
\r
+ {
\r
+ switch (inputs.size())
\r
+ {
\r
case 0:
\r
return;
\r
case 1:
\r
case 0:
\r
return;
\r
case 1:
\r
@@
-88,8
+95,10
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public boolean hasNumericValue() {
\r
- for (Bit b : values) {
\r
+ public boolean hasNumericValue()
\r
+ {
\r
+ for (Bit b : values)
\r
+ {
\r
if (b != Bit.ZERO && b != Bit.ONE)
\r
return false;
\r
}
\r
if (b != Bit.ZERO && b != Bit.ONE)
\r
return false;
\r
}
\r
@@
-103,11
+112,14
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public long getUnsignedValue() {
\r
+ public long getUnsignedValue()
\r
+ {
\r
long val = 0;
\r
long mask = 1;
\r
long val = 0;
\r
long mask = 1;
\r
- for (int i = 0; i < length; i++) {
\r
- switch (values[i]) {
\r
+ for (int i = 0; i < length; i++)
\r
+ {
\r
+ switch (values[i])
\r
+ {
\r
default:
\r
case Z:
\r
case X:
\r
default:
\r
case Z:
\r
case X:
\r
@@
-130,10
+142,12
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public long getSignedValue() {
\r
+ public long getSignedValue()
\r
+ {
\r
long val = getUnsignedValue();
\r
long mask = 1 << (length - 1);
\r
long val = getUnsignedValue();
\r
long mask = 1 << (length - 1);
\r
- if ((mask & val) != 0) {
\r
+ if ((mask & val) != 0)
\r
+ {
\r
int shifts = 64 - length;
\r
return (val << shifts) >> shifts;
\r
}
\r
int shifts = 64 - length;
\r
return (val << shifts) >> shifts;
\r
}
\r
@@
-147,7
+161,8
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public Bit getValue() {
\r
+ public Bit getValue()
\r
+ {
\r
return getValue(0);
\r
}
\r
\r
return getValue(0);
\r
}
\r
\r
@@
-158,11
+173,13
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public Bit getValue(int index) {
\r
+ public Bit getValue(int index)
\r
+ {
\r
return values[index];
\r
}
\r
\r
return values[index];
\r
}
\r
\r
- public Bit[] getValues(int start, int end) {
\r
+ public Bit[] getValues(int start, int end)
\r
+ {
\r
int length = end - start;
\r
Bit[] bits = new Bit[length];
\r
System.arraycopy(values, start, bits, 0, length);
\r
int length = end - start;
\r
Bit[] bits = new Bit[length];
\r
System.arraycopy(values, start, bits, 0, length);
\r
@@
-174,7
+191,8
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public Bit[] getValues() {
\r
+ public Bit[] getValues()
\r
+ {
\r
return values.clone();
\r
}
\r
\r
return values.clone();
\r
}
\r
\r
@@
-186,46
+204,55
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public boolean addObserver(WireArrayObserver ob) {
\r
+ public boolean addObserver(WireArrayObserver ob)
\r
+ {
\r
return observers.add(ob);
\r
}
\r
\r
return observers.add(ob);
\r
}
\r
\r
- private void notifyObservers(Bit[] oldValues) {
\r
+ private void notifyObservers(Bit[] oldValues)
\r
+ {
\r
for (WireArrayObserver o : observers)
\r
o.update(this, oldValues);
\r
}
\r
\r
/**
\r
for (WireArrayObserver o : observers)
\r
o.update(this, oldValues);
\r
}
\r
\r
/**
\r
- * Create and register a {@link WireArray
Input
} object, which is tied to this {@link WireArray}.
\r
+ * Create and register a {@link WireArray
End
} object, which is tied to this {@link WireArray}.
\r
*/
\r
*/
\r
- public WireArrayInput createInput() {
\r
- return new WireArrayInput(this);
\r
+ public WireArrayEnd createInput()
\r
+ {
\r
+ return new WireArrayEnd(this);
\r
}
\r
\r
}
\r
\r
- private void registerInput(WireArrayInput toRegister) {
\r
+ void registerInput(WireArrayEnd toRegister)
\r
+ {
\r
inputs.add(toRegister);
\r
}
\r
\r
/**
\r
inputs.add(toRegister);
\r
}
\r
\r
/**
\r
- * A {@link WireArray
Input} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines
\r
- *
the {@link WireArray}s final value. X dominates all other inputs Z does not affect the final value, unless there are no other inputs
\r
- *
than
Z 0 and 1 turn into X when they are mixed
\r
+ * A {@link WireArray
End} feeds a constant signal into the {@link WireArray} it is tied to. The combination of all inputs determines the
\r
+ *
{@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
+ * Z 0 and 1 turn into X when they are mixed
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public class WireArrayInput {
\r
+ public class WireArrayEnd implements Closeable
\r
+ {
\r
public final WireArray owner;
\r
public final WireArray owner;
\r
- private Bit[] inputValues;
\r
+ private boolean open;
\r
+ Bit[] inputValues;
\r
\r
\r
- private WireArrayInput(WireArray owner) {
\r
+ WireArrayEnd(WireArray owner)
\r
+ {
\r
super();
\r
this.owner = owner;
\r
super();
\r
this.owner = owner;
\r
+ open = true;
\r
initValues();
\r
owner.registerInput(this);
\r
}
\r
\r
initValues();
\r
owner.registerInput(this);
\r
}
\r
\r
- private void initValues() {
\r
- inputValues = Bit.Z.makeArray(length);
\r
+ private void initValues()
\r
+ {
\r
+ inputValues = Bit.U.makeArray(length);
\r
}
\r
\r
/**
\r
}
\r
\r
/**
\r
@@
-235,12
+262,13
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public void feedSignals(Bit... newValues) {
\r
- if (newValues.length == length) {
\r
- feedSignals(0, newValues);
\r
- } else
\r
+ public void feedSignals(Bit... newValues)
\r
+ {
\r
+ if (newValues.length != length)
\r
throw new IllegalArgumentException(
\r
throw new IllegalArgumentException(
\r
- String.format("Attempted to input %o bits instead of %o bits.", newValues.length, length));
\r
+ String.format("Attempted to input %d bits instead of %d bits.", newValues.length, length));
\r
+ feedSignals(0, newValues);
\r
+
\r
}
\r
\r
/**
\r
}
\r
\r
/**
\r
@@
-251,40
+279,49
@@
public class WireArray {
*
\r
* @author Fabian Stemmler
\r
*/
\r
*
\r
* @author Fabian Stemmler
\r
*/
\r
- public void feedSignals(int startingBit, Bit... newValues) {
\r
+ public void feedSignals(int startingBit, Bit... newValues)
\r
+ {
\r
+ if (!open)
\r
+ throw new RuntimeException("Attempted to write to closed WireArrayEnd.");
\r
Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
\r
}
\r
\r
Simulation.TIMELINE.addEvent((e) -> setValues(startingBit, newValues), travelTime);
\r
}
\r
\r
- private void setValues(int startingBit, Bit... newValues) {
\r
+ private void setValues(int startingBit, Bit... newValues)
\r
+ {
\r
int exclLastIndex = startingBit + newValues.length;
\r
if (length < exclLastIndex)
\r
throw new ArrayIndexOutOfBoundsException(
\r
int exclLastIndex = startingBit + newValues.length;
\r
if (length < exclLastIndex)
\r
throw new ArrayIndexOutOfBoundsException(
\r
- String.format("Attempted to input bits from index %
o to %o when there are only %o
wires.", startingBit,
\r
+ String.format("Attempted to input bits from index %
d to %d when there are only %d
wires.", startingBit,
\r
exclLastIndex - 1, length));
\r
exclLastIndex - 1, length));
\r
- if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length)) {
\r
+ if (!Arrays.equals(inputValues, startingBit, exclLastIndex, newValues, 0, newValues.length))
\r
+ {
\r
System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
\r
owner.recalculate();
\r
}
\r
}
\r
\r
/**
\r
System.arraycopy(newValues, 0, inputValues, startingBit, newValues.length);
\r
owner.recalculate();
\r
}
\r
}
\r
\r
/**
\r
- * Returns a copy (safe to modify) of the values the {@link WireArray
Input
} is currently feeding into the associated
\r
+ * Returns a copy (safe to modify) of the values the {@link WireArray
End
} is currently feeding into the associated
\r
* {@link WireArray}.
\r
*/
\r
* {@link WireArray}.
\r
*/
\r
- public Bit[] getValues() {
\r
+ public Bit[] getValues()
\r
+ {
\r
return inputValues.clone();
\r
}
\r
\r
/**
\r
return inputValues.clone();
\r
}
\r
\r
/**
\r
- * {@link WireArray
Input
} now feeds Z into the associated {@link WireArray}.
\r
+ * {@link WireArray
End
} now feeds Z into the associated {@link WireArray}.
\r
*/
\r
*/
\r
- public void clearSignals() {
\r
+ public void clearSignals()
\r
+ {
\r
feedSignals(Bit.Z.makeArray(length));
\r
}
\r
\r
feedSignals(Bit.Z.makeArray(length));
\r
}
\r
\r
- public Bit[] wireValuesExcludingMe() {
\r
+ public Bit[] wireValuesExcludingMe()
\r
+ {
\r
Bit[] bits = Bit.Z.makeArray(length);
\r
Bit[] bits = Bit.Z.makeArray(length);
\r
- for (WireArrayInput wai : inputs) {
\r
+ for (WireArrayEnd wai : inputs)
\r
+ {
\r
if (wai == this)
\r
continue;
\r
Util.combineInto(bits, wai.getValues());
\r
if (wai == this)
\r
continue;
\r
Util.combineInto(bits, wai.getValues());
\r
@@
-292,19
+329,39
@@
public class WireArray {
return bits;
\r
}
\r
\r
return bits;
\r
}
\r
\r
+ public Bit getWireValue()
\r
+ {
\r
+ return owner.getValue();
\r
+ }
\r
+
\r
+ public Bit[] getWireValues()
\r
+ {
\r
+ return owner.getValues();
\r
+ }
\r
+
\r
@Override
\r
@Override
\r
- public String toString() {
\r
+ public String toString()
\r
+ {
\r
return Arrays.toString(inputValues);
\r
}
\r
return Arrays.toString(inputValues);
\r
}
\r
+
\r
+ @Override
\r
+ public void close()
\r
+ {
\r
+ inputs.remove(this);
\r
+ open = false;
\r
+ }
\r
}
\r
\r
@Override
\r
}
\r
\r
@Override
\r
- public String toString() {
\r
+ public String toString()
\r
+ {
\r
return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
\r
}
\r
\r
return String.format("wire 0x%08x value: %s inputs: %s", hashCode(), Arrays.toString(values), inputs);
\r
}
\r
\r
- public static WireArrayInput[] extractInputs(WireArray[] w) {
\r
- WireArrayInput[] inputs = new WireArrayInput[w.length];
\r
+ public static WireArrayEnd[] extractInputs(WireArray[] w)
\r
+ {
\r
+ WireArrayEnd[] inputs = new WireArrayEnd[w.length];
\r
for (int i = 0; i < w.length; i++)
\r
inputs[i] = w[i].createInput();
\r
return inputs;
\r
for (int i = 0; i < w.length; i++)
\r
inputs[i] = w[i].createInput();
\r
return inputs;
\r