// File: /workspace/github_cpp_makefile_star2_O3/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 0x8fae(%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
orl %eax, %ebp
movzbl 0x14(%r14), %eax
subl %eax, %ebp
movl 0x290be(%rip), %eax     # 0x38654
incl %eax
movl %eax, 0x290b6(%rip)     # 0x38654
movq 0x8a2b(%rip), %rbx      # 0x17fd0
movq (%rbx), %rcx
movq -0x18(%rcx), %rdx
leaq (%rbx,%rdx), %r15
cmpb $0x0, 0xe1(%rbx,%rdx)
jne 0xf5d8
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r15)
movl 0x2907f(%rip), %eax     # 0x38654
movq (%rbx), %rcx
movb $0x2d, 0xe0(%r15)
movq -0x18(%rcx), %rcx
cltq
movq %rax, 0x10(%rbx,%rcx)
leaq 0x3e76(%rip), %rsi      # 0x13468
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
leaq 0x3e64(%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), %rcx
leaq (%rbx,%rcx), %r12
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xf658
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq (%rbx), %rax
movb $0x30, 0xe0(%r12)
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 0x3de5(%rip), %rsi      # 0x1347a
movl $0xb, %edx
movq %rbx, %rdi
callq 0x3300
movl 0x28fac(%rip), %esi     # 0x38654
movq %rbx, %rdi
callq 0x3420
leaq 0x3dcf(%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 0xf0fe
cmpb $0x1, (%r15)
movl %eax, 0x14(%rsp)
jne 0xf6fc
addl 0x10(%rsp), %r13d
movzwl %r13w, %r14d
movl %r14d, %edi
callq 0xc490
movw %r14w, 0x28f56(%rip)    # 0x38650
jmp 0xf737
leaq 0x3d8c(%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 0x8dda(%rip), %r13      # 0x18518
movq (%r13), %rax
cmpq %rax, 0x8(%r13)
je 0xf7f7
xorl %ebp, %ebp
leaq 0x3d13(%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), %rcx
leaq (%rbx,%rcx), %r12
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xf79a
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq (%rbx), %rax
movb $0x30, 0xe0(%r12)
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 0xf755
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