// File: /workspace/github_cpp_makefile_star2_O1/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
movq %r15, %rdi
movq %rdx, %rsi
movl $0x4, %edx
callq 0x33a0
movq (%r15), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
je 0x4b7e
movb $0x1, 0x1002f(%rbx)
movq 0x44d9(%rip), %rdi      # 0x8ff0
leaq 0x179e(%rip), %rsi      # 0x62bc
movl $0x22, %edx
callq 0x32a0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x44ba(%rip), %rdi      # 0x8ff0
callq 0x32a0
movq %rax, %rbx
leaq 0x1648(%rip), %rsi      # 0x618d
movl $0x2, %edx
movq %rax, %rdi
callq 0x32a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3180
jmp 0x4d2b
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x32e0
leaq 0x8(%rsp), %rdi
callq 0x3410
movq %rax, %r15
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x32e0
testl %r15d, %r15d
je 0x4c53
cmpl $0x10001, %r15d         # imm = 0x10001
jl 0x4cc5
movb $0x1, 0x1002f(%rbx)
movq 0x4422(%rip), %rdi      # 0x8ff0
leaq 0x1724(%rip), %rsi      # 0x62f9
movl $0x14, %edx
callq 0x32a0
movq 0x440a(%rip), %rdi      # 0x8ff0
movl %r15d, %esi
callq 0x3400
movq %rax, %rbx
leaq 0x1716(%rip), %rsi      # 0x630e
movl $0xa, %edx
movq %rax, %rdi
callq 0x32a0
leaq 0x170d(%rip), %rsi      # 0x6319
movl $0x36, %edx
movq %rbx, %rdi
callq 0x32a0
movq %rbx, %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x3400
movq %rax, %rbx
leaq 0x1720(%rip), %rsi      # 0x6350
movl $0x7, %edx
movq %rax, %rdi
callq 0x32a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3240
jmp 0x4cb0
movb $0x1, 0x1002f(%rbx)
movq 0x438f(%rip), %rdi      # 0x8ff0
leaq 0x1677(%rip), %rsi      # 0x62df
movl $0x19, %edx
callq 0x32a0
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq 0x4370(%rip), %rdi      # 0x8ff0
callq 0x32a0
movq %rax, %rbx
leaq 0x14fe(%rip), %rsi      # 0x618d
movl $0x2, %edx
movq %rax, %rdi
callq 0x32a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3240
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x3180
jmp 0x4d2b
leaq 0x8(%rbx), %r14
xorl %r13d, %r13d
movl $0x10000, %edx          # imm = 0x10000
movq %r14, %rdi
xorl %esi, %esi
callq 0x3110
leaq 0x8(%rsp), %r15
leaq 0x7(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x3310
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x4d0f
movb 0x7(%rsp), %al
movzwl %r13w, %ecx
movb %al, (%r14,%rcx)
incl %r13d
jmp 0x4ce5
xorps %xmm0, %xmm0
movups %xmm0, 0x10018(%rbx)
movups %xmm0, 0x10008(%rbx)
movq $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 0x4d4c
jmp 0x4d4c
jmp 0x4d4c
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3060
movq %rbx, %rdi
callq 0x3430
nop