// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r14
pushq %rbx
callq 0xacca
movl $0x4494, %ecx           # imm = 0x4494
xorl %edx, %edx
divq %rcx
movq %rdx, %r14
movw $0x72, %cx
movl %r14d, %eax
xorl %edx, %edx
divw %cx
movl %eax, %ebp
movzwl %ax, %ebx
movzbl 0x2c6f3(%rip), %eax     # 0x3b65c
cmpw %ax, %bx
sete %cl
cmpq $0x4020, %r14           # imm = 0x4020
setae %dl
orb %cl, %dl
jne 0xefe5
leaq 0x7595(%rip), %rcx      # 0x16519
cmpb $0x1, (%rcx)
jne 0xefdd
leaq 0x7339(%rip), %rcx      # 0x162c9
cmpb $0x0, (%rcx)
jne 0xefdd
movq 0x7034(%rip), %rdi      # 0x15fd0
leaq 0x1c14(%rip), %rsi      # 0x10bb7
callq 0x3260
movq %rax, %rdi
movl %ebx, %esi
callq 0x33e0
leaq 0x1c1e(%rip), %rsi      # 0x10bd7
movq %rax, %rdi
callq 0x3260
movl 0x2c691(%rip), %esi     # 0x3b658
movq %rax, %rdi
callq 0x33e0
movq %rax, %rdi
callq 0x3130
movb 0x2c67f(%rip), %al      # 0x3b65c
movzbl %al, %edi
callq 0xeda7
testb $0x40, 0x2c666(%rip)    # 0x3b652
je 0xf00d
movl $0xff45, %edi           # imm = 0xFF45
callq 0xa6ec
movzbl %al, %eax
cmpw %ax, %bp
jne 0xf00d
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xb0ef
movzbl 0x2c648(%rip), %eax     # 0x3b65c
cmpw %ax, %bp
setne %al
addl $0xffffbfe0, %r14d      # imm = 0xFFFFBFE0
cmpl $0x72, %r14d
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xf11e
callq 0x3290
movl %eax, %r14d
movq %r14, %rax
subq 0x2c6d3(%rip), %rax     # 0x3b718
movq 0x71ec(%rip), %rdi      # 0x16238
cmpq %rax, %rdi
jle 0xf058
subl %eax, %edi
callq 0x31b0
movq %r14, %rax
subq 0x2c6a6(%rip), %rax     # 0x3b708
cmpq $0x3e7, %rax            # imm = 0x3E7
jle 0xf0ab
leaq 0x7258(%rip), %rax      # 0x162c9
cmpb $0x0, (%rax)
jne 0xf09f
movq 0x6f53(%rip), %rdi      # 0x15fd0
leaq 0x1b44(%rip), %rsi      # 0x10bc8
callq 0x3260
movl 0x2c681(%rip), %esi     # 0x3b710
movq %rax, %rdi
callq 0x33e0
movq %rax, %rdi
callq 0x3130
pushq $0x1
popq %rax
movq %r14, 0x2c65f(%rip)     # 0x3b708
jmp 0xf0b3
movl 0x2c65f(%rip), %eax     # 0x3b710
incl %eax
incl 0x2c59f(%rip)           # 0x3b658
movl %eax, 0x2c651(%rip)     # 0x3b710
leaq 0x7203(%rip), %rax      # 0x162c9
cmpb $0x0, (%rax)
jne 0xf104
movq 0x6efe(%rip), %rdi      # 0x15fd0
callq 0x3130
leaq 0x1af0(%rip), %rsi      # 0x10bce
movq %rax, %rdi
callq 0x3260
movl 0x2c56c(%rip), %esi     # 0x3b658
movq %rax, %rdi
callq 0x33e0
movq %rax, %rdi
callq 0x3130
movq %rax, %rdi
callq 0x3130
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0xb0ef
callq 0x3290
movl %eax, %eax
movq %rax, 0x2c5fa(%rip)     # 0x3b718
movb %bl, 0x2c538(%rip)      # 0x3b65c
popq %rbx
popq %r14
popq %rbp
retq
nop