// File: /workspace/github_cpp_makefile_star2_O1/rockytriton[P]dreamsys_emu_gb/src/cpu.cpp
// Function: dsemu::cpu::tick()
void tick() {
    totalTicks++;

    if (remainingTicks) {
        remainingTicks--;
        return;
    }

    if (!haltWaitingForInterrupt) {
        byte b = bus::read(regPC);

        OpCode opCode = opCodes[b];
        n++;
        
        if ((n % 1000) == 0) {
            usleep(1);
        }

        if (n == 0xeae8a) {
            //paused = true;
        }

        std::stringstream ss;
        ss << Int64(n) << ": " << Short(regPC) << ": " << Byte(b) << " " << Byte(bus::read(regPC + 1)) << " " << Byte(bus::read(regPC + 2)) << " (" << std::left << std::setfill(' ') << std::setw(10) << opCode.name << ") "
                << std::right
                << " - AF: " << Short(toShort(regAF.lo, regAF.hi))
                << " - BC: " << Short(toShort(regBC.lo, regBC.hi))
                << " - DE: " << Short(toShort(regDE.lo, regDE.hi))
                << " - HL: " << Short(toShort(regHL.lo, regHL.hi))
                << " - SP: " << Short(toShort(regSP.lo, regSP.hi))
                << " - Cycles: " << (totalTicks - 1)
                << endl;

        if (DEBUG) cout << ss.str();

        int n = handle_op(opCode);

        if (opCode.value == 0xff) {
            cout << "HIT FF" << endl;
            //sleep(5);
        }

        regPC += opCode.length;

        remainingTicks = ((n + opCode.cycles) / 4) - 1;

        if (extraCycles) {
            remainingTicks += extraCycles;
            extraCycles = 0;
        }
    }

    std::this_thread::sleep_for(std::chrono::milliseconds(cpuSpeed));

    if (interruptsEnabled && bus::read(0xFF0F)) {
        handleInterrupt(bus::read(0xFF0F), true, false);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1e0, %rsp            # imm = 0x1E0
incq 0xbecc(%rip)            # 0x18510
movl 0xbeb6(%rip), %eax      # 0x18500
testl %eax, %eax
je 0xc65b
decl %eax
movl %eax, 0xbeaa(%rip)      # 0x18500
jmp 0xcfe3
cmpb $0x0, 0xbc87(%rip)      # 0x182e9
jne 0xcf55
movzwl 0xbe8f(%rip), %edi      # 0x184fe
callq 0xb94c
movzbl %al, %r14d
leaq (%r14,%r14,2), %rax
shll $0x4, %eax
leaq 0x2bfea(%rip), %rcx     # 0x38670
movups (%rcx,%rax), %xmm0
movups 0x10(%rcx,%rax), %xmm1
movups 0x20(%rcx,%rax), %xmm2
movaps %xmm2, 0x40(%rsp)
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movl 0xbe8b(%rip), %eax      # 0x18534
leal 0x1(%rax), %ecx
movl %ecx, 0xbe82(%rip)      # 0x18534
imull $0x26e978d5, %eax, %eax # imm = 0x26E978D5
addl $0x27ef9dad, %eax       # imm = 0x27EF9DAD
rorl $0x3, %eax
cmpl $0x418936, %eax         # imm = 0x418936
ja 0xc6d1
movl $0x1, %edi
callq 0x3360
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x31c0
movslq 0xbe4f(%rip), %r15      # 0x18534
movq 0x10(%rbx), %rax
movq -0x18(%rax), %rcx
movl 0x80(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %r12
addq $0x68, %r12
cmpb $0x0, 0xe1(%r12)
jne 0xc736
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
leaq 0x68(%rsp), %rbx
movb $0x30, 0xe0(%r12)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x10, 0x78(%rsp,%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 0x6941(%rip), %rsi      # 0x130bc
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbd6e(%rip), %r15d     # 0x184fe
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rcx
movl 0x80(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r12
cmpb $0x0, 0x149(%rsp,%rax)
jne 0xc7dd
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r12)
movb $0x1, 0xe1(%r12)
movb $0x30, 0xe0(%r12)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x78(%rsp,%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 0x689d(%rip), %rsi      # 0x130bc
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rcx
movl 0x80(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r15
cmpb $0x0, 0x149(%rsp,%rax)
jne 0xc877
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x30, 0xe0(%r15)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x78(%rsp,%rax)
movq %rbx, %rdi
movl %r14d, %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)
leaq 0x67f5(%rip), %rsi      # 0x130ad
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbc32(%rip), %eax      # 0x184fe
incl %eax
movzwl %ax, %edi
callq 0xb94c
movl %eax, %ebp
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rcx
movl 0x80(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r14
cmpb $0x0, 0x149(%rsp,%rax)
jne 0xc923
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x78(%rsp,%rax)
movzbl %bpl, %esi
movq %rbx, %rdi
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)
leaq 0x6748(%rip), %rsi      # 0x130ad
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbb85(%rip), %eax      # 0x184fe
addl $0x2, %eax
movzwl %ax, %edi
callq 0xb94c
movl %eax, %ebp
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rcx
movl 0x80(%rsp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r14
cmpb $0x0, 0x149(%rsp,%rax)
jne 0xc9d1
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
movq $0x2, 0x78(%rsp,%rax)
movzbl %bpl, %esi
movq %rbx, %rdi
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)
leaq 0x6696(%rip), %rsi      # 0x130a9
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rcx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl 0x80(%rsp,%rcx), %edx
orl $0x20, %edx
movl %edx, 0x80(%rsp,%rcx)
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %r14
cmpb $0x0, 0x149(%rsp,%rax)
jne 0xca6d
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x20, 0xe0(%r14)
movq 0x68(%rsp), %rax
movq -0x18(%rax), %rax
movq $0xa, 0x78(%rsp,%rax)
movq 0x28(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rdx
movq %rbx, %rdi
callq 0x3300
movq %rax, %rbx
leaq 0x6607(%rip), %rsi      # 0x130ac
movl $0x2, %edx
movq %rax, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rax
movl $0xffffff4f, %ecx       # imm = 0xFFFFFF4F
andl 0x18(%rbx,%rax), %ecx
orl $0x80, %ecx
movl %ecx, 0x18(%rbx,%rax)
leaq 0x65dc(%rip), %rsi      # 0x130af
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xba09(%rip), %edi      # 0x184f0
movzbl 0xba03(%rip), %esi      # 0x184f1
callq 0xe69c
movl %eax, %ebp
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), %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xcb38
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movzwl %bp, %esi
movq %rbx, %rdi
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 0x6540(%rip), %rsi      # 0x130b7
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb967(%rip), %edi      # 0x184f2
movzbl 0xb961(%rip), %esi      # 0x184f3
callq 0xe69c
movl %eax, %ebp
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), %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xcbdc
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movzwl %bp, %esi
movq %rbx, %rdi
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 0x64a4(%rip), %rsi      # 0x130bf
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb8c5(%rip), %edi      # 0x184f4
movzbl 0xb8bf(%rip), %esi      # 0x184f5
callq 0xe69c
movl %eax, %ebp
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), %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xcc80
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movzwl %bp, %esi
movq %rbx, %rdi
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 0x6408(%rip), %rsi      # 0x130c7
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb823(%rip), %edi      # 0x184f6
movzbl 0xb81d(%rip), %esi      # 0x184f7
callq 0xe69c
movl %eax, %ebp
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), %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xcd24
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movzwl %bp, %esi
movq %rbx, %rdi
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 0x636c(%rip), %rsi      # 0x130cf
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb785(%rip), %edi      # 0x184fc
movzbl 0xb77f(%rip), %esi      # 0x184fd
callq 0xe69c
movl %eax, %ebp
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
movq %rbx, %r14
addq %rax, %r14
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xcdca
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r14)
movb $0x1, 0xe1(%r14)
movb $0x30, 0xe0(%r14)
movq (%rbx), %rax
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movzwl %bp, %esi
movq %rbx, %rdi
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 0x62ce(%rip), %rsi      # 0x130d7
movl $0xb, %edx
movq %rbx, %rdi
callq 0x3300
movq 0xb6f3(%rip), %rsi      # 0x18510
decq %rsi
movq %rbx, %rdi
callq 0x3190
movq %rax, %rbx
movq (%rax), %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 0xb6e0(%rip), %rax      # 0x18539
cmpb $0x1, (%rax)
jne 0xce9b
leaq 0x70(%rsp), %rsi
movq %rsp, %rdi
callq 0x33a0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0xb155(%rip), %rdi      # 0x17fd0
callq 0x3300
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xce9b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x20(%rsp), %rdi
callq 0x10af9
movl %eax, %ebx
cmpb $-0x1, 0x20(%rsp)
jne 0xcef4
movq 0xb11b(%rip), %r14      # 0x17fd0
leaq 0x6227(%rip), %rsi      # 0x130e3
movl $0x6, %edx
movq %r14, %rdi
callq 0x3300
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq 0xb0e9(%rip), %rdi      # 0x17fd0
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movzbl 0x34(%rsp), %eax
addw %ax, 0xb5fe(%rip)       # 0x184fe
movzbl 0x35(%rsp), %eax
movl %ebx, %ecx
addl %eax, %ecx
leal 0x3(%rbx,%rax), %eax
cmovnsl %ecx, %eax
sarl $0x2, %eax
decl %eax
movl %eax, 0xb5e5(%rip)      # 0x18500
movl 0xb5d7(%rip), %ecx      # 0x184f8
testl %ecx, %ecx
je 0xcf37
addl %ecx, %eax
movl %eax, 0xb5d3(%rip)      # 0x18500
movl $0x0, 0xb5c1(%rip)      # 0x184f8
movq 0xb062(%rip), %rsi      # 0x17fa0
leaq 0x58(%rsp), %rdi
callq 0x31f0
leaq 0xd8(%rsp), %rdi
callq 0x30e0
movslq 0xb5d4(%rip), %rcx      # 0x18530
testq %rcx, %rcx
jle 0xcfb0
movq %rcx, %rax
shrq $0x3, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
mulq %rdx
shrq $0x4, %rdx
imulq $-0x3e8, %rdx, %rax     # imm = 0xFC18
addq %rcx, %rax
imulq $0xf4240, %rax, %rax    # imm = 0xF4240
leaq 0x58(%rsp), %rbx
movq %rdx, (%rbx)
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x30d0
cmpl $-0x1, %eax
jne 0xcfb0
callq 0x3050
cmpl $0x4, (%rax)
je 0xcf96
leaq 0x2b662(%rip), %rax     # 0x38619
cmpb $0x1, (%rax)
jne 0xcfe3
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xb94c
testb %al, %al
je 0xcfe3
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xb94c
movzbl %al, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xd040
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xd01a
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0xd01a
jmp 0xd017
jmp 0xd017
movq %rax, %rbx
movq 0xaf7f(%rip), %rsi      # 0x17fa0
leaq 0x58(%rsp), %rdi
callq 0x31f0
leaq 0xd8(%rsp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3440