我的位级别添加功能总是将第一位设置为0

时间:2016-02-07 03:18:10

标签: c++

我正在尝试为我的模拟器构建位级添加 它在大多数情况下都有效,但我的累加器寄存器的第一位总是设置为0,我无法弄清楚为什么?

int main()
{
    MCS6502 processor;
    char mem1 = 0b00000001;
    char mem2 = 0b00000001;
    char mem3 = 0b00000000;

    processor.LDA(mem1); //loads memory into accumulator
    processor.ADC(mem2); //adds the inputed address to the accumulator
    processor.STA(mem3); //stores the accumulator in memory

    std::cout << +mem3 << std::endl;
    return 0;
}


void MCS6502::LDA(const char &address)
{
    registers[0] = address; //loads Accumulator
}

void MCS6502::STA(char &address)
{
    address = registers[0]; //stores Accumulator
}

void MCS6502::ADC(const char &address)
{
    char carry = 0b00000000;

    if (flags[0] == true)
    {
        carry = 0b00000001;
    }

    char accumulator_bit_array[7];
    char address_bit_array[7];
    char result_bit_array[7];

    ByteToBitArray(accumulator_bit_array, registers[0]);

    std::cout << "accum bit after the function has ran " << +accumulator_bit_array[0] << std::endl;

    ByteToBitArray(address_bit_array, address);

    std::cout << "address bit after the function has ran " << +address_bit_array[0] << std::endl;


    int i = 0;

    while (i < 8)
    {
        std::cout << "counter "<< i << std::endl;
        std::cout << "Accumulator bit in = " << +accumulator_bit_array[i] << std::endl;
        std::cout << "Address bit in = " << +address_bit_array[i] << std::endl;
        result_bit_array[i] = BitwiseAdd(accumulator_bit_array[i], address_bit_array[i], carry);
        std::cout << "Bitwise Add restult = " << +result_bit_array[i] << std::endl;
        i++;
    }

    registers[0] = BitArrayToByte(result_bit_array);

    if (carry == 0b00000001)
    {
        flags[0] = true;
    }

    if (carry == 0b00000000)
    {
        flags[0] = false;
    }
}


char BitwiseAdd(const char &bit1, const char &bit2, char &carry)
{
    char xor_ = bit1 ^ bit2;
    char ret = carry ^ xor_;
    carry = (carry & xor_) | (bit1 & bit2);
    return ret;
}

void ByteToBitArray(char bit_array[], const char &byte)
{
    std::cout << "Byte inside array function before conversion " << +byte << std::endl;

    bit_array[0] = byte & 0b00000001;
    bit_array[1] = byte & 0b00000010;
    bit_array[2] = byte & 0b00000100;
    bit_array[3] = byte & 0b00001000;
    bit_array[4] = byte & 0b00010000;
    bit_array[5] = byte & 0b00100000;
    bit_array[6] = byte & 0b01000000;
    bit_array[7] = byte & 0b10000000;

    bit_array[1] = bit_array[1] >> 1;
    bit_array[2] = bit_array[2] >> 2;
    bit_array[3] = bit_array[3] >> 3;
    bit_array[4] = bit_array[4] >> 4;
    bit_array[5] = bit_array[5] >> 5;
    bit_array[6] = bit_array[6] >> 6;
    bit_array[7] = bit_array[7] >> 7;

    std::cout << "Bit inside array function after conversion " << +bit_array[0] << std::endl;
}

char BitArrayToByte(char bit_array[])
{
    char byte = 0b00000000;

    bit_array[1] = bit_array[1] << 1;
    bit_array[2] = bit_array[2] << 2;
    bit_array[3] = bit_array[3] << 3;
    bit_array[4] = bit_array[4] << 4;
    bit_array[5] = bit_array[5] << 5;
    bit_array[6] = bit_array[6] << 6;
    bit_array[7] = bit_array[7] << 7;

    int i = 0;
    while (i < 8)
    {
        byte = byte | bit_array[i];
        i++;
    }

    return byte;
}

我运行时的输出是:

Byte inside array function before conversion 1
Bit inside array function after conversion 1
accum bit after the function has ran 1
Byte inside array function before conversion 1
Bit inside array function after conversion 1
address bit after the function has ran 1
counter 0
Accumulator bit in = 0
Address bit in = 1
Bitwise Add restult = 1
counter 1
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 2
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 3
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 4
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 5
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 6
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
counter 7
Accumulator bit in = 0
Address bit in = 0
Bitwise Add restult = 0
1

它应该是2,我无法弄清楚它为什么不起作用。

1 个答案:

答案 0 :(得分:1)

ADC中的3个数组中有7个元素,但是你传递它们的函数是8。这导致了未定义的行为,这里显示为覆盖下一个数组的第一个字节。