projects
/
Mograsim.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
1ddc8ec
)
Refactored BitVector methods to resolve ambiguity
author
Christian Femers
<femers@in.tum.de>
Thu, 11 Jul 2019 19:51:38 +0000
(21:51 +0200)
committer
Christian Femers
<femers@in.tum.de>
Thu, 11 Jul 2019 19:51:38 +0000
(21:51 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
patch
|
blob
|
history
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
patch
|
blob
|
history
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
patch
|
blob
|
history
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
patch
|
blob
|
history
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
patch
|
blob
|
history
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
patch
|
blob
|
history
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
patch
|
blob
|
history
net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
patch
|
blob
|
history
diff --git
a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
index
4fce8dd
..
8bff357
100644
(file)
--- a/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
+++ b/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/Bit.java
@@
-91,6
+91,11
@@
public enum Bit implements StrictLogicType<Bit>
return values()[2 + (value & 1)];
}
return values()[2 + (value & 1)];
}
+ public static Bit of(boolean binaryValue)
+ {
+ return binaryValue ? ONE : ZERO;
+ }
+
public static Bit parse(String s)
{
Bit bit = SYMBOL_MAP.get(s);
public static Bit parse(String s)
{
Bit bit = SYMBOL_MAP.get(s);
diff --git
a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
index
9e09f43
..
cce83f4
100644
(file)
--- a/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
+++ b/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
@@
-13,7
+13,6
@@
import java.util.function.UnaryOperator;
/**
* Immutable class representing a {@link Bit}Vector
*
/**
* Immutable class representing a {@link Bit}Vector
*
- *
* @author Christian Femers
*
*/
* @author Christian Femers
*
*/
@@
-23,7
+22,7
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
private BitVector(Bit[] bits)
{
private BitVector(Bit[] bits)
{
- this.bits = Objects.requireNonNull(bits);// do this first to "catch" bits==null before the foreach loop
+ this.bits = Objects.requireNonNull(bits);
// do this first to "catch" bits==null before the foreach loop
for (Bit bit : bits)
if (bit == null)
throw new NullPointerException();
for (Bit bit : bits)
if (bit == null)
throw new NullPointerException();
@@
-44,11
+43,22
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
return BitVectorMutator.of(this);
}
return BitVectorMutator.of(this);
}
- public Bit getBit(int bitIndex)
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public Bit[] getBits()
{
return bits.clone();
public Bit[] getBits()
{
return bits.clone();
@@
-224,16
+234,38
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
return this;
}
return this;
}
- public void setBit(int bitIndex, Bit bit)
+ /**
+ * Set the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public void setMSBit(int bitIndex, Bit bit)
{
bits[bitIndex] = bit;
}
{
bits[bitIndex] = bit;
}
- public Bit getBit(int bitIndex)
+ /**
+ * Set the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public void setLSBit(int bitIndex, Bit bit)
+ {
+ bits[bits.length - bitIndex - 1] = bit;
+ }
+
+ /**
+ * Returns the most significant bit at <code>bitIndex</code>. (leftmost bit of a binary number at the given index)
+ */
+ public Bit getMSBit(int bitIndex)
{
return bits[bitIndex];
}
{
return bits[bitIndex];
}
+ /**
+ * Returns the least significant bit at <code>bitIndex</code>. (rightmost bit of a binary number at the given index)
+ */
+ public Bit getLSBit(int bitIndex)
+ {
+ return bits[bits.length - bitIndex - 1];
+ }
+
public int length()
{
return bits.length;
public int length()
{
return bits.length;
@@
-286,45
+318,26
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
}
return Arrays.equals(bits, offset, offset + other.length(), other.bits, 0, other.length());
}
- @Override
- public String toString()
- {
- return toBitStringMSBFirst();
- }
-
- /**
- * All {@link Bit}s symbols concatenated together
- *
- * @see #parse(String)
- */
- public String toBitStringLSBFirst()
- {
- StringBuilder sb = new StringBuilder(bits.length);
- for (Bit bit : bits)
- sb.append(bit);
- return sb.toString();
- }
-
/**
/**
- * All {@link Bit}s symbols concatenated together
, with the MSB coming first (like a binary number
)
+ * All {@link Bit}s symbols concatenated together
(MSB first
)
*
* @see #parse(String)
*/
*
* @see #parse(String)
*/
- public String toBitStringMSBFirst()
+ @Override
+ public String toString()
{
StringBuilder sb = new StringBuilder(bits.length);
for (Bit bit : bits)
sb.append(bit);
{
StringBuilder sb = new StringBuilder(bits.length);
for (Bit bit : bits)
sb.append(bit);
- sb.reverse();
return sb.toString();
}
/**
return sb.toString();
}
/**
- * Parses a String containing solely {@link Bit} symbols
+ * Parses a String containing solely {@link Bit} symbols
(MSB first)
*
*
- * @see #to
BitStringLSBFirst
()
+ * @see #to
String
()
*/
*/
- public static BitVector parse
LSBFirst
(String s)
+ public static BitVector parse(String s)
{
Bit[] values = new Bit[s.length()];
for (int i = 0; i < s.length(); i++)
{
Bit[] values = new Bit[s.length()];
for (int i = 0; i < s.length(); i++)
@@
-335,20
+348,8
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
}
/**
}
/**
- * Parses a String containing solely {@link Bit} symbols, with the MSB coming first (like a binary number)
- *
- * @see #toBitStringLSBFirst()
+ * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
*/
*/
- public static BitVector parseMSBFirst(String s)
- {
- Bit[] values = new Bit[s.length()];
- for (int i = 0, j = s.length() - 1; j >= 0; i++, j--)
- {
- values[i] = Bit.parse(s, j);
- }
- return new BitVector(values);
- }
-
@Override
public Iterator<Bit> iterator()
{
@Override
public Iterator<Bit> iterator()
{
@@
-361,7
+362,7
@@
public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
{
if (!hasNext())
throw new NoSuchElementException();
{
if (!hasNext())
throw new NoSuchElementException();
- return getBit(pos++);
+ return get
MS
Bit(pos++);
}
@Override
}
@Override
diff --git
a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
index
75e6fc7
..
858f28a
100644
(file)
--- a/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
+++ b/
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
@@
-16,7
+16,7
@@
public class BitVectorFormatter
{
\r
if (bitVector == null)
\r
return "null";
\r
{
\r
if (bitVector == null)
\r
return "null";
\r
- return bitVector.to
BitStringMSBFirst
();
\r
+ return bitVector.to
String
();
\r
}
\r
\r
// TODO doesn't this belong to logic.ui?
\r
}
\r
\r
// TODO doesn't this belong to logic.ui?
\r
@@
-30,7
+30,7
@@
public class BitVectorFormatter
// TODO maybe find a color assignment for multiple-bit bit vectors?
\r
if (bitVector == null || bitVector.length() != 1)
\r
return new ColorDefinition(BuiltInColor.COLOR_BLACK);
\r
// TODO maybe find a color assignment for multiple-bit bit vectors?
\r
if (bitVector == null || bitVector.length() != 1)
\r
return new ColorDefinition(BuiltInColor.COLOR_BLACK);
\r
- switch (bitVector.getBit(0))
\r
+ switch (bitVector.get
LS
Bit(0))
\r
{
\r
case ONE:
\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.one");
\r
{
\r
case ONE:
\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.one");
\r
@@
-43,7
+43,7
@@
public class BitVectorFormatter
case ZERO:
\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.zero");
\r
default:
\r
case ZERO:
\r
return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.zero");
\r
default:
\r
- throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0));
\r
+ throw new IllegalArgumentException("Unknown enum constant: " + bitVector.get
LS
Bit(0));
\r
}
\r
}
\r
\r
}
\r
}
\r
\r
diff --git
a/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
b/net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
index
f424471
..
6310d5b
100644
(file)
--- a/
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
+++ b/
net.mograsim.logic.core/src/net/mograsim/logic/core/wires/Wire.java
@@
-150,14
+150,20
@@
public class Wire
return val;
\r
}
\r
\r
return val;
\r
}
\r
\r
+ /**
\r
+ * Returns the least significant bit (LSB)
\r
+ */
\r
public Bit getValue()
\r
{
\r
return getValue(0);
\r
}
\r
\r
public Bit getValue()
\r
{
\r
return getValue(0);
\r
}
\r
\r
+ /**
\r
+ * Returns the least significant bit (LSB) of the given index
\r
+ */
\r
public Bit getValue(int index)
\r
{
\r
public Bit getValue(int index)
\r
{
\r
- return values.getBit(index);
\r
+ return values.get
LS
Bit(index);
\r
}
\r
\r
public BitVector getValues(int start, int end)
\r
}
\r
\r
public BitVector getValues(int start, int end)
\r
@@
-446,7
+452,8
@@
public class Wire
}
\r
\r
/**
\r
}
\r
\r
/**
\r
- * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.
\r
+ * @return The value (of bit 0) the {@link ReadEnd} is currently feeding into the associated {@link Wire}.Returns the least
\r
+ * significant bit (LSB)
\r
*/
\r
public Bit getInputValue()
\r
{
\r
*/
\r
public Bit getInputValue()
\r
{
\r
@@
-455,10
+462,12
@@
public class Wire
\r
/**
\r
* @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
\r
/**
\r
* @return The value which the {@link ReadEnd} is currently feeding into the associated {@link Wire} at the indexed {@link Bit}.
\r
+ * Returns the least significant bit (LSB)
\r
+ *
\r
*/
\r
public Bit getInputValue(int index)
\r
{
\r
*/
\r
public Bit getInputValue(int index)
\r
{
\r
- return inputValues.getBit(index);
\r
+ return inputValues.get
LS
Bit(index);
\r
}
\r
\r
/**
\r
}
\r
\r
/**
\r
@@
-466,7
+475,7
@@
public class Wire
*/
\r
public BitVector getInputValues()
\r
{
\r
*/
\r
public BitVector getInputValues()
\r
{
\r
- return
getInputValues(0, length)
;
\r
+ return
inputValues
;
\r
}
\r
\r
public BitVector getInputValues(int start, int end)
\r
}
\r
\r
public BitVector getInputValues(int start, int end)
\r
diff --git
a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
index
c5d54c2
..
20fbbaa
100644
(file)
--- a/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
+++ b/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUI_rsLatch.java
@@
-94,7
+94,7
@@
public class GUI_rsLatch extends SimpleRectangularSubmodelComponent
{
case "q":
if (wireQ.hasLogicModelBinding())
{
case "q":
if (wireQ.hasLogicModelBinding())
- return wireQ.getWireValues().getBit(0);
+ return wireQ.getWireValues().get
LS
Bit(0);
return null;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
return null;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
diff --git
a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
index
3468798
..
875843f
100644
(file)
--- a/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
+++ b/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/GUIdlatch4.java
@@
-103,10
+103,10
@@
public class GUIdlatch4 extends SimpleRectangularSubmodelComponent
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
- setHighLevelState("q1", newStateCasted.getBit(0));
- setHighLevelState("q2", newStateCasted.getBit(1));
- setHighLevelState("q3", newStateCasted.getBit(2));
- setHighLevelState("q4", newStateCasted.getBit(3));
+ setHighLevelState("q1", newStateCasted.get
LS
Bit(0));
+ setHighLevelState("q2", newStateCasted.get
LS
Bit(1));
+ setHighLevelState("q3", newStateCasted.get
LS
Bit(2));
+ setHighLevelState("q4", newStateCasted.get
LS
Bit(3));
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
@@
-132,7
+132,7
@@
public class GUIdlatch4 extends SimpleRectangularSubmodelComponent
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
- return BitVector.of(q
1, q2, q3, q4
);
+ return BitVector.of(q
4, q3, q2, q1
);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
diff --git
a/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
b/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
index
9902b24
..
efae9af
100644
(file)
--- a/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
+++ b/
net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/model/components/mi/nandbased/am2901/GUIAm2901QReg.java
@@
-111,10
+111,10
@@
public class GUIAm2901QReg extends SimpleRectangularSubmodelComponent
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
break;
case "q":
BitVector newStateCasted = (BitVector) newState;
- setHighLevelState("q1", newStateCasted.getBit(0));
- setHighLevelState("q2", newStateCasted.getBit(1));
- setHighLevelState("q3", newStateCasted.getBit(2));
- setHighLevelState("q4", newStateCasted.getBit(3));
+ setHighLevelState("q1", newStateCasted.get
LS
Bit(0));
+ setHighLevelState("q2", newStateCasted.get
LS
Bit(1));
+ setHighLevelState("q3", newStateCasted.get
LS
Bit(2));
+ setHighLevelState("q4", newStateCasted.get
LS
Bit(3));
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
break;
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
@@
-140,7
+140,7
@@
public class GUIAm2901QReg extends SimpleRectangularSubmodelComponent
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
Bit q2 = (Bit) getHighLevelState("q2");
Bit q3 = (Bit) getHighLevelState("q3");
Bit q4 = (Bit) getHighLevelState("q4");
- return BitVector.of(q
1, q2, q3, q4
);
+ return BitVector.of(q
4, q3, q2, q1
);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
default:
// should not happen because we tell SubmodelComponent to only allow these state IDs.
throw new IllegalStateException("Illegal atomic state ID: " + stateID);
diff --git
a/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
b/net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
index
55fd8cf
..
609bf74
100644
(file)
--- a/
net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
+++ b/
net.mograsim.logic.model/src/net/mograsim/logic/model/LogicUICanvas.java
@@
-122,7
+122,7
@@
public class LogicUICanvas extends ZoomableCanvas
if (radioBit.getSelection())
value = Bit.parse(valueString);
else if (radioBitVector.getSelection())
if (radioBit.getSelection())
value = Bit.parse(valueString);
else if (radioBitVector.getSelection())
- value = BitVector.parse
MSBFirst
(valueString);
+ value = BitVector.parse(valueString);
else
throw new RuntimeException("No value type selected");
target.setHighLevelState(stateIDText.getText(), value);
else
throw new RuntimeException("No value type selected");
target.setHighLevelState(stateIDText.getText(), value);