用Java模拟ADC指令

问题描述 投票:1回答:1

我正在尝试用Java创建MOS6502的ADC指令。更具体地说,ADC十进制指令使用BCD整数表示而不是2的补码表示。该指令被修改为仅涉及添加8位数字,而不是像正常指令那样添加16位数字。

我有下面的代码成功地添加1 + 1,但在尝试添加$ 49(73)和1时输出0,我不知道为什么会发生这种情况。

在这些测试中,进位位始终设置为0。

private int adcDecimal(int a, int op)
    {
        System.out.println("[CPU] ADC-Decimal: " + a + " + " + op + " + " + getCarryBit());

        int tmp;
        int result;

        tmp = (a & 0x0f) + (op & 0x0f) + getCarryBit();
        if((tmp & 0xff) > 9) 
            tmp += 6;

        result = (tmp & 0x0f);
        result &= 0xff;

        setCarryFlag(tmp > 7);
        setZeroFlag(result == 0);
        setOverflowFlag(false);

        negativeFlag = (result & 0x80) != 0;

        System.out.println("[CPU] ADC-Decimal result: " + result);

        return result;
    }
java bcd
1个回答
0
投票

码:

public class MOS6502 {

    boolean negativeFlag;

    private int getCarryBit() { return 0; }
    private void setCarryFlag( boolean flag ) {}
    private void setZeroFlag( boolean flag ) {}
    private void setOverflowFlag( boolean flag ) {}

    private int adcDecimal( int a, int b ) {
        System.out.printf("[CPU] ADC-Decimal: %04X + %04X + %d",
            a, b, getCarryBit());
        int tmp =
            10*(( a & 0xf0 ) >> 4 ) +
                ( a & 0x0f ) +
            10*(( b & 0xf0 ) >> 4 ) +
                ( b & 0x0f ) +
            getCarryBit();
        setCarryFlag( tmp > 0x99 );
        int result = 0;
        int digit = 0;
        while( tmp > 0 ) {
            result |= (tmp % 10) << (4*digit++);
            tmp /= 10;
        }
        setZeroFlag( result == 0 );
        setOverflowFlag( false );
        negativeFlag = (result & 0x80) != 0;
        System.out.printf(", result: %04X\n", result);
        return result;
    }

    public static void main(String[] args) throws Exception {
        MOS6502 mos6502 = new MOS6502();
        mos6502.adcDecimal(    1,    1 );
        mos6502.adcDecimal(    1,    4 );
        mos6502.adcDecimal(    8,    5 );
        mos6502.adcDecimal( 0x48, 0x49 );
    }
}

输出:

$ javac MOS6502.java ; java MOS6502
[CPU] ADC-Decimal: 0001 + 0001 + 0, result: 0002
[CPU] ADC-Decimal: 0001 + 0004 + 0, result: 0005
[CPU] ADC-Decimal: 0008 + 0005 + 0, result: 0013
[CPU] ADC-Decimal: 0048 + 0049 + 0, result: 0097
© www.soinside.com 2019 - 2024. All rights reserved.