// File: /workspace/github_cpp_makefile_star2_O2/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 0x2896(%rip), %rsi      # 0x9202
leaq -0xa8(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3130
leaq -0xa8(%rbp), %rsi
movq %rbx, %rdi
callq 0x683c
movq %rax, %r12
leaq -0xa8(%rbp), %rdi
callq 0x3508
movzwl (%r12), %esi
movq %r15, %rdi
callq 0x68f0
movzbl (%rax), %r13d
shll $0x8, %r13d
movzwl (%r12), %esi
incq %rsi
movq %r15, %rdi
callq 0x68f0
movzbl (%rax), %r12d
movl %r13d, %eax
andl $0xf000, %eax           # imm = 0xF000
leaq -0x48(%rbp), %rsi
movw %ax, (%rsi)
leaq 0xa9bf(%rip), %rdi      # 0x11398
callq 0x8f12
testq %rax, %rax
je 0x6ac9
movzwl %r12w, %ecx
orl %r13d, %ecx
addq $0x10, %rax
leaq -0x48(%rbp), %rsi
movw %cx, (%rsi)
movq %rbx, 0x8(%rsi)
movq %r15, 0x10(%rsi)
movq %r14, 0x18(%rsi)
movq %rax, %rdi
callq 0x83bc
leaq 0x27f3(%rip), %rsi      # 0x9207
leaq -0x88(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3130
leaq -0x88(%rbp), %rsi
movq %rbx, %rdi
callq 0x683c
movq %rax, %r14
leaq -0x88(%rbp), %rdi
callq 0x3508
movzwl (%r14), %eax
testw %ax, %ax
je 0x6a51
decl %eax
movw %ax, (%r14)
leaq 0x27b2(%rip), %rsi      # 0x920a
leaq -0x68(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0x3130
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0x683c
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x3508
movzwl (%rbx), %eax
testl %eax, %eax
je 0x6ab7
cmpl $0x1, %eax
jne 0x6ab2
movq 0x8538(%rip), %rdi      # 0xefc8
leaq 0x28e2(%rip), %rsi      # 0x9379
callq 0x3330
pushq $0x7
popq %rsi
movq %rax, %rdi
callq 0x33a0
movq %rax, %rdi
callq 0x3170
movzwl (%rbx), %eax
decl %eax
movw %ax, (%rbx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x3180
movq %rax, %r14
leaq 0x2889(%rip), %rsi      # 0x9364
leaq -0x48(%rbp), %rdx
movq %rax, %rdi
callq 0x3130
leaq 0x8182(%rip), %rsi      # 0xec70
movq 0x84a3(%rip), %rdx      # 0xef98
movq %r14, %rdi
callq 0x34c0
movq %rax, %rbx
movq %r14, %rdi
callq 0x3230
jmp 0x6b37
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
jmp 0x6b2d
jmp 0x6b34
movq %rax, %rbx
leaq -0x88(%rbp), %rdi
jmp 0x6b2d
jmp 0x6b34
movq %rax, %rbx
leaq -0xa8(%rbp), %rdi
callq 0x3508
jmp 0x6b37
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34d0