// File: 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 %rbp
movq %rsp, %rbp
subq $0x20, %rsp
callq 0x144e0
movl $0x4494, %ecx           # imm = 0x4494
xorl %edx, %edx
divq %rcx
movl %edx, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
movl $0x72, %ecx
cltd
idivl %ecx
movl %eax, -0x8(%rbp)
movl -0x8(%rbp), %eax
movzbl 0x32518(%rip), %ecx     # 0x5471c
cmpl %ecx, %eax
je 0x2227f
cmpl $0x90, -0x8(%rbp)
jge 0x2227f
leaq 0xd3c1(%rip), %rax      # 0x2f5d9
testb $0x1, (%rax)
je 0x22273
leaq 0xd165(%rip), %rax      # 0x2f389
testb $0x1, (%rax)
jne 0x22273
movq 0xcda0(%rip), %rdi      # 0x2efd0
leaq 0x29ac(%rip), %rsi      # 0x24be3
callq 0x32c0
movq %rax, %rdi
movl -0x8(%rbp), %esi
callq 0x3490
movq %rax, %rdi
leaq 0x29b2(%rip), %rsi      # 0x24c03
callq 0x32c0
movq %rax, %rdi
movl 0x324b9(%rip), %esi     # 0x54718
callq 0x3490
movq %rax, %rdi
movq 0xcd42(%rip), %rsi      # 0x2efb0
callq 0x3310
movzbl 0x324a2(%rip), %edi     # 0x5471c
callq 0x21f40
movzbl 0x3248c(%rip), %eax     # 0x54712
andl $0x40, %eax
cmpl $0x0, %eax
je 0x222b1
movl $0xff45, %edi           # imm = 0xFF45
callq 0x13450
movzbl %al, %eax
cmpl -0x8(%rbp), %eax
jne 0x222b1
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x14cc0
movl -0x8(%rbp), %eax
movzbl 0x32461(%rip), %ecx     # 0x5471c
cmpl %ecx, %eax
je 0x22406
cmpl $0x90, -0x8(%rbp)
jne 0x22406
callq 0x32f0
movl %eax, %eax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
subq 0x324f2(%rip), %rax     # 0x547d8
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq 0xcfbb(%rip), %rax      # 0x2f2b0
jge 0x22309
movq 0xcfb2(%rip), %rax      # 0x2f2b0
subq -0x18(%rbp), %rax
movl %eax, %edi
callq 0x31e0
movq -0x10(%rbp), %rax
subq 0x324b4(%rip), %rax     # 0x547c8
cmpq $0x3e8, %rax            # imm = 0x3E8
jl 0x2236d
leaq 0xd066(%rip), %rax      # 0x2f389
testb $0x1, (%rax)
jne 0x22358
movq 0xcca1(%rip), %rdi      # 0x2efd0
leaq 0x28be(%rip), %rsi      # 0x24bf4
callq 0x32c0
movq %rax, %rdi
movl 0x3248c(%rip), %esi     # 0x547d0
callq 0x3490
movq %rax, %rdi
movq 0xcc5d(%rip), %rsi      # 0x2efb0
callq 0x3310
movl $0x0, 0x3246e(%rip)     # 0x547d0
movq -0x10(%rbp), %rax
movq %rax, 0x3245b(%rip)     # 0x547c8
movl 0x3245d(%rip), %eax     # 0x547d0
addl $0x1, %eax
movl %eax, 0x32454(%rip)     # 0x547d0
movl 0x32396(%rip), %eax     # 0x54718
addl $0x1, %eax
movl %eax, 0x3238d(%rip)     # 0x54718
callq 0x21d30
leaq 0xcff2(%rip), %rax      # 0x2f389
testb $0x1, (%rax)
jne 0x223ea
movq 0xcc2d(%rip), %rdi      # 0x2efd0
movq 0xcc06(%rip), %rsi      # 0x2efb0
callq 0x3310
movq %rax, %rdi
leaq 0x2841(%rip), %rsi      # 0x24bfa
callq 0x32c0
movq %rax, %rdi
movl 0x32351(%rip), %esi     # 0x54718
callq 0x3490
movq %rax, %rdi
movq 0xcbda(%rip), %rsi      # 0x2efb0
callq 0x3310
movq %rax, %rdi
movq 0xcbcb(%rip), %rsi      # 0x2efb0
callq 0x3310
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x14cc0
callq 0x32f0
movl %eax, %eax
movq %rax, 0x323d2(%rip)     # 0x547d8
movl -0x8(%rbp), %eax
movb %al, 0x3230d(%rip)      # 0x5471c
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop