cpu/decoder - Fix ADC edge case

This commit is contained in:
madmaurice 2023-09-09 15:41:02 +02:00
parent 15c4811804
commit 9c20befc60

View file

@ -182,69 +182,53 @@ bool Cpu::decodeCond(u8 cc)
void Cpu::aluop8(AluOp op, u8 lhs, u8 rhs, u8& out, bool update_carry) void Cpu::aluop8(AluOp op, u8 lhs, u8 rhs, u8& out, bool update_carry)
{ {
u16 rhs16 = rhs; u16 rhs16 = rhs;
u16 res_lower;
u16 res16; u16 res16;
u8 res; u8 res;
if ((op == ADC || op == SBC) && state.carry)
rhs16++;
u16 lhs_lower = lhs & 0x0F; u16 lhs_lower = lhs & 0x0F;
u16 lhs_upper = lhs & 0xF0; u16 rhs_lower = rhs & 0x0F;
u16 rhs_lower = rhs16 & 0x0F;
u16 rhs_upper = rhs16 & 0x1F0; if ((op == ADC || op == SBC) && state.carry)
{
rhs_lower++;
rhs16++;
}
switch(op) switch(op)
{ {
case ADD: case ADD:
case ADC: case ADC:
res16 = lhs_lower + rhs_lower; res_lower = lhs_lower + rhs_lower;
res16 = lhs + rhs16;
break; break;
case SUB: case SUB:
case SBC: case SBC:
case CP: case CP:
res16 = lhs_lower - rhs_lower; res_lower = lhs_lower - rhs_lower;
res16 = lhs - rhs16;
break; break;
case AND: case AND:
res16 = lhs_lower & rhs_lower; res_lower = lhs_lower & rhs_lower;
res16 = lhs & rhs16;
break; break;
case OR: case OR:
res16 = lhs_lower | rhs_lower; res_lower = lhs_lower | rhs_lower;
res16 = lhs | rhs16;
break; break;
case XOR: case XOR:
res16 = lhs_lower ^ rhs_lower; res_lower = lhs_lower ^ rhs_lower;
res16 = lhs ^ rhs16;
break; break;
} }
state.halfcarry = ((res16 & 0x10) != 0) || op == AND; state.halfcarry = ((res_lower & 0x10) != 0) || op == AND;
state.subtract = (op == SUB) || (op == SBC) || (op == CP); 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) if(update_carry)
state.carry = ((res16 & 0x100) != 0); state.carry = ((res16 & 0x100) != 0);
res = (u8)(res16 & 0xFF);
state.zero = (res == 0); state.zero = (res == 0);
if (op != CP) if (op != CP)