// File: /workspace/github_cpp_makefile_star2_O3/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 0x3886(%rip), %rsi      # 0xa1fd
leaq 0x3881(%rip), %rdx      # 0xa1ff
leaq -0xa8(%rbp), %r12
movq %r12, %rdi
callq 0x5616
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6830
movq %rax, %r12
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x69b6
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%r12), %esi
movq %r15, %rdi
callq 0x68fc
movzbl (%rax), %r13d
shll $0x8, %r13d
movzwl (%r12), %esi
incq %rsi
movq %r15, %rdi
callq 0x68fc
movzbl (%rax), %r12d
movl %r13d, %eax
andl $0xf000, %eax           # imm = 0xF000
leaq -0x48(%rbp), %rsi
movw %ax, (%rsi)
leaq 0xa9a3(%rip), %rdi      # 0x11398
callq 0x9e22
testq %rax, %rax
je 0x6b51
movzwl %r12w, %ecx
orl %r13d, %ecx
movw %cx, -0x48(%rbp)
movq %rbx, -0x40(%rbp)
movq %r15, -0x38(%rbp)
movq %r14, -0x30(%rbp)
cmpq $0x0, 0x20(%rax)
je 0x6b91
movq %rax, %rdi
addq $0x10, %rdi
leaq -0x48(%rbp), %rsi
callq *0x28(%rax)
leaq -0x78(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x37c0(%rip), %rsi      # 0xa202
leaq 0x37bb(%rip), %rdx      # 0xa204
leaq -0x88(%rbp), %r14
movq %r14, %rdi
callq 0x5616
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6830
movq %rax, %r14
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x6a7e
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%r14), %eax
testw %ax, %ax
je 0x6a8d
decl %eax
movw %ax, (%r14)
leaq -0x58(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x3769(%rip), %rsi      # 0xa205
leaq 0x3764(%rip), %rdx      # 0xa207
leaq -0x68(%rbp), %r14
movq %r14, %rdi
callq 0x5616
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6830
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x6ad2
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x3340
movzwl (%rbx), %eax
testl %eax, %eax
je 0x6b3f
cmpl $0x1, %eax
jne 0x6b3a
movq 0x84e3(%rip), %r14      # 0xefc8
leaq 0x38b9(%rip), %rsi      # 0xa3a5
movl $0x4, %edx
movq %r14, %rdi
callq 0x33a0
leaq -0x48(%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
movzwl (%rbx), %eax
decl %eax
movw %ax, (%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)
leaq 0x3824(%rip), %rsi      # 0xa390
leaq 0x3831(%rip), %rdx      # 0xa3a4
movq %r14, %rdi
callq 0x5616
leaq 0x80ee(%rip), %rsi      # 0xec70
movq 0x840f(%rip), %rdx      # 0xef98
movq %r14, %rdi
callq 0x34b0
callq 0x3120
movq %rax, %rbx
movq %r14, %rdi
callq 0x3220
jmp 0x6be8
movq %rax, %rbx
movq -0x68(%rbp), %rdi
cmpq %r15, %rdi
je 0x6be8
movq -0x58(%rbp), %rsi
jmp 0x6be0
movq %rax, %rbx
movq -0x88(%rbp), %rdi
cmpq %r15, %rdi
je 0x6be8
movq -0x78(%rbp), %rsi
jmp 0x6be0
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0x6be8
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x3340
movq %rbx, %rdi
callq 0x34c0