// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
leaq 0x8e18(%rip), %r12      # 0x184fe
movzwl (%r12), %r13d
movzbl 0x14(%rdi), %eax
movl %eax, 0x10(%rsp)
leal 0x1(%r13), %edi
movl $0xffff, %ebp           # imm = 0xFFFF
andl %ebp, %edi
callq 0xb94c
movl %eax, %ebx
movzwl (%r12), %edi
addl $0x2, %edi
andl %ebp, %edi
callq 0xb94c
movzbl %al, %eax
shll $0x8, %eax
movzbl %bl, %ebp
movzbl 0x14(%r14), %ecx
incl 0x28f2c(%rip)           # 0x38654
orl %eax, %ebp
subl %ecx, %ebp
movq 0x889d(%rip), %rbx      # 0x17fd0
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r15
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xf764
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x2d, 0xe0(%r15)
movslq 0x28ee1(%rip), %rax     # 0x38654
movq (%rbx), %rcx
movq -0x18(%rcx), %rcx
movq %rax, 0x10(%rbx,%rcx)
leaq 0x3ce2(%rip), %rsi      # 0x13468
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
leaq 0x3cd0(%rip), %rsi      # 0x1346a
movl $0xf, %edx
movq %rbx, %rdi
callq 0x3300
movzwl (%r12), %r15d
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rbx,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rbx,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r12
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xf7f1
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x30, 0xe0(%r12)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x3c49(%rip), %rsi      # 0x1347a
movl $0xb, %edx
movq %rbx, %rdi
callq 0x3300
movl 0x28e10(%rip), %esi     # 0x38654
movq %rbx, %rdi
callq 0x3420
leaq 0x3c33(%rip), %rsi      # 0x13486
movl $0x8, %edx
movq %rax, %rdi
callq 0x3300
movzwl %bp, %edi
leaq 0xf(%rsp), %r15
movq %r14, %rsi
movq %r15, %rdx
callq 0xf267
cmpb $0x1, (%r15)
movl %eax, 0x14(%rsp)
jne 0xf898
addl 0x10(%rsp), %r13d
movzwl %r13w, %r14d
movl %r14d, %edi
callq 0xc4e4
movw %r14w, 0x28dba(%rip)    # 0x38650
jmp 0xf8d3
leaq 0x3bf0(%rip), %rsi      # 0x1348f
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
leaq 0x8c3e(%rip), %r13      # 0x18518
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
je 0xf99b
xorl %ebp, %ebp
leaq 0x3b77(%rip), %r14      # 0x13468
movzbl (%rax,%rbp), %r15d
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rbx,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rbx,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r12
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xf93b
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x30, 0xe0(%r12)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x10(%rbx,%rax)
movq %rbx, %rdi
movl %r15d, %esi
callq 0x3420
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3300
incq %rbp
movq (%r13), %rax
movq 0x8(%r13), %rcx
subq %rax, %rcx
cmpq %rcx, %rbp
jb 0xf8f1
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movl 0x14(%rsp), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq