Skip to content

Commit

Permalink
Merge ra4king#94: Call floating wires "z" instead of "x"
Browse files Browse the repository at this point in the history
  • Loading branch information
ausbin committed Sep 5, 2023
2 parents 837a6bf + 8928ea3 commit d3c8006
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 57 deletions.
Binary file modified docs/docs/wires/img/wire-colors-pt-2-blue-wire.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/docs/wires/img/wire-colors-pt-4-orange-wire.PNG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 18 additions & 19 deletions docs/docs/wires/wire-colors.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,37 @@ light green wire - 1-bit wire with value of 1

![pt-1-light-green-wire]

### Part 2: Blue
### Part 2: Light Blue / Dark Blue

blue indicates an unitialized 1-bit wire
Light blue indicates a 1-bit wire that is "floating" or in a "high impedance
state" — in other words, the wire is not connected to anything.

The wire below is unitialized because no value is being driven onto it.
The wire below is floating because no value is being driven onto it.

![pt-2-blue-wire]

Since you may have both initialized and unitialized wires in a
properly functioning circuit, an unitialized wire is not considered an error.
Dark blue indicates a floating multi-bit wire.

![pt-3-dark-blue-wire-uninit]

Since you may have both floating wires and wires with 0/1 on them in a properly
functioning circuit, a floating wire is not considered an error.

### Part 3: Black

black indicates either an initialized or unitialized multi-bit wire
Black indicates a multi-bit wire carrying no floating values (only 0s and 1s).

black wire - multi-bit wire, initialized
Black wire - multi-bit wire carrying only 0s and 1s:

![pt-3-black-wire]

black wire - multi-bit wire, unitialized

![pt-3-black-wire-unint]

Also note that a multi-bit wire can have both initialized and unitialized bits.
This error usually pops up when you combine wires together with a splitter, and
some of the individual wires that make up the multi-bit wire are unitialized.

As stated above, since you may have both initialized and unitialized wires in a
properly functioning circuit, an unitialized wire is not considered an error.

Also note that a multi-bit wire can carry both floating and 0/1 bits.
This usually pops up when you combine wires together with a splitter, and
some of the individual wires that make up the multi-bit wire are floating.

As stated above, since you may have both floating wires and wires with 0/1 on
them in a properly functioning circuit, a floating wire is not considered an
error.

## Error State Wires

Expand Down Expand Up @@ -101,6 +100,6 @@ These multi-bit wires are known as "wire bundles" in industry.
[pt-1-dark-green-wire]: img/wire-colors-pt-1-dark-green-wire.PNG "dark green CircuitSim wire"
[pt-2-blue-wire]: img/wire-colors-pt-2-blue-wire.PNG "blue CircuitSim wire"
[pt-3-black-wire]: img/wire-colors-pt-3-black-wire.PNG "black CircuitSim wire"
[pt-3-black-wire-unint]: img/wire-colors-pt-3-black-wire-uninit.PNG "black CircuitSim wire uninitialized"
[pt-3-dark-blue-wire-uninit]: img/wire-colors-pt-3-dark-blue-wire-uninit.PNG "dark blue CircuitSim wire"
[pt-4-orange-wire]: img/wire-colors-pt-4-orange-wire.PNG "orange CircuitSim wire"
[pt-5-red-wire]: img/wire-colors-pt-5-red-wire.PNG "red CircuitSim wire"
16 changes: 8 additions & 8 deletions src/main/java/com/ra4king/circuitsim/gui/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static void setBitColor(GraphicsContext graphics, CircuitState circuitSta
setBitColor(graphics, circuitState.getMergedValue(link));
}
} else {
setBitColor(graphics, State.X);
setBitColor(graphics, State.Z);
}
} else {
graphics.setStroke(Color.ORANGE);
Expand All @@ -230,8 +230,8 @@ public static void setBitColor(GraphicsContext graphics, CircuitState circuitSta

private static final Color ONE_COLOR = Color.GREEN.brighter();
private static final Color ZERO_COLOR = Color.GREEN.darker();
private static final Color X_1BIT_COLOR = Color.BLUE;
private static final Color X_MULTIBIT_COLOR = Color.BLUE.darker();
private static final Color Z_1BIT_COLOR = Color.BLUE;
private static final Color Z_MULTIBIT_COLOR = Color.BLUE.darker();

public static void setBitColor(GraphicsContext graphics, WireValue value) {
if (value.getBitSize() == 1) {
Expand All @@ -240,8 +240,8 @@ public static void setBitColor(GraphicsContext graphics, WireValue value) {
graphics.setStroke(Color.BLACK);
graphics.setFill(Color.BLACK);
} else {
graphics.setStroke(X_MULTIBIT_COLOR);
graphics.setFill(X_MULTIBIT_COLOR);
graphics.setStroke(Z_MULTIBIT_COLOR);
graphics.setFill(Z_MULTIBIT_COLOR);
}
}

Expand All @@ -255,9 +255,9 @@ public static void setBitColor(GraphicsContext graphics, State bitState) {
graphics.setStroke(ZERO_COLOR);
graphics.setFill(ZERO_COLOR);
}
case X -> {
graphics.setStroke(X_1BIT_COLOR);
graphics.setFill(X_1BIT_COLOR);
case Z -> {
graphics.setStroke(Z_1BIT_COLOR);
graphics.setFill(Z_1BIT_COLOR);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ void link(LinkState other) {

cachedMergedValue = null;
isShortCircuited = null;
participants.forEach((port, info) -> info.lastPropagated.setAllBits(State.X));
participants.forEach((port, info) -> info.lastPropagated.setAllBits(State.Z));

linkStates.remove(other.link);
getCircuit().getSimulator().linkRemoved(other.link);
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/com/ra4king/circuitsim/simulator/WireValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class WireValue {
public enum State {
ONE('1'), ZERO('0'), X('x');
ONE('1'), ZERO('0'), Z('z');

public final char repr;

Expand All @@ -16,14 +16,14 @@ public enum State {
}

public State negate() {
return this == X ? X : this == ONE ? ZERO : ONE;
return this == Z ? Z : this == ONE ? ZERO : ONE;
}
}

private State[] bits;

public WireValue(int bitSize) {
this(bitSize, State.X);
this(bitSize, State.Z);
}

public WireValue(int bitSize, State state) {
Expand Down Expand Up @@ -53,9 +53,9 @@ public WireValue merge(WireValue value) {
}

for (int i = 0; i < getBitSize(); i++) {
if (getBit(i) == State.X) {
if (getBit(i) == State.Z) {
setBit(i, value.getBit(i));
} else if (value.getBit(i) == State.X) {
} else if (value.getBit(i) == State.Z) {
setBit(i, getBit(i));
} else if (value.getBit(i) != getBit(i)) {
throw new ShortCircuitException(this, value);
Expand All @@ -81,7 +81,7 @@ public void setBitSize(int bitSize) {
State[] oldBits = bits;

bits = new State[bitSize];
setAllBits(State.X);
setAllBits(State.Z);
System.arraycopy(oldBits, 0, bits, 0, Math.min(bitSize, oldBits.length));
}

Expand Down Expand Up @@ -129,7 +129,7 @@ public boolean isValidValue() {
}

for (State bit : bits) {
if (bit == State.X) {
if (bit == State.Z) {
return false;
}
}
Expand All @@ -140,7 +140,7 @@ public boolean isValidValue() {
public int getValue() {
int value = 0;
for (int i = 0; i < bits.length; i++) {
if (bits[i] == State.X) {
if (bits[i] == State.Z) {
throw new IllegalStateException("Invalid value");
}

Expand All @@ -153,29 +153,29 @@ public int getValue() {
* Converts the value held on this wire to a hex string.
*
* @return a lowercase hex string if all bits are defined, otherwise
* getBitSize() 'x's
* getBitSize() 'z's
*/
public String toHexString() {
int hexDigits = 1 + (getBitSize() - 1) / 4;
if (isValidValue()) {
return String.format("%0" + hexDigits + "x", getValue());
} else {
return "x".repeat(Math.max(0, hexDigits));
return "z".repeat(Math.max(0, hexDigits));
}
}

/**
* Converts the value held on this wire to a decimal string.
*
* @return a decimal string if all bits are defined, otherwise
* getBitSize() 'x's
* getBitSize() 'z's
*/
public String toDecString() {
int decDigits = (int)Math.ceil(getBitSize() / 3.322);
if (isValidValue()) {
return String.format("%0" + decDigits + "d", getValue());
} else {
return "x".repeat(Math.max(0, decDigits));
return "z".repeat(Math.max(0, decDigits));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
state.pushValue(getPort(PORT_EQ), new WireValue(1, valueA == valueB ? State.ONE : State.ZERO));
state.pushValue(getPort(PORT_GT), new WireValue(1, valueA > valueB ? State.ONE : State.ZERO));
} else {
WireValue xValue = new WireValue(1, State.X);
WireValue xValue = new WireValue(1, State.Z);
state.pushValue(getPort(PORT_LT), xValue);
state.pushValue(getPort(PORT_EQ), xValue);
state.pushValue(getPort(PORT_GT), xValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @author Roi Atalla
*/
public class ControlledBuffer extends Component {
private final WireValue X_VALUE;
private final WireValue Z_VALUE;

public static final int PORT_IN = 0;
public static final int PORT_ENABLE = 1;
public static final int PORT_OUT = 2;

public ControlledBuffer(String name, int bitSize) {
super(name, new int[] { bitSize, 1, bitSize });
X_VALUE = new WireValue(bitSize);
Z_VALUE = new WireValue(bitSize);
}

@Override
Expand All @@ -29,7 +29,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
if (state.getLastReceived(getPort(PORT_ENABLE)).getBit(0) == State.ONE) {
state.pushValue(getPort(PORT_OUT), state.getLastReceived(getPort(PORT_IN)));
} else {
state.pushValue(getPort(PORT_OUT), X_VALUE);
state.pushValue(getPort(PORT_OUT), Z_VALUE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
}

result.setBit(bit, portBit);
boolean isX = result.getBit(bit) == State.X;
boolean isX = result.getBit(bit) == State.Z;

for (int port = 1; port < numInputs; port++) {
portBit = state.getLastReceived(getPort(port)).getBit(bit);
if (negateInputs[port]) {
portBit = portBit.negate();
}

isX &= portBit == State.X;
isX &= portBit == State.Z;
result.setBit(bit, operate(result.getBit(bit), portBit));
}

if (isX) {
result.setBit(bit, State.X);
result.setBit(bit, State.Z);
} else if (negateOutput) {
result.setBit(bit, result.getBit(bit) == State.ONE ? State.ZERO : State.ONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ protected XorGate(String name, int bitSize, int numInputs, boolean[] negateInput

@Override
protected State operate(State acc, State bit) {
return acc != State.X && bit != State.X && acc != bit ? State.ONE : State.ZERO;
return acc != State.Z && bit != State.Z && acc != bit ? State.ONE : State.ZERO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
pushValue(state, State.ONE);
} else if (enable != State.ZERO && portIndex == PORT_CLOCK && value.getBit(0) == State.ONE) {
State d = state.getLastReceived(getPort(PORT_D)).getBit(0);
if (d != State.X) {
if (d != State.Z) {
pushValue(state, d);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
// The only other input Port are the indexed inputs
if (!this.isEnabled) {
state.pushValue(getEnabledOutPort(), new WireValue(1, State.ZERO));
state.pushValue(out, new WireValue(out.getLink().getBitSize(), State.X));
state.pushValue(out, new WireValue(out.getLink().getBitSize(), State.Z));
state.pushValue(getGroupSignalPort(), new WireValue(1, State.ZERO));
return;
}
Expand All @@ -59,7 +59,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {

if (highest == -1) {
state.pushValue(getEnabledOutPort(), new WireValue(1, State.ONE));
state.pushValue(out, new WireValue(out.getLink().getBitSize(), State.X));
state.pushValue(out, new WireValue(out.getLink().getBitSize(), State.Z));
state.pushValue(getGroupSignalPort(), new WireValue(1, State.ZERO));

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class SimpleTransistor extends Component {
public static final int PORT_GATE = 1;
public static final int PORT_DRAIN = 2;

private static final WireValue X_VALUE = new WireValue(1);
private static final WireValue Z_VALUE = new WireValue(1);

private boolean isIllegallyWired;
private boolean isPType;
Expand Down Expand Up @@ -124,7 +124,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
if (pTypeOk && nTypeOk && state.getLastReceived(getPort(PORT_GATE)).getBit(0) == enableBit) {
state.pushValue(getPort(PORT_DRAIN), new WireValue(sourceBit));
} else {
state.pushValue(getPort(PORT_DRAIN), X_VALUE);
state.pushValue(getPort(PORT_DRAIN), Z_VALUE);

if (!nTypeOk) {
throw new SimulationException("N-type transistor must not be connected to logic-level high!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Transistor extends Component {
public static final int PORT_GATE = 1;
public static final int PORT_OUT = 2;

private static final WireValue X_VALUE = new WireValue(1);
private static final WireValue Z_VALUE = new WireValue(1);

private State enableBit;

Expand All @@ -32,7 +32,7 @@ public void valueChanged(CircuitState state, WireValue value, int portIndex) {
if (state.getLastReceived(getPort(PORT_GATE)).getBit(0) == enableBit) {
state.pushValue(getPort(PORT_OUT), state.getLastReceived(getPort(PORT_IN)));
} else {
state.pushValue(getPort(PORT_OUT), X_VALUE);
state.pushValue(getPort(PORT_OUT), Z_VALUE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WireValueTest {
public void testBasic() {
WireValue value = new WireValue(10);
assertThat(value.getBitSize()).isEqualTo(10);
assertThat(WireValueUtils.allBitsEqualTo(value, State.X)).isTrue();
assertThat(WireValueUtils.allBitsEqualTo(value, State.Z)).isTrue();

value = new WireValue(15, State.ONE);
assertThat(value.getBitSize()).isEqualTo(15);
Expand Down Expand Up @@ -44,7 +44,7 @@ public void testMerge() {
value2.setBit(2, State.ZERO);

WireValue merge = new WireValue(value1).merge(value2);
assertThat(WireValueUtils.allBitsEqualTo(merge, State.ONE, State.ZERO, State.ZERO, State.X)).isTrue();
assertThat(WireValueUtils.allBitsEqualTo(merge, State.ONE, State.ZERO, State.ZERO, State.Z)).isTrue();
}

@Test
Expand Down

0 comments on commit d3c8006

Please sign in to comment.