// File: /workspace/github_cpp_makefile_star2_O3/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 0x183d8
testb $0x8, 0x29(%rbx)
jne 0x1841c
movq %r15, %rax
xorq $0xf1, %rax
orq %r14, %rax
jne 0x183fc
orb $-0x80, 0x2a(%rbx)
cmpb $0x0, 0x28(%rbx)
jns 0x184e9
movb $0x1, (%rbx)
jmp 0x184e9
movl $0x105, %ecx            # imm = 0x105
cmpq %rcx, %r15
sete %al
xorq %rcx, %r15
orq %r14, %r15
jne 0x184cc
andb $0x1f, 0x2a(%rbx)
jmp 0x184e9
cmpq $0xff, %r14
setne %al
cmpq $0xf0, %r15
setae %cl
orb %al, %cl
jne 0x1844a
movq %rbx, %rdi
movl %r15d, %esi
callq 0x1878c
movq 0x8(%rbx), %rdi
callq 0x208d2
jmp 0x184c2
movzwl %r14w, %eax
cmpl $0x101, %eax            # imm = 0x101
je 0x18466
cmpl $0x100, %eax            # imm = 0x100
jne 0x18480
movq %rbx, %rdi
callq 0x17f08
jmp 0x184c2
movl $0xfffffbe0, %eax       # imm = 0xFFFFFBE0
andl 0x138(%rbx), %eax
movzwl 0x13a(%rbx), %ecx
andl $0x41f, %ecx            # imm = 0x41F
jmp 0x184b9
cmpq $0x105, %r15            # imm = 0x105
setne %al
leal -0x118(%r14), %ecx
cmpw $0x19, %cx
setae %cl
orb %al, %cl
jne 0x183d8
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)
cmpq $0x105, %r15            # imm = 0x105
sete %al
cmpq $0x154, %r14            # imm = 0x154
setne %cl
xorb $0x1, %al
orb %cl, %al
jne 0x184e9
movq 0x10(%rbx), %rdi
callq 0x21a94
incq 0x20(%rbx)
incq 0x18(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq