fixed some minor issues on barrel shifter

This commit is contained in:
hneemann 2017-07-07 12:40:09 +02:00
parent fbcc3d6cf2
commit cbfa95f457
3 changed files with 85 additions and 85 deletions

View File

@ -8,17 +8,18 @@ import de.neemann.digital.core.element.Element;
import de.neemann.digital.core.element.ElementAttributes;
import de.neemann.digital.core.element.ElementTypeDescription;
import de.neemann.digital.core.element.Keys;
import static de.neemann.digital.core.element.PinInfo.input;
/**
* A barrelshifter
* A barrel shifter
*
* @author heintz
*/
public class BarrelShifter extends Node implements Element {
/**
* The barrelshifter description
* The barrel shifter description
*/
public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(BarrelShifter.class, input("in"), input("shift"))
.addAttribute(Keys.ROTATE)
@ -30,7 +31,7 @@ public class BarrelShifter extends Node implements Element {
private final ObservableValue out;
private final int bits;
private final int shiftbits;
private final int shiftBits;
private final BarrelShifterMode mode;
private final boolean signed;
private final LeftRightFormat direction;
@ -42,8 +43,7 @@ public class BarrelShifter extends Node implements Element {
/**
* Creates a new instance
*
* @param attributes
* the attributes
* @param attributes the attributes
*/
public BarrelShifter(ElementAttributes attributes) {
direction = attributes.get(Keys.DIRECTION);
@ -51,53 +51,53 @@ public class BarrelShifter extends Node implements Element {
bits = attributes.get(Keys.BITS);
signed = attributes.get(Keys.BARREL_SIGNED);
int sbits = 1;
while ((1 << sbits) < bits)
sbits++;
int sBits = 1;
while ((1 << sBits) < bits)
sBits++;
if (signed)
sbits++;
shiftbits = sbits;
sBits++;
shiftBits = sBits;
this.out = new ObservableValue("out", bits).setPinDescription(DESCRIPTION);
}
@Override
public void readInputs() throws NodeException {
long inval = in.getValue();
long shiftval;
long inVal = in.getValue();
long shiftVal;
if (signed) {
shiftval = shift.getValueSigned();
shiftVal = shift.getValueSigned();
} else {
shiftval = shift.getValue();
shiftVal = shift.getValue();
}
if (direction == LeftRightFormat.right) {
shiftval = -shiftval;
shiftVal = -shiftVal;
}
value = 0;
if (shiftval < 0) { // shift or rotate right
shiftval = -shiftval;
if (shiftVal < 0) { // shift or rotate right
shiftVal = -shiftVal;
if (mode == BarrelShifterMode.rotate) {
shiftval = shiftval % bits;
value |= inval << (bits - shiftval);
shiftVal = shiftVal % bits;
value |= inVal << (bits - shiftVal);
}
value |= inval >> shiftval;
if ((mode == BarrelShifterMode.arithmetic) && ((inval & (1 << (bits - 1))) != 0)) {
value |= inVal >> shiftVal;
if ((mode == BarrelShifterMode.arithmetic) && ((inVal & (1 << (bits - 1))) != 0)) {
int mask = (1 << (bits)) - 1;
mask = mask >> shiftval;
mask = mask >> shiftVal;
value |= ~mask;
}
} else { // shift or rotate left
if (mode == BarrelShifterMode.rotate) {
shiftval = shiftval % bits;
value |= inval >> (bits - shiftval);
shiftVal = shiftVal % bits;
value |= inVal >> (bits - shiftVal);
}
value |= inval << shiftval;
value |= inVal << shiftVal;
}
}
@ -109,7 +109,7 @@ public class BarrelShifter extends Node implements Element {
@Override
public void setInputs(ObservableValues inputs) throws NodeException {
in = inputs.get(0).addObserverToValue(this).checkBits(bits, this, 0);
shift = inputs.get(1).addObserverToValue(this).checkBits(shiftbits, this, 1);
shift = inputs.get(1).addObserverToValue(this).checkBits(shiftBits, this, 1);
}
@Override

View File

@ -212,7 +212,7 @@ Es können sowohl komplette Takte als auch einzelne Gatter-Veränderungen angeze
<string name="elem_Mul_pin_b">Eingang b für Multiplikation.</string>
<string name="elem_Mul_pin_mul">Ausgang mit dem Ergebnis der Multiplikation.</string>
<string name="elem_BarrelShifter">Verschieber</string>
<string name="elem_BarrelShifter_tt">Ein Baustein zum Bitschieben. Verschiebt Eingang in den an Eingang shift angegebenen Wert.</string>
<string name="elem_BarrelShifter_tt">Ein Baustein zum Bitschieben. Verschiebt einen Wert um die am Eingang shift angegebene Anzahl von Bits.</string>
<string name="elem_BarrelShifter_pin_in">Eingang mit zu verschiebenden Bits.</string>
<string name="elem_BarrelShifter_pin_shift">Eingang mit Weite der Verschiebung.</string>
<string name="elem_BarrelShifter_pin_out">Ausgang mit dem Ergebnis der Verschiebeoperation.</string>

View File

@ -15,8 +15,8 @@ import junit.framework.TestCase;
*/
public class BarrelShifterTest extends TestCase {
public void testNormalUnsignedLeft() throws Exception{
TestExecuter bsTest=getTestExecuter( BarrelShifterMode.normal, false, LeftRightFormat.left,6, 3);
public void testNormalUnsignedLeft() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.normal, false, LeftRightFormat.left, 6, 3);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b011000);
bsTest.check(0b001100, 2, 0b110000);
@ -28,8 +28,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b100000);
}
public void testRotateUnsignedLeft() throws Exception{
TestExecuter bsTest =getTestExecuter( BarrelShifterMode.rotate, false, LeftRightFormat.left,6, 3);
public void testRotateUnsignedLeft() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 6, 3);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b011000);
bsTest.check(0b001100, 2, 0b110000);
@ -41,8 +41,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b100001);
}
public void testNormalSignedLeft() throws Exception{
TestExecuter bsTest=getTestExecuter( BarrelShifterMode.normal, true , LeftRightFormat.left, 6, 4);
public void testNormalSignedLeft() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.normal, true, LeftRightFormat.left, 6, 4);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b011000);
bsTest.check(0b001100, 2, 0b110000);
@ -54,8 +54,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b000000);
}
public void testRotateSignedLeft() throws Exception{
TestExecuter bsTest =getTestExecuter( BarrelShifterMode.rotate, true , LeftRightFormat.left, 6, 4);
public void testRotateSignedLeft() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 6, 4);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b011000);
bsTest.check(0b001100, 2, 0b110000);
@ -67,8 +67,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b011000);
}
public void testNormalUnsignedRigth() throws Exception{
TestExecuter bsTest =getTestExecuter( BarrelShifterMode.normal, false, LeftRightFormat.right,6, 3);
public void testNormalUnsignedRight() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.normal, false, LeftRightFormat.right, 6, 3);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b000110);
bsTest.check(0b001100, 2, 0b000011);
@ -80,8 +80,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b000001);
}
public void testRotateUnsignedRigth() throws Exception{
TestExecuter bsTest =getTestExecuter( BarrelShifterMode.rotate, false, LeftRightFormat.right,6, 3);
public void testRotateUnsignedRight() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.right, 6, 3);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b000110);
bsTest.check(0b001100, 2, 0b000011);
@ -93,8 +93,8 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b100001);
}
public void testArithmeticUnsignedRigth() throws Exception{
TestExecuter bsTest =getTestExecuter( BarrelShifterMode.arithmetic, false, LeftRightFormat.right,6, 3);
public void testArithmeticUnsignedRight() throws Exception {
TestExecuter bsTest = getTestExecuter(BarrelShifterMode.arithmetic, false, LeftRightFormat.right, 6, 3);
bsTest.check(0b001100, 0, 0b001100);
bsTest.check(0b001100, 1, 0b000110);
bsTest.check(0b001100, 2, 0b000011);
@ -114,21 +114,21 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b101000, -5, 0b111101);
}
public void testShiftSizeCalculationTest() throws Exception{
getTestExecuter( BarrelShifterMode.rotate, false ,LeftRightFormat.left, 7, 3);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.left, 7, 4);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.right, 7, 4);
getTestExecuter( BarrelShifterMode.rotate, false ,LeftRightFormat.left, 8, 3);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.left, 8, 4);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.right, 8, 4);
getTestExecuter( BarrelShifterMode.rotate, false ,LeftRightFormat.left, 9, 4);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.left, 9, 5);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.right, 9, 5);
getTestExecuter( BarrelShifterMode.rotate, false ,LeftRightFormat.left, 16, 4);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.left, 16, 5);
getTestExecuter( BarrelShifterMode.rotate, false ,LeftRightFormat.left, 17, 5);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.left, 17, 6);
getTestExecuter( BarrelShifterMode.rotate, true ,LeftRightFormat.right, 17, 6);
public void testShiftSizeCalculationTest() throws Exception {
getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 7, 3);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 7, 4);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.right, 7, 4);
getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 8, 3);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 8, 4);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.right, 8, 4);
getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 9, 4);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 9, 5);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.right, 9, 5);
getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 16, 4);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 16, 5);
getTestExecuter(BarrelShifterMode.rotate, false, LeftRightFormat.left, 17, 5);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.left, 17, 6);
getTestExecuter(BarrelShifterMode.rotate, true, LeftRightFormat.right, 17, 6);
}
private TestExecuter getTestExecuter(BarrelShifterMode mode, boolean signed, LeftRightFormat direction, int valueWidth, int shiftWidth) throws Exception {
@ -136,10 +136,10 @@ public class BarrelShifterTest extends TestCase {
ObservableValue shift = new ObservableValue("shift", shiftWidth);
ElementAttributes attributes = new ElementAttributes()
.set(Keys.BARREL_SHIFTER_MODE,mode)
.set(Keys.BARREL_SIGNED,signed)
.set(Keys.DIRECTION,direction)
.set(Keys.BITS,valueWidth);
.set(Keys.BARREL_SHIFTER_MODE, mode)
.set(Keys.BARREL_SIGNED, signed)
.set(Keys.DIRECTION, direction)
.set(Keys.BITS, valueWidth);
Model model = new Model();
BarrelShifter bs = new BarrelShifter(attributes);