// File: nicoeps[P]NicoGB/src/memory.cpp
// Function: Memory::Memory(Cartridge&, Joypad&, Timer&)
Memory::Memory(Cartridge& cartridge, Joypad& joypad, Timer& timer)
    : cartridge(cartridge), joypad(joypad), timer(timer) {
    vram.reserve(0x2000);
    wram.reserve(0x2000);
    oam.reserve(0xA0);
    io.reserve(0x100);
    hram.reserve(0x7F);
    boot = {
        0x31, 0xFE, 0xFF, 0xAF, 0x21, 0xFF, 0x9F, 0x32, 0xCB, 0x7C, 0x20, 0xFB, 0x21, 0x26, 0xFF, 0x0E,
        0x11, 0x3E, 0x80, 0x32, 0xE2, 0x0C, 0x3E, 0xF3, 0xE2, 0x32, 0x3E, 0x77, 0x77, 0x3E, 0xFC, 0xE0,
        0x47, 0x11, 0xA8, 0x00, 0x21, 0x10, 0x80, 0x1A, 0xCD, 0x95, 0x00, 0xCD, 0x96, 0x00, 0x13, 0x7B,
        0xFE, 0xD8, 0x20, 0xF3, 0x11, 0xD8, 0x00, 0x06, 0x08, 0x1A, 0x13, 0x22, 0x23, 0x05, 0x20, 0xF9,
        0x3E, 0x19, 0xEA, 0x10, 0x99, 0x21, 0x2F, 0x99, 0x0E, 0x0C, 0x3D, 0x28, 0x08, 0x32, 0x0D, 0x20,
        0xF9, 0x2E, 0x0F, 0x18, 0xF3, 0x67, 0x3E, 0x64, 0x57, 0xE0, 0x42, 0x3E, 0x91, 0xE0, 0x40, 0x04,
        0x1E, 0x02, 0x0E, 0x0C, 0xF0, 0x44, 0xFE, 0x90, 0x20, 0xFA, 0x0D, 0x20, 0xF7, 0x1D, 0x20, 0xF2,
        0x0E, 0x13, 0x24, 0x7C, 0x1E, 0x83, 0xFE, 0x62, 0x28, 0x06, 0x1E, 0xC1, 0xFE, 0x64, 0x20, 0x06,
        0x7B, 0xE2, 0x0C, 0x3E, 0x87, 0xE2, 0xF0, 0x42, 0x90, 0xE0, 0x42, 0x15, 0x20, 0xD2, 0x05, 0x20,
        0x4F, 0x16, 0x20, 0x18, 0xCB, 0x4F, 0x06, 0x04, 0xC5, 0xCB, 0x11, 0x17, 0xC1, 0xCB, 0x11, 0x17,
        0x05, 0x20, 0xF5, 0x22, 0x23, 0x22, 0x23, 0xC9, 0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0C, 0x00, 0x0F,
        0x00, 0x01, 0x00, 0x0E, 0x36, 0x66, 0xC6, 0x60, 0xFC, 0xCF, 0x8D, 0xD9, 0xCE, 0xFF, 0x6F, 0xFF,
        0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDC, 0x98, 0x9F, 0xB3, 0xB1, 0x33, 0x3E, 0x66, 0x63, 0xEE, 0x6E,
        0xCC, 0xCF, 0xCC, 0xC8, 0xF7, 0x31, 0xEC, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x21, 0x04, 0x01, 0x11, 0xA8, 0x00, 0x1A, 0x13, 0xBE, 0x00, 0x00, 0x23, 0x7D, 0xFE, 0x34, 0x20,
        0xF5, 0x06, 0x19, 0x78, 0x86, 0x23, 0x05, 0x20, 0xFB, 0x86, 0x20, 0x01, 0x3E, 0x01, 0xE0, 0x50
    };
    init();
}

// Assembly:
subq $0x188, %rsp            # imm = 0x188
movq %rdi, 0x180(%rsp)
movq %rsi, 0x178(%rsp)
movq %rdx, 0x170(%rsp)
movq %rcx, 0x168(%rsp)
movq 0x180(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movq 0x178(%rsp), %rax
movq %rax, (%rdi)
movq 0x170(%rsp), %rax
movq %rax, 0x8(%rdi)
movq 0x168(%rsp), %rax
movq %rax, 0x10(%rdi)
addq $0x28, %rdi
movq %rdi, 0x40(%rsp)
callq 0x3f00
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
movq %rdi, 0x10(%rsp)
callq 0x3f00
movq 0x30(%rsp), %rdi
addq $0x58, %rdi
movq %rdi, 0x18(%rsp)
callq 0x3f00
movq 0x30(%rsp), %rdi
addq $0x70, %rdi
movq %rdi, 0x20(%rsp)
callq 0x3f00
movq 0x30(%rsp), %rdi
addq $0x88, %rdi
movq %rdi, 0x28(%rsp)
callq 0x3f00
movq 0x30(%rsp), %rdi
addq $0xa0, %rdi
movq %rdi, 0x38(%rsp)
callq 0x3f00
movq 0x40(%rsp), %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xf510
jmp 0xe56b
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
movl $0x2000, %esi           # imm = 0x2000
callq 0xf510
jmp 0xe580
movq 0x30(%rsp), %rdi
addq $0x58, %rdi
movl $0xa0, %esi
callq 0xf510
jmp 0xe595
movq 0x30(%rsp), %rdi
addq $0x70, %rdi
movl $0x100, %esi            # imm = 0x100
callq 0xf510
jmp 0xe5aa
movq 0x30(%rsp), %rdi
addq $0x88, %rdi
movl $0x7f, %esi
callq 0xf510
jmp 0xe5c2
leaq 0x838a(%rip), %rsi      # 0x16953
leaq 0x48(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x100, %edx            # imm = 0x100
callq 0x3220
movq 0x30(%rsp), %rdi
movq 0x8(%rsp), %rax
movq %rax, 0x148(%rsp)
movq $0x100, 0x150(%rsp) # imm = 0x100
addq $0xa0, %rdi
movq 0x148(%rsp), %rsi
movq 0x150(%rsp), %rdx
callq 0xf610
jmp 0xe619
movq 0x30(%rsp), %rdi
callq 0xe690
jmp 0xe625
addq $0x188, %rsp            # imm = 0x188
retq
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
callq 0x3f50
movq 0x28(%rsp), %rdi
callq 0x3f50
movq 0x20(%rsp), %rdi
callq 0x3f50
movq 0x18(%rsp), %rdi
callq 0x3f50
movq 0x10(%rsp), %rdi
callq 0x3f50
movq 0x40(%rsp), %rdi
callq 0x3f50
movq 0x160(%rsp), %rdi
callq 0x33d0
nopw (%rax,%rax)