// File: nicoeps[P]NicoGB/src/cartridge.cpp
// Function: Cartridge::load(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Cartridge::load(std::string path) {
    std::ifstream cartridgeFile(path, std::ifstream::binary);

    if (!cartridgeFile.is_open()) {
        loaded = false;
        return;
    } else {
        cartridgeFile.seekg(0, cartridgeFile.end);
        romSize = cartridgeFile.tellg();
        cartridgeFile.seekg(0, cartridgeFile.beg);
        cartridgeFile.clear();
        if (romSize % 32768 != 0) {
            loaded = false;
            cartridgeFile.close();
            return;
        }
        cartridgeROM.resize(romSize);
        cartridgeFile.read((char*) cartridgeROM.data(), romSize);
        cartridgeFile.close();
        loaded = true;

        cartridgeType = cartridgeROM[0x0147];
        switch (cartridgeROM[0x0149]) {
            case 0:  ramSize = 1;         break;
            case 1:  ramSize = 2048;      break;
            case 2:  ramSize = 8192;      break;
            case 3:  ramSize = 4 * 8192;  break;
            case 4:  ramSize = 16 * 8192; break;
            case 5:  ramSize = 8 * 8192;  break;
            default: ramSize = 1;         break;
        }

        if (cartridgeType == 0x05 || cartridgeType == 0x06) { // MBC2
            ramSize = 512;
        }

        cartridgeRAM.resize(ramSize);
        std::fill_n(cartridgeRAM.begin(), ramSize, 0);

        int titleSize = cartridgeROM[0x0143] & 0x80 ? 15 : 16; // CGB Flag
        title.assign(&cartridgeROM[0x0134], &cartridgeROM[0x0134] + titleSize);

        switch (cartridgeType) {
            // ROM
            case 0x00:
            case 0x08:
            case 0x09:
                mbc.reset(new MBC0(cartridgeROM, cartridgeRAM, ramSize));
                break;

            // MBC1
            case 0x01:
            case 0x02:
            case 0x03:
                mbc.reset(new MBC1(cartridgeROM, cartridgeRAM, romSize, ramSize));
                break;

            // MBC2
            case 0x05:
            case 0x06:
                mbc.reset(new MBC2(cartridgeROM, cartridgeRAM, romSize, ramSize));
                break;

            // MMM01
            // case 0x0B:
            // case 0x0C:
            // case 0x0D:
                // break;

            // MBC3
            case 0x0F:
            case 0x10:
            case 0x11:
            case 0x12:
            case 0x13:
                mbc.reset(new MBC3(cartridgeROM, cartridgeRAM, romSize, ramSize));
                break;

            // MBC5
            case 0x19:
            case 0x1A:
            case 0x1B:
            case 0x1C:
            case 0x1D:
            case 0x1E:
                mbc.reset(new MBC5(cartridgeROM, cartridgeRAM, romSize, ramSize));
                break;

            // MBC6
            // case 0x20:
            //     break;

            // MBC7
            // case 0x22:
            //     break;

            default:
                mbc.reset(new MBC0(cartridgeROM, cartridgeRAM, ramSize));
                break;
        }
    }
}

// Assembly:
subq $0x318, %rsp            # imm = 0x318
movq %rdi, 0x310(%rsp)
movq %rsi, 0x308(%rsp)
movq 0x310(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x100(%rsp), %rdi
movq %rdi, 0xb8(%rsp)
movl $0x4, %edx
callq 0x3370
movq 0xb8(%rsp), %rdi
callq 0x3360
movb %al, 0xc3(%rsp)
jmp 0x3717
movb 0xc3(%rsp), %al
testb $0x1, %al
jne 0x3759
jmp 0x3724
movq 0xb0(%rsp), %rax
movb $0x0, 0x20(%rax)
movl $0x1, 0xf0(%rsp)
jmp 0x3e06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
jmp 0x3e1b
xorl %eax, %eax
movl %eax, %esi
leaq 0x100(%rsp), %rdi
movl $0x2, %edx
callq 0x3310
jmp 0x3771
leaq 0x100(%rsp), %rdi
callq 0x33b0
movq %rdx, 0xa0(%rsp)
movq %rax, 0xa8(%rsp)
jmp 0x3790
movq 0xa0(%rsp), %rax
movq 0xa8(%rsp), %rcx
movq %rcx, 0xe0(%rsp)
movq %rax, 0xe8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x4020
movq %rax, 0x98(%rsp)
jmp 0x37c7
movq 0xb0(%rsp), %rax
movq 0x98(%rsp), %rcx
movq %rcx, 0x68(%rax)
xorl %edx, %edx
movl %edx, %esi
leaq 0x100(%rsp), %rdi
callq 0x3310
jmp 0x37ee
movq 0x100(%rsp), %rax
movq -0x18(%rax), %rax
leaq 0x100(%rsp,%rax), %rdi
xorl %esi, %esi
callq 0x33a0
jmp 0x380b
movq 0xb0(%rsp), %rax
movq 0x68(%rax), %rax
andq $0x7fff, %rax           # imm = 0x7FFF
cmpq $0x0, %rax
je 0x384e
movq 0xb0(%rsp), %rax
movb $0x0, 0x20(%rax)
leaq 0x100(%rsp), %rdi
callq 0x30a0
jmp 0x383e
movl $0x1, 0xf0(%rsp)
jmp 0x3e06
movq 0xb0(%rsp), %rax
movq %rax, %rdi
addq $0x30, %rdi
movq 0x68(%rax), %rsi
callq 0x4030
jmp 0x3868
movq 0xb0(%rsp), %rdi
addq $0x30, %rdi
callq 0x40d0
movq %rax, %rsi
movq 0xb0(%rsp), %rax
movq 0x68(%rax), %rdx
leaq 0x100(%rsp), %rdi
callq 0x3300
jmp 0x3897
leaq 0x100(%rsp), %rdi
callq 0x30a0
jmp 0x38a6
movq 0xb0(%rsp), %rdi
movb $0x1, 0x20(%rdi)
addq $0x30, %rdi
movq %rdi, 0x88(%rsp)
movl $0x147, %esi            # imm = 0x147
callq 0x40f0
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movb (%rcx), %cl
movb %cl, 0x60(%rax)
movl $0x149, %esi            # imm = 0x149
callq 0x40f0
movzbl (%rax), %eax
movq %rax, 0x90(%rsp)
subq $0x5, %rax
ja 0x3979
movq 0x90(%rsp), %rax
leaq 0x126fa(%rip), %rcx     # 0x16004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xb0(%rsp), %rax
movl $0x1, 0x70(%rax)
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x800, 0x70(%rax)      # imm = 0x800
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x2000, 0x70(%rax)     # imm = 0x2000
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x8000, 0x70(%rax)     # imm = 0x8000
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x20000, 0x70(%rax)    # imm = 0x20000
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x10000, 0x70(%rax)    # imm = 0x10000
jmp 0x3988
movq 0xb0(%rsp), %rax
movl $0x1, 0x70(%rax)
movq 0xb0(%rsp), %rax
movzbl 0x60(%rax), %eax
cmpl $0x5, %eax
je 0x39aa
movq 0xb0(%rsp), %rax
movzbl 0x60(%rax), %eax
cmpl $0x6, %eax
jne 0x39b9
movq 0xb0(%rsp), %rax
movl $0x200, 0x70(%rax)      # imm = 0x200
movq 0xb0(%rsp), %rax
movq %rax, %rdi
addq $0x48, %rdi
movslq 0x70(%rax), %rsi
callq 0x4030
jmp 0x39d3
movq 0xb0(%rsp), %rdi
addq $0x48, %rdi
callq 0x4170
movq %rax, %rcx
movq 0xb0(%rsp), %rax
movq %rcx, 0xd8(%rsp)
movl 0x70(%rax), %esi
movl $0x0, 0xd4(%rsp)
movq 0xd8(%rsp), %rdi
leaq 0xd4(%rsp), %rdx
callq 0x4110
movq %rax, 0x80(%rsp)
jmp 0x3a24
movq 0xb0(%rsp), %rdi
movq 0x80(%rsp), %rax
movq %rax, 0xc8(%rsp)
addq $0x30, %rdi
movq %rdi, 0x68(%rsp)
movl $0x143, %esi            # imm = 0x143
callq 0x40f0
movq 0x68(%rsp), %rdi
movb (%rax), %al
shrb $0x7, %al
movzbl %al, %ecx
movl $0x10, %eax
subl %ecx, %eax
movl %eax, 0xc4(%rsp)
movl $0x134, %esi            # imm = 0x134
movq %rsi, 0x70(%rsp)
callq 0x40f0
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq %rax, 0x78(%rsp)
callq 0x40f0
movq 0xb0(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq %rax, %rdx
movslq 0xc4(%rsp), %rax
addq %rax, %rdx
callq 0x41a0
jmp 0x3aaf
movq 0xb0(%rsp), %rax
movzbl 0x60(%rax), %eax
movq %rax, 0x60(%rsp)
subq $0x1e, %rax
ja 0x3d7e
movq 0x60(%rsp), %rax
leaq 0x12546(%rip), %rcx     # 0x1601c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x50(%rsp)
movl $0x20, %edi
callq 0x3270
movq %rax, 0x58(%rsp)
jmp 0x3b01
movq 0x58(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x70(%rax), %ecx
callq 0x4250
jmp 0x3b26
movq 0x58(%rsp), %rsi
movq 0x50(%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x20, %esi
callq 0x3280
jmp 0x3e1b
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x40(%rsp)
movl $0x30, %edi
callq 0x3270
movq %rax, 0x48(%rsp)
jmp 0x3b84
movq 0x48(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x68(%rax), %ecx
movl 0x70(%rax), %r8d
callq 0x42b0
jmp 0x3bad
movq 0x48(%rsp), %rsi
movq 0x40(%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x30, %esi
callq 0x3280
jmp 0x3e1b
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x30(%rsp)
movl $0x28, %edi
callq 0x3270
movq %rax, 0x38(%rsp)
jmp 0x3c0b
movq 0x38(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x68(%rax), %ecx
movl 0x70(%rax), %r8d
callq 0x4330
jmp 0x3c34
movq 0x38(%rsp), %rsi
movq 0x30(%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x28, %esi
callq 0x3280
jmp 0x3e1b
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x20(%rsp)
movl $0x30, %edi
callq 0x3270
movq %rax, 0x28(%rsp)
jmp 0x3c92
movq 0x28(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x68(%rax), %ecx
movl 0x70(%rax), %r8d
callq 0x43a0
jmp 0x3cbb
movq 0x28(%rsp), %rsi
movq 0x20(%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x28(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x30, %esi
callq 0x3280
jmp 0x3e1b
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, 0x10(%rsp)
movl $0x28, %edi
callq 0x3270
movq %rax, 0x18(%rsp)
jmp 0x3d19
movq 0x18(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x68(%rax), %ecx
movl 0x70(%rax), %r8d
callq 0x4440
jmp 0x3d42
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x28, %esi
callq 0x3280
jmp 0x3e1b
movq 0xb0(%rsp), %rax
addq $0x28, %rax
movq %rax, (%rsp)
movl $0x20, %edi
callq 0x3270
movq %rax, 0x8(%rsp)
jmp 0x3d9f
movq 0x8(%rsp), %rdi
movq 0xb0(%rsp), %rax
movq %rax, %rsi
addq $0x30, %rsi
movq %rax, %rdx
addq $0x48, %rdx
movl 0x70(%rax), %ecx
callq 0x4250
jmp 0x3dc4
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
callq 0x4220
jmp 0x3df9
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xf8(%rsp)
movl %eax, 0xf4(%rsp)
movl $0x20, %esi
callq 0x3280
jmp 0x3e1b
jmp 0x3dfb
movl $0x0, 0xf0(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x30b0
addq $0x318, %rsp            # imm = 0x318
retq
leaq 0x100(%rsp), %rdi
callq 0x30b0
movq 0xf8(%rsp), %rdi
callq 0x33d0
nopw %cs:(%rax,%rax)