// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x1e0, %rsp            # imm = 0x1E0
incq 0xb7d4(%rip)            # 0x164f0
movl 0xb7be(%rip), %eax      # 0x164e0
testl %eax, %eax
je 0xad33
decl %eax
movl %eax, 0xb7b2(%rip)      # 0x164e0
jmp 0xb0be
cmpb $0x0, 0xb58f(%rip)      # 0x162c9
jne 0xb079
movzwl 0xb797(%rip), %edi      # 0x164de
callq 0xa6ec
movzbl %al, %ebx
imulq $0x30, %rbx, %rax
leaq 0x2b8f6(%rip), %rcx     # 0x36650
movups (%rcx,%rax), %xmm0
movups 0x10(%rcx,%rax), %xmm1
movups 0x20(%rcx,%rax), %xmm2
movaps %xmm2, 0x20(%rsp)
movaps %xmm1, 0x10(%rsp)
movaps %xmm0, (%rsp)
movl 0xb798(%rip), %eax      # 0x16514
incl %eax
movl %eax, 0xb790(%rip)      # 0x16514
movl $0x3e8, %ecx            # imm = 0x3E8
cltd
idivl %ecx
testl %edx, %edx
jne 0xad98
pushq $0x1
popq %rdi
callq 0x3320
leaq 0x58(%rsp), %rdi
callq 0x3190
leaq 0x68(%rsp), %rdi
movslq 0xb766(%rip), %rsi      # 0x16514
callq 0xb30f
leaq 0x5302(%rip), %rsi      # 0x100bc
movq %rax, %rdi
callq 0x3260
movzwl 0xb715(%rip), %esi      # 0x164de
movq %rax, %rdi
callq 0xb2b0
leaq 0x52e4(%rip), %rsi      # 0x100bc
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl %ebx, %esi
callq 0xaba6
leaq 0x52bc(%rip), %rsi      # 0x100ad
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzwl 0xb6db(%rip), %eax      # 0x164de
incl %eax
movzwl %ax, %edi
callq 0xa6ec
movzbl %al, %esi
movq %rbx, %rdi
callq 0xaba6
leaq 0x528e(%rip), %rsi      # 0x100ad
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzwl 0xb6ad(%rip), %eax      # 0x164de
addl $0x2, %eax
movzwl %ax, %edi
callq 0xa6ec
movzbl %al, %esi
movq %rbx, %rdi
callq 0xaba6
leaq 0x525b(%rip), %rsi      # 0x100a9
movq %rax, %rdi
callq 0x3260
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl 0x18(%rax,%rcx), %edx
orl $0x20, %edx
movl %edx, 0x18(%rax,%rcx)
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x3360
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movq $0xa, 0x10(%rax,%rcx)
movq 0x8(%rsp), %rsi
movq %rax, %rdi
callq 0x3220
leaq 0x5210(%rip), %rsi      # 0x100ac
movq %rax, %rdi
callq 0x3260
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl $0xffffff4f, %edx       # imm = 0xFFFFFF4F
andl 0x18(%rax,%rcx), %edx
orl $0x80, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x51ea(%rip), %rsi      # 0x100af
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzbl 0xb5f9(%rip), %edi      # 0x164d0
movzbl 0xb5f3(%rip), %esi      # 0x164d1
callq 0xc36c
movq %rbx, %rdi
movl %eax, %esi
callq 0xb2b0
leaq 0x51c3(%rip), %rsi      # 0x100b7
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzbl 0xb5cc(%rip), %edi      # 0x164d2
movzbl 0xb5c6(%rip), %esi      # 0x164d3
callq 0xc36c
movq %rbx, %rdi
movl %eax, %esi
callq 0xb2b0
leaq 0x519c(%rip), %rsi      # 0x100bf
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzbl 0xb59f(%rip), %edi      # 0x164d4
movzbl 0xb599(%rip), %esi      # 0x164d5
callq 0xc36c
movq %rbx, %rdi
movl %eax, %esi
callq 0xb2b0
leaq 0x5175(%rip), %rsi      # 0x100c7
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzbl 0xb572(%rip), %edi      # 0x164d6
movzbl 0xb56c(%rip), %esi      # 0x164d7
callq 0xc36c
movq %rbx, %rdi
movl %eax, %esi
callq 0xb2b0
leaq 0x514e(%rip), %rsi      # 0x100cf
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
movzbl 0xb549(%rip), %edi      # 0x164dc
movzbl 0xb543(%rip), %esi      # 0x164dd
callq 0xc36c
movq %rbx, %rdi
movl %eax, %esi
callq 0xb2b0
leaq 0x5127(%rip), %rsi      # 0x100d7
movq %rax, %rdi
callq 0x3260
movq 0xb531(%rip), %rsi      # 0x164f0
decq %rsi
movq %rax, %rdi
callq 0x3170
movq %rax, %rdi
callq 0x3130
leaq 0xb540(%rip), %rax      # 0x16519
cmpb $0x1, (%rax)
jne 0xb008
leaq 0x70(%rsp), %rsi
leaq 0x38(%rsp), %rdi
callq 0x3370
movq 0xafdc(%rip), %rdi      # 0x15fd0
leaq 0x38(%rsp), %rsi
callq 0x3220
leaq 0x38(%rsp), %rdi
callq 0x3438
movq %rsp, %rdi
callq 0xe125
movl %eax, %ebx
cmpb $-0x1, (%rsp)
jne 0xb033
movq 0xafb1(%rip), %rdi      # 0x15fd0
leaq 0x50bd(%rip), %rsi      # 0x100e3
callq 0x3260
movq %rax, %rdi
callq 0x3130
movzbl 0x14(%rsp), %eax
addw %ax, 0xb49f(%rip)       # 0x164de
movzbl 0x15(%rsp), %eax
addl %eax, %ebx
pushq $0x4
popq %rcx
movl %ebx, %eax
cltd
idivl %ecx
decl %eax
movl %eax, 0xb48a(%rip)      # 0x164e0
movl 0xb47c(%rip), %ecx      # 0x164d8
testl %ecx, %ecx
je 0xb06f
addl %ecx, %eax
movl %eax, 0xb478(%rip)      # 0x164e0
andl $0x0, 0xb469(%rip)      # 0x164d8
leaq 0x58(%rsp), %rdi
callq 0x31c0
movslq 0xb490(%rip), %rax      # 0x16510
leaq 0x58(%rsp), %rdi
movq %rax, (%rdi)
callq 0xb371
leaq 0x2b565(%rip), %rax     # 0x365f9
cmpb $0x1, (%rax)
jne 0xb0be
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xa6ec
testb %al, %al
je 0xb0be
movl $0xff0f, %edi           # imm = 0xFF0F
callq 0xa6ec
movzbl %al, %edi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xb0ef
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbx
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3438
jmp 0xb0dd
jmp 0xb0da
jmp 0xb0da
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x31c0
movq %rbx, %rdi
callq 0x3400