From 9c20befc6049ab51b31bdf14116dc7b16fbc5f56 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Sat, 9 Sep 2023 15:41:02 +0200 Subject: [PATCH] cpu/decoder - Fix ADC edge case --- cpu/cpu.cpp | 56 +++++++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/cpu/cpu.cpp b/cpu/cpu.cpp index 7049c78..fd2fcd2 100644 --- a/cpu/cpu.cpp +++ b/cpu/cpu.cpp @@ -182,69 +182,53 @@ bool Cpu::decodeCond(u8 cc) void Cpu::aluop8(AluOp op, u8 lhs, u8 rhs, u8& out, bool update_carry) { u16 rhs16 = rhs; + u16 res_lower; u16 res16; u8 res; - if ((op == ADC || op == SBC) && state.carry) - rhs16++; - u16 lhs_lower = lhs & 0x0F; - u16 lhs_upper = lhs & 0xF0; - u16 rhs_lower = rhs16 & 0x0F; - u16 rhs_upper = rhs16 & 0x1F0; + u16 rhs_lower = rhs & 0x0F; + + if ((op == ADC || op == SBC) && state.carry) + { + rhs_lower++; + rhs16++; + } switch(op) { case ADD: case ADC: - res16 = lhs_lower + rhs_lower; + res_lower = lhs_lower + rhs_lower; + res16 = lhs + rhs16; break; case SUB: case SBC: case CP: - res16 = lhs_lower - rhs_lower; + res_lower = lhs_lower - rhs_lower; + res16 = lhs - rhs16; break; case AND: - res16 = lhs_lower & rhs_lower; + res_lower = lhs_lower & rhs_lower; + res16 = lhs & rhs16; break; case OR: - res16 = lhs_lower | rhs_lower; + res_lower = lhs_lower | rhs_lower; + res16 = lhs | rhs16; break; case XOR: - res16 = lhs_lower ^ rhs_lower; + res_lower = lhs_lower ^ rhs_lower; + res16 = lhs ^ rhs16; break; } - state.halfcarry = ((res16 & 0x10) != 0) || op == AND; + state.halfcarry = ((res_lower & 0x10) != 0) || op == AND; state.subtract = (op == SUB) || (op == SBC) || (op == CP); - switch(op) - { - case ADD: - case ADC: - res16 += lhs_upper + rhs_upper; - break; - case SUB: - case SBC: - case CP: - res16 += lhs_upper - rhs_upper; - break; - case AND: - res16 |= lhs_upper & rhs_upper; - break; - case OR: - res16 |= lhs_upper | rhs_upper; - break; - case XOR: - res16 |= lhs_upper ^ rhs_upper; - break; - } - - res = (u8)(res16 & 0xFF); - if(update_carry) state.carry = ((res16 & 0x100) != 0); + res = (u8)(res16 & 0xFF); state.zero = (res == 0); if (op != CP)