// File: /workspace/github_cpp_makefile_star2_O3/khedoros[P]khednes/rom.cpp
// Function: rom::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
bool rom::load(std::string& filename) {
    std::ifstream filein;
    filein.open(filename.c_str(),std::ios::binary|std::ios::in);
    if(!(filein&&filein.is_open())) {
        filein.close();
        return false;
    }
    //get filesize
    filein.seekg(0,std::ios::end);
    filesize=int(filein.tellg());
    filein.seekg(0,std::ios::beg);
    if(filesize<MIN_FILE_SIZE) {
        filein.close();
        return false;
    }
    //parse the header, assign appropriate values to properties
    header.resize(16,0);
    filein.read(reinterpret_cast<char*>(&header[0]),16);

    std::cout<<"Header: ";
    for(int i=0;i<16;++i) {
        printf(" %02x ",header[i]);
    }
    std::cout<<"\n        ";
    for(int i=0;i<16;++i) {
        printf("%03d ", header[i]);
    }
    std::cout<<"\n        ";;
    for(int i=0;i<16;++i) {
        if(header[i] >= 32 && header[i] < 127)
            printf("  %c ", header[i]);
        else printf("    ");
    }
    std::cout<<std::endl;

    if(header[0]!='N'||header[1]!='E'||header[2]!='S'||header[3]!=0x1a) {
        printf("(%02x)  (%02x)  (%02x)  (%02x)", header[0],header[1],header[2],header[3]);
        filein.close();
        return false;
    }

    prom_pages=(int)header[4];
    crom_pages=(int)header[5];

    if(0x01&header[6]) {
        mirror_mode=VERT; //vertical mirrored
    }
    else if(0x08&header[6]) {
        mirror_mode=FOUR_SCR; //4 screen mirror
    }
    else {
        mirror_mode=HORIZ; //horizontal mirror
    }

    if(mapper_num == 0) {
        //std::cout<<std::hex<<(header[6]>>(4))<<" "<<(header[7]&0xF0)<<std::endl;
        mapper_num=((header[6]>>(4)));//|(header[7]&0xF0));//often incorrect
        if(header[7] != 'D') mapper_num |= (header[7]/16)<<(4);
    }
    if(mapper_num == 0 && (prom_pages > 2 || crom_pages != 1)) {
        std::cout<<"It's not possible that this is mapper 0. Taking a guess! =/"<<std::endl;
        mapper_num = mapper_guess();
    }

    sram=((0x02&header[6])>0)?true:false;
    trainer=((0x04&header[6])>0)?true:false;

    prom.resize(prom_pages*PRG_PAGE_SIZE);
    //prom_w = new unsigned int[prom_pages*PRG_PAGE_SIZE];
    prom_w.resize(prom_pages * PRG_PAGE_SIZE);
    filein.read(reinterpret_cast<char*>(&prom[0]),PRG_PAGE_SIZE*prom_pages);
    for(int i=0;i<prom_pages*PRG_PAGE_SIZE-1;++i)
        prom_w[i] = prom[i] | (prom[i+1]<<(8));

    if(crom_pages == 0) {//For ROMs with no CHR-ROM, create one page of RAM for the PRG to fill
        crom.resize(CHR_PAGE_SIZE,0);
    }
    else {//Read in the appropriate number of CHROM pages, if any exist.
          //cpage can be used for bank switching if necessary.
        crom.resize(crom_pages * CHR_PAGE_SIZE, 0);
        filein.read(reinterpret_cast<char*>(&crom[0]),CHR_PAGE_SIZE*crom_pages);
    }

    switch(mapper_num) {
    case 0:
        map = new mapper(this);
        break;
    case 1:
        map = new mapper_001(this);
        break;
    case 2:
        map = new mapper_002(this);
        break;
    case 3:
        map = new mapper_003(this);
        break;
    case 11:
        map = new mapper_011(this);
        break;
    default:
        std::cout<<"Mapper not implemented yet."<<std::endl;
        filein.close();
        return false;
    }

    filein.close();

    //Find the reset.power-on bit vector
    int startl=get_pbyte(RST_INT_INSERTION);
    int starth=get_pbyte(RST_INT_INSERTION+1);
    rst_addr=startl|starth<<8;
    //if(get_pword(RST_INT_INSERTION) != rst_addr) std::cout<<"Unexpected error!"<<std::endl;

    //Find the IRQ bit vector
    startl=get_pbyte(IRQ_INT_INSERTION);
    starth=get_pbyte(IRQ_INT_INSERTION+1);
    irq_addr=startl|starth<<8;
    //if(get_pword(IRQ_INT_INSERTION) != irq_addr) std::cout<<"Unexpected error!"<<std::endl;

    //Find the NMI bit vector
    startl=get_pbyte(NM_INT_INSERTION);
    starth=get_pbyte(NM_INT_INSERTION+1);
    nmi_addr=startl|starth<<8;
    //if(get_pword(NM_INT_INSERTION) != nmi_addr) std::cout<<"Unexpected error!"<<std::endl;

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x4400
movq (%r14), %rsi
movq %r15, %rdi
movl $0xc, %edx
callq 0x4240
movq 0x10(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x30(%rsp,%rax)
jne 0x902b
leaq 0x88(%rsp), %rdi
callq 0x40e0
testb %al, %al
je 0x902b
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x4480
leaq 0x10(%rsp), %rdi
callq 0x45f0
movl %eax, 0x8c(%rbx)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4480
cmpl $0x4010, 0x8c(%rbx)     # imm = 0x4010
jl 0x902b
leaq 0x10(%rbx), %r14
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
movl $0x10, %esi
movq %r14, %rdi
callq 0xa08e
movq (%r14), %rsi
leaq 0x10(%rsp), %rdi
movl $0x10, %edx
callq 0x4450
movq 0x130e6(%rip), %rdi     # 0x1bfb0
leaq 0xb5c9(%rip), %rsi      # 0x1449a
movl $0x8, %edx
callq 0x4410
leaq 0xb5c1(%rip), %r15      # 0x144a3
xorl %r12d, %r12d
movq (%r14), %rax
movzbl (%rax,%r12), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4050
incq %r12
cmpq $0x10, %r12
jne 0x8ee5
movq 0x130a9(%rip), %rdi     # 0x1bfb0
leaq 0xb59c(%rip), %rsi      # 0x144aa
movl $0x9, %edx
callq 0x4410
leaq 0xb595(%rip), %r15      # 0x144b4
xorl %r12d, %r12d
movq (%r14), %rax
movzbl (%rax,%r12), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4050
incq %r12
cmpq $0x10, %r12
jne 0x8f22
movq 0x1306c(%rip), %r15     # 0x1bfb0
leaq 0xb55f(%rip), %rsi      # 0x144aa
movl $0x9, %edx
movq %r15, %rdi
callq 0x4410
leaq 0xb55b(%rip), %r12      # 0x144ba
leaq 0xb549(%rip), %r13      # 0x144af
xorl %ebp, %ebp
movq (%r14), %rax
movzbl (%rax,%rbp), %esi
leal -0x20(%rsi), %eax
cmpb $0x5e, %al
ja 0x8f82
movq %r12, %rdi
xorl %eax, %eax
callq 0x4050
jmp 0x8f8c
movq %r13, %rdi
xorl %eax, %eax
callq 0x4050
incq %rbp
cmpq $0x10, %rbp
jne 0x8f68
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq 0x12ffd(%rip), %rdi     # 0x1bfb0
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq (%r14), %rax
movzbl (%rax), %esi
movl %esi, %edx
xorb $0x4e, %dl
movb 0x1(%rax), %cl
movl %ecx, %edi
xorb $0x45, %dil
orb %dl, %dil
jne 0x9011
movb $0x45, %cl
cmpb $0x53, 0x2(%rax)
jne 0x9011
cmpb $0x1a, 0x3(%rax)
jne 0x9011
movzbl 0x4(%rax), %ecx
movl %ecx, 0x78(%rbx)
movzbl 0x5(%rax), %edx
movl %edx, 0x7c(%rbx)
movb 0x6(%rax), %sil
testb $0x1, %sil
jne 0x9055
testb $0x8, %sil
jne 0x9061
movl $0x1, 0x88(%rbx)
jmp 0x906b
movzbl %cl, %edx
movzbl 0x2(%rax), %ecx
movzbl 0x3(%rax), %r8d
leaq 0xb49c(%rip), %rdi      # 0x144c0
xorl %eax, %eax
callq 0x4050
leaq 0x10(%rsp), %rdi
callq 0x40c0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x40f0
movl %ebx, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x0, 0x88(%rbx)
jmp 0x906b
movl $0x2, 0x88(%rbx)
cmpl $0x0, 0x90(%rbx)
jne 0x90e7
movzbl 0x6(%rax), %esi
shrl $0x4, %esi
movl %esi, 0x90(%rbx)
movb 0x7(%rax), %dil
cmpb $0x44, %dil
je 0x909c
andb $-0x10, %dil
orb %sil, %dil
movzbl %dil, %esi
movl %esi, 0x90(%rbx)
testl %esi, %esi
jne 0x90e7
cmpb $0x3, %cl
setb %sil
cmpb $0x1, %dl
sete %dl
testb %dl, %sil
jne 0x90e7
movq 0x12ef7(%rip), %rdi     # 0x1bfb0
leaq 0xb41f(%rip), %rsi      # 0x144df
movl $0x3b, %edx
callq 0x4410
movq 0x12edf(%rip), %rdi     # 0x1bfb0
callq 0x41d0
movl $0x1, 0x90(%rbx)
movq 0x10(%rbx), %rax
movl 0x78(%rbx), %ecx
movb 0x6(%rax), %dl
shrb %dl
andb $0x1, %dl
movb %dl, 0xa0(%rbx)
movb 0x6(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movb %al, 0xa1(%rbx)
leaq 0x28(%rbx), %r14
shll $0xe, %ecx
movslq %ecx, %rsi
movq %r14, %rdi
callq 0xa0bc
leaq 0x40(%rbx), %r15
movslq 0x78(%rbx), %rsi
shlq $0xe, %rsi
movq %r15, %rdi
callq 0xa0e8
movq 0x28(%rbx), %rsi
movslq 0x78(%rbx), %rdx
shlq $0xe, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4450
cmpl $0x0, 0x78(%rbx)
jle 0x916b
movq (%r14), %rax
movq (%r15), %rcx
xorl %edx, %edx
movzwl (%rax,%rdx), %esi
movl %esi, (%rcx,%rdx,4)
leaq 0x1(%rdx), %rsi
movslq 0x78(%rbx), %rdi
shlq $0xe, %rdi
decq %rdi
movq %rsi, %rdx
cmpq %rdi, %rsi
jl 0x914d
movslq 0x7c(%rbx), %rsi
leaq 0x58(%rbx), %rdi
testq %rsi, %rsi
je 0x91a1
shlq $0xd, %rsi
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
callq 0xa08e
movq 0x58(%rbx), %rsi
movslq 0x7c(%rbx), %rdx
shlq $0xd, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4450
jmp 0x91b3
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
movl $0x2000, %esi           # imm = 0x2000
callq 0xa08e
movl 0x90(%rbx), %eax
cmpq $0xb, %rax
ja 0x91ec
leaq 0xb266(%rip), %rcx      # 0x1442c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x10, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x12870
jmp 0x927b
movq 0x12dbd(%rip), %rdi     # 0x1bfb0
leaq 0xb321(%rip), %rsi      # 0x1451b
movl $0x1b, %edx
callq 0x4410
movq 0x12da5(%rip), %rdi     # 0x1bfb0
callq 0x41d0
jmp 0x902b
movl $0x18, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x12eb4
jmp 0x927b
movl $0x58, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1298c
jmp 0x927b
movl $0x30, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x12fc0
jmp 0x927b
movl $0x38, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1330c
movq %r14, (%rbx)
leaq 0x10(%rsp), %rdi
callq 0x40c0
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xfffc, %esi           # imm = 0xFFFC
callq *(%rax)
movl %eax, %ebp
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xfffd, %esi           # imm = 0xFFFD
callq *(%rax)
shll $0x8, %eax
orl %ebp, %eax
movl %eax, 0x94(%rbx)
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xfffe, %esi           # imm = 0xFFFE
callq *(%rax)
movl %eax, %ebp
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xffff, %esi           # imm = 0xFFFF
callq *(%rax)
shll $0x8, %eax
orl %ebp, %eax
movl %eax, 0x9c(%rbx)
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xfffa, %esi           # imm = 0xFFFA
callq *(%rax)
movl %eax, %ebp
movq (%rbx), %rdi
movq (%rdi), %rax
movl $0xfffb, %esi           # imm = 0xFFFB
callq *(%rax)
shll $0x8, %eax
orl %ebp, %eax
movl %eax, 0x98(%rbx)
movb $0x1, %bl
jmp 0x9037
jmp 0x930c
jmp 0x930c
jmp 0x930c
jmp 0x930c
movq %rax, %rbx
movq %r14, %rdi
callq 0x4380
jmp 0x9328
jmp 0x9325
jmp 0x9325
jmp 0x9325
jmp 0x9325
jmp 0x9325
jmp 0x9325
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x4610