// File: khedoros[P]khednes/ppu3.cpp
// Function: ppu::get_Surface(int, int, int, int, int)
SDL_Surface * ppu::get_Surface(int table, int tile, int palnum, int hf, int vf) {
    //cout<<"T: "<<table<<" t: "<<tile<<" pal: "<<palnum<<" hf: "<<hf<<" vf: "<<vf<<endl;
    if(!(tile_cache[table][tile][palnum][hf][vf])) {
        //build the tile (8x8 palettized)
        SDL_Surface * temp = SDL_CreateRGBSurface(0,8,8,8,0,0,0,0);
        SDL_SetSurfacePalette(temp, screen.get_palette());
        SDL_SetColorKey(temp, SDL_TRUE, NES_TRANSPARENT);
        bool needs_lock = SDL_MUSTLOCK(temp);
        if(needs_lock) {
            SDL_LockSurface(temp);
            cout<<"Surfaces need locked!"<<endl;
        }
        int tilenum = table * 256 + tile;
        for(int y = 0; y < 8; ++y) {
            int outy;
            if(vf) outy = 7 - y;
            else outy = y;
            for(int x = 0; x < 8; ++x) {
                int outx;
                if(hf) outx = 7 - x;
                else outx = x;
                int color = get_color(tilenum,x,y);
                if(color == 0) ((uint8_t *)(temp->pixels))[outy*(temp->pitch)+outx]=NES_TRANSPARENT; //Transparent
                else {
                    //cout<<hex<<"Palnum: "<<palnum<<" Colorpart: "<<color<<" Both: "<<((palnum<<(2))+color);
                    color = *(pal[(palnum<<(2)) + color]);
                    //cout<<" val: "<<color<<dec<<endl;
                    ((uint8_t *)(temp->pixels))[outy*(temp->pitch)+outx]=color;
                }
            }
        }
        if(needs_lock) SDL_UnlockSurface(temp);
        tile_cache[table][tile][palnum][hf][vf] = temp;
    }
   return tile_cache[table][tile][palnum][hf][vf];
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movl %esi, 0x5c(%rsp)
movl %edx, 0x58(%rsp)
movl %ecx, 0x54(%rsp)
movl %r8d, 0x50(%rsp)
movl %r9d, 0x4c(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
addq $0x1850, %rax           # imm = 0x1850
movslq 0x5c(%rsp), %rcx
shlq $0x10, %rcx
addq %rcx, %rax
movslq 0x58(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq 0x54(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x50(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x4c(%rsp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x1e1f7
xorl %r9d, %r9d
movl $0x8, %ecx
movl %r9d, %edi
movl %ecx, %esi
movl %ecx, %edx
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x4540
movq 0x18(%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x10(%rsp)
addq $0x8, %rdi
callq 0x261f0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x41e0
movq 0x40(%rsp), %rdi
movl $0x1, %esi
movl $0x4a, %edx
callq 0x44a0
movq 0x40(%rsp), %rax
movl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, 0x3f(%rsp)
testb $0x1, 0x3f(%rsp)
je 0x1e07c
movq 0x40(%rsp), %rdi
callq 0x44f0
movq 0x18f4f(%rip), %rdi     # 0x36fb0
leaq 0xe615(%rip), %rsi      # 0x2c67d
callq 0x43c0
movq %rax, %rdi
movq 0x18f19(%rip), %rsi     # 0x36f90
callq 0x4400
movl 0x5c(%rsp), %eax
shll $0x8, %eax
addl 0x58(%rsp), %eax
movl %eax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
cmpl $0x8, 0x34(%rsp)
jge 0x1e19d
cmpl $0x0, 0x4c(%rsp)
je 0x1e0b4
movl $0x7, %eax
subl 0x34(%rsp), %eax
movl %eax, 0x30(%rsp)
jmp 0x1e0bc
movl 0x34(%rsp), %eax
movl %eax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
cmpl $0x8, 0x2c(%rsp)
jge 0x1e18b
cmpl $0x0, 0x50(%rsp)
je 0x1e0e5
movl $0x7, %eax
subl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
jmp 0x1e0ed
movl 0x2c(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x18(%rsp), %rdi
movl 0x38(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x34(%rsp), %ecx
callq 0x1f970
movl %eax, 0x24(%rsp)
cmpl $0x0, 0x24(%rsp)
jne 0x1e131
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x30(%rsp), %ecx
movq 0x40(%rsp), %rdx
imull 0x18(%rdx), %ecx
addl 0x28(%rsp), %ecx
movslq %ecx, %rcx
movb $0x4a, (%rax,%rcx)
jmp 0x1e179
movq 0x18(%rsp), %rax
movl 0x54(%rsp), %ecx
shll $0x2, %ecx
addl 0x24(%rsp), %ecx
movslq %ecx, %rcx
movq 0x1750(%rax,%rcx,8), %rax
movzbl (%rax), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movb %al, %dl
movq 0x40(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x30(%rsp), %ecx
movq 0x40(%rsp), %rsi
imull 0x18(%rsi), %ecx
addl 0x28(%rsp), %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
jmp 0x1e17b
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1e0c4
jmp 0x1e18d
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x1e093
testb $0x1, 0x3f(%rsp)
je 0x1e1ae
movq 0x40(%rsp), %rdi
callq 0x4550
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rdx
addq $0x1850, %rax           # imm = 0x1850
movslq 0x5c(%rsp), %rcx
shlq $0x10, %rcx
addq %rcx, %rax
movslq 0x58(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq 0x54(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x50(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x4c(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movq 0x18(%rsp), %rax
addq $0x1850, %rax           # imm = 0x1850
movslq 0x5c(%rsp), %rcx
shlq $0x10, %rcx
addq %rcx, %rax
movslq 0x58(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq 0x54(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x50(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x4c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
addq $0x68, %rsp
retq