added bit count element to gui

This commit is contained in:
hneemann 2016-08-09 09:55:32 +02:00
parent 840c2c9805
commit d7c0052b82
5 changed files with 27 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import de.neemann.digital.core.element.Keys;
import static de.neemann.digital.core.element.PinInfo.input; import static de.neemann.digital.core.element.PinInfo.input;
/** /**
* Negation, twos complement * Bit count
* *
* @author hneemann * @author hneemann
*/ */
@ -38,8 +38,8 @@ public class BitCount extends Node implements Element {
public BitCount(ElementAttributes attributes) { public BitCount(ElementAttributes attributes) {
inBits = attributes.get(Keys.BITS); inBits = attributes.get(Keys.BITS);
int outBits = 1; int outBits = 1;
while (outBits < inBits) while ((1 << outBits) <= inBits)
outBits *= 2; outBits++;
output = new ObservableValue("out", outBits); output = new ObservableValue("out", outBits);
} }

View File

@ -95,6 +95,7 @@ public class ElementLibrary implements Iterable<ElementLibrary.ElementContainer>
add(Mul.DESCRIPTION, menu); add(Mul.DESCRIPTION, menu);
add(Comparator.DESCRIPTION, menu); add(Comparator.DESCRIPTION, menu);
add(Neg.DESCRIPTION, menu); add(Neg.DESCRIPTION, menu);
add(BitCount.DESCRIPTION, menu);
menu = Lang.get("lib_test"); menu = Lang.get("lib_test");
add(TestCaseElement.TESTCASEDESCRIPTION, menu); add(TestCaseElement.TESTCASEDESCRIPTION, menu);

View File

@ -61,6 +61,10 @@ Auf diese Weise kann jedes kombinatorische Gatter erzeugt werden.</string>
<string name="elem_NAnd">Nicht Und</string> <string name="elem_NAnd">Nicht Und</string>
<string name="elem_NOr">Nicht Oder</string> <string name="elem_NOr">Nicht Oder</string>
<string name="elem_Not">Nicht</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_Or">Oder</string>
<string name="elem_Out">Ausgang</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> <string name="elem_Out_tt">Ein Ausgang der genutzt werden kann, um eine Verbindung zu einem eingebettetem Element herzustellen.</string>

View File

@ -61,6 +61,10 @@ So this gate can emulate every combinatorial gate.</string>
<string name="elem_NAnd">NAnd</string> <string name="elem_NAnd">NAnd</string>
<string name="elem_NOr">NOr</string> <string name="elem_NOr">NOr</string>
<string name="elem_Not">Not</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_Or">Or</string>
<string name="elem_Out">Out</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> <string name="elem_Out_tt">A output which can be used to connect the circuit if it is included in an other circuit.</string>

View File

@ -3,6 +3,7 @@ package de.neemann.digital.core.arithmetic;
import de.neemann.digital.TestExecuter; import de.neemann.digital.TestExecuter;
import de.neemann.digital.core.Model; import de.neemann.digital.core.Model;
import de.neemann.digital.core.ObservableValue; import de.neemann.digital.core.ObservableValue;
import de.neemann.digital.core.ObservableValues;
import de.neemann.digital.core.element.ElementAttributes; import de.neemann.digital.core.element.ElementAttributes;
import junit.framework.TestCase; import junit.framework.TestCase;
@ -11,9 +12,9 @@ import junit.framework.TestCase;
*/ */
public class BitCountTest extends TestCase { public class BitCountTest extends TestCase {
public void testBitCount() throws Exception { public void testBitCount() throws Exception {
ObservableValue a = new ObservableValue("a", 6); ObservableValue a = new ObservableValue("a", 6);
ObservableValue b = new ObservableValue("b", 3);
Model model = new Model(); Model model = new Model();
@ -21,7 +22,11 @@ public class BitCountTest extends TestCase {
node.setInputs(a.asList()); node.setInputs(a.asList());
model.add(node); 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(0, 0);
sc.check(1, 1); sc.check(1, 1);
sc.check(2, 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());
}
} }