// File: rfotino[P]consolite-emulator/src/processor.cpp
// Function: EmuProcessor::EmuProcessor(EmuWindow*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
EmuProcessor::EmuProcessor(EmuWindow *window,
                           const std::string& infile_name)
                           : _window(window),
                             _error(false),
                             _running(true) {
  // Open up the input file
  std::ifstream input(infile_name, std::ifstream::binary);
  if (!input.good()) {
    _error = true;
    std::cerr << "Error: Failed to read input file '"
              << infile_name << "'." << std::endl;
    return;
  }

  // Make sure the file meets the size requirements
  input.seekg(0, input.end);
  int file_size = input.tellg();
  input.seekg(0, input.beg);
  if (0 == file_size) {
    _error = true;
    std::cerr << "Error: Empty input file '" << infile_name
              << "'." << std::endl;
    return;
  } else if (MAIN_MEMORY_SIZE < file_size) {
    _error = true;
    std::cerr << "Error: File size of " << file_size << " bytes is "
              << "larger than main memory. Input file has a max size of "
              << MAIN_MEMORY_SIZE << " bytes." << std::endl;
    return;
  }

  // Read file contents into main memory
  memset(_mainMem, 0, sizeof(_mainMem));
  char c;
  uint16_t pos = 0;
  while (input.get(c)) {
    _mainMem[pos] = c;
    pos++;
  }

  // Initialize registers and other data
  memset(_registers, 0, sizeof(_registers));
  _instructionPointer = 0;
  _colorRegister = 0;
  _overflowFlag = false;
  _carryFlag = false;
  _zeroFlag = false;
  _signFlag = false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2e0, %rsp            # imm = 0x2E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x258(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, (%rax)
movb $0x0, 0x1002f(%rax)
movb $0x1, 0x10030(%rax)
movq -0x18(%rbp), %rsi
leaq -0x220(%rbp), %rdi
movl $0x4, %edx
callq 0x3480
movq -0x220(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x220(%rbp,%rax), %rdi
callq 0x34c0
movb %al, -0x24d(%rbp)
jmp 0x85fc
movb -0x24d(%rbp), %al
testb $0x1, %al
jne 0x86b2
jmp 0x860c
movq -0x258(%rbp), %rax
movb $0x1, 0x1002f(%rax)
movq 0x69cf(%rip), %rdi      # 0xeff0
leaq 0x1d08(%rip), %rsi      # 0xa330
callq 0x32c0
movq %rax, -0x260(%rbp)
jmp 0x8636
movq -0x260(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3260
movq %rax, -0x268(%rbp)
jmp 0x864f
movq -0x268(%rbp), %rdi
leaq 0x1ba0(%rip), %rsi      # 0xa1fd
callq 0x32c0
movq %rax, -0x270(%rbp)
jmp 0x866b
movq -0x270(%rbp), %rdi
movq 0x6947(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x8680
movl $0x1, -0x230(%rbp)
jmp 0x8999
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x228(%rbp)
movl %eax, -0x22c(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3070
jmp 0x89ae
xorl %eax, %eax
movl %eax, %esi
leaq -0x220(%rbp), %rdi
movl $0x2, %edx
callq 0x33c0
jmp 0x86c9
leaq -0x220(%rbp), %rdi
callq 0x3500
movq %rdx, -0x280(%rbp)
movq %rax, -0x278(%rbp)
jmp 0x86e5
movq -0x280(%rbp), %rax
movq -0x278(%rbp), %rcx
movq %rcx, -0x248(%rbp)
movq %rax, -0x240(%rbp)
leaq -0x248(%rbp), %rdi
callq 0x9490
movq %rax, -0x288(%rbp)
jmp 0x8716
movq -0x288(%rbp), %rax
movl %eax, -0x234(%rbp)
xorl %edx, %edx
movl %edx, %esi
leaq -0x220(%rbp), %rdi
callq 0x33c0
jmp 0x8735
xorl %eax, %eax
cmpl -0x234(%rbp), %eax
jne 0x87c6
movq -0x258(%rbp), %rax
movb $0x1, 0x1002f(%rax)
movq 0x6898(%rip), %rdi      # 0xeff0
leaq 0x1bf4(%rip), %rsi      # 0xa353
callq 0x32c0
movq %rax, -0x290(%rbp)
jmp 0x876d
movq -0x290(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x3260
movq %rax, -0x298(%rbp)
jmp 0x8786
movq -0x298(%rbp), %rdi
leaq 0x1a69(%rip), %rsi      # 0xa1fd
callq 0x32c0
movq %rax, -0x2a0(%rbp)
jmp 0x87a2
movq -0x2a0(%rbp), %rdi
movq 0x6810(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x87b7
movl $0x1, -0x230(%rbp)
jmp 0x8999
movl $0x10000, %eax          # imm = 0x10000
cmpl -0x234(%rbp), %eax
jge 0x88ae
movq -0x258(%rbp), %rax
movb $0x1, 0x1002f(%rax)
movq 0x6804(%rip), %rdi      # 0xeff0
leaq 0x1b7a(%rip), %rsi      # 0xa36d
callq 0x32c0
movq %rax, -0x2a8(%rbp)
jmp 0x8801
movq -0x2a8(%rbp), %rdi
movl -0x234(%rbp), %esi
callq 0x34f0
movq %rax, -0x2b0(%rbp)
jmp 0x881c
movq -0x2b0(%rbp), %rdi
leaq 0x1b58(%rip), %rsi      # 0xa382
callq 0x32c0
movq %rax, -0x2b8(%rbp)
jmp 0x8838
movq -0x2b8(%rbp), %rdi
leaq 0x1b47(%rip), %rsi      # 0xa38d
callq 0x32c0
movq %rax, -0x2c0(%rbp)
jmp 0x8854
movq -0x2c0(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x34f0
movq %rax, -0x2c8(%rbp)
jmp 0x886e
movq -0x2c8(%rbp), %rdi
leaq 0x1b48(%rip), %rsi      # 0xa3c4
callq 0x32c0
movq %rax, -0x2d0(%rbp)
jmp 0x888a
movq -0x2d0(%rbp), %rdi
movq 0x6728(%rip), %rsi      # 0xefc0
callq 0x3300
jmp 0x889f
movl $0x1, -0x230(%rbp)
jmp 0x8999
jmp 0x88b0
movq -0x258(%rbp), %rdi
addq $0x8, %rdi
xorl %esi, %esi
movl $0x10000, %edx          # imm = 0x10000
callq 0x3150
movw $0x0, -0x24c(%rbp)
leaq -0x220(%rbp), %rdi
leaq -0x249(%rbp), %rsi
callq 0x3400
movq %rax, -0x2d8(%rbp)
jmp 0x88ec
movq -0x2d8(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3390
movb %al, -0x2d9(%rbp)
jmp 0x890a
movb -0x2d9(%rbp), %al
testb $0x1, %al
jne 0x8916
jmp 0x8942
movq -0x258(%rbp), %rax
movb -0x249(%rbp), %dl
movzwl -0x24c(%rbp), %ecx
movb %dl, 0x8(%rax,%rcx)
movw -0x24c(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x24c(%rbp)
jmp 0x88d0
movq -0x258(%rbp), %rdi
addq $0x10008, %rdi          # imm = 0x10008
xorl %esi, %esi
movl $0x20, %edx
callq 0x3150
movq -0x258(%rbp), %rax
movw $0x0, 0x10028(%rax)
movb $0x0, 0x1002a(%rax)
movb $0x0, 0x1002b(%rax)
movb $0x0, 0x1002c(%rax)
movb $0x0, 0x1002d(%rax)
movb $0x0, 0x1002e(%rax)
movl $0x0, -0x230(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x3070
addq $0x2e0, %rsp            # imm = 0x2E0
popq %rbp
retq
movq -0x228(%rbp), %rdi
callq 0x3520
nopw (%rax,%rax)