Skip to content

Commit 7c459a4

Browse files
committed
Fix SBC instruction
1 parent b25ae67 commit 7c459a4

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

src/huc6280_opcodes_inline.h

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -368,51 +368,38 @@ INLINE void HuC6280::OPCodes_ROR_Memory(u16 address)
368368

369369
INLINE void HuC6280::OPCodes_SBC(u8 value)
370370
{
371-
u16 result = 0;
371+
s32 result;
372+
u8 a = m_A.GetValue();
373+
u8 inverted_value = (u8)(~value);
372374

373375
if (IsSetFlag(FLAG_DECIMAL))
374376
{
375377
m_cycles++;
376378

377-
u16 tmp = (m_A.GetValue() & 0x0f) - (value & 0x0f) - (IsSetFlag(FLAG_CARRY) ? 0 : 1);
378-
result = m_A.GetValue() - value - (IsSetFlag(FLAG_CARRY) ? 0 : 1);
379-
380-
if (result & 0x8000)
381-
result -= 0x60;
382-
383-
if (tmp & 0x8000)
379+
result = (a & 0x0F) + (inverted_value & 0x0F) + (IsSetFlag(FLAG_CARRY) ? 1 : 0);
380+
if (result <= 0x0F)
384381
result -= 0x06;
385382

386-
#if defined(GG_TESTING)
387-
u16 bin_result = m_A.GetValue() + ~value + (IsSetFlag(FLAG_CARRY) ? 1 : 0);
388-
if ((m_A.GetValue() ^ bin_result) & (~value ^ bin_result) & 0x80)
389-
SetFlag(FLAG_OVERFLOW);
390-
else
391-
ClearFlag(FLAG_OVERFLOW);
392-
#endif
393-
394-
if ((u16)result <= (u16)m_A.GetValue() || (result & 0xff0) == 0xff0)
395-
SetFlag(FLAG_CARRY);
396-
else
397-
ClearFlag(FLAG_CARRY);
383+
result = (a & 0xF0) + (inverted_value & 0xF0) + (result > 0x0F ? 0x10 : 0) + (result & 0x0F);
384+
if (result <= 0xFF)
385+
result -= 0x60;
398386
}
399387
else
400388
{
401-
value = ~value;
402-
result = (u16)m_A.GetValue() + (u16)value + (IsSetFlag(FLAG_CARRY) ? 1 : 0);
389+
result = (u16)a + (u16)inverted_value + (IsSetFlag(FLAG_CARRY) ? 1 : 0);
403390

404-
if(~(m_A.GetValue() ^ value) & (m_A.GetValue() ^ result) & 0x80)
391+
if (~(a ^ inverted_value) & (a ^ result) & 0x80)
405392
SetFlag(FLAG_OVERFLOW);
406393
else
407394
ClearFlag(FLAG_OVERFLOW);
408-
409-
if(result > 0xFF)
410-
SetFlag(FLAG_CARRY);
411-
else
412-
ClearFlag(FLAG_CARRY);
413395
}
414396

415-
SetOrClearZNFlags((u8)result);
397+
ClearFlag(FLAG_CARRY | FLAG_NEGATIVE | FLAG_ZERO);
398+
SetZNFlags((u8)result);
399+
400+
if (result > 0xFF)
401+
SetFlag(FLAG_CARRY);
402+
416403
m_A.SetValue((u8)result);
417404
}
418405

0 commit comments

Comments
 (0)