// File: donfreiday[P]gb/mmu.cpp
// Function: MMU::load(char*)
bool MMU::load(char *filename) {
  romFilename = filename;

  std::ifstream file;
  file.open(filename, std::ios::binary);
  if (!file.is_open()) {
    return false;
  }

  // Get file size and read ROM into vector
  file.seekg(0, file.end);
  u32 romSize = file.tellg();
  rom.resize(romSize, 0);
  file.seekg(0, file.beg);
  file.read((char *)(&rom[0]), romSize);
  file.close();

  // Map ROM to memory
  memory.assign(rom.begin(),
                rom.begin() + (0x7FFF > rom.size() ? rom.size() : 0x7FFF));

  // Get MBC type from ROM header
  mbc.type = rom[0x147];

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x18(%rbp), %rcx
movq %rcx, 0x68(%rax)
leaq -0x220(%rbp), %rdi
movq %rdi, -0x270(%rbp)
callq 0x72f0
movq -0x270(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x7580
jmp 0x4eaa0
leaq -0x220(%rbp), %rdi
callq 0x7710
movb %al, -0x279(%rbp)
jmp 0x4eab4
movb -0x279(%rbp), %al
testb $0x1, %al
jne 0x4eaf6
jmp 0x4eac0
movb $0x0, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
jmp 0x4ecc3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x7040
jmp 0x4ecdd
xorl %eax, %eax
movl %eax, %esi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x7290
jmp 0x4eb0d
leaq -0x220(%rbp), %rdi
callq 0x7120
movq %rdx, -0x290(%rbp)
movq %rax, -0x288(%rbp)
jmp 0x4eb29
movq -0x290(%rbp), %rax
movq -0x288(%rbp), %rcx
movq %rcx, -0x248(%rbp)
movq %rax, -0x240(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x4f3b0
movq %rax, -0x298(%rbp)
jmp 0x4eb5a
movq -0x278(%rbp), %rdi
movq -0x298(%rbp), %rax
movl %eax, -0x234(%rbp)
addq $0x38, %rdi
movl -0x234(%rbp), %eax
movl %eax, %esi
movb $0x0, -0x249(%rbp)
leaq -0x249(%rbp), %rdx
callq 0x4f300
jmp 0x4eb8f
xorl %edx, %edx
movl %edx, %esi
leaq -0x220(%rbp), %rdi
callq 0x7290
jmp 0x4eba1
movq -0x278(%rbp), %rdi
addq $0x38, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x4f3d0
movq %rax, %rsi
movl -0x234(%rbp), %eax
movl %eax, %edx
leaq -0x220(%rbp), %rdi
callq 0x73c0
jmp 0x4ebce
leaq -0x220(%rbp), %rdi
callq 0x74e0
jmp 0x4ebdc
movq -0x278(%rbp), %rdi
movq %rdi, %rax
addq $0x8, %rax
movq %rax, -0x2a0(%rbp)
addq $0x38, %rdi
callq 0x4f430
movq -0x278(%rbp), %rdi
movq %rax, -0x258(%rbp)
addq $0x38, %rdi
callq 0x4f430
movq -0x278(%rbp), %rdi
movq %rax, -0x268(%rbp)
addq $0x38, %rdi
callq 0x4f4a0
movq %rax, %rcx
movl $0x7fff, %eax           # imm = 0x7FFF
cmpq %rcx, %rax
jbe 0x4ec4e
movq -0x278(%rbp), %rdi
addq $0x38, %rdi
callq 0x4f4a0
movq %rax, -0x2a8(%rbp)
jmp 0x4ec5c
movl $0x7fff, %eax           # imm = 0x7FFF
movq %rax, -0x2a8(%rbp)
jmp 0x4ec5c
movq -0x2a8(%rbp), %rsi
leaq -0x268(%rbp), %rdi
callq 0x4f460
movq -0x2a0(%rbp), %rdi
movq %rax, -0x260(%rbp)
movq -0x258(%rbp), %rsi
movq -0x260(%rbp), %rdx
callq 0x4f3f0
jmp 0x4ec92
movq -0x278(%rbp), %rdi
addq $0x38, %rdi
movl $0x147, %esi            # imm = 0x147
callq 0x4f3d0
movq %rax, %rcx
movq -0x278(%rbp), %rax
movb (%rcx), %cl
movb %cl, (%rax)
movb $0x1, -0x1(%rbp)
movl $0x1, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x7040
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
movq -0x228(%rbp), %rdi
callq 0x74f0
nopl (%rax)