From 9c240b7ea841eb0a6b20c5857d764f65f35d8088 Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Wed, 19 Jul 2017 23:16:17 +0100 Subject: [PATCH] Correct for CF not being the first bit of the flags register (why!!!!) Signed-off-by: Adrian.Conlon --- LR35902/src/LR35902.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/LR35902/src/LR35902.cpp b/LR35902/src/LR35902.cpp index 621e674..7ee6a87 100644 --- a/LR35902/src/LR35902.cpp +++ b/LR35902/src/LR35902.cpp @@ -136,7 +136,8 @@ void EightBit::LR35902::sbc(uint8_t& f, register16_t& operand, register16_t valu auto before = operand; - auto result = before.word - value.word - (f & CF); + auto carry = (f & CF) >> 4; + auto result = before.word - value.word - carry; operand.word = result; clearFlag(f, ZF, operand.word); @@ -149,7 +150,8 @@ void EightBit::LR35902::adc(uint8_t& f, register16_t& operand, register16_t valu auto before = operand; - auto result = before.word + value.word + (f & CF); + auto carry = (f & CF) >> 4; + auto result = before.word + value.word + carry; operand.word = result; clearFlag(f, ZF, result); @@ -190,7 +192,7 @@ void EightBit::LR35902::add(uint8_t& f, uint8_t& operand, uint8_t value, int car } void EightBit::LR35902::adc(uint8_t& f, uint8_t& operand, uint8_t value) { - add(operand, value, f & CF); + add(operand, value, (f & CF) >> 4); } void EightBit::LR35902::subtract(uint8_t& f, uint8_t& operand, uint8_t value, int carry) { @@ -208,7 +210,7 @@ void EightBit::LR35902::subtract(uint8_t& f, uint8_t& operand, uint8_t value, in } void EightBit::LR35902::sbc(uint8_t& f, uint8_t& operand, uint8_t value) { - subtract(operand, value, f & CF); + subtract(operand, value, (f & CF) >> 4); } void EightBit::LR35902::andr(uint8_t& f, uint8_t& operand, uint8_t value) { @@ -254,7 +256,7 @@ void EightBit::LR35902::rrc(uint8_t& f, uint8_t& operand) { void EightBit::LR35902::rl(uint8_t& f, uint8_t& operand) { clearFlag(f, NF | HC); - const auto carry = f & CF; + const auto carry = (f & CF) >> 4; setFlag(f, CF, operand & Bit7); operand = (operand << 1) | carry; adjustZero(f, operand); @@ -262,7 +264,7 @@ void EightBit::LR35902::rl(uint8_t& f, uint8_t& operand) { void EightBit::LR35902::rr(uint8_t& f, uint8_t& operand) { clearFlag(f, NF | HC); - const auto carry = f & CF; + const auto carry = (f & CF) >> 4; setFlag(f, CF, operand & Bit0); operand = (operand >> 1) | (carry << 7); adjustZero(f, operand);