// File: bloody76[P]Chip8-Disassembler/src/disassembler.cc
// Function: parse(std::basic_ifstream<char, std::char_traits<char>>&)
void parse(std::ifstream& rom)
{
    uint8   op1 = 0;
    uint8   op2 = 0;
    uint16  op  = 0;

    size = rom.tellg();
    mem = new uint8[size];
    rom.seekg(0, std::ios::beg);
    rom.read((char*)mem, size);
    rom.close();

    for (int i = 0; i < size; i += 2)
    {
        op1 = mem[i];
        if (i + 1 < size)
            op2 = mem[i + 1];
        else
        {
            std::cerr << "Invalid opcode, exiting." << std::endl;
            exit(1);
        }

        op = (op1 << 8) | op2;
        switch ((op & 0xF000) >> 12)
        {
            case 1:
                functions[op & 0x0FFF] = Type::ADR;
                break;
            case 2:
                functions[op & 0x0FFF] = Type::SUB;
                break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movb $0x0, -0x9(%rbp)
movb $0x0, -0xa(%rbp)
movw $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x22b0
movq %rax, -0x20(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x20(%rbp), %rax
movq %rax, 0x5d0a(%rip)      # 0x81e0
movq -0x18(%rbp), %rax
movq %rax, 0x5d07(%rip)      # 0x81e8
leaq 0x5cf8(%rip), %rdi      # 0x81e0
callq 0x3400
movq %rax, %rdi
callq 0x2030
movq %rax, 0x5cdc(%rip)      # 0x81d8
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x2230
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x5cbe(%rip), %rax      # 0x81d8
movq %rax, -0x30(%rbp)
leaq 0x5cbb(%rip), %rdi      # 0x81e0
callq 0x3400
movq -0x38(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x2210
movq -0x8(%rbp), %rdi
callq 0x2060
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq 0x5c87(%rip), %rdi      # 0x81e0
callq 0x3400
movq %rax, %rcx
movq -0x40(%rbp), %rax
cmpq %rcx, %rax
jge 0x266b
movq 0x5c63(%rip), %rax      # 0x81d8
movslq -0x24(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x9(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
cltq
movq %rax, -0x48(%rbp)
leaq 0x5c4e(%rip), %rdi      # 0x81e0
callq 0x3400
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
jge 0x25bb
movq 0x5c2e(%rip), %rax      # 0x81d8
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0xa(%rbp)
jmp 0x25e7
movq 0x5a2e(%rip), %rdi      # 0x7ff0
leaq 0x2ab7(%rip), %rsi      # 0x5080
callq 0x2160
movq %rax, %rdi
movq 0x59e8(%rip), %rsi      # 0x7fc0
callq 0x21a0
movl $0x1, %edi
callq 0x21f0
movzbl -0x9(%rbp), %eax
shll $0x8, %eax
movzbl -0xa(%rbp), %ecx
orl %ecx, %eax
movw %ax, -0xc(%rbp)
movzwl -0xc(%rbp), %eax
shrl $0xc, %eax
movl %eax, -0x4c(%rbp)
subl $0x1, %eax
je 0x2613
jmp 0x2609
movl -0x4c(%rbp), %eax
subl $0x2, %eax
je 0x2638
jmp 0x265b
movzwl -0xc(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, -0x26(%rbp)
leaq 0x5b81(%rip), %rdi      # 0x81a8
leaq -0x26(%rbp), %rsi
callq 0x3420
movl $0x1, (%rax)
jmp 0x265b
movzwl -0xc(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movw %ax, -0x28(%rbp)
leaq 0x5b5c(%rip), %rdi      # 0x81a8
leaq -0x28(%rbp), %rsi
callq 0x3420
movl $0x0, (%rax)
jmp 0x265d
movl -0x24(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x24(%rbp)
jmp 0x254a
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)