// File: /workspace/github_cpp_makefile_star2_O3/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 0xbf1b(%rip)            # 0x18510
movl 0xbf05(%rip), %eax      # 0x18500
testl %eax, %eax
je 0xc60c
decl %eax
movl %eax, 0xbef9(%rip)      # 0x18500
jmp 0xcf3c
cmpb $0x0, 0xbcd6(%rip)      # 0x182e9
jne 0xceb9
movzwl 0xbede(%rip), %edi      # 0x184fe
callq 0xb94c
movzbl %al, %r14d
leaq (%r14,%r14,2), %rax
shll $0x4, %eax
leaq 0x2c039(%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 0xbeda(%rip), %eax      # 0x18534
leal 0x1(%rax), %ecx
movl %ecx, 0xbed1(%rip)      # 0x18534
imull $0x26e978d5, %eax, %eax # imm = 0x26E978D5
addl $0x27ef9dad, %eax       # imm = 0x27EF9DAD
rorl $0x3, %eax
cmpl $0x418936, %eax         # imm = 0x418936
ja 0xc682
movl $0x1, %edi
callq 0x3360
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
callq 0x31c0
movslq 0xbe9e(%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), %rcx
leaq (%rsp,%rcx), %r12
addq $0x68, %r12
cmpb $0x0, 0xe1(%r12)
jne 0xc6e4
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq 0x68(%rsp), %rax
leaq 0x68(%rsp), %rbx
movb $0x30, 0xe0(%r12)
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 0x6996(%rip), %rsi      # 0x130bc
movl $0x2, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbdc3(%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), %rcx
leaq (%rbx,%rcx), %r12
cmpb $0x0, 0x149(%rsp,%rcx)
jne 0xc785
movq %r12, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r12)
movq 0x68(%rsp), %rax
movb $0x30, 0xe0(%r12)
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 0x68fa(%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), %rcx
leaq (%rbx,%rcx), %r15
cmpb $0x0, 0x149(%rsp,%rcx)
jne 0xc818
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r15)
movq 0x68(%rsp), %rax
movb $0x30, 0xe0(%r15)
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 0x6859(%rip), %rsi      # 0x130ad
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbc96(%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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0x149(%rsp,%rcx)
jne 0xc8bd
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq 0x68(%rsp), %rax
movb $0x30, 0xe0(%r14)
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 0x67b3(%rip), %rsi      # 0x130ad
movl $0x1, %edx
movq %rbx, %rdi
callq 0x3300
movzwl 0xbbf0(%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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0x149(%rsp,%rcx)
jne 0xc964
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq 0x68(%rsp), %rax
movb $0x30, 0xe0(%r14)
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 0x6708(%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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0x149(%rsp,%rcx)
jne 0xc9f9
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq 0x68(%rsp), %rax
movb $0x20, 0xe0(%r14)
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 0x6680(%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 0x6655(%rip), %rsi      # 0x130af
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xba82(%rip), %edi      # 0x184f0
movzbl 0xba7c(%rip), %esi      # 0x184f1
callq 0xe548
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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xcabb
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x30, 0xe0(%r14)
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 0x65c0(%rip), %rsi      # 0x130b7
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb9e7(%rip), %edi      # 0x184f2
movzbl 0xb9e1(%rip), %esi      # 0x184f3
callq 0xe548
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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xcb58
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x30, 0xe0(%r14)
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 0x652b(%rip), %rsi      # 0x130bf
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb94c(%rip), %edi      # 0x184f4
movzbl 0xb946(%rip), %esi      # 0x184f5
callq 0xe548
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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xcbf5
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x30, 0xe0(%r14)
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 0x6496(%rip), %rsi      # 0x130c7
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb8b1(%rip), %edi      # 0x184f6
movzbl 0xb8ab(%rip), %esi      # 0x184f7
callq 0xe548
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), %rcx
leaq (%rbx,%rcx), %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xcc92
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x30, 0xe0(%r14)
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 0x6401(%rip), %rsi      # 0x130cf
movl $0x7, %edx
movq %rbx, %rdi
callq 0x3300
movzbl 0xb81a(%rip), %edi      # 0x184fc
movzbl 0xb814(%rip), %esi      # 0x184fd
callq 0xe548
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), %rcx
movq %rbx, %r14
addq %rcx, %r14
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xcd31
movq %r14, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r14)
movq (%rbx), %rax
movb $0x30, 0xe0(%r14)
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 0x636a(%rip), %rsi      # 0x130d7
movl $0xb, %edx
movq %rbx, %rdi
callq 0x3300
movq 0xb78f(%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 0xb77c(%rip), %rax      # 0x18539
cmpb $0x1, (%rax)
jne 0xcdff
leaq 0x70(%rsp), %rsi
movq %rsp, %rdi
callq 0x33a0
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0xb1f1(%rip), %rdi      # 0x17fd0
callq 0x3300
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xcdff
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x32b0
leaq 0x20(%rsp), %rdi
callq 0x108f6
movl %eax, %ebx
cmpb $-0x1, 0x20(%rsp)
jne 0xce58
movq 0xb1b7(%rip), %r14      # 0x17fd0
leaq 0x62c3(%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 0xb185(%rip), %rdi      # 0x17fd0
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movzbl 0x34(%rsp), %eax
addw %ax, 0xb69a(%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, 0xb681(%rip)      # 0x18500
movl 0xb673(%rip), %ecx      # 0x184f8
testl %ecx, %ecx
je 0xce9b
addl %ecx, %eax
movl %eax, 0xb66f(%rip)      # 0x18500
movl $0x0, 0xb65d(%rip)      # 0x184f8
movq 0xb0fe(%rip), %rsi      # 0x17fa0
leaq 0x58(%rsp), %rdi
callq 0x31f0
leaq 0xd8(%rsp), %rdi
callq 0x30e0
movslq 0xb670(%rip), %rax      # 0x18530
testq %rax, %rax
jle 0xcf09
movl %eax, %ecx
imulq $0x10624dd3, %rcx, %rcx # imm = 0x10624DD3
shrq $0x26, %rcx
imulq $-0x3e8, %rcx, %rdx     # imm = 0xFC18
addq %rax, %rdx
imulq $0xf4240, %rdx, %rax    # imm = 0xF4240
leaq 0x58(%rsp), %rbx
movq %rcx, (%rbx)
movq %rax, 0x8(%rbx)
movq %rbx, %rdi
movq %rbx, %rsi
callq 0x30d0
cmpl $-0x1, %eax
jne 0xcf09
callq 0x3050
cmpl $0x4, (%rax)
je 0xceef
leaq 0x2b709(%rip), %rax     # 0x38619
cmpb $0x1, (%rax)
jne 0xcf3c
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xb94c
testb %al, %al
je 0xcf3c
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xb94c
movzbl %al, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xcf99
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 0xcf73
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x32b0
jmp 0xcf73
jmp 0xcf70
jmp 0xcf70
movq %rax, %rbx
movq 0xb026(%rip), %rsi      # 0x17fa0
leaq 0x58(%rsp), %rdi
callq 0x31f0
leaq 0xd8(%rsp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x3440