// File: 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
subq $0x1390, %rsp           # imm = 0x1390
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0xa6fd
movq 0x218f2(%rip), %rdi     # 0x2bfc8
leaq 0x11b03(%rip), %rsi     # 0x1c1e0
callq 0x4380
movq %rax, %rdi
movq 0x2189c(%rip), %rsi     # 0x2bf88
callq 0x43c0
movl $0x1, -0x4(%rbp)
jmp 0xab76
movl $0x20, %edi
callq 0x4310
cmpl $0x0, %eax
je 0xa755
movq 0x218b5(%rip), %rdi     # 0x2bfc8
leaq 0x11ad9(%rip), %rsi     # 0x1c1f3
callq 0x4380
movq %rax, -0x1340(%rbp)
callq 0x42b0
movq -0x1340(%rbp), %rdi
movq %rax, %rsi
callq 0x4380
movq %rax, %rdi
movq 0x21844(%rip), %rsi     # 0x2bf88
callq 0x43c0
movl $0x1, -0x4(%rbp)
jmp 0xab76
leaq -0x218(%rbp), %rdi
movq %rdi, -0x1348(%rbp)
callq 0x43f0
movq -0x1348(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x4, %edx
callq 0x4230
jmp 0xa783
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x4040
movb %al, -0x1349(%rbp)
jmp 0xa7a3
movb -0x1349(%rbp), %al
testb $0x1, %al
jne 0xa7af
jmp 0xa814
movq 0x21812(%rip), %rdi     # 0x2bfc8
leaq 0x11a47(%rip), %rsi     # 0x1c204
callq 0x4380
movq %rax, -0x1358(%rbp)
jmp 0xa7cb
movq -0x1358(%rbp), %rdi
movq 0x217af(%rip), %rsi     # 0x2bf88
callq 0x43c0
jmp 0xa7e0
callq 0x4090
jmp 0xa7e7
movl $0x1, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0xab5a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0xab68
leaq -0x238(%rbp), %rdi
leaq -0x218(%rbp), %rsi
callq 0xab90
leaq -0x248(%rbp), %rdi
callq 0xabf0
movups -0x238(%rbp), %xmm0
movaps %xmm0, -0x270(%rbp)
movups -0x248(%rbp), %xmm0
movaps %xmm0, -0x280(%rbp)
leaq -0x281(%rbp), %rdi
movq %rdi, -0x1360(%rbp)
callq 0x4560
movq -0x1360(%rbp), %r9
movq -0x270(%rbp), %rsi
movl -0x268(%rbp), %edx
movq -0x280(%rbp), %rcx
movl -0x278(%rbp), %r8d
leaq -0x260(%rbp), %rdi
callq 0xac20
jmp 0xa892
leaq -0x281(%rbp), %rdi
callq 0x43d0
leaq -0x2f8(%rbp), %rdi
callq 0xacb0
jmp 0xa8ac
leaq -0x12f8(%rbp), %rdi
movq %rdi, -0x1368(%rbp)
callq 0xb3c0
leaq -0x1318(%rbp), %rdi
callq 0xb3f0
movq -0x1368(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x10b40
jmp 0xa8e0
leaq 0x21a01(%rip), %rsi     # 0x2c2e8
leaq -0x12f8(%rbp), %rdi
callq 0x10ba0
jmp 0xa8f5
leaq -0x218(%rbp), %rdi
callq 0x40d0
jmp 0xa903
leaq 0x1190f(%rip), %rax     # 0x1c219
movq %rax, -0x1320(%rbp)
movq -0x1320(%rbp), %rdi
movl $0x64, %edx
movl $0x280, %ecx            # imm = 0x280
movl $0x140, %r8d            # imm = 0x140
movl $0x4, %r9d
movl %edx, %esi
callq 0x4070
movq %rax, -0x1370(%rbp)
jmp 0xa93e
movq -0x1370(%rbp), %rax
movq %rax, -0x1328(%rbp)
cmpq $0x0, -0x1328(%rbp)
jne 0xaa23
movq 0x21667(%rip), %rdi     # 0x2bfc8
leaq 0x118c1(%rip), %rsi     # 0x1c229
callq 0x4380
movq %rax, -0x1378(%rbp)
jmp 0xa976
callq 0x42b0
movq %rax, -0x1380(%rbp)
jmp 0xa984
movq -0x1380(%rbp), %rsi
movq -0x1378(%rbp), %rdi
callq 0x4380
movq %rax, -0x1388(%rbp)
jmp 0xa9a0
movq -0x1388(%rbp), %rdi
movq 0x215da(%rip), %rsi     # 0x2bf88
callq 0x43c0
jmp 0xa9b5
callq 0x4090
jmp 0xa9bc
movl $0x1, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0xab26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x281(%rbp), %rdi
callq 0x43d0
jmp 0xab68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0xab4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0xab40
movq -0x1328(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x6, %edx
callq 0x4050
movq %rax, -0x1390(%rbp)
jmp 0xaa42
movq -0x1390(%rbp), %rax
movq %rax, -0x1330(%rbp)
cmpq $0x0, -0x1330(%rbp)
jne 0xaa85
movq -0x1328(%rbp), %rdi
callq 0x4030
jmp 0xaa68
callq 0x4090
jmp 0xaa6f
movl $0x1, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0xab26
leaq 0x6e34(%rip), %rsi      # 0x118c0
leaq -0x1338(%rbp), %rdi
leaq -0x2f8(%rbp), %rdx
leaq -0x12f8(%rbp), %rcx
leaq -0x1318(%rbp), %r8
callq 0xb420
jmp 0xaaaf
movq -0x1328(%rbp), %rdi
movq -0x1330(%rbp), %rsi
callq 0x63b0
jmp 0xaac4
leaq -0x1338(%rbp), %rdi
callq 0x4530
jmp 0xaad2
movq -0x1328(%rbp), %rdi
callq 0x4030
jmp 0xaae0
callq 0x4090
jmp 0xaae7
movl $0x0, -0x4(%rbp)
movl $0x1, -0x228(%rbp)
leaq -0x1338(%rbp), %rdi
callq 0xb4f0
jmp 0xab26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x1338(%rbp), %rdi
callq 0xb4f0
jmp 0xab40
leaq -0x2f8(%rbp), %rdi
callq 0xb520
leaq -0x260(%rbp), %rdi
callq 0xb550
jmp 0xab5a
leaq -0x2f8(%rbp), %rdi
callq 0xb520
leaq -0x260(%rbp), %rdi
callq 0xb550
jmp 0xab68
leaq -0x218(%rbp), %rdi
callq 0x4100
jmp 0xab76
leaq -0x218(%rbp), %rdi
callq 0x4100
jmp 0xab82
movl -0x4(%rbp), %eax
addq $0x1390, %rsp           # imm = 0x1390
popq %rbp
retq
movq -0x220(%rbp), %rdi
callq 0x4550
nop