// File: khedoros[P]khednes/mapper_003_cnrom.cpp
// Function: mapper_003::cycle_forward(unsigned int)
rom::ppu_change_t mapper_003::cycle_forward(unsigned int cycle) {
    unsigned int next_c = cycle + 1;
    if(ppu_queue.size() > 0) {
        next_c = ppu_queue.front().first;
    }
    rom::ppu_change_t changes;
    changes.val = 0;
    while(ppu_queue.size() > 0 && next_c < cycle) {
        rom_command_t comm = ppu_queue.front().second.first;
        int val = ppu_queue.front().second.second;
        switch(comm) {
        case lo_crom:
            changes.lo_crom = 1;
            chr_lo_offset = val;
            break;
        case hi_crom:
            changes.hi_crom = 1;
            chr_hi_offset = val;
            break;
        case set_horiz:
            changes.set_horiz = 1;
            cart->mirror_mode = HORIZ;
            break;
        case set_vert:
            changes.set_vert = 1;
            cart->mirror_mode = VERT;
            break;
        case set_4_scr:
            changes.set_4_scr = 1;
            cart->mirror_mode = FOUR_SCR;
            break;
        case set_bg:
            if(val == 0)
                changes.set_bg0 = 1;
            else
                changes.set_bg1 = 1;
            break;
        case set_spr:
            if(val == 0)
                changes.set_spr0 = 1;
            else
                changes.set_spr1 = 1;
            break;
        default:
            std::cout<<"Unknown command!"<<std::endl;
            break;
        }
        ppu_queue.pop_front();
        if(ppu_queue.size() > 0)
            next_c = ppu_queue.front().first;
    }
    return changes;

}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movl %esi, 0x34(%rsp)
movq 0x38(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
addq $0x10, %rdi
callq 0x28820
cmpq $0x0, %rax
jbe 0x28525
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x28850
movl (%rax), %eax
movl %eax, 0x30(%rsp)
movw 0x44(%rsp), %ax
andw $0xfe00, %ax            # imm = 0xFE00
orw $0x0, %ax
movw %ax, 0x44(%rsp)
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x28820
movq %rax, %rcx
xorl %eax, %eax
cmpq $0x0, %rcx
movb %al, 0x1f(%rsp)
jbe 0x28563
movl 0x30(%rsp), %eax
cmpl 0x34(%rsp), %eax
setb %al
movb %al, 0x1f(%rsp)
movb 0x1f(%rsp), %al
testb $0x1, %al
jne 0x28570
jmp 0x28742
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
movq %rdi, 0x8(%rsp)
callq 0x28850
movq 0x8(%rsp), %rdi
movl 0x4(%rax), %eax
movl %eax, 0x2c(%rsp)
callq 0x28850
movl 0x8(%rax), %eax
movl %eax, 0x28(%rsp)
movl 0x2c(%rsp), %eax
movq %rax, 0x10(%rsp)
subq $0x6, %rax
ja 0x286e5
movq 0x10(%rsp), %rax
leaq 0x501e(%rip), %rcx      # 0x2d5d8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x20(%rsp), %rax
movw 0x44(%rsp), %cx
andw $-0x2, %cx
orw $0x1, %cx
movw %cx, 0x44(%rsp)
movl 0x28(%rsp), %ecx
movl %ecx, 0x28(%rax)
jmp 0x28707
movq 0x20(%rsp), %rax
movw 0x44(%rsp), %cx
andw $-0x3, %cx
orw $0x2, %cx
movw %cx, 0x44(%rsp)
movl 0x28(%rsp), %ecx
movl %ecx, 0x2c(%rax)
jmp 0x28707
movq 0x20(%rsp), %rax
movw 0x44(%rsp), %cx
andw $-0x5, %cx
orw $0x4, %cx
movw %cx, 0x44(%rsp)
movq 0x8(%rax), %rax
movl $0x1, 0x88(%rax)
jmp 0x28707
movq 0x20(%rsp), %rax
movw 0x44(%rsp), %cx
andw $-0x9, %cx
orw $0x8, %cx
movw %cx, 0x44(%rsp)
movq 0x8(%rax), %rax
movl $0x0, 0x88(%rax)
jmp 0x28707
movq 0x20(%rsp), %rax
movw 0x44(%rsp), %cx
andw $-0x11, %cx
orw $0x10, %cx
movw %cx, 0x44(%rsp)
movq 0x8(%rax), %rax
movl $0x2, 0x88(%rax)
jmp 0x28707
cmpl $0x0, 0x28(%rsp)
jne 0x286a2
movw 0x44(%rsp), %ax
andw $-0x21, %ax
orw $0x20, %ax
movw %ax, 0x44(%rsp)
jmp 0x286b4
movw 0x44(%rsp), %ax
andw $-0x41, %ax
orw $0x40, %ax
movw %ax, 0x44(%rsp)
jmp 0x28707
cmpl $0x0, 0x28(%rsp)
jne 0x286d1
movw 0x44(%rsp), %ax
andw $0xff7f, %ax            # imm = 0xFF7F
orw $0x80, %ax
movw %ax, 0x44(%rsp)
jmp 0x286e3
movw 0x44(%rsp), %ax
andw $0xfeff, %ax            # imm = 0xFEFF
orw $0x100, %ax             # imm = 0x100
movw %ax, 0x44(%rsp)
jmp 0x28707
movq 0xe8c4(%rip), %rdi      # 0x36fb0
leaq 0x4f0e(%rip), %rsi      # 0x2d601
callq 0x43c0
movq %rax, %rdi
movq 0xe88e(%rip), %rsi      # 0x36f90
callq 0x4400
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x28880
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x28820
cmpq $0x0, %rax
jbe 0x2873d
movq 0x20(%rsp), %rdi
addq $0x10, %rdi
callq 0x28850
movl (%rax), %eax
movl %eax, 0x30(%rsp)
jmp 0x28537
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)