// File: Escapingbug[P]mos6502-emulator-debugging/mos6502.cpp
// Function: mos6502::Op_SBC(unsigned short)
void mos6502::Op_SBC(uint16_t src)
{
	uint8_t m = Read(src);
	unsigned int tmp = A - m - (IF_CARRY() ? 0 : 1);
	SET_NEGATIVE(tmp & 0x80);
	SET_ZERO(!(tmp & 0xFF));
    SET_OVERFLOW(((A ^ tmp) & 0x80) && ((A ^ m) & 0x80));
	
    if (IF_DECIMAL())
    {
    	if ( ((A & 0x0F) - (IF_CARRY() ? 0 : 1)) < (m & 0x0F)) tmp -= 6;
        if (tmp > 0x99)
        {
        	tmp -= 0x60;
        }
    }
    SET_CARRY(tmp < 0x100);
    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 (%rcx), %eax
movzbl -0xb(%rbp), %edx
subl %edx, %eax
movb 0x12(%rcx), %sil
andb $0x1, %sil
negb %sil
movl $0x1, %ecx
xorl %edx, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
subl %ecx, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x8c3b
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x80, %ecx
movb %cl, 0x12(%rax)
jmp 0x8c53
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $0xffffff7f, %ecx       # imm = 0xFFFFFF7F
movb %cl, 0x12(%rax)
movl -0x10(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x8c77
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x2, %ecx
movb %cl, 0x12(%rax)
jmp 0x8c8c
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 (%rax), %eax
xorl -0x10(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x8cce
movq -0x18(%rbp), %rax
movzbl (%rax), %eax
movzbl -0xb(%rbp), %ecx
xorl %ecx, %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x8cce
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x40, %ecx
movb %cl, 0x12(%rax)
jmp 0x8ce3
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
andl $-0x41, %ecx
movb %cl, 0x12(%rax)
movq -0x18(%rbp), %rax
movzbl 0x12(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x8cfb
movb $0x1, %al
testb $0x1, %al
jne 0x8d03
jmp 0x8d50
xorl %eax, %eax
testb $0x1, %al
jne 0x8d03
jmp 0x8d50
movq -0x18(%rbp), %rcx
movzbl (%rcx), %eax
andl $0xf, %eax
movb 0x12(%rcx), %sil
andb $0x1, %sil
negb %sil
movl $0x1, %ecx
xorl %edx, %edx
testb $0x1, %sil
cmovnel %edx, %ecx
subl %ecx, %eax
movzbl -0xb(%rbp), %ecx
andl $0xf, %ecx
cmpl %ecx, %eax
jge 0x8d3c
movl -0x10(%rbp), %eax
subl $0x6, %eax
movl %eax, -0x10(%rbp)
cmpl $0x99, -0x10(%rbp)
jbe 0x8d4e
movl -0x10(%rbp), %eax
subl $0x60, %eax
movl %eax, -0x10(%rbp)
jmp 0x8d50
cmpl $0x100, -0x10(%rbp)     # imm = 0x100
jae 0x8d70
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x12, %rcx
movzbl 0x12(%rax), %ecx
orl $0x1, %ecx
movb %cl, 0x12(%rax)
jmp 0x8d85
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 (%rax,%rax)