// File: /workspace/github_cpp_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x210, %rsp            # imm = 0x210
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, (%rdi)
movw $0x100, 0x1002f(%rdi)   # imm = 0x100
leaq 0x8(%rsp), %r15
pushq $0x4
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x33b0
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
je 0x4968
movb $0x1, 0x1002f(%rbx)
movq 0x36bb(%rip), %rdi      # 0x7ff0
leaq 0x980(%rip), %rsi       # 0x52bc
callq 0x3280
movq %rax, %rdi
movq %r14, %rsi
callq 0x3220
leaq 0x83a(%rip), %rsi       # 0x518d
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3120
jmp 0x4a9e
leaq 0x8(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x3310
leaq 0x8(%rsp), %rdi
callq 0x3420
movq %rax, %r15
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3310
testl %r15d, %r15d
je 0x49fd
cmpl $0x10001, %r15d         # imm = 0x10001
jl 0x4a3b
movb $0x1, 0x1002f(%rbx)
movq 0x363e(%rip), %rdi      # 0x7ff0
leaq 0x940(%rip), %rsi       # 0x52f9
callq 0x3280
movq %rax, %rdi
movl %r15d, %esi
callq 0x3410
leaq 0x93e(%rip), %rsi       # 0x530e
movq %rax, %rdi
callq 0x3280
leaq 0x93a(%rip), %rsi       # 0x5319
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x3410
leaq 0x955(%rip), %rsi       # 0x5350
jmp 0x4a29
movb $0x1, 0x1002f(%rbx)
movq 0x35e5(%rip), %rdi      # 0x7ff0
leaq 0x8cd(%rip), %rsi       # 0x52df
callq 0x3280
movq %rax, %rdi
movq %r14, %rsi
callq 0x3220
leaq 0x764(%rip), %rsi       # 0x518d
movq %rax, %rdi
callq 0x3280
movq %rax, %rdi
callq 0x3120
jmp 0x4a9e
leaq 0x8(%rbx), %r14
xorl %r13d, %r13d
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
callq 0x3140
leaq 0x8(%rsp), %r15
leaq 0x7(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3340
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4a85
movb 0x7(%rsp), %al
movzwl %r13w, %ecx
movb %al, (%r14,%rcx)
incl %r13d
jmp 0x4a5b
xorps %xmm0, %xmm0
movups %xmm0, 0x10018(%rbx)
movups %xmm0, 0x10008(%rbx)
andq $0x0, 0x10027(%rbx)
leaq 0x8(%rsp), %rdi
callq 0x3060
addq $0x210, %rsp            # imm = 0x210
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x4abf
jmp 0x4abf
jmp 0x4abf
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3440