// File: /workspace/github_cpp_makefile_star2_O3/rockytriton[P]dreamsys_emu_gb/src/ppu.cpp
// Function: dsemu::ppu::drawLine(int)
void drawLine(int lineNum) {
    int mapy = (lineNum + getYScroll()) % 256;
    byte tileY = ((lineNum) % 8) * 2;

    auto sprites = getSpritesOnLine(lineNum);

    for (int x=0; x<XRES; x += 1) {
        auto sprite = getSpriteOnX(sprites, x);

        int mapx = (x + getXScroll()) % 256;

        ushort bgTileNum = bus::read(ppu::bgMapStart() + (mapx/8) + ((mapy/8) * 32));

        if (ppu::bgTileStart() == 0x8800) {
            bgTileNum += 128;
        }

        if (sprite != nullptr) {
            bgTileNum = sprite->tile;
        }

        byte b1 = bus::read(ppu::bgTileStart() + (bgTileNum * 16) + tileY);
        byte b2 = bus::read(ppu::bgTileStart() + (bgTileNum * 16) + 1 + tileY);

        int bit = 7 - (x % 8);
        byte hi = !!(b1 & (1 << bit)) << 1;
        byte lo = !!(b2 & (1 << bit));
        byte color = hi|lo;
        unsigned long c = colors[color];

        videoBuffer[x + (lineNum * XRES)] = c;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edi, %ebx
movzbl 0x2c0d7(%rip), %r12d    # 0x3d674
addl %edi, %r12d
movl $0xff, %r15d
leal (%r12,%r15), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
andl $0xffffff00, %eax       # imm = 0xFFFFFF00
subl %eax, %r12d
leal 0x7(%rbx), %eax
testl %edi, %edi
cmovnsl %edi, %eax
andl $0x78, %eax
movl %edi, %r14d
subl %eax, %r14d
addl %r14d, %r14d
leaq 0x10(%rsp), %rdi
movl %ebx, %esi
callq 0x1149b
leal 0x7(%r12), %ebp
testw %r12w, %r12w
cmovnsl %r12d, %ebp
shll $0x2, %ebp
andl $0xffe0, %ebp           # imm = 0xFFE0
andl %r15d, %r14d
leal 0x1(%r14), %eax
movl %eax, 0xc(%rsp)
shll $0x5, %ebx
leal (%rbx,%rbx,4), %eax
movslq %eax, %r12
xorl %r13d, %r13d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rcx
cmpq %rcx, %rax
je 0x1163c
leaq 0x8(%r13), %rdx
movq (%rax), %rbx
movzbl 0x1(%rbx), %esi
cmpq %rsi, %r13
seta %dil
cmpq %rsi, %rdx
setbe %sil
orb %dil, %sil
je 0x1163e
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x11619
xorl %ebx, %ebx
movb 0x2c02d(%rip), %al      # 0x3d671
andb $0x8, %al
shrb $0x3, %al
movb 0x2c024(%rip), %cl      # 0x3d673
addb %r13b, %cl
movzbl %al, %eax
shll $0xa, %eax
shrb $0x3, %cl
movzbl %cl, %ecx
orl %ebp, %ecx
addl %ecx, %eax
addl $0xffff9800, %eax       # imm = 0xFFFF9800
movzwl %ax, %edi
callq 0xb94c
movzbl %al, %ecx
movb 0x2bff9(%rip), %al      # 0x3d671
leal 0x80(%rcx), %r15d
testb $0x10, %al
cmovnel %ecx, %r15d
testq %rbx, %rbx
je 0x1168f
movzbl 0x2(%rbx), %r15d
xorl %ecx, %ecx
testb $0x10, %al
sete %cl
shll $0xb, %ecx
orl %r14d, %ecx
shll $0x4, %r15d
leal (%r15,%rcx), %edi
addl $0x8000, %edi           # imm = 0x8000
callq 0xb94c
movl %eax, %ebx
xorl %eax, %eax
testb $0x10, 0x2bfb7(%rip)    # 0x3d671
sete %al
shll $0xb, %eax
addl 0xc(%rsp), %r15d
leal (%rax,%r15), %edi
addl $0x8000, %edi           # imm = 0x8000
callq 0xb94c
movzbl %bl, %edx
movl %r13d, %ecx
notb %cl
andb $0x7, %cl
shrl %cl, %edx
andl $0x1, %edx
movzbl %al, %eax
movzbl %cl, %ecx
xorl %esi, %esi
btl %ecx, %eax
setb %sil
leal (%rsi,%rdx,2), %eax
leaq (,%r13,8), %rcx
addq 0x2c01b(%rip), %rcx     # 0x3d720
leaq 0x24d4(%rip), %rdx      # 0x13be0
movq (%rdx,%rax,8), %rax
movq %rax, (%rcx,%r12,8)
incq %r13
cmpq $0xa0, %r13
jne 0x11606
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x1173b
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x32b0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1174e
jmp 0x1174e
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11768
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3440