// File: klyushinmisha[P]GBEmu/./src/Memory.cpp
// Function: Memory::LoadCartridge(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Memory::LoadCartridge(std::string path)
{
	std::fstream is;
    is.open(path, std::fstream::in);

    long int length = is.tellg();
    is.seekg(0, is.end);
    length = (long int)is.tellg() - length;
    is.seekg(0, is.beg);

    try
    {
        if (length % 0x8000 != 0)
            throw new std::exception();
        is.read((char*)RAM, 0x8000);
        
        if (length == 0x8000)
        {
            is.close();
            return;
        }

        cartridgeData = new byte*[length % 0x8000 - 1];

        // проверить??? 0x8000?
        for (int i = 0; i < length % 0x8000 - 1; i++)
        {
            cartridgeData[i] = new byte[0x8000];
            is.read((char*)cartridgeData[i], 0x8000);
        }
    }
    catch (std::exception e)
    {
        std::cout << "Incorrect format" << std::endl;
    }

    
    is.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2f0, %rsp            # imm = 0x2F0
movq %rsi, -0x270(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x280(%rbp)
leaq -0x220(%rbp), %rdi
movq %rdi, -0x278(%rbp)
callq 0x22d0
movq -0x278(%rbp), %rdi
movq -0x270(%rbp), %rsi
movl $0x8, %edx
callq 0x2330
jmp 0x8c62
leaq -0x220(%rbp), %rdi
callq 0x23a0
movq %rdx, -0x290(%rbp)
movq %rax, -0x288(%rbp)
jmp 0x8c7e
movq -0x290(%rbp), %rax
movq -0x288(%rbp), %rcx
movq %rcx, -0x248(%rbp)
movq %rax, -0x240(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x9650
movq %rax, -0x298(%rbp)
jmp 0x8caf
movq -0x298(%rbp), %rax
movq %rax, -0x238(%rbp)
xorl %eax, %eax
movl %eax, %esi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x2320
jmp 0x8cd4
leaq -0x220(%rbp), %rdi
callq 0x23a0
movq %rdx, -0x2a8(%rbp)
movq %rax, -0x2a0(%rbp)
jmp 0x8cf0
movq -0x2a8(%rbp), %rax
movq -0x2a0(%rbp), %rcx
movq %rcx, -0x258(%rbp)
movq %rax, -0x250(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x9650
movq %rax, -0x2b0(%rbp)
jmp 0x8d21
movq -0x2b0(%rbp), %rax
movq -0x238(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x238(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x220(%rbp), %rdi
callq 0x2320
jmp 0x8d4b
movq -0x238(%rbp), %rax
movl $0x8000, %ecx           # imm = 0x8000
cqto
idivq %rcx
cmpq $0x0, %rdx
je 0x8ec2
movl $0x8, %edi
movq %rdi, -0x2c8(%rbp)
callq 0x2120
movq -0x2c8(%rbp), %rdi
movq %rax, -0x2c0(%rbp)
callq 0x2250
movq %rax, -0x2b8(%rbp)
jmp 0x8d93
movq -0x2b8(%rbp), %rdi
callq 0x8620
movq -0x2b8(%rbp), %rax
movq -0x2c0(%rbp), %rdi
movq %rax, (%rdi)
leaq 0x5fc1(%rip), %rsi      # 0xed78
xorl %eax, %eax
movl %eax, %edx
callq 0x2390
jmp 0x9051
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x9031
movq -0x2c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x21a0
jmp 0x8e0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
movl -0x22c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x9031
movq -0x228(%rbp), %rdi
movq %rdi, -0x2d8(%rbp)
callq 0x22f0
movq %rax, %rsi
leaq -0x268(%rbp), %rdi
callq 0x9670
movq -0x2d8(%rbp), %rdi
callq 0x20e0
movq 0x6182(%rip), %rdi      # 0xefd8
leaq 0x2add(%rip), %rsi      # 0xb93a
callq 0x2240
movq %rax, -0x2d0(%rbp)
jmp 0x8e6b
movq -0x2d0(%rbp), %rdi
movq 0x6147(%rip), %rsi      # 0xefc0
callq 0x2270
jmp 0x8e80
leaq -0x268(%rbp), %rdi
callq 0x23f0
callq 0x2380
jmp 0x8e93
jmp 0x8e95
leaq -0x220(%rbp), %rdi
callq 0x2070
jmp 0x8ea3
movl $0x0, -0x25c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x20b0
addq $0x2f0, %rsp            # imm = 0x2F0
popq %rbp
retq
movq -0x280(%rbp), %rax
movq 0x28(%rax), %rsi
leaq -0x220(%rbp), %rdi
movl $0x8000, %edx           # imm = 0x8000
callq 0x2300
jmp 0x8ee0
cmpq $0x8000, -0x238(%rbp) # imm = 0x8000
jne 0x8f07
leaq -0x220(%rbp), %rdi
callq 0x2070
jmp 0x8efb
movl $0x1, -0x25c(%rbp)
jmp 0x8ead
movq -0x238(%rbp), %rcx
movq %rcx, %rdx
addq $0x7fff, %rdx           # imm = 0x7FFF
testq %rcx, %rcx
movq %rcx, %rax
cmovsq %rdx, %rax
notq %rax
orq $0x7fff, %rax           # imm = 0x7FFF
addq %rcx, %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2040
movq %rax, -0x2e0(%rbp)
jmp 0x8f52
movq -0x280(%rbp), %rax
movq -0x2e0(%rbp), %rcx
movq %rcx, 0x20(%rax)
movl $0x0, -0x260(%rbp)
movslq -0x260(%rbp), %rax
movq %rax, -0x2e8(%rbp)
movq -0x238(%rbp), %rax
movl $0x8000, %ecx           # imm = 0x8000
cqto
idivq %rcx
movq -0x2e8(%rbp), %rax
subq $0x1, %rdx
cmpq %rdx, %rax
jge 0x9005
movl $0x8000, %edi           # imm = 0x8000
callq 0x2040
movq %rax, -0x2f0(%rbp)
jmp 0x8fb0
movq -0x280(%rbp), %rax
movq -0x2f0(%rbp), %rsi
movq 0x20(%rax), %rcx
movslq -0x260(%rbp), %rdx
movq %rsi, (%rcx,%rdx,8)
movq 0x20(%rax), %rax
movslq -0x260(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
leaq -0x220(%rbp), %rdi
movl $0x8000, %edx           # imm = 0x8000
callq 0x2300
jmp 0x8fef
jmp 0x8ff1
movl -0x260(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x260(%rbp)
jmp 0x8f6e
jmp 0x8e95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x23f0
callq 0x2380
jmp 0x902f
jmp 0x9031
leaq -0x220(%rbp), %rdi
callq 0x20b0
movq -0x228(%rbp), %rdi
callq 0x23b0
movq %rax, %rdi
callq 0x3a20
nopw %cs:(%rax,%rax)