// File: /workspace/github_cpp_makefile_star2_O3/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, %r14d
movq %rdi, %rbx
movzbl 0x28(%rdi), %eax
movl %eax, -0x3c(%rbp)
addq $0x14c, %rdi            # imm = 0x14C
xorl %r12d, %r12d
movl $0x500, %edx            # imm = 0x500
movq %rdi, -0x68(%rbp)
xorl %esi, %esi
callq 0xa280
movq %rbx, %rdi
movb 0x29(%rbx), %al
testb $0x8, %al
movq %rbx, -0x38(%rbp)
movl %r14d, -0x48(%rbp)
je 0x18946
movzwl -0x3c(%rbp), %eax
andl $0x10, %eax
shll $0x8, %eax
movl 0x134(%rdi), %r15d
movzwl 0x138(%rdi), %r13d
movzwl %ax, %eax
movl %eax, -0x40(%rbp)
movl %r13d, %esi
andl $0xfff, %esi            # imm = 0xFFF
orl $0x2000, %esi           # imm = 0x2000
movq %rdi, %r14
movq 0x8(%rdi), %rdi
callq 0x20852
movzbl %al, %eax
movzwl 0x138(%r14), %esi
shrdw $0xc, %ax, %si
andl $0xff7, %esi            # imm = 0xFF7
orl -0x40(%rbp), %esi
movl %r13d, %eax
andl $0xc00, %eax            # imm = 0xC00
movl %r13d, %ecx
shrl $0x4, %ecx
andl $0x38, %ecx
orl %eax, %ecx
movl %r13d, %eax
shrl $0x2, %eax
andl $0x7, %eax
leal (%rax,%rcx), %ebx
addl $0x23c0, %ebx           # imm = 0x23C0
movq %r14, %rdi
callq 0x17fb2
movw %ax, -0x30(%rbp)
movq 0x8(%r14), %rdi
movl %ebx, %esi
callq 0x20852
cmpl $0x7, %r15d
ja 0x18905
movzwl %r13w, %ecx
movzbl %al, %edx
movl %ecx, %eax
shrl $0x4, %eax
andl $0x4, %eax
andl $0x2, %ecx
orl %eax, %ecx
shrl %cl, %edx
movzwl -0x30(%rbp), %eax
movl %eax, -0x50(%rbp)
andl $0x3, %edx
movl %edx, -0x30(%rbp)
movl %r12d, %ebx
movl $0xe, %r13d
subl %r15d, %r13d
subl %r15d, %r13d
movl %r15d, %r14d
movl -0x50(%rbp), %r12d
movl %r13d, %ecx
shrl %cl, %r12d
movl %r12d, %edx
andl $0x3, %edx
movq -0x38(%rbp), %rdi
movl -0x30(%rbp), %esi
xorl %ecx, %ecx
callq 0x17e68
movq -0x38(%rbp), %rdi
movl %eax, 0x24c(%rdi,%rbx,4)
andl $0x3, %r12d
je 0x188df
movb $0x1, 0x14c(%rdi,%rbx)
incl %r15d
andl $0x7, %r15d
leaq 0x1(%rbx), %r12
cmpl $0x6, %r14d
ja 0x18909
incl %r14d
addl $-0x2, %r13d
cmpq $0xff, %rbx
movq %r12, %rbx
jb 0x188a8
jmp 0x18909
movq -0x38(%rbp), %rdi
movl 0x138(%rdi), %r13d
movl %r13d, %eax
notl %eax
leal 0x1(%r13), %ecx
andl $-0x20, %r13d
xorl $0x400, %r13d           # imm = 0x400
testb $0x1f, %al
cmovnel %ecx, %r13d
movw %r13w, 0x138(%rdi)
cmpl $0x100, %r12d           # imm = 0x100
jb 0x187f9
movb 0x29(%rdi), %al
movl -0x48(%rbp), %r14d
testb $0x10, %al
je 0x18abf
movl -0x3c(%rbp), %eax
andb $0x20, %al
shrb $0x5, %al
movzbl %al, %eax
leal 0x8(,%rax,8), %ecx
movzbl 0x28(%rdi), %eax
andl $0x8, %eax
shll $0x9, %eax
movl %ecx, -0x54(%rbp)
subl %r14d, %ecx
movl %ecx, -0x5c(%rbp)
xorl %edx, %edx
movzwl %ax, %eax
movl %eax, -0x44(%rbp)
xorl %ecx, %ecx
movl $0x3f, %eax
subq %rdx, %rax
movq %rax, -0x30(%rbp)
movzbl 0x30(%rdi,%rax,4), %eax
cmpl %r14d, %eax
jae 0x18aac
addl -0x54(%rbp), %eax
cmpl %r14d, %eax
jb 0x18aac
movl %ecx, -0x58(%rbp)
movq -0x30(%rbp), %rax
movb 0x32(%rdi,%rax,4), %al
movb %al, -0x40(%rbp)
xorl %ebx, %ebx
movl $0x7, %r12d
movq %rdx, -0x50(%rbp)
movq -0x30(%rbp), %rdx
movzbl 0x30(%rdi,%rdx,4), %r8d
movl %r8d, %eax
notl %eax
addl %r14d, %eax
addl -0x5c(%rbp), %r8d
movb -0x40(%rbp), %cl
testb %cl, %cl
cmovnsl %eax, %r8d
testb $0x40, %cl
movl %r12d, %ecx
cmovel %ebx, %ecx
movzbl 0x31(%rdi,%rdx,4), %edx
testb $0x20, -0x3c(%rbp)
jne 0x189fd
movq %rdi, %r15
movl -0x44(%rbp), %esi
callq 0x18b3e
jmp 0x18a08
movq %rdi, %r15
movl -0x44(%rbp), %esi
callq 0x18c0a
movl %eax, %r13d
movq -0x30(%rbp), %rax
movzbl 0x33(%r15,%rax,4), %r14d
addl %ebx, %r14d
testb %r13b, %r13b
movq %r15, %rdi
je 0x18a67
cmpl $0xff, %r14d
ja 0x18a67
movq -0x30(%rbp), %rax
movzbl 0x32(%rdi,%rax,4), %esi
movl %r14d, %r15d
testb $0x20, %sil
je 0x18a46
movq -0x68(%rbp), %rax
cmpb $0x0, (%rax,%r15)
jne 0x18a67
movzbl %r13b, %edx
andl $0x3, %esi
movq -0x38(%rbp), %rdi
movl $0x1, %ecx
callq 0x17e68
movq -0x38(%rbp), %rdi
movl %eax, 0x24c(%rdi,%r15,4)
movq -0x50(%rbp), %rdx
cmpq $0x3f, %rdx
jne 0x18a90
testb %r13b, %r13b
je 0x18a90
cmpl $0xfe, %r14d
ja 0x18a90
movl %r14d, %eax
movq -0x68(%rbp), %rcx
cmpb $0x1, (%rcx,%rax)
jne 0x18a90
orb $0x40, 0x2a(%rdi)
incl %ebx
decl %r12d
cmpl $0x8, %ebx
movl -0x48(%rbp), %r14d
jne 0x189bd
movl -0x58(%rbp), %ecx
incl %ecx
cmpl $0x8, %ecx
ja 0x18abb
incq %rdx
cmpq $0x40, %rdx
jne 0x1897d
jmp 0x18abf
orb $0x20, 0x2a(%rdi)
xorl %r15d, %r15d
movl $0xff, %ebx
movl 0x24c(%rdi,%r15,4), %r9d
movl %r9d, %ecx
shrl $0x10, %ecx
movl %r9d, %r8d
shrl $0x8, %r8d
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
andl %ebx, %r9d
andl %ebx, %ecx
andl %ebx, %r8d
movl %r15d, %esi
movl %r14d, %edx
callq 0x21a6c
movq -0x38(%rbp), %rdi
incq %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x18ac7
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq