// File: /workspace/github_cpp_makefile_star2_O1/mdpkh[P]retro6k/src/fake6502.c
// Function: adc
static void adc() {
    penaltyop = 1;
    value = getvalue();
#ifdef NES_CPU
    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) {
        // set or clear zero flag based on binary-mode result
        zerocalc((uint16_t)a + value + (uint16_t)(status & FLAG_CARRY));
        
        result = ((uint16_t)a & 0x0f) + ((uint16_t)value & 0x0f) + ((uint16_t)status & FLAG_CARRY); // MODIFIED BY MDPKH

        clearcarry();
        if (result > 0x09) {
            result = (result + 0x06) & 0x0f | 0x10;
        }
        result += ((uint16_t)a & 0xf0) + ((uint16_t)value & 0xf0); // MODIFIED BY MDPKH, PART 2 OF CALCULATION
        
        overflowcalc(result, a, value);
        signcalc(result);
        
        if (result > 0x9f) {
            result += 0x60;
            setcarry();
        }
        carrycalc(result);
        
        // clockticks6502++; // 65C02 only!
    } else {
        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:
movb $0x1, 0x80fa29(%rip)    # 0x821786
movzbl 0x80fa21(%rip), %eax    # 0x821785
leaq 0x9045(%rip), %rcx      # 0x1adb0
leaq -0x8d6(%rip), %rdx      # 0x1149c
cmpq %rdx, (%rcx,%rax,8)
je 0x11d8b
pushq %rax
movzwl 0x80fa12(%rip), %edi    # 0x821792
callq 0xe3d3
addq $0x8, %rsp
jmp 0x11d91
movb 0x80f9ef(%rip), %al     # 0x821780
movzbl %al, %edx
movw %dx, 0x80f9fb(%rip)     # 0x821796
movzbl 0x80f9e1(%rip), %esi    # 0x821783
movl %esi, %edi
andl $0x1, %edi
movzbl 0x80f9d1(%rip), %r9d    # 0x821780
testb $0x8, %sil
jne 0x11e2c
movzwl %dx, %ecx
movzwl %di, %eax
movzwl %r9w, %edx
addl %edx, %eax
addl %ecx, %eax
movl %esi, %edi
andb $-0xa, %dil
orb $0x1, %sil
cmpl $0x100, %eax            # imm = 0x100
movzbl %dil, %edi
movzbl %sil, %esi
cmovbl %edi, %esi
movl %esi, %edi
orb $0x2, %dil
andb $-0xb, %sil
movzbl %dil, %edi
movzbl %sil, %esi
testb %al, %al
cmovel %edi, %esi
xorl %eax, %edx
xorl %eax, %ecx
movl %esi, %edi
andb $-0x49, %dil
orb $0x40, %sil
testb %dl, %cl
movzbl %dil, %edx
movzbl %sil, %ecx
cmovnsl %edx, %ecx
movw %ax, 0x80f982(%rip)     # 0x821798
movb %cl, 0x80f967(%rip)     # 0x821783
testb %al, %al
js 0x11f0c
andb $0x7f, %cl
jmp 0x11f14
movzwl %si, %r8d
movzwl %r9w, %eax
movzwl %dx, %ecx
addb %al, %dl
addb %dil, %dl
sete %dl
andb $-0x3, %sil
addb %dl, %dl
orb %sil, %dl
movl %eax, %esi
andl $0xf, %esi
movl %ecx, %edi
andl $0xf, %edi
andl $0x1, %r8d
addl %esi, %r8d
addl %edi, %r8d
movw %r8w, 0x80f934(%rip)    # 0x821798
movl %edx, %edi
andb $-0x2, %dil
movb %dil, 0x80f912(%rip)    # 0x821783
cmpw $0xa, %r8w
jb 0x11e8c
movzwl %r8w, %esi
addl $0x6, %esi
andl $0xf, %esi
orl $0x10, %esi
movw %si, 0x80f90c(%rip)     # 0x821798
movl %eax, %r8d
andl $-0x10, %r8d
movl %ecx, %esi
andl $-0x10, %esi
addl %r8d, %esi
addw 0x80f8f6(%rip), %si     # 0x821798
xorl %esi, %eax
xorl %esi, %ecx
andb $-0x42, %dl
orb $0x40, %dil
testb %al, %cl
movzbl %dl, %eax
movzbl %dil, %ecx
cmovnsl %eax, %ecx
movl %ecx, %eax
andb $0x7e, %al
orb $-0x80, %cl
testb %sil, %sil
movzbl %al, %edx
movzbl %cl, %eax
cmovnsl %edx, %eax
movw %si, 0x80f8c5(%rip)     # 0x821798
movb %al, 0x80f8aa(%rip)     # 0x821783
movzwl %si, %ecx
cmpl $0xa0, %ecx
jb 0x11ef6
addl $0x60, %esi
movw %si, 0x80f8aa(%rip)     # 0x821798
orb $0x1, %al
movb %al, 0x80f88d(%rip)     # 0x821783
cmpw $0x100, 0x80f899(%rip)  # imm = 0x100
movb 0x80f87e(%rip), %cl     # 0x821783
jae 0x11f11
andb $-0x2, %cl
jmp 0x11f14
orb $-0x80, %cl
jmp 0x11f14
orb $0x1, %cl
movb %cl, 0x80f869(%rip)     # 0x821783
movb 0x80f878(%rip), %al     # 0x821798
movb %al, 0x80f85a(%rip)     # 0x821780
retq