// File: /workspace/github_cpp_makefile_star2_O3/mdpkh[P]retro6k/src/fake6502.c
// Function: sbc
static void sbc() {
    penaltyop = 1;
#ifdef NES_CPU
    value = getvalue() ^ 0x00FF;
    result = (uint16_t)a + value + (uint16_t)(status & FLAG_CARRY); // ORIGINAL BY MC

    carrycalc(result);
    zerocalc(result);
    overflowcalc(result, a, value);
    signcalc(result);

#else
    if (status & FLAG_DECIMAL) {
        value = getvalue();
        // Set flags based on result of binary ADC with ones complement of operand
        uint16_t cvalue = value ^ 0x00ff;
        uint16_t bresult = (uint16_t)a + cvalue + (uint16_t)(status & FLAG_CARRY);
        // but incorporate carry flag into decimal-mode ADC result calculation before changing carry flag
        result = ((uint16_t)a & 0x0f) - ((uint16_t)value & 0x0f) + ((uint16_t)status & FLAG_CARRY) - 0x01; // MODIFIED BY MDPKH
        carrycalc(bresult);
        zerocalc(bresult);
        overflowcalc(bresult, a, cvalue);
        signcalc(bresult);

        if (result & 0x8000) {
            result = (result - 0x06) & 0x0f | 0xfff0;
        }
        result += ((uint16_t)a & 0xf0) - ((uint16_t)value & 0xf0); // MODIFIED BY MDPKH, PART 2 OF CALCULATION
        if (result & 0x8000) {
            result -= 0x60;
        }

        // clockticks6502++; // 65C02 only!
    } else {
        value = getvalue() ^ 0x00FF;
        result = (uint16_t)a + value + (uint16_t)(status & FLAG_CARRY); // ORIGINAL BY MC

        carrycalc(result);
        zerocalc(result);
        overflowcalc(result, a, value);
        signcalc(result);
    }
#endif
   
    saveaccum(result);
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movb $0x1, 0x80eaa1(%rip)    # 0x821786
movb 0x80ea98(%rip), %cl     # 0x821783
movzbl 0x80ea93(%rip), %eax    # 0x821785
leaq 0x80b7(%rip), %rdx      # 0x1adb0
movq (%rdx,%rax,8), %rax
leaq -0x12ce(%rip), %rdx     # 0x11a36
testb $0x8, %cl
jne 0x12d29
cmpq %rdx, %rax
je 0x12d4c
movzwl 0x80ea7d(%rip), %edi    # 0x821792
callq 0xe9ae
movb 0x80ea5f(%rip), %sil    # 0x821780
movb 0x80ea5c(%rip), %cl     # 0x821783
jmp 0x12d55
cmpq %rdx, %rax
je 0x12db9
movzwl 0x80ea59(%rip), %edi    # 0x821792
callq 0xe9ae
movb 0x80ea3c(%rip), %dl     # 0x821780
movb 0x80ea39(%rip), %cl     # 0x821783
jmp 0x12dc1
movb 0x80ea2d(%rip), %sil    # 0x821780
movl %esi, %eax
notb %al
movzbl %al, %edx
movzbl %sil, %esi
movl %ecx, %eax
andb $0x1, %al
movzbl %al, %eax
addl %esi, %eax
addl %edx, %eax
cmpl $0x100, %eax            # imm = 0x100
setae %dil
andb $-0x44, %cl
orb %dil, %cl
testb %al, %al
sete %dil
addb %dil, %dil
orb %cl, %dil
xorl %eax, %esi
movl %eax, %ecx
xorl %edx, %ecx
andl %esi, %ecx
shrb %cl
andb $0x40, %cl
orb %dil, %cl
movw %dx, 0x80e9fb(%rip)     # 0x821796
movw %ax, 0x80e9f6(%rip)     # 0x821798
testb %al, %al
js 0x12dab
andb $0x7f, %cl
jmp 0x12dae
orb $-0x80, %cl
movb %cl, 0x80e9cf(%rip)     # 0x821783
jmp 0x12e73
movb 0x80e9c1(%rip), %dl     # 0x821780
movl %edx, %eax
movzbl %al, %eax
movw %ax, 0x80e9cb(%rip)     # 0x821796
movl %eax, %r10d
xorl $0xff, %r10d
movzbl %dl, %edx
movl %ecx, %esi
andb $0x1, %sil
movzbl %sil, %edi
leal (%rdx,%rdi), %esi
addl %r10d, %esi
movl %edx, %r8d
andl $0xf, %r8d
movl %eax, %r9d
andl $0xf, %r9d
subl %r9d, %r8d
leal (%r8,%rdi), %r9d
leal (%r8,%rdi), %r11d
decl %r11d
cmpl $0x100, %esi            # imm = 0x100
setae %bl
andb $0x3c, %cl
orb %bl, %cl
testb %sil, %sil
sete %bl
addb %bl, %bl
movl %esi, %ebp
xorl %edx, %ebp
movl %esi, %r14d
xorl %r10d, %r14d
andl %ebp, %r14d
shrb %r14b
andb $0x40, %r14b
andb $-0x80, %sil
orb %cl, %sil
orb %bl, %sil
orb %r14b, %sil
leal (%r8,%rdi), %ecx
addl $0x9, %ecx
orl $-0x10, %ecx
testl %r9d, %r9d
cmovgl %r11d, %ecx
movb %sil, 0x80e930(%rip)    # 0x821783
andb $-0x10, %dl
movzbl %dl, %edx
andl $-0x10, %eax
subl %eax, %edx
leal (%rcx,%rdx), %eax
addl $-0x60, %eax
addl %edx, %ecx
testw %cx, %cx
cmovnsl %ecx, %eax
movw %ax, 0x80e925(%rip)     # 0x821798
movb %al, 0x80e907(%rip)     # 0x821780
popq %rbx
popq %r14
popq %rbp
retq