// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %esi, -0x50(%rbp)
movq %rdi, %rbx
movzbl 0x28(%rdi), %eax
movl %eax, -0x48(%rbp)
addq $0x14c, %rdi            # imm = 0x14C
xorl %r12d, %r12d
movl $0x500, %edx            # imm = 0x500
movq %rdi, -0x60(%rbp)
xorl %esi, %esi
callq 0xb280
movq %rbx, %rdi
testb $0x8, 0x29(%rbx)
movq %rbx, -0x40(%rbp)
je 0x19946
movzwl -0x48(%rbp), %eax
andl $0x10, %eax
shll $0x8, %eax
movl 0x134(%rdi), %ebx
movzwl %ax, %eax
movl %eax, -0x44(%rbp)
movzwl 0x138(%rdi), %r13d
movl %r13d, %esi
andl $0xfff, %esi            # imm = 0xFFF
orl $0x2000, %esi           # imm = 0x2000
movq %rdi, %r15
movq 0x8(%rdi), %rdi
callq 0x217f2
movzbl %al, %eax
movzwl 0x138(%r15), %esi
shrdw $0xc, %ax, %si
andl $0xff7, %esi            # imm = 0xFF7
orl -0x44(%rbp), %esi
movl %r13d, %eax
andl $0xc00, %eax            # imm = 0xC00
movl %r13d, %r14d
shrl $0x4, %r14d
movl %r14d, %ecx
andl $0x38, %ecx
orl %eax, %ecx
movl %r13d, %eax
shrl $0x2, %eax
andl $0x7, %eax
addl %ecx, %eax
addl $0x23c0, %eax           # imm = 0x23C0
movl %eax, -0x38(%rbp)
movq %r15, %rdi
callq 0x18f7c
movw %ax, -0x2c(%rbp)
movq 0x8(%r15), %rdi
movl -0x38(%rbp), %esi
callq 0x217f2
cmpl $0x7, %ebx
ja 0x19913
movzbl %al, %eax
andl $0x4, %r14d
andl $0x2, %r13d
orl %r14d, %r13d
movl %r13d, %ecx
shrl %cl, %eax
movzwl -0x2c(%rbp), %ecx
movl %ecx, -0x2c(%rbp)
andl $0x3, %eax
movl %eax, -0x38(%rbp)
movl %r12d, %r15d
movl $0xe, %r13d
subl %ebx, %r13d
subl %ebx, %r13d
movl %ebx, %r14d
movl -0x2c(%rbp), %r12d
movl %r13d, %ecx
shrl %cl, %r12d
movl %r12d, %edx
andl $0x3, %edx
movq -0x40(%rbp), %rdi
movl -0x38(%rbp), %esi
xorl %ecx, %ecx
callq 0x18e1c
movq -0x40(%rbp), %rdi
movl %eax, 0x24c(%rdi,%r15,4)
andl $0x3, %r12d
je 0x198ef
movb $0x1, 0x14c(%rdi,%r15)
incl %ebx
andl $0x7, %ebx
leaq 0x1(%r15), %r12
cmpl $0x6, %r14d
ja 0x19917
incl %r14d
addl $-0x2, %r13d
cmpq $0xff, %r15
movq %r12, %r15
jb 0x198b6
jmp 0x19917
movq -0x40(%rbp), %rdi
movl 0x138(%rdi), %eax
movl %eax, %ecx
notl %ecx
leal 0x1(%rax), %edx
andl $-0x20, %eax
xorl $0x400, %eax            # imm = 0x400
testb $0x1f, %cl
cmovnel %edx, %eax
movw %ax, 0x138(%rdi)
cmpl $0x100, %r12d           # imm = 0x100
jb 0x197fd
testb $0x10, 0x29(%rdi)
movl -0x50(%rbp), %r14d
je 0x19ae9
movl -0x48(%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, -0x44(%rbp)
movq $0x0, -0x38(%rbp)
movzwl %ax, %eax
movl %eax, -0x4c(%rbp)
xorl %edx, %edx
movl $0x3f, %r9d
subq -0x38(%rbp), %r9
movzbl 0x30(%rdi,%r9,4), %ecx
movl $0x9, %eax
cmpl %r14d, %ecx
jae 0x19acb
addl -0x54(%rbp), %ecx
cmpl %r14d, %ecx
jb 0x19acb
movl %edx, -0x58(%rbp)
movb 0x32(%rdi,%r9,4), %al
movb %al, -0x2c(%rbp)
xorl %ebx, %ebx
movl $0x7, %r12d
movzbl 0x30(%rdi,%r9,4), %r8d
movl %r8d, %eax
notl %eax
addl %r14d, %eax
addl -0x44(%rbp), %r8d
movb -0x2c(%rbp), %cl
testb %cl, %cl
cmovnsl %eax, %r8d
testb $0x40, %cl
movl %r12d, %ecx
cmovel %ebx, %ecx
testb $0x20, -0x48(%rbp)
jne 0x19a06
movzbl 0x31(%rdi,%r9,4), %edx
movq %rdi, %r15
movl -0x4c(%rbp), %esi
movq %r9, %r13
callq 0x19b68
jmp 0x19a1a
movzbl 0x31(%rdi,%r9,4), %edx
movq %rdi, %r15
movl -0x4c(%rbp), %esi
movq %r9, %r13
callq 0x19c34
movl %eax, %edx
movzbl 0x33(%r15,%r13,4), %r14d
addl %ebx, %r14d
testb %al, %al
movq %r15, %rdi
movq %r13, %r9
je 0x19a7f
cmpl $0xff, %r14d
ja 0x19a7f
movzbl 0x32(%rdi,%r9,4), %esi
testb $0x20, %sil
je 0x19a51
movl %r14d, %eax
movq -0x60(%rbp), %rcx
cmpb $0x0, (%rcx,%rax)
jne 0x19a7f
movl %edx, %r13d
movzbl %dl, %edx
andl $0x3, %esi
movq -0x40(%rbp), %rdi
movl $0x1, %ecx
movq %r9, %r15
callq 0x18e1c
movl %r13d, %edx
movq %r15, %r9
movq -0x40(%rbp), %rdi
movl %r14d, %ecx
movl %eax, 0x24c(%rdi,%rcx,4)
cmpq $0x3f, -0x38(%rbp)
jne 0x19aa4
testb %dl, %dl
je 0x19aa4
cmpl $0xfe, %r14d
ja 0x19aa4
movl %r14d, %eax
movq -0x60(%rbp), %rcx
cmpb $0x1, (%rcx,%rax)
jne 0x19aa4
orb $0x40, 0x2a(%rdi)
incl %ebx
decl %r12d
cmpl $0x8, %ebx
movl -0x50(%rbp), %r14d
jne 0x199c6
movl -0x58(%rbp), %edx
incl %edx
xorl %eax, %eax
cmpl $0x8, %edx
jbe 0x19acb
orb $0x20, 0x2a(%rdi)
movl $0x7, %eax
cmpl $0x9, %eax
je 0x19ad4
testl %eax, %eax
jne 0x19ae9
movq -0x38(%rbp), %rcx
incq %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x40, %rcx
jne 0x19989
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 -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
andl %ebx, %r9d
andl %ebx, %ecx
andl %ebx, %r8d
movl %r15d, %esi
movl %r14d, %edx
callq 0x22a40
movq -0x40(%rbp), %rdi
incq %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x19af1
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq