// File: /workspace/github_cpp_makefile_star2_O2/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 0x43e0
movq (%r14), %rsi
pushq $0xc
popq %rdx
movq %r15, %rdi
callq 0x4230
movq 0x10(%rsp), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x30(%rsp,%rax)
jne 0x8214
leaq 0x88(%rsp), %rdi
callq 0x40d0
testb %al, %al
je 0x8214
leaq 0x10(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x4460
leaq 0x10(%rsp), %rdi
callq 0x4620
movl %eax, 0x8c(%rbx)
leaq 0x10(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4460
cmpl $0x4010, 0x8c(%rbx)     # imm = 0x4010
jl 0x8214
leaq 0x10(%rbx), %r14
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x8f6a
movq (%r14), %rsi
leaq 0x10(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x4420
movq 0x12ed4(%rip), %rdi     # 0x1afb0
leaq 0xa39b(%rip), %rsi      # 0x1247e
callq 0x4390
leaq 0xa398(%rip), %r15      # 0x12487
xorl %r12d, %r12d
cmpq $0x10, %r12
je 0x810f
movq (%r14), %rax
movzbl (%rax,%r12), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4040
incq %r12
jmp 0x80f2
movq 0x12e9a(%rip), %rdi     # 0x1afb0
leaq 0xa371(%rip), %rsi      # 0x1248e
callq 0x4390
leaq 0xa36f(%rip), %r15      # 0x12498
xorl %r12d, %r12d
cmpq $0x10, %r12
je 0x8149
movq (%r14), %rax
movzbl (%rax,%r12), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x4040
incq %r12
jmp 0x812c
movq 0x12e60(%rip), %rdi     # 0x1afb0
leaq 0xa337(%rip), %rsi      # 0x1248e
callq 0x4390
leaq 0xa33b(%rip), %r15      # 0x1249e
leaq 0xa329(%rip), %r12      # 0x12493
xorl %r13d, %r13d
cmpq $0x10, %r13
je 0x819d
movq (%r14), %rax
movzbl (%rax,%r13), %esi
leal -0x20(%rsi), %eax
cmpb $0x5e, %al
ja 0x818e
movq %r15, %rdi
xorl %eax, %eax
callq 0x4040
jmp 0x8198
movq %r12, %rdi
xorl %eax, %eax
callq 0x4040
incq %r13
jmp 0x816d
movq 0x12e0c(%rip), %rdi     # 0x1afb0
callq 0x41c0
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 0x81fa
movb $0x45, %cl
cmpb $0x53, 0x2(%rax)
jne 0x81fa
cmpb $0x1a, 0x3(%rax)
jne 0x81fa
movzbl 0x4(%rax), %ecx
movl %ecx, 0x78(%rbx)
movzbl 0x5(%rax), %edx
movl %edx, 0x7c(%rbx)
movb 0x6(%rax), %sil
testb $0x1, %sil
jne 0x823e
testb $0x8, %sil
jne 0x8247
movl $0x1, 0x88(%rbx)
jmp 0x8251
movzbl %cl, %edx
movzbl 0x2(%rax), %ecx
movzbl 0x3(%rax), %r8d
leaq 0xa297(%rip), %rdi      # 0x124a4
xorl %eax, %eax
callq 0x4040
leaq 0x10(%rsp), %rdi
callq 0x40b0
xorl %ebx, %ebx
leaq 0x10(%rsp), %rdi
callq 0x40e0
movl %ebx, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
andl $0x0, 0x88(%rbx)
jmp 0x8251
movl $0x2, 0x88(%rbx)
cmpl $0x0, 0x90(%rbx)
jne 0x82c4
movzbl 0x6(%rax), %esi
shrl $0x4, %esi
movl %esi, 0x90(%rbx)
movb 0x7(%rax), %dil
cmpb $0x44, %dil
je 0x8282
andb $-0x10, %dil
orb %sil, %dil
movzbl %dil, %esi
movl %esi, 0x90(%rbx)
testl %esi, %esi
jne 0x82c4
cmpb $0x3, %cl
setb %sil
cmpb $0x1, %dl
sete %dl
testb %dl, %sil
jne 0x82c4
movq 0x12d11(%rip), %rdi     # 0x1afb0
leaq 0xa21d(%rip), %rsi      # 0x124c3
callq 0x4390
movq %rax, %rdi
callq 0x41c0
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 0x8f98
leaq 0x40(%rbx), %r15
movslq 0x78(%rbx), %rsi
shlq $0xe, %rsi
movq %r15, %rdi
callq 0x8fc4
movq 0x28(%rbx), %rsi
movslq 0x78(%rbx), %rdx
shlq $0xe, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4420
movq (%r14), %rax
movq (%r15), %rcx
xorl %edx, %edx
movslq 0x78(%rbx), %rsi
shlq $0xe, %rsi
decq %rsi
cmpq %rsi, %rdx
jge 0x8340
movzwl (%rax,%rdx), %esi
movl %esi, (%rcx,%rdx,4)
incq %rdx
jmp 0x8324
movslq 0x7c(%rbx), %rsi
leaq 0x58(%rbx), %rdi
testq %rsi, %rsi
je 0x8376
shlq $0xd, %rsi
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
callq 0x8f6a
movq 0x58(%rbx), %rsi
movslq 0x7c(%rbx), %rdx
shlq $0xd, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4420
jmp 0x8388
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
movl $0x2000, %esi           # imm = 0x2000
callq 0x8f6a
movl 0x90(%rbx), %eax
cmpq $0xb, %rax
ja 0x83bc
leaq 0xa069(%rip), %rcx      # 0x12404
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
pushq $0x10
popq %rdi
callq 0x43a0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x107a4
jmp 0x843a
movq 0x12bed(%rip), %rdi     # 0x1afb0
leaq 0xa135(%rip), %rsi      # 0x124ff
callq 0x4390
movq %rax, %rdi
callq 0x41c0
jmp 0x8214
pushq $0x18
popq %rdi
callq 0x43a0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10dc4
jmp 0x843a
pushq $0x58
popq %rdi
callq 0x43a0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1088c
jmp 0x843a
pushq $0x30
popq %rdi
callq 0x43a0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x10ecc
jmp 0x843a
pushq $0x38
popq %rdi
callq 0x43a0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x11270
movq %r14, (%rbx)
leaq 0x10(%rsp), %rdi
callq 0x40b0
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 0x8220
jmp 0x84cb
jmp 0x84cb
jmp 0x84cb
jmp 0x84cb
movq %rax, %rbx
movq %r14, %rdi
callq 0x4370
jmp 0x84e7
jmp 0x84e4
jmp 0x84e4
jmp 0x84e4
jmp 0x84e4
jmp 0x84e4
jmp 0x84e4
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x40e0
movq %rbx, %rdi
callq 0x4640
nop