// File: /workspace/github_cpp_makefile_star2_O1/mindbeast[P]rnes/ppu.cpp
// Function: Rnes::Ppu::tick()
void Ppu::tick() {
  uint32_t scanline = getScanline();
  uint32_t lineClock = getScanlineOffset();
  bool isVblank = scanline >= 240 and scanline <= 260;

  // Update vram registers
  if (!isVblank and renderBackgroundEnabled()) {
    if (lineClock == 255 and scanline < 240) {
      render(scanline);
      nes->notifyScanlineComplete();
    }
    if (lineClock == 256) {
      vramYInc();
    }
    if (lineClock == 257) {
      vramXReset();
    }
    if (scanline == 261 and lineClock >= 280 and lineClock <= 304) {
      vramYReset();
    }
  }

  // set and unset vblank register
  if (scanline == vblankScanline and lineClock == 0) {
    setVblankFlag();
    if (nmiOnVblank()) {
      nmiRequested = true;
    }
  } else if (scanline == vblankScanelineEnd and lineClock == 0) {
    clearVblankFlag();
    clearSprite0Hit();
    clearLostSprites();
  } else if (scanline == vblankScanelineEnd and lineClock == (ticksPerScanline - 1)) {
    sdl->renderSync();
    /*
    float currentTime = timerGetMs();
    float diffTime = currentTime - lastFrameTimeMs;
    if (diffTime < frameTimeMs) {
        float waitTime = frameTimeMs - diffTime;
        sleepMs(waitTime);
    }
    lastFrameTimeMs = timerGetMs();
    */
    frame += 1;
  }
  cycle += 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x18(%rdi), %r14
movabsq $-0x7f9fe7f9fe7f9fe7, %rcx # imm = 0x8060180601806019
movq %r14, %rax
mulq %rcx
movq %r14, %r15
subq %rdx, %r15
shrq %r15
addq %rdx, %r15
shrq $0x8, %r15
movabsq $0xfa232cf252138b, %rcx # imm = 0xFA232CF252138B
movq %r15, %rax
mulq %rcx
imulq $0x106, %rdx, %rax      # imm = 0x106
imulq $0x155, %r15, %rcx      # imm = 0x155
subq %rax, %r15
subq %rcx, %r14
leal -0xf0(%r15), %eax
cmpw $0x15, %ax
jb 0x194b3
testb $0x8, 0x29(%rbx)
je 0x194b3
cmpq $0xff, %r14
setne %al
cmpq $0xf0, %r15
setae %cl
orb %al, %cl
jne 0x19438
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1979e
movq 0x8(%rbx), %rdi
callq 0x21890
movzwl %r14w, %eax
cmpl $0x101, %eax            # imm = 0x101
je 0x19454
cmpl $0x100, %eax            # imm = 0x100
jne 0x19475
movq %rbx, %rdi
callq 0x18ebc
jmp 0x19475
movl $0xfffffbe0, %eax       # imm = 0xFFFFFBE0
andl 0x138(%rbx), %eax
movzwl 0x13a(%rbx), %ecx
andl $0x41f, %ecx            # imm = 0x41F
orl %eax, %ecx
movw %cx, 0x138(%rbx)
cmpq $0x105, %r15            # imm = 0x105
setne %al
leal -0x118(%r14), %ecx
cmpw $0x19, %cx
setae %cl
orb %al, %cl
jne 0x194b3
movzwl 0x138(%rbx), %eax
andl $0x41f, %eax            # imm = 0x41F
movzwl 0x13a(%rbx), %ecx
andl $0xfffffbe0, %ecx       # imm = 0xFFFFFBE0
orl %eax, %ecx
movw %cx, 0x138(%rbx)
movq %r15, %rax
xorq $0xf1, %rax
orq %r14, %rax
jne 0x194d0
orb $-0x80, 0x2a(%rbx)
cmpb $0x0, 0x28(%rbx)
jns 0x194fe
movb $0x1, (%rbx)
jmp 0x194fe
xorq $0x105, %r15            # imm = 0x105
movq %r14, %rax
orq %r15, %rax
jne 0x194e5
andb $0x1f, 0x2a(%rbx)
jmp 0x194fe
xorq $0x154, %r14            # imm = 0x154
orq %r15, %r14
jne 0x194fe
movq 0x10(%rbx), %rdi
callq 0x22a68
incq 0x20(%rbx)
incq 0x18(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop