// File: /workspace/github_cpp_makefile_star2_O1/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 0x914a
leaq 0x88(%rsp), %rdi
callq 0x40e0
testb %al, %al
je 0x914a
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 0x914a
leaq 0x10(%rbx), %r14
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
movl $0x10, %esi
movq %r14, %rdi
callq 0xa1a0
movq (%r14), %rsi
leaq 0x10(%rsp), %rdi
movl $0x10, %edx
callq 0x4450
movq 0x12fbe(%rip), %rdi     # 0x1bfb0
leaq 0xb4a1(%rip), %rsi      # 0x1449a
movl $0x8, %edx
callq 0x4410
leaq 0xb499(%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 0x900d
movq 0x12f81(%rip), %rdi     # 0x1bfb0
leaq 0xb474(%rip), %rsi      # 0x144aa
movl $0x9, %edx
callq 0x4410
leaq 0xb46d(%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 0x904a
movq 0x12f44(%rip), %r15     # 0x1bfb0
leaq 0xb437(%rip), %rsi      # 0x144aa
movl $0x9, %edx
movq %r15, %rdi
callq 0x4410
leaq 0xb433(%rip), %r12      # 0x144ba
leaq 0xb421(%rip), %r13      # 0x144af
xorl %ebp, %ebp
movq (%r14), %rax
movzbl (%rax,%rbp), %esi
leal -0x20(%rsi), %eax
cmpb $0x5e, %al
ja 0x90aa
movq %r12, %rdi
xorl %eax, %eax
callq 0x4050
jmp 0x90b4
movq %r13, %rdi
xorl %eax, %eax
callq 0x4050
incq %rbp
cmpq $0x10, %rbp
jne 0x9090
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x4390
movsbl %al, %esi
movq 0x12ed5(%rip), %rdi     # 0x1bfb0
callq 0x4030
movq %rax, %rdi
callq 0x4290
movq (%r14), %rax
movzbl (%rax), %esi
cmpl $0x4e, %esi
jne 0x912f
cmpb $0x45, 0x1(%rax)
jne 0x912f
cmpb $0x53, 0x2(%rax)
jne 0x912f
cmpb $0x1a, 0x3(%rax)
jne 0x912f
movzbl 0x4(%rax), %ecx
movl %ecx, 0x78(%rbx)
movzbl 0x5(%rax), %edx
movl %edx, 0x7c(%rbx)
movb 0x6(%rax), %sil
testb $0x1, %sil
jne 0x9174
testb $0x8, %sil
jne 0x9180
movl $0x1, 0x88(%rbx)
jmp 0x918a
movzbl 0x1(%rax), %edx
movzbl 0x2(%rax), %ecx
movzbl 0x3(%rax), %r8d
leaq 0xb37d(%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 0x918a
movl $0x2, 0x88(%rbx)
cmpl $0x0, 0x90(%rbx)
jne 0x91b5
movzbl 0x6(%rax), %esi
shrl $0x4, %esi
movl %esi, 0x90(%rbx)
movb 0x7(%rax), %al
cmpb $0x44, %al
je 0x91b5
andb $-0x10, %al
orb %sil, %al
movzbl %al, %eax
movl %eax, 0x90(%rbx)
cmpl $0x0, 0x90(%rbx)
jne 0x91fc
cmpb $0x3, %cl
setb %al
cmpb $0x1, %dl
sete %cl
testb %cl, %al
jne 0x91fc
movq 0x12ddb(%rip), %rdi     # 0x1bfb0
leaq 0xb303(%rip), %rsi      # 0x144df
movl $0x3b, %edx
callq 0x4410
movq 0x12dc3(%rip), %rdi     # 0x1bfb0
callq 0x41d0
movl $0x1, 0x90(%rbx)
movq 0x10(%rbx), %rax
movb 0x6(%rax), %cl
shrb %cl
andb $0x1, %cl
movb %cl, 0xa0(%rbx)
movb 0x6(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movb %al, 0xa1(%rbx)
leaq 0x28(%rbx), %r14
movslq 0x78(%rbx), %rsi
shlq $0xe, %rsi
movq %r14, %rdi
callq 0xa1ce
leaq 0x40(%rbx), %r15
movslq 0x78(%rbx), %rsi
shlq $0xe, %rsi
movq %r15, %rdi
callq 0xa1fa
movq 0x28(%rbx), %rsi
movslq 0x78(%rbx), %rdx
shlq $0xe, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4450
cmpl $0x0, 0x78(%rbx)
jle 0x9286
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 0x9268
movslq 0x7c(%rbx), %rsi
leaq 0x58(%rbx), %rdi
testq %rsi, %rsi
je 0x92bc
shlq $0xd, %rsi
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
callq 0xa1a0
movq 0x58(%rbx), %rsi
movslq 0x7c(%rbx), %rdx
shlq $0xd, %rdx
leaq 0x10(%rsp), %rdi
callq 0x4450
jmp 0x92ce
leaq 0xf(%rsp), %rdx
movb $0x0, (%rdx)
movl $0x2000, %esi           # imm = 0x2000
callq 0xa1a0
movl 0x90(%rbx), %eax
cmpq $0xb, %rax
ja 0x9307
leaq 0xb14b(%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 0x1298c
jmp 0x9396
movq 0x12ca2(%rip), %rdi     # 0x1bfb0
leaq 0xb206(%rip), %rsi      # 0x1451b
movl $0x1b, %edx
callq 0x4410
movq 0x12c8a(%rip), %rdi     # 0x1bfb0
callq 0x41d0
jmp 0x914a
movl $0x18, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x12fd8
jmp 0x9396
movl $0x58, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x12aa8
jmp 0x9396
movl $0x30, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x130e4
jmp 0x9396
movl $0x38, %edi
callq 0x43b0
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x13444
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 0x9156
jmp 0x9427
jmp 0x9427
jmp 0x9427
jmp 0x9427
movq %rax, %rbx
movq %r14, %rdi
callq 0x4380
jmp 0x9443
jmp 0x9440
jmp 0x9440
jmp 0x9440
jmp 0x9440
jmp 0x9440
jmp 0x9440
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x40f0
movq %rbx, %rdi
callq 0x4610
nop