cpu/decoder - Fix H and C bits for ADD SP, e8 and LD HL, SP + e8

This commit is contained in:
madmaurice 2023-09-02 00:38:01 +02:00
parent c7cb345c08
commit cfa32424c7
3 changed files with 15 additions and 2 deletions

View file

@ -261,6 +261,18 @@ void Cpu::add16(u16& out, u16 lhs, u16 rhs)
out = (u16)res32; out = (u16)res32;
} }
void Cpu::add16_8(u16& out, u16 lhs, s8 rhs)
{
s16 rhs16 = rhs;
u16 res4 = (lhs & 0x0F) + (rhs & 0x0F);
state.halfcarry = (res4 & 0x10);
u16 res8 = (lhs & 0xFF) + (rhs & 0xFF);
state.carry = (res8 & 0x100);
state.subtract = false;
u32 res32 = lhs + rhs;
out = (u16)res32;
}
bool Cpu::handleInterrupts() bool Cpu::handleInterrupts()
{ {
// Once there's an interrupt we exit halt mode // Once there's an interrupt we exit halt mode

View file

@ -149,6 +149,7 @@ private:
void aluop8(AluOp op, u8 lhs, u8 rhs, u8& out, bool update_carry = true); void aluop8(AluOp op, u8 lhs, u8 rhs, u8& out, bool update_carry = true);
void add16(u16& out, u16 lhs, u16 rhs); void add16(u16& out, u16 lhs, u16 rhs);
void add16_8(u16& out, u16 lhs, s8 rhs);
inline inline
void aluop8(AluOp op, u8 rhs, bool update_carry = true) void aluop8(AluOp op, u8 rhs, bool update_carry = true)

View file

@ -427,12 +427,12 @@ void Cpu::executeInstruction()
state.stopped = true; state.stopped = true;
break; break;
case 0xE8: // ADD SP, e8 case 0xE8: // ADD SP, e8
add16(state.SP, state.SP, (s32)((s8)readPC8())); add16_8(state.SP, state.SP, readPC8());
state.zero = false; state.zero = false;
mcycles = 4; mcycles = 4;
break; break;
case 0xF8: // LD HL, SP + e8 case 0xF8: // LD HL, SP + e8
add16(state.HL, state.SP, (s32)((s8)readPC8())); add16_8(state.SP, state.SP, readPC8());
state.zero = false; state.zero = false;
mcycles = 3; mcycles = 3;
break; break;