mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-17 08:55:05 -04:00
added bit count element to gui
This commit is contained in:
parent
840c2c9805
commit
d7c0052b82
@ -12,7 +12,7 @@ import de.neemann.digital.core.element.Keys;
|
||||
import static de.neemann.digital.core.element.PinInfo.input;
|
||||
|
||||
/**
|
||||
* Negation, twos complement
|
||||
* Bit count
|
||||
*
|
||||
* @author hneemann
|
||||
*/
|
||||
@ -38,8 +38,8 @@ public class BitCount extends Node implements Element {
|
||||
public BitCount(ElementAttributes attributes) {
|
||||
inBits = attributes.get(Keys.BITS);
|
||||
int outBits = 1;
|
||||
while (outBits < inBits)
|
||||
outBits *= 2;
|
||||
while ((1 << outBits) <= inBits)
|
||||
outBits++;
|
||||
output = new ObservableValue("out", outBits);
|
||||
}
|
||||
|
||||
|
@ -95,6 +95,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
|
||||
add(Mul.DESCRIPTION, menu);
|
||||
add(Comparator.DESCRIPTION, menu);
|
||||
add(Neg.DESCRIPTION, menu);
|
||||
add(BitCount.DESCRIPTION, menu);
|
||||
|
||||
menu = Lang.get("lib_test");
|
||||
add(TestCaseElement.TESTCASEDESCRIPTION, menu);
|
||||
|
@ -61,6 +61,10 @@ Auf diese Weise kann jedes kombinatorische Gatter erzeugt werden.</string>
|
||||
<string name="elem_NAnd">Nicht Und</string>
|
||||
<string name="elem_NOr">Nicht Oder</string>
|
||||
<string name="elem_Not">Nicht</string>
|
||||
<string name="elem_Neg">Negation</string>
|
||||
<string name="elem_Neg_tt">Negation im 2-er Komplement</string>
|
||||
<string name="elem_BitCount">Bitzähler</string>
|
||||
<string name="elem_BitCount_tt">Gibt die Anzahl der 1-Bits im Eingangswert aus.</string>
|
||||
<string name="elem_Or">Oder</string>
|
||||
<string name="elem_Out">Ausgang</string>
|
||||
<string name="elem_Out_tt">Ein Ausgang der genutzt werden kann, um eine Verbindung zu einem eingebettetem Element herzustellen.</string>
|
||||
|
@ -61,6 +61,10 @@ So this gate can emulate every combinatorial gate.</string>
|
||||
<string name="elem_NAnd">NAnd</string>
|
||||
<string name="elem_NOr">NOr</string>
|
||||
<string name="elem_Not">Not</string>
|
||||
<string name="elem_Neg">Negation</string>
|
||||
<string name="elem_Neg_tt">Negation in the 2th complement</string>
|
||||
<string name="elem_BitCount">Bit counter</string>
|
||||
<string name="elem_BitCount_tt">Returns the number of 1-bits in the input value.</string>
|
||||
<string name="elem_Or">Or</string>
|
||||
<string name="elem_Out">Out</string>
|
||||
<string name="elem_Out_tt">A output which can be used to connect the circuit if it is included in an other circuit.</string>
|
||||
|
@ -3,6 +3,7 @@ package de.neemann.digital.core.arithmetic;
|
||||
import de.neemann.digital.TestExecuter;
|
||||
import de.neemann.digital.core.Model;
|
||||
import de.neemann.digital.core.ObservableValue;
|
||||
import de.neemann.digital.core.ObservableValues;
|
||||
import de.neemann.digital.core.element.ElementAttributes;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
@ -11,9 +12,9 @@ import junit.framework.TestCase;
|
||||
*/
|
||||
public class BitCountTest extends TestCase {
|
||||
|
||||
|
||||
public void testBitCount() throws Exception {
|
||||
ObservableValue a = new ObservableValue("a", 6);
|
||||
ObservableValue b = new ObservableValue("b", 3);
|
||||
|
||||
|
||||
Model model = new Model();
|
||||
@ -21,7 +22,11 @@ public class BitCountTest extends TestCase {
|
||||
node.setInputs(a.asList());
|
||||
model.add(node);
|
||||
|
||||
TestExecuter sc = new TestExecuter(model).setInputs(a).setOutputs(node.getOutputs());
|
||||
ObservableValues outputs = node.getOutputs();
|
||||
assertEquals(1, outputs.size());
|
||||
assertEquals(3, outputs.get(0).getBits());
|
||||
|
||||
TestExecuter sc = new TestExecuter(model).setInputs(a).setOutputs(outputs);
|
||||
sc.check(0, 0);
|
||||
sc.check(1, 1);
|
||||
sc.check(2, 1);
|
||||
@ -39,4 +44,12 @@ public class BitCountTest extends TestCase {
|
||||
}
|
||||
|
||||
|
||||
public void testBits() {
|
||||
assertEquals(1, new BitCount(new ElementAttributes().setBits(1)).getOutputs().get(0).getBits());
|
||||
assertEquals(2, new BitCount(new ElementAttributes().setBits(2)).getOutputs().get(0).getBits());
|
||||
assertEquals(3, new BitCount(new ElementAttributes().setBits(6)).getOutputs().get(0).getBits());
|
||||
assertEquals(3, new BitCount(new ElementAttributes().setBits(7)).getOutputs().get(0).getBits());
|
||||
assertEquals(4, new BitCount(new ElementAttributes().setBits(8)).getOutputs().get(0).getBits());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user