diff --git a/src/Interpreter/Interpreter.cpp b/src/Interpreter/Interpreter.cpp index d5fd8a9..bf16105 100644 --- a/src/Interpreter/Interpreter.cpp +++ b/src/Interpreter/Interpreter.cpp @@ -363,16 +363,9 @@ void Interpreter::add_vx_vy(const Instruction& instruction) const const auto& vy = this->machine_state->v[instruction.y]; auto& vf = this->machine_state->v[0xF]; - if (vx + vy > 0x100) - { - vf = 1; - } - else - { - vf = 0; - } - + const bool carry = vx + vy > 0x100; vx = (vx + vy) & 0xFF; + vf = carry ? 1 : 0; } void Interpreter::sub_vx_vy(const Instruction& instruction) const @@ -381,16 +374,9 @@ void Interpreter::sub_vx_vy(const Instruction& instruction) const const auto& vy = this->machine_state->v[instruction.y]; auto& vf = this->machine_state->v[0xF]; - if (vx >= vy) - { - vf = 1; - } - else - { - vf = 0; - } - + const bool carry = vx >= vy; vx = (vx - vy) & 0xFF; + vf = carry ? 1 : 0; } void Interpreter::shr_vx_vy(const Instruction& instruction) const @@ -404,16 +390,9 @@ void Interpreter::shr_vx_vy(const Instruction& instruction) const vx = vy; } - if (vx & 0x01) - { - vf = 1; - } - else - { - vf = 0; - } - + const bool carry = vx & 0x01; vx = vx >> 1; + vf = carry ? 1 : 0; } void Interpreter::subn_vx_vy(const Instruction& instruction) const @@ -422,16 +401,9 @@ void Interpreter::subn_vx_vy(const Instruction& instruction) const const auto& vy = this->machine_state->v[instruction.y]; auto& vf = this->machine_state->v[0xF]; - if (vy >= vx) - { - vf = 1; - } - else - { - vf = 0; - } - + const bool carry = vy >= vx; vx = (vy - vx) & 0xFF; + vf = carry ? 1 : 0; } void Interpreter::shl_vx_vy(const Instruction& instruction) const @@ -445,16 +417,9 @@ void Interpreter::shl_vx_vy(const Instruction& instruction) const vx = vy; } - if (vx & 0x80) - { - vf = 1; - } - else - { - vf = 0; - } - + const bool carry = vx & 0x80; vx = vx << 1 & 0xFF; + vf = carry ? 1 : 0; } void Interpreter::sne_vx_vy(const Instruction& instruction) const @@ -503,6 +468,7 @@ void Interpreter::drw_vx_vy_nibble(const Instruction& instruction) const const uint8_t start_y = vy & 31; vf = 0; + bool carry = false; for (auto row = 0; row < instruction.n; row++) { const auto current_y = start_y + row; @@ -531,7 +497,7 @@ void Interpreter::drw_vx_vy_nibble(const Instruction& instruction) const if (display[index]) { display[index] = false; - vf = 1; + carry = true; } else { @@ -540,6 +506,8 @@ void Interpreter::drw_vx_vy_nibble(const Instruction& instruction) const } } } + + vf = carry ? 1 : 0; } void Interpreter::skp_vx(const Instruction& instruction) const