// File: /workspace/github_cpp_makefile_star2_O1/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 0x2beac(%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 0x116c2
leal 0x7(%r12), %ebp
testl %r12d, %r12d
cmovnsl %r12d, %ebp
shll $0x2, %ebp
andl $-0x20, %ebp
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 0x11867
leaq 0x8(%r13), %rdx
movq (%rax), %rsi
movzbl 0x1(%rsi), %edi
cmpq %rdi, %r13
seta %r8b
cmpq %rdi, %rdx
setbe %dil
orb %r8b, %dil
cmoveq %rsi, %rbx
je 0x11869
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x11840
xorl %ebx, %ebx
movb 0x2be02(%rip), %al      # 0x3d671
andb $0x8, %al
shrb $0x3, %al
movb 0x2bdf9(%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 $0x9800, %eax           # imm = 0x9800
movzwl %ax, %edi
callq 0xb94c
movzbl %al, %ecx
movb 0x2bdce(%rip), %al      # 0x3d671
leal 0x80(%rcx), %r15d
testb $0x10, %al
cmovnel %ecx, %r15d
testq %rbx, %rbx
je 0x118ba
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, 0x2bd8c(%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 0x2bdf0(%rip), %rcx     # 0x3d720
leaq 0x22a9(%rip), %rdx      # 0x13be0
movq (%rdx,%rax,8), %rax
movq %rax, (%rcx,%r12,8)
incq %r13
cmpq $0xa0, %r13
jne 0x1182d
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11966
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 0x11979
jmp 0x11979
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x11993
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x32b0
movq %rbx, %rdi
callq 0x3440