// File: /workspace/github_cpp_makefile_star2_O1/rockytriton[P]dreamsys_emu_gb/src/ppu.cpp
// Function: dsemu::ppu::tick()
void tick() {
    int f = cpu::getTickCount() % (TICKS_PER_FRAME);
    int l = f / 114;

    if (l != currentLine && l < 144) {
        if (DEBUG && !cpu::haltWaitingForInterrupt) cout << "PPU:> NEW LINE: " << l << " FRAME: " << currentFrame << endl;

        drawLine(currentLine);
    }

    if (lcdStats & 0x40 && bus::read(0xFF45) == l) {
        cpu::handleInterrupt(2, true, false);
    }
    
    if (l != currentLine && l == 144) {
        long end = SDL_GetTicks();
		long frameTime = end - prev;

		if (frameTime < targetFrameTime) 
		{
			SDL_Delay(targetFrameTime - frameTime);
		}

		if (end - start >= 1000) {
			if (!cpu::haltWaitingForInterrupt) cout << "FPS: " << count << endl;
			count = 0;
			start = end;
		}

		count++;

        currentFrame++;
        drawFrame();
        if (!cpu::haltWaitingForInterrupt) cout << endl << "PPU:> NEW FRAME: " << currentFrame << endl << endl;

        cpu::handleInterrupt(cpu::IVBlank, true, false);

        prev = SDL_GetTicks();
    }

    currentLine = l;


}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
callq 0xc5eb
movq %rax, %r14
movabsq $0x77747af4648e8d81, %rcx # imm = 0x77747AF4648E8D81
mulq %rcx
shrq $0xd, %rdx
imulq $0x4494, %rdx, %rax     # imm = 0x4494
subq %rax, %r14
imulq $0x23ee090, %r14, %rbx  # imm = 0x23EE090
shrq $0x20, %rbx
cmpq $0x401f, %r14           # imm = 0x401F
ja 0x11a86
movzbl 0x2bc9a(%rip), %eax     # 0x3d67c
cmpl %eax, %ebx
je 0x11a86
leaq 0x6b48(%rip), %rax      # 0x18539
cmpb $0x1, (%rax)
jne 0x11a7a
leaq 0x68e8(%rip), %rax      # 0x182e9
cmpb $0x0, (%rax)
jne 0x11a7a
movq 0x65c3(%rip), %r15      # 0x17fd0
leaq 0x21a3(%rip), %rsi      # 0x13bb7
movl $0x10, %edx
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
movl %ebx, %esi
callq 0x3420
movq %rax, %r15
leaq 0x21a2(%rip), %rsi      # 0x13bd7
movl $0x8, %edx
movq %rax, %rdi
callq 0x3300
movl 0x2bc30(%rip), %esi     # 0x3d678
movq %r15, %rdi
callq 0x3420
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r15, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movzbl 0x2bbfb(%rip), %edi     # 0x3d67c
callq 0x117b0
testb $0x40, 0x2bbe5(%rip)    # 0x3d672
je 0x11ab1
movl $0xff45, %edi           # imm = 0xFF45
callq 0xb94c
movzbl %al, %eax
cmpl %eax, %ebx
jne 0x11ab1
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xd040
movzbl 0x2bbc4(%rip), %eax     # 0x3d67c
cmpl %eax, %ebx
setne %al
addl $0xffffbfe0, %r14d      # imm = 0xFFFFBFE0
cmpl $0x72, %r14d
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x11c5f
callq 0x32c0
movl %eax, %r15d
movq %r15, %rax
subq 0x2bc50(%rip), %rax     # 0x3d738
movq 0x6769(%rip), %rdi      # 0x18258
cmpq %rax, %rdi
jle 0x11afb
subl %eax, %edi
callq 0x31e0
movq %r15, %rax
subq 0x2bc23(%rip), %rax     # 0x3d728
cmpq $0x3e8, %rax            # imm = 0x3E8
jl 0x11b7d
leaq 0x67d5(%rip), %rax      # 0x182e9
cmpb $0x0, (%rax)
jne 0x11b6c
movq 0x64b0(%rip), %r14      # 0x17fd0
leaq 0x20a1(%rip), %rsi      # 0x13bc8
movl $0x5, %edx
movq %r14, %rdi
callq 0x3300
movl 0x2bbf6(%rip), %esi     # 0x3d730
movq %r14, %rdi
callq 0x3420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movl $0x0, 0x2bbba(%rip)     # 0x3d730
movq %r15, 0x2bbab(%rip)     # 0x3d728
incl 0x2bbad(%rip)           # 0x3d730
incl 0x2baef(%rip)           # 0x3d678
leaq 0x6759(%rip), %rax      # 0x182e9
cmpb $0x0, (%rax)
jne 0x11c40
movq 0x6430(%rip), %r14      # 0x17fd0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %rax, %r14
leaq 0x1ffd(%rip), %rsi      # 0x13bce
movl $0x11, %edx
movq %rax, %rdi
callq 0x3300
movl 0x2ba94(%rip), %esi     # 0x3d678
movq %r14, %rdi
callq 0x3420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xd040
callq 0x32c0
movl %eax, %eax
movq %rax, 0x2bad9(%rip)     # 0x3d738
movb %bl, 0x2ba17(%rip)      # 0x3d67c
popq %rbx
popq %r14
popq %r15
retq
nop