// File: /workspace/github_cpp_makefile_star2_O1/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 %rax
testb $0x8, 0x80f025(%rip)    # 0x821783
movb $0x1, 0x80f021(%rip)    # 0x821786
movzbl 0x80f019(%rip), %eax    # 0x821785
leaq 0x863d(%rip), %rcx      # 0x1adb0
movq (%rcx,%rax,8), %rax
leaq -0x12e2(%rip), %rcx     # 0x1149c
jne 0x12793
cmpq %rcx, %rax
je 0x127aa
movzwl 0x80f006(%rip), %edi    # 0x821792
callq 0xe3d3
jmp 0x127b0
cmpq %rcx, %rax
je 0x12823
movzwl 0x80efef(%rip), %edi    # 0x821792
callq 0xe3d3
jmp 0x12829
movb 0x80efd0(%rip), %al     # 0x821780
notb %al
movzbl %al, %eax
movzbl 0x80efc4(%rip), %esi    # 0x821780
movzbl 0x80efc0(%rip), %edx    # 0x821783
movl %edx, %ecx
andl $0x1, %ecx
addl %esi, %ecx
addl %eax, %ecx
cmpl $0x100, %ecx            # imm = 0x100
setae %dil
andb $-0x44, %dl
orb %dil, %dl
testb %cl, %cl
sete %dil
addb %dil, %dil
orb %dl, %dil
xorl %ecx, %esi
movl %ecx, %edx
xorl %eax, %edx
andl %esi, %edx
shrb %dl
andb $0x40, %dl
orb %dil, %dl
movw %ax, 0x80ef97(%rip)     # 0x821796
movw %cx, 0x80ef92(%rip)     # 0x821798
movb %dl, 0x80ef77(%rip)     # 0x821783
testb %cl, %cl
js 0x12815
andb $0x7f, %dl
jmp 0x12818
orb $-0x80, %dl
movb %dl, 0x80ef65(%rip)     # 0x821783
jmp 0x128fa
movb 0x80ef57(%rip), %al     # 0x821780
movzbl %al, %eax
movw %ax, 0x80ef63(%rip)     # 0x821796
movl %eax, %r8d
xorl $0xff, %r8d
movzbl 0x80ef3c(%rip), %ecx    # 0x821780
movzbl 0x80ef38(%rip), %edx    # 0x821783
movl %edx, %r9d
andl $0x1, %r9d
leal (%rcx,%r9), %esi
addl %r8d, %esi
movl %ecx, %r10d
andl $0xf, %r10d
movl %eax, %edi
andl $0xf, %edi
subl %edi, %r10d
leal (%r10,%r9), %edi
addl %r10d, %r9d
decl %r9d
movw %r9w, 0x80ef1e(%rip)    # 0x821798
cmpl $0x100, %esi            # imm = 0x100
setae %r9b
andb $0x3c, %dl
orb %r9b, %dl
testb %sil, %sil
sete %r9b
addb %r9b, %r9b
movl %ecx, %r10d
xorl %esi, %r10d
movl %esi, %r11d
xorl %r8d, %r11d
andl %r10d, %r11d
shrb %r11b
andb $0x40, %r11b
andb $-0x80, %sil
orb %dl, %sil
orb %r9b, %sil
orb %r11b, %sil
movb %sil, 0x80eec5(%rip)    # 0x821783
testl %edi, %edi
jg 0x128d5
movswl %di, %edx
addl $0x9, %edx
orl $0xfff0, %edx           # imm = 0xFFF0
movw %dx, 0x80eec3(%rip)     # 0x821798
movzwl %cx, %ecx
andl $-0x10, %ecx
movzwl %ax, %eax
andl $-0x10, %eax
subl %eax, %ecx
addw 0x80eeae(%rip), %cx     # 0x821798
leal -0x60(%rcx), %eax
testw %cx, %cx
cmovnsl %ecx, %eax
movw %ax, 0x80ee9e(%rip)     # 0x821798
movb 0x80ee98(%rip), %al     # 0x821798
movb %al, 0x80ee7a(%rip)     # 0x821780
popq %rax
retq