// File: 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
movq %rsp, %rbp
subq $0x10, %rsp
movb $0x1, 0x813997(%rip)    # 0x832876
movzbl 0x81398d(%rip), %eax    # 0x832873
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1f0be
callq 0x1d420
movw %ax, 0x813988(%rip)     # 0x832886
movzwl 0x813981(%rip), %eax    # 0x832886
xorl $0xff, %eax
movw %ax, -0x2(%rbp)
movzbl 0x81395b(%rip), %eax    # 0x832870
movzwl %ax, %eax
movzwl -0x2(%rbp), %ecx
addl %ecx, %eax
movzbl 0x81394e(%rip), %ecx    # 0x832873
andl $0x1, %ecx
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, -0x4(%rbp)
movzbl 0x813938(%rip), %eax    # 0x832870
movzwl %ax, %eax
andl $0xf, %eax
movzwl 0x813941(%rip), %ecx    # 0x832886
andl $0xf, %ecx
subl %ecx, %eax
movzbl 0x813922(%rip), %ecx    # 0x832873
movzwl %cx, %ecx
andl $0x1, %ecx
addl %ecx, %eax
subl $0x1, %eax
movw %ax, 0x813925(%rip)     # 0x832888
movzwl -0x4(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
cmpl $0x0, %eax
je 0x1ef83
movzbl 0x8138fb(%rip), %eax    # 0x832873
orl $0x1, %eax
movb %al, 0x8138f2(%rip)     # 0x832873
jmp 0x1ef93
movzbl 0x8138e9(%rip), %eax    # 0x832873
andl $-0x2, %eax
movb %al, 0x8138e0(%rip)     # 0x832873
movzwl -0x4(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
je 0x1efb3
movzbl 0x8138cb(%rip), %eax    # 0x832873
andl $-0x3, %eax
movb %al, 0x8138c2(%rip)     # 0x832873
jmp 0x1efc3
movzbl 0x8138b9(%rip), %eax    # 0x832873
orl $0x2, %eax
movb %al, 0x8138b0(%rip)     # 0x832873
movzwl -0x4(%rbp), %eax
movzbl 0x8138a2(%rip), %ecx    # 0x832870
movzwl %cx, %ecx
xorl %ecx, %eax
movzwl -0x4(%rbp), %ecx
movzwl -0x2(%rbp), %edx
xorl %edx, %ecx
andl %ecx, %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1effb
movzbl 0x813883(%rip), %eax    # 0x832873
orl $0x40, %eax
movb %al, 0x81387a(%rip)     # 0x832873
jmp 0x1f00b
movzbl 0x813871(%rip), %eax    # 0x832873
andl $-0x41, %eax
movb %al, 0x813868(%rip)     # 0x832873
movzwl -0x4(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1f02d
movzbl 0x813853(%rip), %eax    # 0x832873
orl $0x80, %eax
movb %al, 0x813848(%rip)     # 0x832873
jmp 0x1f03f
movzbl 0x81383f(%rip), %eax    # 0x832873
andl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
movb %al, 0x813834(%rip)     # 0x832873
movzwl 0x813842(%rip), %eax    # 0x832888
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x1f069
movzwl 0x813831(%rip), %eax    # 0x832888
subl $0x6, %eax
andl $0xf, %eax
orl $0xfff0, %eax           # imm = 0xFFF0
movw %ax, 0x81381f(%rip)     # 0x832888
movzbl 0x813800(%rip), %eax    # 0x832870
movzwl %ax, %ecx
andl $0xf0, %ecx
movzwl 0x813806(%rip), %eax    # 0x832886
andl $0xf0, %eax
subl %eax, %ecx
movzwl 0x8137fa(%rip), %eax    # 0x832888
addl %ecx, %eax
movw %ax, 0x8137f1(%rip)     # 0x832888
movzwl 0x8137ea(%rip), %eax    # 0x832888
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x1f0b9
movzwl 0x8137d9(%rip), %eax    # 0x832888
subl $0x60, %eax
movw %ax, 0x8137cf(%rip)     # 0x832888
jmp 0x1f1eb
callq 0x1d420
movzwl %ax, %eax
xorl $0xff, %eax
movw %ax, 0x8137b4(%rip)     # 0x832886
movzbl 0x813797(%rip), %eax    # 0x832870
movzwl %ax, %eax
movzwl 0x8137a3(%rip), %ecx    # 0x832886
addl %ecx, %eax
movzbl 0x813787(%rip), %ecx    # 0x832873
andl $0x1, %ecx
movzwl %cx, %ecx
addl %ecx, %eax
movw %ax, 0x81378d(%rip)     # 0x832888
movzwl 0x813786(%rip), %eax    # 0x832888
andl $0xff00, %eax           # imm = 0xFF00
cmpl $0x0, %eax
je 0x1f11e
movzbl 0x813760(%rip), %eax    # 0x832873
orl $0x1, %eax
movb %al, 0x813757(%rip)     # 0x832873
jmp 0x1f12e
movzbl 0x81374e(%rip), %eax    # 0x832873
andl $-0x2, %eax
movb %al, 0x813745(%rip)     # 0x832873
movzwl 0x813753(%rip), %eax    # 0x832888
andl $0xff, %eax
cmpl $0x0, %eax
je 0x1f151
movzbl 0x81372d(%rip), %eax    # 0x832873
andl $-0x3, %eax
movb %al, 0x813724(%rip)     # 0x832873
jmp 0x1f161
movzbl 0x81371b(%rip), %eax    # 0x832873
orl $0x2, %eax
movb %al, 0x813712(%rip)     # 0x832873
movzwl 0x813720(%rip), %eax    # 0x832888
movzbl 0x813701(%rip), %ecx    # 0x832870
movzwl %cx, %ecx
xorl %ecx, %eax
movzwl 0x81370d(%rip), %ecx    # 0x832888
movzwl 0x813704(%rip), %edx    # 0x832886
xorl %edx, %ecx
andl %ecx, %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1f1a2
movzbl 0x8136dc(%rip), %eax    # 0x832873
orl $0x40, %eax
movb %al, 0x8136d3(%rip)     # 0x832873
jmp 0x1f1b2
movzbl 0x8136ca(%rip), %eax    # 0x832873
andl $-0x41, %eax
movb %al, 0x8136c1(%rip)     # 0x832873
movzwl 0x8136cf(%rip), %eax    # 0x832888
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1f1d7
movzbl 0x8136a9(%rip), %eax    # 0x832873
orl $0x80, %eax
movb %al, 0x81369e(%rip)     # 0x832873
jmp 0x1f1e9
movzbl 0x813695(%rip), %eax    # 0x832873
andl $0xffffff7f, %eax       # imm = 0xFFFFFF7F
movb %al, 0x81368a(%rip)     # 0x832873
jmp 0x1f1eb
movzwl 0x813696(%rip), %eax    # 0x832888
andl $0xff, %eax
movb %al, 0x813673(%rip)     # 0x832870
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)