// File: /workspace/github_cpp_makefile_star2_O1/AnthonyCalandra[P]chip8-emu/src/cpu.cc
// Function: performCpuCycle(Registers&, MemoryStore&, Stack&)
void performCpuCycle(Registers& registers, MemoryStore& memory, Stack& stack) {
  unsigned short& pc = registers.getSpecialRegister("PC");
  unsigned short opcode = memory[pc] << 8 | memory[pc + 1];
  // Find instruction by using first 4 bits of opcode (identifier).
  auto instIter = instructionTable.find(opcode & 0xF000);
  if (instIter == instructionTable.cend()) {
    throw std::string("Unknown instruction.");
  } else {
    (instIter->second)(ChipContext(opcode, registers, memory, stack));
  }

  unsigned short& delayTimer = registers.getSpecialRegister("DT");
  if (delayTimer > 0) {
    delayTimer--;
  }

  unsigned short& soundTimer = registers.getSpecialRegister("ST");
  if (soundTimer > 0) {
    if (soundTimer == 1) {
      std::cout << "BEEP" << '\a' << std::endl;
    }

    soundTimer--;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq -0x98(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x3806(%rip), %rsi      # 0xa1fd
leaq 0x3801(%rip), %rdx      # 0xa1ff
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
callq 0x56a8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x68b0
movq %rax, %r12
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x6a36
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%r12), %esi
movq %r15, %rdi
callq 0x697c
movzbl (%rax), %r13d
shll $0x8, %r13d
movzwl (%r12), %esi
incq %rsi
movq %r15, %rdi
callq 0x697c
movzbl (%rax), %r8d
movl %r13d, %ecx
andl $0xf000, %ecx           # imm = 0xF000
movq 0xa931(%rip), %r9       # 0x113a0
xorl %r11d, %r11d
movq %rcx, %rax
xorl %edx, %edx
divq %r9
movq %rdx, %rsi
movq 0xa914(%rip), %rax      # 0x11398
movl %esi, %edx
movq (%rax,%rdx,8), %rax
testq %rax, %rax
je 0x6acc
movq (%rax), %r10
movq %rax, %r11
cmpw 0x8(%r10), %cx
je 0x6acc
movq %r10, %r12
movq (%r10), %r10
testq %r10, %r10
je 0x6ac9
movzwl 0x8(%r10), %edi
movq %rdi, %rax
xorl %edx, %edx
divq %r9
movl $0x0, %r11d
cmpq %rsi, %rdx
jne 0x6acc
movq %r12, %r11
cmpw %di, %cx
jne 0x6a9c
jmp 0x6acc
xorl %r11d, %r11d
testq %r11, %r11
je 0x6ad6
movq (%r11), %rax
jmp 0x6ad8
xorl %eax, %eax
testq %rax, %rax
je 0x6c2a
movzwl %r8w, %ecx
orl %r13d, %ecx
movw %cx, -0x88(%rbp)
movq %rbx, -0x80(%rbp)
movq %r15, -0x78(%rbp)
movq %r14, -0x70(%rbp)
cmpq $0x0, 0x20(%rax)
je 0x6c70
leaq 0x10(%rax), %rdi
leaq -0x88(%rbp), %rsi
callq *0x28(%rax)
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x36df(%rip), %rsi      # 0xa202
leaq 0x36da(%rip), %rdx      # 0xa204
leaq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x56a8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68b0
movq %rax, %r14
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x6b59
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%r14), %eax
testw %ax, %ax
je 0x6b68
decl %eax
movw %ax, (%r14)
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x368e(%rip), %rsi      # 0xa205
leaq 0x3689(%rip), %rdx      # 0xa207
leaq -0x48(%rbp), %r14
movq %r14, %rdi
callq 0x56a8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x68b0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x6bad
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%rbx), %eax
testl %eax, %eax
je 0x6c18
cmpl $0x1, %eax
jne 0x6c15
movq 0x8408(%rip), %r14      # 0xefc8
leaq 0x37de(%rip), %rsi      # 0xa3a5
movl $0x4, %edx
movq %r14, %rdi
callq 0x33a0
leaq -0x88(%rbp), %rsi
movb $0x7, (%rsi)
movl $0x1, %edx
movq %r14, %rdi
callq 0x33a0
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3310
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31f0
decw (%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x20, %edi
callq 0x3160
movq %rax, %r14
addq $0x10, %rax
movq %rax, (%r14)
movb $0x1, %r15b
leaq 0x3748(%rip), %rsi      # 0xa390
leaq 0x3755(%rip), %rdx      # 0xa3a4
movq %r14, %rdi
callq 0x56a8
xorl %r15d, %r15d
leaq 0x800f(%rip), %rsi      # 0xec70
movq 0x8330(%rip), %rdx      # 0xef98
movq %r14, %rdi
callq 0x34b0
callq 0x3120
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x6cc9
movq -0x38(%rbp), %rsi
jmp 0x6cc1
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x6cc9
movq -0x58(%rbp), %rsi
jmp 0x6cc1
movq %rax, %rbx
testb %r15b, %r15b
je 0x6cc9
movq %r14, %rdi
callq 0x3220
jmp 0x6cc9
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x6cc9
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3340
movq %rbx, %rdi
callq 0x34c0