// File: StanHash[P]nesdump/nes.cc
// Function: NesRom::from_ines_file(char const*)
NesRom NesRom::from_ines_file(char const* filename)
{
    static constexpr std::size_t INES_PRG_BANK_SIZE = KIBI*16;
    static constexpr std::size_t INES_CHR_BANK_SIZE = KIBI*8;

    NesRom result {};

    std::ifstream f(filename, std::ios::in | std::ios::binary);

    if (!f.is_open())
        throw std::runtime_error("Couldn't open file for binary read");

    std::array<byte_type, 16> ines_header {};

    if (!f.read(reinterpret_cast<char*>(ines_header.data()), ines_header.size()))
        throw std::runtime_error("Couldn't read iNES header");

    if (ines_header[0] != 'N' || ines_header[1] != 'E' || ines_header[2] != 'S' || ines_header[3] != '\x1A')
        throw std::runtime_error("file doesn't contain valid iNES header (bad magic)");

    std::size_t const prg_banks = ines_header[4];
    result.m_prg.resize(prg_banks*INES_PRG_BANK_SIZE);

    if (!f.read(reinterpret_cast<char*>(result.m_prg.data()), result.m_prg.size()))
        throw std::runtime_error("Couldn't read PRG-ROM (reached EOF early)");

    std::size_t const chr_banks = ines_header[5];
    result.m_chr.resize(chr_banks*INES_CHR_BANK_SIZE);

    if (!f.read(reinterpret_cast<char*>(result.m_chr.data()), result.m_chr.size()))
        throw std::runtime_error("Couldn't read CHR-ROM (reached EOF early)");

    result.m_mapper_id = ((ines_header[6] & 0xF0) >> 4) | (ines_header[7] & 0xF0);
    result.m_ines_flags = (ines_header[6] & 0x0F) | ((ines_header[7] & 0x0F) << 4);

    return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x300, %rsp            # imm = 0x300
movq %rdi, -0x270(%rbp)
movq %rdi, %rax
movq %rax, -0x268(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movups %xmm0, (%rdi)
movq $0x0, 0x30(%rdi)
callq 0x5310
movq -0x10(%rbp), %rax
movq %rax, -0x260(%rbp)
movl $0x8, %edi
movl $0x4, %esi
callq 0x5370
movl %eax, -0x254(%rbp)
jmp 0x4cb5
movl -0x254(%rbp), %edx
movq -0x260(%rbp), %rsi
leaq -0x220(%rbp), %rdi
callq 0x22d0
jmp 0x4cd0
leaq -0x220(%rbp), %rdi
callq 0x2270
movb %al, -0x271(%rbp)
jmp 0x4ce4
movb -0x271(%rbp), %al
testb $0x1, %al
jne 0x4d89
jmp 0x4cf4
movl $0x10, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x280(%rbp)
leaq 0x14ee(%rip), %rsi      # 0x6200
callq 0x20a0
jmp 0x4d19
movq -0x280(%rbp), %rdi
movq 0x52c9(%rip), %rsi      # 0x9ff0
movq 0x5282(%rip), %rdx      # 0x9fb0
callq 0x22a0
jmp 0x523f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x5227
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
jmp 0x51fd
movq -0x280(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x2160
jmp 0x51fd
xorps %xmm0, %xmm0
movaps %xmm0, -0x240(%rbp)
leaq -0x240(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x5390
movq -0x298(%rbp), %rdi
movq %rax, -0x290(%rbp)
callq 0x53b0
movq -0x290(%rbp), %rsi
movq %rax, %rdx
leaq -0x220(%rbp), %rdi
callq 0x21f0
movq %rax, -0x288(%rbp)
jmp 0x4dd8
movq -0x288(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2100
movb %al, -0x299(%rbp)
jmp 0x4df6
movb -0x299(%rbp), %al
testb $0x1, %al
jne 0x4e02
jmp 0x4e69
movl $0x10, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2a8(%rbp)
leaq 0x1403(%rip), %rsi      # 0x6223
callq 0x20a0
jmp 0x4e27
movq -0x2a8(%rbp), %rdi
movq 0x51bb(%rip), %rsi      # 0x9ff0
movq 0x5174(%rip), %rdx      # 0x9fb0
callq 0x22a0
jmp 0x523f
movq -0x2a8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x2160
jmp 0x51fd
leaq -0x240(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x53c0
movzbl (%rax), %eax
cmpl $0x4e, %eax
jne 0x4ecc
leaq -0x240(%rbp), %rdi
movl $0x1, %esi
callq 0x53c0
movzbl (%rax), %eax
cmpl $0x45, %eax
jne 0x4ecc
leaq -0x240(%rbp), %rdi
movl $0x2, %esi
callq 0x53c0
movzbl (%rax), %eax
cmpl $0x53, %eax
jne 0x4ecc
leaq -0x240(%rbp), %rdi
movl $0x3, %esi
callq 0x53c0
movzbl (%rax), %eax
cmpl $0x1a, %eax
je 0x4f33
movl $0x10, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2b0(%rbp)
leaq 0x1353(%rip), %rsi      # 0x623d
callq 0x20a0
jmp 0x4ef1
movq -0x2b0(%rbp), %rdi
movq 0x50f1(%rip), %rsi      # 0x9ff0
movq 0x50aa(%rip), %rdx      # 0x9fb0
callq 0x22a0
jmp 0x523f
movq -0x2b0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x2160
jmp 0x51fd
leaq -0x240(%rbp), %rdi
movl $0x4, %esi
callq 0x53c0
movq -0x270(%rbp), %rdi
movzbl (%rax), %eax
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rsi
shlq $0xe, %rsi
callq 0x53f0
jmp 0x4f67
movq -0x270(%rbp), %rdi
callq 0x5480
movq -0x270(%rbp), %rdi
movq %rax, -0x2c0(%rbp)
callq 0x45c0
movq -0x2c0(%rbp), %rsi
movq %rax, %rdx
leaq -0x220(%rbp), %rdi
callq 0x21f0
movq %rax, -0x2b8(%rbp)
jmp 0x4fa5
movq -0x2b8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2100
movb %al, -0x2c1(%rbp)
jmp 0x4fc3
movb -0x2c1(%rbp), %al
testb $0x1, %al
jne 0x4fcf
jmp 0x5036
movl $0x10, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2d0(%rbp)
leaq 0x1283(%rip), %rsi      # 0x6270
callq 0x20a0
jmp 0x4ff4
movq -0x2d0(%rbp), %rdi
movq 0x4fee(%rip), %rsi      # 0x9ff0
movq 0x4fa7(%rip), %rdx      # 0x9fb0
callq 0x22a0
jmp 0x523f
movq -0x2d0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x2160
jmp 0x51fd
leaq -0x240(%rbp), %rdi
movl $0x5, %esi
callq 0x53c0
movq -0x270(%rbp), %rdi
movzbl (%rax), %eax
movq %rax, -0x250(%rbp)
addq $0x18, %rdi
movq -0x250(%rbp), %rsi
shlq $0xd, %rsi
callq 0x53f0
jmp 0x506e
movq -0x270(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0x2e8(%rbp)
callq 0x5480
movq -0x2e8(%rbp), %rdi
movq %rax, -0x2e0(%rbp)
callq 0x45c0
movq -0x2e0(%rbp), %rsi
movq %rax, %rdx
leaq -0x220(%rbp), %rdi
callq 0x21f0
movq %rax, -0x2d8(%rbp)
jmp 0x50b7
movq -0x2d8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x2100
movb %al, -0x2e9(%rbp)
jmp 0x50d5
movb -0x2e9(%rbp), %al
testb $0x1, %al
jne 0x50e1
jmp 0x5148
movl $0x10, %edi
callq 0x2110
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2f8(%rbp)
leaq 0x119b(%rip), %rsi      # 0x629a
callq 0x20a0
jmp 0x5106
movq -0x2f8(%rbp), %rdi
movq 0x4edc(%rip), %rsi      # 0x9ff0
movq 0x4e95(%rip), %rdx      # 0x9fb0
callq 0x22a0
jmp 0x523f
movq -0x2f8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
callq 0x2160
jmp 0x51fd
leaq -0x240(%rbp), %rdi
movl $0x6, %esi
callq 0x53c0
movzbl (%rax), %eax
andl $0xf0, %eax
sarl $0x4, %eax
movl %eax, -0x300(%rbp)
leaq -0x240(%rbp), %rdi
movl $0x7, %esi
callq 0x53c0
movl -0x300(%rbp), %ecx
movq %rax, %rdx
movq -0x270(%rbp), %rax
movzbl (%rdx), %edx
andl $0xf0, %edx
orl %edx, %ecx
movb %cl, 0x30(%rax)
leaq -0x240(%rbp), %rdi
movl $0x6, %esi
callq 0x53c0
movzbl (%rax), %eax
andl $0xf, %eax
movl %eax, -0x2fc(%rbp)
leaq -0x240(%rbp), %rdi
movl $0x7, %esi
callq 0x53c0
movl -0x2fc(%rbp), %ecx
movq %rax, %rdx
movq -0x270(%rbp), %rax
movzbl (%rdx), %edx
andl $0xf, %edx
shll $0x4, %edx
orl %edx, %ecx
movb %cl, 0x31(%rax)
movb $0x1, -0x11(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x2050
testb $0x1, -0x11(%rbp)
jne 0x5217
jmp 0x520b
leaq -0x220(%rbp), %rdi
callq 0x2050
jmp 0x5227
movq -0x270(%rbp), %rdi
callq 0x35a0
movq -0x268(%rbp), %rax
addq $0x300, %rsp            # imm = 0x300
popq %rbp
retq
movq -0x270(%rbp), %rdi
callq 0x35a0
movq -0x228(%rbp), %rdi
callq 0x22b0
nop