// File: khedoros[P]khednes/ppu3.cpp
// Function: ppu::blit_sprite(int)
void ppu::blit_sprite(int spr) {
    if(spr < 0 || spr > 63) return;
    int spry = sprites[spr].y;
    int sprx = sprites[spr].x;
    bool priority = !sprites[spr].priority;
    bool hf = sprites[spr].hflip;
    bool vf = sprites[spr].vflip;
    int palnum = sprites[spr].pal | 4;
    int tile1 = sprites[spr].tile;
    int table = control0.sprite_table;
    int tile2 = -1;
    if(control0.big_sprites) {
        if(tile1%2 == 0)
            table = 0;
        else
            table = 1;
        tile1 &= 0xfe;
        tile2 = tile1 + 1;
    }
    if(priority) {
        if(control0.big_sprites) {
            if(vf) {
                screen.blit_hps(get_Surface(table,tile2,palnum,hf,vf), sprx,spry+1);
                screen.blit_hps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+9);
            }
            else {
                screen.blit_hps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+1);
                screen.blit_hps(get_Surface(table,tile2,palnum,hf,vf), sprx,spry+9);
            }
        }
        else {
            screen.blit_hps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+1);
        }
    }
    else {
        if(control0.big_sprites) {
            if(vf) {
                screen.blit_lps(get_Surface(table,tile2,palnum,hf,vf), sprx,spry+1);
                screen.blit_lps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+9);
            }
            else {
                screen.blit_lps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+1);
                screen.blit_lps(get_Surface(table,tile2,palnum,hf,vf), sprx,spry+9);
            }
        }
        else {
            screen.blit_lps(get_Surface(table,tile1,palnum,hf,vf), sprx,spry+1);
        }
    }
}

// Assembly:
subq $0x88, %rsp
movq %rdi, 0x80(%rsp)
movl %esi, 0x7c(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x58(%rsp)
cmpl $0x0, 0x7c(%rsp)
jl 0x1f3ce
cmpl $0x3f, 0x7c(%rsp)
jle 0x1f3d3
jmp 0x1f876
movq 0x58(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
andl $0xff, %ecx
movl %ecx, 0x78(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x18, %ecx
movl %ecx, 0x74(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x15, %ecx
andl $0x1, %ecx
cmpl $0x0, %ecx
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
movb %cl, 0x73(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x16, %ecx
andl $0x1, %ecx
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, 0x72(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x17, %ecx
andl $0x1, %ecx
cmpl $0x0, %ecx
setne %cl
andb $0x1, %cl
movb %cl, 0x71(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x10, %ecx
andl $0x3, %ecx
orl $0x4, %ecx
movl %ecx, 0x6c(%rsp)
movslq 0x7c(%rsp), %rcx
movl 0x208(%rax,%rcx,4), %ecx
shrl $0x8, %ecx
andl $0xff, %ecx
movl %ecx, 0x68(%rsp)
movb 0x21850(%rax), %cl
shrb $0x3, %cl
andb $0x1, %cl
movzbl %cl, %ecx
movl %ecx, 0x64(%rsp)
movl $0xffffffff, 0x60(%rsp) # imm = 0xFFFFFFFF
movb 0x21850(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1f4fc
movl 0x68(%rsp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1f4dc
movl $0x0, 0x64(%rsp)
jmp 0x1f4e4
movl $0x1, 0x64(%rsp)
movl 0x68(%rsp), %eax
andl $0xfe, %eax
movl %eax, 0x68(%rsp)
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
testb $0x1, 0x73(%rsp)
je 0x1f6c0
movq 0x58(%rsp), %rax
movb 0x21850(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1f66d
testb $0x1, 0x71(%rsp)
je 0x1f5cf
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x48(%rsp)
movl 0x64(%rsp), %esi
movl 0x60(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x48(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x26070
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x50(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x50(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x9, %ecx
callq 0x26070
jmp 0x1f66b
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x38(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x38(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x26070
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x40(%rsp)
movl 0x64(%rsp), %esi
movl 0x60(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x40(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x9, %ecx
callq 0x26070
jmp 0x1f6bb
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x30(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x30(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x26070
jmp 0x1f876
movq 0x58(%rsp), %rax
movb 0x21850(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1f826
testb $0x1, 0x71(%rsp)
je 0x1f788
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
movl 0x64(%rsp), %esi
movl 0x60(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x20(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x260d0
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x28(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x28(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x9, %ecx
callq 0x260d0
jmp 0x1f824
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x10(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x10(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x260d0
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x18(%rsp)
movl 0x64(%rsp), %esi
movl 0x60(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x18(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x9, %ecx
callq 0x260d0
jmp 0x1f874
movq 0x58(%rsp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movb 0x72(%rsp), %al
andb $0x1, %al
movzbl %al, %r8d
movb 0x71(%rsp), %al
andb $0x1, %al
movzbl %al, %r9d
callq 0x1df60
movq 0x8(%rsp), %rdi
movq %rax, %rsi
movl 0x74(%rsp), %edx
movl 0x78(%rsp), %ecx
addl $0x1, %ecx
callq 0x260d0
jmp 0x1f876
addq $0x88, %rsp
retq
nop