// File: rockytriton[P]dreamsys_emu_gb/src/op_handler.cpp
// Function: dsemu::cpu::handleCALL(dsemu::cpu::OpCode const&)
int handleCALL(const OpCode &op) {
    ushort lca = regPC + op.length;
    bool didJump;
    ushort location = toShort(bus::read(regPC + 1), bus::read(regPC + 2)) - op.length;
    //lastCallAddress = regPC + op.length;

    //for (int i=0; i<callSize; i++) {
    //    cout << " ";
    //}

    //char sz[256];
    //sprintf("%%dc", callSize);

    callSize++;

    if (callSize > 30) {
        callSize = callSize + 0;
    }

    cout << std::setfill('-') << std::setw(callSize) << "-" << "HANDLING CALL: " << Short(regPC) << " CALLSIZE: " << callSize << " STACK: ";


    if (regPC == 0x2200) {
        regPC += 0;
    }

    int ret = conditionalJump(location, op, didJump);

    if (didJump) {
        cpu::push((ushort)(lca));
        lastCallAddress = lca;
    } else {
        cout << "NO" << endl;
    }

    for (size_t i=0; i<THESTACK.size(); i++) {
        cout << Byte(THESTACK[i]) << "-";
    }
    
    cout << endl;

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
leaq 0x13edb(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
movq -0x8(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
addl %ecx, %eax
movw %ax, -0xa(%rbp)
leaq 0x13ec3(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
addl $0x1, %eax
movzwl %ax, %edi
callq 0x13450
movb %al, -0x2a(%rbp)
leaq 0x13eab(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
addl $0x2, %eax
movzwl %ax, %edi
callq 0x13450
movb -0x2a(%rbp), %cl
movzbl %cl, %edi
movzbl %al, %esi
callq 0x19ad0
movzwl %ax, %eax
movq -0x8(%rbp), %rcx
movzbl 0x14(%rcx), %ecx
subl %ecx, %eax
movw %ax, -0xe(%rbp)
movl 0x33fce(%rip), %eax     # 0x4f6f4
addl $0x1, %eax
movl %eax, 0x33fc5(%rip)     # 0x4f6f4
cmpl $0x1e, 0x33fbe(%rip)    # 0x4f6f4
jle 0x1b747
movl 0x33fb6(%rip), %eax     # 0x4f6f4
addl $0x0, %eax
movl %eax, 0x33fad(%rip)     # 0x4f6f4
movl $0x2d, %edi
callq 0x138b0
movb %al, -0xf(%rbp)
movq 0x13875(%rip), %rdi     # 0x2efd0
movzbl -0xf(%rbp), %esi
callq 0x3430
movq %rax, -0x40(%rbp)
movl 0x33f86(%rip), %edi     # 0x4f6f4
callq 0x138d0
movq -0x40(%rbp), %rdi
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %esi
callq 0x3380
movq %rax, %rdi
leaq 0x8d33(%rip), %rsi      # 0x244bf
callq 0x32c0
movq %rax, %rdi
leaq 0x8d26(%rip), %rsi      # 0x244c1
callq 0x32c0
movq %rax, -0x38(%rbp)
leaq -0x16(%rbp), %rdi
leaq 0x13def(%rip), %rax     # 0x2f59e
movzwl (%rax), %esi
callq 0x13840
movq -0x38(%rbp), %rdi
movzwl -0x16(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
leaq 0x8d03(%rip), %rsi      # 0x244d1
callq 0x32c0
movq %rax, %rdi
movl 0x33f18(%rip), %esi     # 0x4f6f4
callq 0x3490
movq %rax, %rdi
leaq 0x8cf2(%rip), %rsi      # 0x244dd
callq 0x32c0
leaq 0x13da7(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
cmpl $0x2200, %eax           # imm = 0x2200
jne 0x1b81b
leaq 0x13d96(%rip), %rax     # 0x2f59e
movzwl (%rax), %eax
addl $0x0, %eax
movw %ax, %cx
leaq 0x13d86(%rip), %rax     # 0x2f59e
movw %cx, (%rax)
movw -0xe(%rbp), %ax
movq -0x8(%rbp), %rsi
leaq -0xb(%rbp), %rdx
movzwl %ax, %edi
callq 0x1b160
movl %eax, -0x1c(%rbp)
testb $0x1, -0xb(%rbp)
je 0x1b84e
movzwl -0xa(%rbp), %edi
callq 0x14360
movw -0xa(%rbp), %ax
movw %ax, 0x33ea4(%rip)      # 0x4f6f0
jmp 0x1b870
movq 0x1377b(%rip), %rdi     # 0x2efd0
leaq 0x8c8a(%rip), %rsi      # 0x244e6
callq 0x32c0
movq %rax, %rdi
movq 0x13745(%rip), %rsi     # 0x2efb0
callq 0x3310
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x13d31(%rip), %rdi     # 0x2f5b8
callq 0x15e20
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jae 0x1b8e1
movq -0x28(%rbp), %rsi
leaq 0x13d15(%rip), %rdi     # 0x2f5b8
callq 0x1dbd0
leaq -0x29(%rbp), %rdi
movzbl (%rax), %esi
callq 0x13860
movq 0x13715(%rip), %rdi     # 0x2efd0
movzbl -0x29(%rbp), %esi
callq 0x13720
movq %rax, %rdi
leaq 0x8bf1(%rip), %rsi      # 0x244bf
callq 0x32c0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1b878
movq 0x136e8(%rip), %rdi     # 0x2efd0
movq 0x136c1(%rip), %rsi     # 0x2efb0
callq 0x3310
movl -0x1c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)