// File: Escapingbug[P]mos6502-emulator-debugging/mos6502.cpp
// Function: mos6502::Op_ADC(unsigned short)
void mos6502::Op_ADC(uint16_t src)
{
	uint8_t m = Read(src);
	unsigned int tmp = m + A + (IF_CARRY() ? 1 : 0);
	SET_ZERO(!(tmp & 0xFF));
    if (IF_DECIMAL())
    {
        if (((A & 0xF) + (m & 0xF) + (IF_CARRY() ? 1 : 0)) > 9) tmp += 6;
        SET_NEGATIVE(tmp & 0x80);
        SET_OVERFLOW(!((A ^ m) & 0x80) && ((A ^ tmp) & 0x80));
        if (tmp > 0x99)
        {
        	tmp += 96;
        }
        SET_CARRY(tmp > 0x99);
    }
	else
	{
		SET_NEGATIVE(tmp & 0x80);
		SET_OVERFLOW(!((A ^ m) & 0x80) && ((A ^ tmp) & 0x80));
		SET_CARRY(tmp > 0xFF);
    }
	
    A = tmp & 0xFF;
	return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movw %si, %ax
movq %rdi, -0x8(%rbp)
movw %ax, -0xa(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x18(%rbp)
movzwl -0xa(%rbp), %eax
movl %eax, %esi
callq 0x34e0
movq -0x18(%rbp), %rcx
movb %al, -0xb(%rbp)
movzbl -0xb(%rbp), %eax
movzbl (%rcx), %edx
addl %edx, %eax
movb 0x12(%rcx), %sil
andb $0x1, %sil
negb %sil
xorl %ecx, %ecx
movl $0x1, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x6978
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x12(%rax)
jmp 0x698d
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x3, %ecx
movb %cl, 0x12(%rax)
movq -0x18(%rbp), %rax
movzbl 0x12(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x69a8
movb $0x1, %al
testb $0x1, %al
jne 0x69b3
jmp 0x6ad1
xorl %eax, %eax
testb $0x1, %al
jne 0x69b3
jmp 0x6ad1
movq -0x18(%rbp), %rcx
movzbl (%rcx), %eax
andl $0xf, %eax
movzbl -0xb(%rbp), %edx
andl $0xf, %edx
addl %edx, %eax
movb 0x12(%rcx), %sil
andb $0x1, %sil
negb %sil
xorl %ecx, %ecx
movl $0x1, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
addl %ecx, %eax
cmpl $0x9, %eax
jle 0x69ef
movl -0x10(%rbp), %eax
addl $0x6, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6a16
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x80, %ecx
movb %cl, 0x12(%rax)
jmp 0x6a2e
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movb %cl, 0x12(%rax)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movzbl -0xb(%rbp), %ecx
xorl %ecx, %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x6a70
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
xorl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6a70
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x12(%rax)
jmp 0x6a85
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x41, %ecx
movb %cl, 0x12(%rax)
cmpl $0x99, -0x10(%rbp)
jbe 0x6a97
movl -0x10(%rbp), %eax
addl $0x60, %eax
movl %eax, -0x10(%rbp)
cmpl $0x99, -0x10(%rbp)
jbe 0x6ab7
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x12(%rax)
jmp 0x6acc
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x2, %ecx
movb %cl, 0x12(%rax)
jmp 0x6b9c
movl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6af8
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x80, %ecx
movb %cl, 0x12(%rax)
jmp 0x6b10
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movb %cl, 0x12(%rax)
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movzbl -0xb(%rbp), %ecx
xorl %ecx, %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x6b52
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
xorl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x6b52
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x12(%rax)
jmp 0x6b67
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x41, %ecx
movb %cl, 0x12(%rax)
cmpl $0xff, -0x10(%rbp)
jbe 0x6b87
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x12(%rax)
jmp 0x6b9c
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x2, %ecx
movb %cl, 0x12(%rax)
movq -0x18(%rbp), %rax
movl -0x10(%rbp), %ecx
andl $0xff, %ecx
movb %cl, (%rax)
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)