// File: /workspace/github_cpp_makefile_star2_O3/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 0xc59c
movq %rax, %r14
movabsq $0x77747af4648e8d81, %rcx # imm = 0x77747AF4648E8D81
mulq %rcx
shrq $0xd, %rdx
imulq $0x4494, %rdx, %rax     # imm = 0x4494
subq %rax, %r14
imull $0x8fb9, %r14d, %ebx    # imm = 0x8FB9
shrl $0x16, %ebx
movzbl 0x2bed3(%rip), %eax     # 0x3d67c
cmpw %ax, %bx
sete %cl
cmpq $0x4020, %r14           # imm = 0x4020
setae %dl
orb %cl, %dl
jne 0x1185f
leaq 0x6d71(%rip), %rcx      # 0x18539
cmpb $0x1, (%rcx)
jne 0x11857
leaq 0x6b11(%rip), %rcx      # 0x182e9
cmpb $0x0, (%rcx)
jne 0x11857
movq 0x67ec(%rip), %r15      # 0x17fd0
leaq 0x23cc(%rip), %rsi      # 0x13bb7
movl $0x10, %edx
movq %r15, %rdi
callq 0x3300
movq %r15, %rdi
movl %ebx, %esi
callq 0x3420
movq %rax, %r15
leaq 0x23cb(%rip), %rsi      # 0x13bd7
movl $0x8, %edx
movq %rax, %rdi
callq 0x3300
movl 0x2be59(%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
movb 0x2be25(%rip), %al      # 0x3d67c
movzbl %al, %edi
callq 0x11585
testb $0x40, 0x2be0c(%rip)    # 0x3d672
je 0x1188b
movl $0xff45, %edi           # imm = 0xFF45
callq 0xb94c
movzbl %al, %eax
cmpw %ax, %bx
jne 0x1188b
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xcf99
movzbl 0x2bdea(%rip), %eax     # 0x3d67c
cmpw %ax, %bx
setne %al
leal -0x4020(%r14), %ecx
cmpl $0x72, %ecx
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x11a3e
callq 0x32c0
movl %eax, %r15d
movq %r15, %rax
subq 0x2be76(%rip), %rax     # 0x3d738
movq 0x698f(%rip), %rdi      # 0x18258
cmpq %rax, %rdi
jle 0x118d5
subl %eax, %edi
callq 0x31e0
movq %r15, %rax
subq 0x2be49(%rip), %rax     # 0x3d728
cmpq $0x3e7, %rax            # imm = 0x3E7
jle 0x11954
leaq 0x69fb(%rip), %rax      # 0x182e9
cmpb $0x0, (%rax)
jne 0x11946
movq 0x66d6(%rip), %r14      # 0x17fd0
leaq 0x22c7(%rip), %rsi      # 0x13bc8
movl $0x5, %edx
movq %r14, %rdi
callq 0x3300
movl 0x2be1c(%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
movq %r15, 0x2bddb(%rip)     # 0x3d728
movl $0x1, %eax
jmp 0x1195c
movl 0x2bdd6(%rip), %eax     # 0x3d730
incl %eax
incl 0x2bd16(%rip)           # 0x3d678
movl %eax, 0x2bdc8(%rip)     # 0x3d730
leaq 0x697a(%rip), %rax      # 0x182e9
cmpb $0x0, (%rax)
jne 0x11a1f
movq 0x6651(%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 0x221e(%rip), %rsi      # 0x13bce
movl $0x11, %edx
movq %rax, %rdi
callq 0x3300
movl 0x2bcb5(%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 0xcf99
callq 0x32c0
movl %eax, %eax
movq %rax, 0x2bcfa(%rip)     # 0x3d738
movb %bl, 0x2bc38(%rip)      # 0x3d67c
popq %rbx
popq %r14
popq %r15
retq