// File: /workspace/github_cpp_makefile_star2_O2/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, 0x80fd5e(%rip)    # 0x820796
movb 0x80fd55(%rip), %bl     # 0x820793
callq 0xff48
testb $0x8, %bl
jne 0x10aad
xorl $0xff, %eax
movzbl 0x80fd3c(%rip), %esi    # 0x820790
movzbl 0x80fd38(%rip), %edi    # 0x820793
movl %edi, %edx
andl $0x1, %edx
addl %eax, %edx
addl %esi, %edx
cmpw $0x100, %dx             # imm = 0x100
setae %cl
andb $-0x44, %dil
orb %cl, %dil
testb %dl, %dl
sete %r8b
addb %r8b, %r8b
orb %dil, %r8b
xorl %edx, %esi
movl %edx, %ecx
xorl %eax, %ecx
andl %esi, %ecx
shrb %cl
andb $0x40, %cl
orb %r8b, %cl
movw %ax, 0x80fd10(%rip)     # 0x8207a6
movw %dx, 0x80fd0b(%rip)     # 0x8207a8
testb %dl, %dl
js 0x10b64
andb $0x7f, %cl
jmp 0x10b67
movw %ax, 0x80fcf2(%rip)     # 0x8207a6
movl %eax, %edx
xorl $0xff, %edx
movzbl 0x80fccd(%rip), %ecx    # 0x820790
movzwl %dx, %r11d
leal (%rcx,%r11), %edx
movzbl 0x80fcc1(%rip), %edi    # 0x820793
movl %edi, %esi
andl $0x1, %esi
addl %esi, %edx
movl %ecx, %r8d
andl $0xf, %r8d
movl %eax, %r9d
andl $0xf, %r9d
subl %r9d, %r8d
leal (%r8,%rsi), %r9d
leal (%r8,%rsi), %r10d
decl %r10d
cmpl $0x100, %edx            # imm = 0x100
setae %bl
andb $0x3c, %dil
orb %bl, %dil
testb %dl, %dl
sete %bl
addb %bl, %bl
movl %edx, %ebp
xorl %ecx, %ebp
movl %edx, %r14d
xorl %r11d, %r14d
andl %ebp, %r14d
shrb %r14b
andb $0x40, %r14b
andb $-0x80, %dl
orb %dil, %dl
orb %bl, %dl
orb %r14b, %dl
movb %dl, 0x80fc62(%rip)     # 0x820793
addl %r8d, %esi
addl $0x9, %esi
orl $-0x10, %esi
testl %r9d, %r9d
cmovgl %r10d, %esi
andl $-0x10, %ecx
andl $0xf0, %eax
subl %eax, %ecx
leal (%rsi,%rcx), %edx
addl $-0x60, %edx
movl %esi, %eax
addl %ecx, %eax
testw %ax, %ax
cmovnsl %eax, %edx
movw %dx, 0x80fc46(%rip)     # 0x8207a8
jmp 0x10b6d
orb $-0x80, %cl
movb %cl, 0x80fc26(%rip)     # 0x820793
movb %dl, 0x80fc1d(%rip)     # 0x820790
popq %rbx
popq %r14
popq %rbp
retq