// File: rockytriton[P]dreamsys_emu_gb/src/op_handler.cpp
// Function: dsemu::cpu::handleDEC(dsemu::cpu::OpCode const&)
int handleDEC(const OpCode &op) {
    ushort val = 0;

    switch(op.params[0]) {
        case A: regAF.hi--; val = regAF.hi; break;
        case B: regBC.hi--; val = regBC.hi; break;
        case C: regBC.lo--; val = regBC.lo; break;
        case D: regDE.hi--; val = regDE.hi; break;
        case E: regDE.lo--; val = regDE.lo; break;
        case H: regHL.hi--; val = regHL.hi; break;
        case L: regHL.lo--; val = regHL.lo; break;
        case BC: (*getReg16Pointer(regBC))--; val = (getReg16Value(regBC)); return 0;
        case DE: (*getReg16Pointer(regDE))--; val = (getReg16Value(regDE)); return 0;
        case SP: (*getReg16Pointer(regSP))--; val = (getReg16Value(regSP)); return 0;
        case HL: {
            if (op.mode == ATypeA) {
                byte b = bus::read((getReg16Value(regHL)));
                b--;
                bus::write((getReg16Value(regHL)), b);
                val = b;
            } else {
                (*getReg16Pointer(regHL))--; 
                val = (getReg16Value(regHL)); 
                return 0;
            }
        } break;
        default:
            cout << "INVALID DEC FLAG" << endl;
            exit(-1);
            return 0;
    }

    setFlag(FlagZ, val == 0);
    setFlag(FlagN, 1);
    setFlag(FlagH, (val & 0xF) == 0x0F);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movw $0x0, -0x12(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0x20(%rbp)
subq $0xa, %rax
ja 0x1cb81
movq -0x20(%rbp), %rax
leaq 0x7a66(%rip), %rcx      # 0x243a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x12c3e(%rip), %rax     # 0x2f590
movb 0x1(%rax), %cl
addb $-0x1, %cl
leaq 0x12c31(%rip), %rax     # 0x2f590
movb %cl, 0x1(%rax)
leaq 0x12c27(%rip), %rax     # 0x2f590
movzbl 0x1(%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12c15(%rip), %rax     # 0x2f592
movb 0x1(%rax), %cl
addb $-0x1, %cl
leaq 0x12c08(%rip), %rax     # 0x2f592
movb %cl, 0x1(%rax)
leaq 0x12bfe(%rip), %rax     # 0x2f592
movzbl 0x1(%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12bea(%rip), %rax     # 0x2f592
movb (%rax), %cl
addb $-0x1, %cl
leaq 0x12bde(%rip), %rax     # 0x2f592
movb %cl, (%rax)
leaq 0x12bd5(%rip), %rax     # 0x2f592
movzbl (%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12bc4(%rip), %rax     # 0x2f594
movb 0x1(%rax), %cl
addb $-0x1, %cl
leaq 0x12bb7(%rip), %rax     # 0x2f594
movb %cl, 0x1(%rax)
leaq 0x12bad(%rip), %rax     # 0x2f594
movzbl 0x1(%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12b99(%rip), %rax     # 0x2f594
movb (%rax), %cl
addb $-0x1, %cl
leaq 0x12b8d(%rip), %rax     # 0x2f594
movb %cl, (%rax)
leaq 0x12b84(%rip), %rax     # 0x2f594
movzbl (%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12b73(%rip), %rax     # 0x2f596
movb 0x1(%rax), %cl
addb $-0x1, %cl
leaq 0x12b66(%rip), %rax     # 0x2f596
movb %cl, 0x1(%rax)
leaq 0x12b5c(%rip), %rax     # 0x2f596
movzbl 0x1(%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12b48(%rip), %rax     # 0x2f596
movb (%rax), %cl
addb $-0x1, %cl
leaq 0x12b3c(%rip), %rax     # 0x2f596
movb %cl, (%rax)
leaq 0x12b33(%rip), %rax     # 0x2f596
movzbl (%rax), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cbad
leaq 0x12b1c(%rip), %rdi     # 0x2f592
callq 0x153e0
movw (%rax), %cx
addw $-0x1, %cx
movw %cx, (%rax)
leaq 0x12b06(%rip), %rdi     # 0x2f592
callq 0x15070
movw %ax, -0x12(%rbp)
movl $0x0, -0x4(%rbp)
jmp 0x1cbfa
leaq 0x12aec(%rip), %rdi     # 0x2f594
callq 0x153e0
movw (%rax), %cx
addw $-0x1, %cx
movw %cx, (%rax)
leaq 0x12ad6(%rip), %rdi     # 0x2f594
callq 0x15070
movw %ax, -0x12(%rbp)
movl $0x0, -0x4(%rbp)
jmp 0x1cbfa
leaq 0x12ac2(%rip), %rdi     # 0x2f59c
callq 0x153e0
movw (%rax), %cx
addw $-0x1, %cx
movw %cx, (%rax)
leaq 0x12aac(%rip), %rdi     # 0x2f59c
callq 0x15070
movw %ax, -0x12(%rbp)
movl $0x0, -0x4(%rbp)
jmp 0x1cbfa
movq -0x10(%rbp), %rax
cmpl $0xc, 0x18(%rax)
jne 0x1cb50
leaq 0x12a80(%rip), %rdi     # 0x2f596
callq 0x15070
movzwl %ax, %edi
callq 0x13450
movb %al, -0x13(%rbp)
movb -0x13(%rbp), %al
addb $-0x1, %al
movb %al, -0x13(%rbp)
leaq 0x12a61(%rip), %rdi     # 0x2f596
callq 0x15070
movzwl %ax, %edi
movzbl -0x13(%rbp), %esi
callq 0x13590
movzbl -0x13(%rbp), %eax
movw %ax, -0x12(%rbp)
jmp 0x1cb7f
leaq 0x12a3f(%rip), %rdi     # 0x2f596
callq 0x153e0
movw (%rax), %cx
addw $-0x1, %cx
movw %cx, (%rax)
leaq 0x12a29(%rip), %rdi     # 0x2f596
callq 0x15070
movw %ax, -0x12(%rbp)
movl $0x0, -0x4(%rbp)
jmp 0x1cbfa
jmp 0x1cbad
movq 0x12448(%rip), %rdi     # 0x2efd0
leaq 0x798e(%rip), %rsi      # 0x2451d
callq 0x32c0
movq %rax, %rdi
movq 0x12412(%rip), %rsi     # 0x2efb0
callq 0x3310
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3390
movzwl -0x12(%rbp), %eax
cmpl $0x0, %eax
sete %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x7, %edi
callq 0x1da80
movl $0x6, %edi
movl $0x1, %esi
callq 0x1da80
movzwl -0x12(%rbp), %eax
andl $0xf, %eax
cmpl $0xf, %eax
sete %al
movzbl %al, %esi
andl $0x1, %esi
movl $0x5, %edi
callq 0x1da80
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)