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,6 +8,7 @@ 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;
/**
@ -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

@ -67,7 +67,7 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b011000);
}
public void testNormalUnsignedRigth() throws Exception{
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);
@ -80,7 +80,7 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b000001);
}
public void testRotateUnsignedRigth() throws Exception{
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);
@ -93,7 +93,7 @@ public class BarrelShifterTest extends TestCase {
bsTest.check(0b001100, -5, 0b100001);
}
public void testArithmeticUnsignedRigth() throws Exception{
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);