// File: /workspace/github_cpp_makefile_star2_O3/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 0x4c63
movq %rsi, %rbx
movl $0x20, %edi
callq 0x32b0
testl %eax, %eax
je 0x4cb8
movq 0xa3a1(%rip), %rbx      # 0xefc8
leaq 0x5580(%rip), %rsi      # 0xa1ae
movl $0x10, %edx
movq %rbx, %rdi
callq 0x33a0
callq 0x3250
testq %rax, %rax
je 0x4d42
movq %rax, %r14
movq %rax, %rdi
callq 0x3140
movq 0xa36d(%rip), %rdi      # 0xefc8
movq %r14, %rsi
movq %rax, %rdx
jmp 0x4c79
movq 0xa35e(%rip), %rbx      # 0xefc8
leaq 0x552a(%rip), %rsi      # 0xa19b
movl $0x12, %edx
movq %rbx, %rdi
callq 0x33a0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
movl %ebx, %eax
addq $0x12c0, %rsp           # imm = 0x12C0
popq %rbx
popq %r14
popq %rbp
retq
leaq -0x2c8(%rbp), %r14
movq %r14, %rdi
callq 0x3390
movq 0x8(%rbx), %rsi
movq %r14, %rdi
movl $0x4, %edx
callq 0x31d0
movq -0x2c8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x2a8(%rbp,%rax)
je 0x4d5e
movq 0xa2d4(%rip), %rbx      # 0xefc8
leaq 0x54c4(%rip), %rsi      # 0xa1bf
movl $0x14, %edx
movq %rbx, %rdi
callq 0x33a0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq 0xa2a2(%rip), %rdi      # 0xefc8
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
callq 0x3090
jmp 0x4f4e
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3490
jmp 0x4c7e
movq -0x1e0(%rbp,%rax), %rsi
leaq -0x30(%rbp), %rdi
leaq -0x12c8(%rbp), %r9
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x4fc2
leaq -0xc0(%rbp), %rdi
callq 0x5004
leaq -0x12c8(%rbp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x3180
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
leaq -0x30(%rbp), %rsi
movq %rbx, %rdi
callq 0x6906
leaq 0xa4d5(%rip), %rsi      # 0xf298
leaq -0x12c8(%rbp), %rdi
callq 0x6938
leaq -0x2c8(%rbp), %rdi
callq 0x30c0
leaq 0x53f2(%rip), %rdi      # 0xa1d4
movl $0x64, %esi
movl $0x64, %edx
movl $0x280, %ecx            # imm = 0x280
movl $0x140, %r8d            # imm = 0x140
movl $0x4, %r9d
callq 0x3070
movq %rax, %rbx
testq %rax, %rax
je 0x4e7d
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x6, %edx
callq 0x3050
movq %rax, %r14
testq %rax, %rax
je 0x4ec1
leaq 0x1dc1(%rip), %rsi      # 0x6bf0
leaq -0x18(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
leaq -0x12c8(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x552e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3eec
leaq -0x18(%rbp), %rdi
callq 0x34a0
movq %rbx, %rdi
callq 0x3030
callq 0x3090
cmpq $0x0, -0x18(%rbp)
jne 0x4f71
xorl %ebx, %ebx
jmp 0x4f21
movq 0xa144(%rip), %rbx      # 0xefc8
leaq 0x5359(%rip), %rsi      # 0xa1e4
movl $0x18, %edx
movq %rbx, %rdi
callq 0x33a0
callq 0x3250
movq %rax, %r14
testq %rax, %rax
je 0x4ed5
movq %r14, %rdi
callq 0x3140
movq 0xa114(%rip), %rdi      # 0xefc8
movq %r14, %rsi
movq %rax, %rdx
callq 0x33a0
jmp 0x4eec
movq %rbx, %rdi
callq 0x3030
movl $0x1, %ebx
callq 0x3090
jmp 0x4f21
movq (%rbx), %rax
movq -0x18(%rax), %rax
leaq (%rbx,%rax), %rdi
movl 0x20(%rbx,%rax), %esi
orl $0x1, %esi
callq 0x3490
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq 0xa0be(%rip), %rdi      # 0xefc8
callq 0x3040
movq %rax, %rdi
callq 0x31f0
movl $0x1, %ebx
callq 0x3090
leaq -0x88(%rbp), %rdi
callq 0x62cc
leaq -0xc0(%rbp), %rdi
callq 0x6298
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4f4e
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x3340
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
jmp 0x4caa
jmp 0x4f78
movq %rax, %rbx
jmp 0x4f93
jmp 0x4faa
jmp 0x4f78
cmpq $0x0, -0x18(%rbp)
je 0x4f78
callq 0x30f0
jmp 0x4f78
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
callq 0x62cc
leaq -0xc0(%rbp), %rdi
callq 0x6298
movq -0x30(%rbp), %rdi
testq %rdi, %rdi
je 0x4fad
movq -0x20(%rbp), %rsi
subq %rdi, %rsi
callq 0x3340
jmp 0x4fad
movq %rax, %rbx
leaq -0x2c8(%rbp), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x34c0
nop