// File: /workspace/github_cpp_makefile_star2_O2/AnthonyCalandra[P]chip8-emu/src/main.cc
// Function: main
int main(int argc, char* argv[]) {
  if (argc != 2) {
    std::cout << "Usage: emu ROMPath" << std::endl;
    return 1;
  }

  if (SDL_Init(SDL_INIT_VIDEO) != 0){
    std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    return 1;
  }

  std::ifstream input;
  input.open(argv[1], std::ios::binary);
  if (input.fail()) {
    std::cout << "Failed to open file." << std::endl;
    SDL_Quit();
    return 1;
  }

  auto inputIt = std::istreambuf_iterator<char>(input);
  auto eos = std::istreambuf_iterator<char>();
  std::vector<char> romBuffer(inputIt, eos);
  Registers registers;
  MemoryStore memory;
  Stack stack;
  memory.loadRom(std::move(romBuffer));
  memory.loadFont(std::move(chip8Fontset));
  input.close();

  const char* windowTitle = "CHIP-8 Emulator";
  SDL_Window* window = SDL_CreateWindow(windowTitle, 100, 100, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
  if (window == nullptr){
    std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
    SDL_Quit();
    return 1;
  }

  SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  if (renderer == nullptr) {
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 1;
  }

  std::thread chipThread(executeChipCpu, std::move(registers), std::move(memory), std::move(stack));
  gameLoop(window, renderer);

  chipThread.join();
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x12c0, %rsp           # imm = 0x12C0
cmpl $0x2, %edi
jne 0x4e29
movq %rsi, %rbx
pushq $0x20
popq %rdi
callq 0x32c0
testl %eax, %eax
je 0x4e55
movq 0xa1bb(%rip), %rdi      # 0xefc8
leaq 0x439f(%rip), %rsi      # 0x91b3
callq 0x3330
movq %rax, %rbx
callq 0x3260
movq %rbx, %rdi
movq %rax, %rsi
jmp 0x4e37
movq 0xa198(%rip), %rdi      # 0xefc8
leaq 0x4369(%rip), %rsi      # 0x91a0
callq 0x3330
movq %rax, %rdi
callq 0x3170
pushq $0x1
popq %rbx
movl %ebx, %eax
addq $0x12c0, %rsp           # imm = 0x12C0
popq %rbx
popq %r14
popq %rbp
retq
leaq -0x2c8(%rbp), %r14
movq %r14, %rdi
callq 0x3380
movq 0x8(%rbx), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x3200
movq -0x2c8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x2a8(%rbp,%rax)
je 0x4eb0
movq 0xa139(%rip), %rdi      # 0xefc8
leaq 0x432e(%rip), %rsi      # 0x91c4
callq 0x3330
movq %rax, %rdi
callq 0x3170
callq 0x3080
pushq $0x1
popq %rbx
jmp 0x5014
movq -0x1e0(%rbp,%rax), %rsi
leaq -0x30(%rbp), %rdi
pushq $-0x1
popq %rdx
leaq -0x12c8(%rbp), %r9
xorl %ecx, %ecx
movl %edx, %r8d
callq 0x5072
leaq -0xc0(%rbp), %rdi
callq 0x50a4
leaq -0x12c8(%rbp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x31b0
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x68fa
leaq 0xa388(%rip), %rsi      # 0xf298
leaq -0x12c8(%rbp), %rdi
callq 0x6912
leaq -0x2c8(%rbp), %rdi
callq 0x30b0
leaq 0x42aa(%rip), %rdi      # 0x91d9
pushq $0x64
popq %rdx
pushq $0x4
popq %r9
movl %edx, %esi
movl $0x280, %ecx            # imm = 0x280
movl $0x140, %r8d            # imm = 0x140
callq 0x3060
movq %rax, %rbx
testq %rax, %rax
je 0x4fba
pushq $-0x1
popq %rsi
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0x3040
movq %rax, %r14
testq %rax, %rax
je 0x4fef
leaq 0x1bce(%rip), %rsi      # 0x6b3f
leaq -0x18(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq -0x12c8(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x5480
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3eb0
leaq -0x18(%rbp), %rdi
callq 0x34b0
movq %rbx, %rdi
callq 0x3030
callq 0x3080
leaq -0x18(%rbp), %rdi
callq 0x5522
xorl %ebx, %ebx
jmp 0x4fff
movq 0xa007(%rip), %rdi      # 0xefc8
leaq 0x4221(%rip), %rsi      # 0x91e9
callq 0x3330
movq %rax, %rbx
callq 0x3260
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3330
movq %rax, %rdi
callq 0x3170
callq 0x3080
jmp 0x4ffc
movq %rbx, %rdi
callq 0x3030
callq 0x3080
pushq $0x1
popq %rbx
leaq -0xc0(%rbp), %rdi
callq 0x5532
leaq -0x30(%rbp), %rdi
callq 0x631a
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
jmp 0x4e47
jmp 0x5040
movq %rax, %rbx
jmp 0x504f
jmp 0x505a
jmp 0x5040
movq %rax, %rbx
leaq -0x18(%rbp), %rdi
callq 0x5522
jmp 0x5043
jmp 0x5040
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x5532
leaq -0x30(%rbp), %rdi
callq 0x631a
jmp 0x505d
movq %rax, %rbx
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x34d0
nop