// File: khedoros[P]khednes/ppu3.cpp
// Function: ppu::ppu(rom&, int)
ppu::ppu(rom& romi, int res) : cart(romi), screen(1), scroll_shifts(0), frame(0) {
    if(screen.dummy) {
        dummy = true;
    }
    else {
        dummy = false;
    }
    //cout<<"Start ppu constructor"<<endl;
    //cout<<"\tRes: "<<res<<endl;
    //cout<<"End ppu constructor"<<endl<<endl;
    s.cycles = 0;
    s.reads = 0;
    s.twok2_reads = 0;
    s.writes = 0;
    s.dmas = 0;
    s.reads_during_render = 0;
    s.writes_during_render = 0;
    frame = 0;
    cleared_vsync = false;
    late_vsync_clear = false;
    spr_overflow_cycle = -1;
    spr_hit0_cycle = -1;
    recalc_overflow = true;
    recalc_s0 = true;
    table_view = false;
    init_scroll_x = init_scroll_y = 0;
    cycles_per_frame = CLK_PER_LINE * LINE_PER_FRAME;

    set_mirror(cart.get_mode());

    for(int i=0; i < 0x20; ++i)
        pal[i] = &(palette[i]);
    pal[0x10] = &(palette[0]);
    pal[0x14] = &(palette[4]);
    pal[0x18] = &(palette[8]);
    pal[0x1C] = &(palette[0x0c]);

    //tile_cache[table#][tile#][pal#][hflip][vflip]
    //        SDL_Surface * tile_cache[2][256][8][2][2];
    for(int table = 0; table < 2; ++table) {
        for(int tile = 0; tile < 256; ++tile) {
            for(int palette = 0; palette < 8; ++palette) {
                for(int hflip = 0; hflip < 2; ++hflip) {
                    for(int vflip = 0; vflip < 2; ++vflip) {
                        tile_cache[table][tile][palette][hflip][vflip] = NULL;
                    }
                }
            }
        }
    }
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movq 0x60(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movq 0x58(%rsp), %rax
movq %rax, (%rdi)
addq $0x8, %rdi
movq %rdi, 0x18(%rsp)
movl $0x1, %esi
callq 0x25140
movq 0x20(%rsp), %rdi
addq $0x1b8, %rdi            # imm = 0x1B8
callq 0x1fff0
jmp 0x1c5da
movq 0x20(%rsp), %rax
addq $0x1f0, %rax            # imm = 0x1F0
movq %rax, 0x8(%rsp)
leaq 0x43(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x20040
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x20050
jmp 0x1c60e
leaq 0x43(%rsp), %rdi
callq 0x200f0
movq 0x20(%rsp), %rax
movl $0x0, 0x2185c(%rax)
testb $0x1, 0x1b0(%rax)
je 0x1c66e
movq 0x20(%rsp), %rax
movb $0x1, 0x21861(%rax)
jmp 0x1c67a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
jmp 0x1c8ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
leaq 0x43(%rsp), %rdi
callq 0x200f0
jmp 0x1c8ee
movq 0x20(%rsp), %rax
movb $0x0, 0x21861(%rax)
movq 0x20(%rsp), %rax
movq $0x0, 0x1d8(%rax)
movl $0x0, 0x1b8(%rax)
movl $0x0, 0x1bc(%rax)
movl $0x0, 0x1cc(%rax)
movl $0x0, 0x1c8(%rax)
movl $0x0, 0x1c0(%rax)
movl $0x0, 0x1c4(%rax)
movl $0x0, 0x2185c(%rax)
movb $0x0, 0x6e3(%rax)
movb $0x0, 0x6e4(%rax)
movl $0xffffffff, 0x6cc(%rax) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x6d0(%rax) # imm = 0xFFFFFFFF
movb $0x1, 0x6d4(%rax)
movb $0x1, 0x6d5(%rax)
movb $0x0, 0x21860(%rax)
movl $0x0, 0x1ec(%rax)
movl $0x0, 0x1e8(%rax)
movl $0x15cfe, 0x6e8(%rax)   # imm = 0x15CFE
movq (%rax), %rdi
callq 0x11100
movl %eax, 0x4(%rsp)
jmp 0x1c733
movl 0x4(%rsp), %esi
movq 0x20(%rsp), %rdi
callq 0x1c910
jmp 0x1c743
movl $0x0, 0x3c(%rsp)
cmpl $0x20, 0x3c(%rsp)
jge 0x1c7a0
movq 0x20(%rsp), %rax
movq %rax, %rdx
addq $0x1730, %rdx           # imm = 0x1730
movslq 0x3c(%rsp), %rcx
addq %rcx, %rdx
movslq 0x3c(%rsp), %rcx
movq %rdx, 0x1750(%rax,%rcx,8)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1c74b
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x48(%rsp)
movl %eax, 0x44(%rsp)
callq 0x20100
jmp 0x1c8ee
movq 0x20(%rsp), %rax
movq %rax, %rcx
addq $0x1730, %rcx           # imm = 0x1730
movq %rcx, 0x17d0(%rax)
movq %rax, %rcx
addq $0x1730, %rcx           # imm = 0x1730
addq $0x4, %rcx
movq %rcx, 0x17f0(%rax)
movq %rax, %rcx
addq $0x1730, %rcx           # imm = 0x1730
addq $0x8, %rcx
movq %rcx, 0x1810(%rax)
movq %rax, %rcx
addq $0x1730, %rcx           # imm = 0x1730
addq $0xc, %rcx
movq %rcx, 0x1830(%rax)
movl $0x0, 0x38(%rsp)
cmpl $0x2, 0x38(%rsp)
jge 0x1c8e9
movl $0x0, 0x34(%rsp)
cmpl $0x100, 0x34(%rsp)      # imm = 0x100
jge 0x1c8d7
movl $0x0, 0x30(%rsp)
cmpl $0x8, 0x30(%rsp)
jge 0x1c8c5
movl $0x0, 0x2c(%rsp)
cmpl $0x2, 0x2c(%rsp)
jge 0x1c8b3
movl $0x0, 0x28(%rsp)
cmpl $0x2, 0x28(%rsp)
jge 0x1c8a4
movq 0x20(%rsp), %rax
addq $0x1850, %rax           # imm = 0x1850
movslq 0x38(%rsp), %rcx
shlq $0x10, %rcx
addq %rcx, %rax
movslq 0x34(%rsp), %rcx
shlq $0x8, %rcx
addq %rcx, %rax
movslq 0x30(%rsp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x2c(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movslq 0x28(%rsp), %rcx
movq $0x0, (%rax,%rcx,8)
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x1c848
jmp 0x1c8a6
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x1c839
jmp 0x1c8b5
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x1c826
jmp 0x1c8c7
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x1c810
jmp 0x1c8d9
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x38(%rsp)
jmp 0x1c7fd
addq $0x68, %rsp
retq
movq 0x18(%rsp), %rdi
callq 0x25630
movq 0x48(%rsp), %rdi
callq 0x4680
nopw %cs:(%rax,%rax)