// File: /workspace/github_cpp_makefile_star2_O2/rockytriton[P]dreamsys_emu_gb/src/cart.cpp
// Function: dsemu::cart::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
bool load(const string &romFile) {
    std::ifstream in(romFile, std::ios::binary);

    if (!in) {
        cout << "Unable to open ROM: " << romFile << " - " << strerror(errno) << endl;
        return false;
    }

    if (g_romData != nullptr) {
        delete[] g_romData;
    }

    in.seekg(0, in.end);
    g_romSize = in.tellg();
    in.seekg(0, in.beg);

    g_romData = new byte[g_romSize];

    in.read((char *)g_romData, g_romSize);

    memcpy(&g_header, g_romData + 0x100, sizeof(Header));
    in.close();

    cout << "Loaded Rom: " << romFile << endl;
    cout << "\t    Size: " << g_romSize << endl;
    cout << "\t   Title: " << g_header.title << endl;
    cout << "\tCart Typ: " << Byte(g_header.cartType) << endl;
    cout << "\tCGB Flag: " << Byte(g_header.gbcFlag) << endl;
    cout << "\tSGB Flag: " << Byte(g_header.sgbFlag) << endl;
    cout << "\tROM Size: " << Byte(g_header.romSize) << endl;
    cout << "\tRAM Size: " << Byte(g_header.ramSize) << endl;
    cout << "\tJap Flag: " << Byte(g_header.japFlag) << endl;
    cout << "\t   Entry: " << Byte(g_header.entry[0]) << " "
                           << Byte(g_header.entry[1]) << " "
                           << Byte(g_header.entry[2]) << " "
                           << Byte(g_header.entry[3]) << endl;

    switch(g_header.cartType) {
        case 0: {
            mapper = new mappers::NROMMapper();
        } break;
        case 1: {
            mapper = new mappers::MPC1();
        } break;
        default: {
            cout << "UNSUPPORTED MAPPER" << endl;
            exit(-1);
        }
    }

    return true;

}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movq %rdi, %rbx
leaq 0x8(%rsp), %r14
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3380
movq (%r14), %rax
movq -0x18(%rax), %rax
movl 0x28(%rsp,%rax), %ebp
testb $0x5, %bpl
je 0xa862
movq 0xb7bb(%rip), %rdi      # 0x15fd0
leaq 0x57e8(%rip), %rsi      # 0x10004
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3220
leaq 0x57e6(%rip), %rsi      # 0x10019
movq %rax, %rdi
callq 0x3260
movq %rax, %rbx
callq 0x3040
movl (%rax), %edi
callq 0x30d0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3260
movq %rax, %rdi
callq 0x3130
jmp 0xab28
movq 0xba4f(%rip), %rdi      # 0x162b8
testq %rdi, %rdi
je 0xa873
callq 0x32f0
leaq 0x8(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x3300
leaq 0x8(%rsp), %rdi
callq 0x33f0
movl %eax, 0xba2e(%rip)      # 0x162c0
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3300
movslq 0xba19(%rip), %r14      # 0x162c0
movq %r14, %rdi
callq 0x3030
movq %rax, 0xba02(%rip)      # 0x162b8
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x32e0
movl $0x100, %esi            # imm = 0x100
addq 0xb9e6(%rip), %rsi      # 0x162b8
leaq 0xb980(%rip), %rdi      # 0x16259
pushq $0x51
popq %rdx
callq 0x3200
leaq 0x8(%rsp), %rdi
callq 0x3090
movq 0xb6de(%rip), %rdi      # 0x15fd0
leaq 0x5724(%rip), %rsi      # 0x1001d
callq 0x3260
movq %rax, %rdi
movq %rbx, %rsi
callq 0x3220
movq %rax, %rdi
callq 0x3130
movq 0xb6b8(%rip), %rdi      # 0x15fd0
leaq 0x570b(%rip), %rsi      # 0x1002a
callq 0x3260
movl 0xb996(%rip), %esi      # 0x162c0
movq %rax, %rdi
callq 0x33e0
movq %rax, %rdi
callq 0x3130
movq 0xb68f(%rip), %rdi      # 0x15fd0
leaq 0x56ee(%rip), %rsi      # 0x10036
callq 0x3260
leaq 0xb939(%rip), %rsi      # 0x1628d
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3130
movq 0xb665(%rip), %rdi      # 0x15fd0
leaq 0x56d0(%rip), %rsi      # 0x10042
callq 0x3260
movzbl 0xb923(%rip), %esi      # 0x162a1
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb63b(%rip), %rdi      # 0x15fd0
leaq 0x56b2(%rip), %rsi      # 0x1004e
callq 0x3260
movzbl 0xb8f5(%rip), %esi      # 0x1629d
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb611(%rip), %rdi      # 0x15fd0
leaq 0x5694(%rip), %rsi      # 0x1005a
callq 0x3260
movzbl 0xb8ce(%rip), %esi      # 0x162a0
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb5e7(%rip), %rdi      # 0x15fd0
leaq 0x5676(%rip), %rsi      # 0x10066
callq 0x3260
movzbl 0xb8a6(%rip), %esi      # 0x162a2
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb5bd(%rip), %rdi      # 0x15fd0
leaq 0x5658(%rip), %rsi      # 0x10072
callq 0x3260
movzbl 0xb87d(%rip), %esi      # 0x162a3
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb593(%rip), %rdi      # 0x15fd0
leaq 0x563a(%rip), %rsi      # 0x1007e
callq 0x3260
movzbl 0xb854(%rip), %esi      # 0x162a4
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movq 0xb569(%rip), %rdi      # 0x15fd0
leaq 0x561c(%rip), %rsi      # 0x1008a
callq 0x3260
movzbl 0xb7df(%rip), %esi      # 0x16259
movq %rax, %rdi
callq 0xaba6
leaq 0x5624(%rip), %rsi      # 0x100ad
movq %rax, %rdi
callq 0x3260
movzbl 0xb7c2(%rip), %esi      # 0x1625a
movq %rax, %rdi
callq 0xaba6
leaq 0x5606(%rip), %rsi      # 0x100ad
movq %rax, %rdi
callq 0x3260
movzbl 0xb7a5(%rip), %esi      # 0x1625b
movq %rax, %rdi
callq 0xaba6
leaq 0x55e8(%rip), %rsi      # 0x100ad
movq %rax, %rdi
callq 0x3260
movzbl 0xb788(%rip), %esi      # 0x1625c
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
movzbl 0xb7b6(%rip), %eax      # 0x162a1
testl %eax, %eax
je 0xab0b
cmpl $0x1, %eax
jne 0xab47
pushq $0x10
popq %rdi
callq 0x3270
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
leaq 0xb237(%rip), %rcx      # 0x15d40
jmp 0xab1a
pushq $0x8
popq %rdi
callq 0x3270
leaq 0xb1ee(%rip), %rcx      # 0x15d08
addq $0x10, %rcx
movq %rcx, (%rax)
movq %rax, 0xb788(%rip)      # 0x162b0
testb $0x5, %bpl
sete %bl
leaq 0x8(%rsp), %rdi
callq 0x30a0
movl %ebx, %eax
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r14
popq %rbp
retq
movq 0xb482(%rip), %rdi      # 0x15fd0
leaq 0x5541(%rip), %rsi      # 0x10096
callq 0x3260
movq %rax, %rdi
callq 0x3130
pushq $-0x1
popq %rdi
callq 0x32d0
jmp 0xab6c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x30a0
movq %rbx, %rdi
callq 0x3400