// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0x64(%rbp)
callq 0x17450
movb %al, %cl
movl -0x64(%rbp), %eax
movzbl %cl, %ecx
addl %ecx, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %edx, -0x8(%rbp)
movl -0x4(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
shll %edx
movb %dl, %al
movb %al, -0x9(%rbp)
movl -0x4(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0x21dd0
movl $0x0, -0x2c(%rbp)
cmpl $0xa0, -0x2c(%rbp)
jge 0x221ab
movl -0x2c(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0x21ea0
movq %rax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x6c(%rbp)
callq 0x172d0
movb %al, -0x65(%rbp)
jmp 0x21fc0
movl -0x6c(%rbp), %edx
movb -0x65(%rbp), %al
movzbl %al, %esi
movl %edx, %eax
addl %esi, %eax
movl %edx, %ecx
movl %esi, %edx
leal 0xff(%rcx,%rdx), %edx
testl %eax, %eax
movl %eax, %ecx
cmovsl %edx, %ecx
andl $0xffffff00, %ecx       # imm = 0xFFFFFF00
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
callq 0x225f0
movw %ax, -0x6e(%rbp)
jmp 0x21ff5
movw -0x6e(%rbp), %ax
movw %ax, %dx
movl -0x3c(%rbp), %eax
movl %eax, %ecx
addl $0x7, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
shrl $0x3, %eax
addl %eax, %edx
movl -0x8(%rbp), %eax
movl %eax, %ecx
addl $0x7, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
andl $0x3ff8, %eax           # imm = 0x3FF8
movl %eax, %ecx
movl %edx, %eax
leal (%rax,%rcx,4), %eax
movzwl %ax, %edi
callq 0x13450
movb %al, -0x6f(%rbp)
jmp 0x22034
movb -0x6f(%rbp), %al
movzbl %al, %eax
movw %ax, -0x4e(%rbp)
callq 0x22620
movw %ax, -0x72(%rbp)
jmp 0x22049
movw -0x72(%rbp), %ax
movzwl %ax, %eax
cmpl $0x8800, %eax           # imm = 0x8800
jne 0x22080
movzwl -0x4e(%rbp), %eax
addl $0x80, %eax
movw %ax, -0x4e(%rbp)
jmp 0x22080
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x224b0
jmp 0x221bd
cmpq $0x0, -0x38(%rbp)
je 0x22093
movq -0x38(%rbp), %rax
movzbl 0x2(%rax), %eax
movw %ax, -0x4e(%rbp)
callq 0x22620
movw %ax, -0x74(%rbp)
jmp 0x2209e
movw -0x74(%rbp), %cx
movw %cx, %ax
movzwl -0x4e(%rbp), %ecx
shll $0x4, %ecx
addl %ecx, %eax
movzbl -0x9(%rbp), %ecx
addl %ecx, %eax
movzwl %ax, %edi
callq 0x13450
movb %al, -0x75(%rbp)
jmp 0x220c1
movb -0x75(%rbp), %al
movb %al, -0x4f(%rbp)
callq 0x22620
movw %ax, -0x78(%rbp)
jmp 0x220d2
movw -0x78(%rbp), %ax
movw %ax, %dx
movzwl -0x4e(%rbp), %eax
shll $0x4, %eax
addl %eax, %edx
movzbl -0x9(%rbp), %ecx
movl %ecx, %eax
movl %edx, %ecx
leal 0x1(%rax,%rcx), %eax
movzwl %ax, %edi
callq 0x13450
movb %al, -0x79(%rbp)
jmp 0x220fb
movb -0x79(%rbp), %al
movb %al, -0x50(%rbp)
movl -0x2c(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl $0x7, %eax
subl %edx, %eax
movl %eax, -0x54(%rbp)
movzbl -0x4f(%rbp), %eax
movl -0x54(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
shll %eax
movb %al, -0x55(%rbp)
movzbl -0x50(%rbp), %eax
movl -0x54(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x56(%rbp)
movzbl -0x55(%rbp), %eax
movzbl -0x56(%rbp), %ecx
orl %ecx, %eax
movb %al, -0x57(%rbp)
movzbl -0x57(%rbp), %eax
movl %eax, %ecx
leaq 0xd149(%rip), %rax      # 0x2f2c0
movq (%rax,%rcx,8), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdx
movq 0x32636(%rip), %rax     # 0x547c0
movl -0x2c(%rbp), %ecx
imull $0xa0, -0x4(%rbp), %esi
addl %esi, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x21f93
leaq -0x28(%rbp), %rdi
callq 0x224b0
addq $0x80, %rsp
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x34b0
nopw %cs:(%rax,%rax)