// File: /workspace/github_cpp_makefile_star2_O2/mindbeast[P]rnes/ppu.cpp
// Function: Rnes::Ppu::render(unsigned int)
void Ppu::render(uint32_t scanline) {
  bool spriteSize8x8 = isSpriteSize8x8();
  uint32_t spriteSize = spriteSize8x8 ? 8 : 16;

  // Indicate that all bg elements are transparent
  memset(pixelWritten, 0, sizeof(pixelWritten));
  memset(scanlineBuffer, 0, sizeof(scanlineBuffer));

  // render bg
  if (renderBackgroundEnabled()) {
    uint32_t patternTableAddr = getBgPatternTableAddr();
    uint32_t tempFineXScroll = vramFineXScroll;

    // render background
    for (uint32_t xBgOffset = 0; xBgOffset < renderWidth;) {
      // Get next tile and attribute addresses
      uint16_t nameAddr = getTileAddr(vramCurrentAddr);
      uint16_t attrAddr = getAttrAddr(vramCurrentAddr);

      // Load attribute and pattern
      uint16_t patternAddr = load(nameAddr) * 16 + patternTableAddr + getFineY(vramCurrentAddr);
      assert(patternAddr >= patternTableAddr and
             patternAddr < (patternTableAddr + patternTableSize));
      uint16_t pattern = loadPatternTile(patternAddr);
      uint8_t attr = load(attrAddr);
      uint32_t tileOffsetY = ((nameAddr & 0x3ff) >> 5 >> 1) % 2;

      // Render the pattern
      for (uint32_t tileXOffset = tempFineXScroll; tileXOffset < 8 and xBgOffset < renderWidth;
           tileXOffset++, xBgOffset++) {

        uint32_t color = (pattern >> ((7 - tileXOffset) * 2)) & 0x3;
        uint32_t tileOffsetX = (((nameAddr & 0x3ff) & 0x1f) >> 1) % 2;
        uint32_t subNibble = tileOffsetX + tileOffsetY * 2;
        assert(subNibble >= 0 and subNibble < 4);
        uint32_t palette = (attr >> (2 * subNibble)) & 0x3;
        bool transparent = (color == 0);
        uint32_t pixel = getColor(palette, color, false);

        scanlineBuffer[xBgOffset] = pixel;
        if (!transparent) {
          pixelWritten[xBgOffset] = true;
        }
        tempFineXScroll++;
        tempFineXScroll &= 0x7;
      }
      vramCoarseXInc();
    }
  }

  // render sprites
  if (renderSpritesEnabled()) {
    // Render up to 8 colliding sprites
    uint32_t patternTableAddr = getSpritePatternTableAddr();
    uint32_t renderedSprites = 0;
    for (uint32_t i = 0; i < maxSpriteCount; i++) {

      // Determine if the provide collides with the scanline
      uint32_t sprite = maxSpriteCount - 1 - i;
      if ((scanline >= (spriteRam[sprite].yCoordMinus1 + 1u)) and
          (scanline <= (spriteRam[sprite].yCoordMinus1 + 1u + spriteSize - 1u))) {
      } else {
        continue;
      }

      // Render the sprite
      bool spriteVerticalFlip = (spriteRam[sprite].attr & (1u << 7)) != 0;
      bool spriteHorizontalFlip = (spriteRam[sprite].attr & (1u << 6)) != 0;
      for (int j = 0; j < 8; j++) {
        uint32_t spriteLine = scanline - (spriteRam[sprite].yCoordMinus1 + 1);
        if (spriteVerticalFlip) {
          spriteLine = spriteSize - 1 - spriteLine;
        }

        uint32_t xOffset = j;
        if (spriteHorizontalFlip) {
          xOffset = 8 - 1 - xOffset;
        }
        uint32_t color;
        if (spriteSize8x8) {
          color = getColorFromPatternTable<true>(patternTableAddr, spriteRam[sprite].tileIndex,
                                                 xOffset, spriteLine);
        } else {
          color = getColorFromPatternTable<false>(patternTableAddr, spriteRam[sprite].tileIndex,
                                                  xOffset, spriteLine);
        }

        // Need to do some bounds checking bullshit here
        uint32_t xCoordinate = spriteRam[sprite].xCoord + j;
        if ((color != 0) and (xCoordinate < renderWidth)) {
          bool bgPixelInFront = (spriteRam[sprite].attr & (1u << 5)) != 0;
          if ((bgPixelInFront and !pixelWritten[xCoordinate]) or !bgPixelInFront) {
            scanlineBuffer[xCoordinate] = getColor(spriteRam[sprite].attr & 0x3, color, true);
          }
        }
        // Do sprite 0 collision detection
        if ((sprite == 0) and (color != 0) and (xCoordinate < renderWidth - 1)) {
          if (pixelWritten[xCoordinate]) {
            setSprite0Hit();
          }
        }
      }
      renderedSprites++;
      if (renderedSprites > maxRenderedSpritePerScanline) {
        setLostSprites();
        break;
      }
    }
  }

  // dump pixels to display
  for (uint32_t i = 0; i < renderWidth; i++) {
    setPixel(i, scanline, scanlineBuffer[i]);
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, -0x48(%rbp)
movq %rdi, %r14
movzbl 0x28(%rdi), %eax
movl %eax, -0x44(%rbp)
addq $0x14c, %rdi            # imm = 0x14C
xorl %ebx, %ebx
movl $0x500, %edx            # imm = 0x500
movq %rdi, -0x68(%rbp)
xorl %esi, %esi
callq 0xb2b0
movb 0x29(%r14), %al
testb $0x8, %al
movq %r14, -0x40(%rbp)
je 0x13dfc
movzwl -0x44(%rbp), %eax
andl $0x10, %eax
shll $0x8, %eax
movl 0x134(%r14), %r15d
movzwl %ax, %eax
movl %eax, -0x58(%rbp)
cmpl $0xff, %ebx
ja 0x13df4
movq -0x40(%rbp), %r14
movzwl 0x138(%r14), %r12d
movl %r12d, %esi
andl $0xfff, %esi            # imm = 0xFFF
orl $0x2000, %esi           # imm = 0x2000
movq %r14, %rdi
callq 0x13440
movzbl %al, %eax
movzwl 0x138(%r14), %esi
shrdw $0xc, %ax, %si
andl $0xff7, %esi            # imm = 0xFF7
orl -0x58(%rbp), %esi
movl %r12d, %eax
andl $0xc00, %eax            # imm = 0xC00
movl %r12d, %r13d
shrl $0x4, %r13d
movl %r13d, %ecx
andl $0x38, %ecx
orl %eax, %ecx
movl %r12d, %eax
shrl $0x2, %eax
andl $0x7, %eax
addl %ecx, %eax
addl $0x23c0, %eax           # imm = 0x23C0
movl %eax, -0x30(%rbp)
movq %r14, %rdi
callq 0x1358e
movw %ax, -0x38(%rbp)
movq %r14, %rdi
movl -0x30(%rbp), %esi
callq 0x13440
movq %r14, %rdi
movzbl %al, %eax
andl $0x4, %r13d
andl $0x2, %r12d
orl %r13d, %r12d
movl %r12d, %ecx
shrl %cl, %eax
movzwl -0x38(%rbp), %ecx
movl %ecx, -0x30(%rbp)
andl $0x3, %eax
movl %eax, -0x38(%rbp)
movl %ebx, %ebx
pushq $0xe
popq %r13
subl %r15d, %r13d
subl %r15d, %r13d
movl %r15d, %r12d
cmpl $0x7, %r12d
ja 0x13dea
cmpq $0x100, %rbx            # imm = 0x100
jae 0x13dea
movl -0x30(%rbp), %r14d
movl %r13d, %ecx
shrl %cl, %r14d
movl %r14d, %edx
andl $0x3, %edx
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl %ecx, %ecx
callq 0x1344e
movq -0x40(%rbp), %rdi
movl %eax, 0x24c(%rdi,%rbx,4)
andl $0x3, %r14d
je 0x13dd7
movb $0x1, 0x14c(%rdi,%rbx)
incl %r15d
andl $0x7, %r15d
incl %r12d
incq %rbx
addl $-0x2, %r13d
jmp 0x13d91
callq 0x134bc
jmp 0x13cd6
movq -0x40(%rbp), %r14
movb 0x29(%r14), %al
testb $0x10, %al
movl -0x48(%rbp), %ebx
je 0x13f80
movl -0x44(%rbp), %eax
andb $0x20, %al
shrb $0x5, %al
movzbl %al, %eax
leal 0x8(,%rax,8), %ecx
movzbl 0x28(%r14), %eax
andl $0x8, %eax
shll $0x9, %eax
movl %ecx, -0x5c(%rbp)
subl %ebx, %ecx
movl %ecx, -0x60(%rbp)
xorl %ecx, %ecx
movzwl %ax, %eax
movl %eax, -0x50(%rbp)
movl $0x0, -0x4c(%rbp)
cmpq $0x40, %rcx
je 0x13f80
pushq $0x3f
popq %rax
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movzbl 0x30(%r14,%rax,4), %eax
cmpl %ebx, %eax
jae 0x13f73
addl -0x5c(%rbp), %eax
cmpl %ebx, %eax
jb 0x13f73
movq -0x38(%rbp), %rax
movb 0x32(%r14,%rax,4), %al
movb %al, -0x29(%rbp)
xorl %ebx, %ebx
pushq $0x7
popq %rsi
movq %rcx, -0x58(%rbp)
cmpl $0x8, %ebx
je 0x13f63
movq -0x38(%rbp), %rdx
movzbl 0x30(%r14,%rdx,4), %r8d
movl %r8d, %eax
notl %eax
addl -0x48(%rbp), %eax
addl -0x60(%rbp), %r8d
movb -0x29(%rbp), %cl
testb %cl, %cl
cmovnsl %eax, %r8d
testb $0x40, %cl
movl %esi, -0x30(%rbp)
movl %esi, %ecx
cmovel %ebx, %ecx
movzbl 0x31(%r14,%rdx,4), %edx
testb $0x20, -0x44(%rbp)
jne 0x13ec9
movq %r14, %rdi
movl -0x50(%rbp), %esi
callq 0x13fe2
jmp 0x13ed4
movq %r14, %rdi
movl -0x50(%rbp), %esi
callq 0x140aa
movl %eax, %r13d
movq -0x38(%rbp), %rax
movzbl 0x33(%r14,%rax,4), %r15d
addl %ebx, %r15d
movl %r15d, %r12d
testb %r13b, %r13b
je 0x13f2f
cmpl $0xff, %r15d
ja 0x13f2f
movq -0x38(%rbp), %rax
movzbl 0x32(%r14,%rax,4), %esi
testb $0x20, %sil
je 0x13f10
movq -0x68(%rbp), %rax
cmpb $0x0, (%rax,%r12)
jne 0x13f2f
movzbl %r13b, %edx
andl $0x3, %esi
movq -0x40(%rbp), %rdi
pushq $0x1
popq %rcx
callq 0x1344e
movq -0x40(%rbp), %r14
movl %eax, 0x24c(%r14,%r12,4)
movq -0x58(%rbp), %rcx
cmpq $0x3f, %rcx
jne 0x13f57
testb %r13b, %r13b
je 0x13f57
cmpl $0xfe, %r15d
ja 0x13f57
movq -0x68(%rbp), %rax
cmpb $0x1, (%rax,%r12)
jne 0x13f57
orb $0x40, 0x2a(%r14)
incl %ebx
movl -0x30(%rbp), %esi
decl %esi
jmp 0x13e7d
movl -0x4c(%rbp), %eax
incl %eax
movl %eax, -0x4c(%rbp)
cmpl $0x8, %eax
movl -0x48(%rbp), %ebx
ja 0x13f7b
incq %rcx
jmp 0x13e3b
orb $0x20, 0x2a(%r14)
xorl %r15d, %r15d
cmpq $0x100, %r15            # imm = 0x100
je 0x13fab
movl 0x24c(%r14,%r15,4), %ecx
movq -0x40(%rbp), %rdi
movl %r15d, %esi
movl %ebx, %edx
callq 0x13fba
movq -0x40(%rbp), %r14
incq %r15
jmp 0x13f83
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq