// File: /workspace/github_cpp_makefile_star2_O3/Ciaxur[P]yagb-emu/src/CPU.cc
// Function: CPU::execute()
void CPU::execute() {
  // Handle Pre-Execution Process
  if (!this->running) return;
  handleInterrupt();

  // Verify PC Validity
  if (this->PC > 0xFFFF || this->PC < 0x0000) {
    std::cout << "Something went wrong...\n";
    std::cout << "Invalid PC = " << this->PC << '\n';
    this->running = false;

    // CPU Dump
    std::cout << "Dumping CPU State to 'cpu_state.dump'\n";
    std::ofstream out("cpu_state.dump", std::ios::out);
    dump(out);
    out.close();

    return;
  }

  // Execute current Instruction
  // Get Opcode Value
  uint8_t currentOpcode = this->memory.read(this->PC);

  // Condition between Opcode & PREFIX Table
  Opcode *opcodeObj;
  if (currentOpcode!= 0xCB) {
    opcodeObj = oMap[currentOpcode];
  } else {
    opcodeObj = pMap[this->memory.read(this->PC + 1)];
  }

  // DEBUG: Keep track of Previous 10 Instructions
  std::ostringstream ss;
  ss << std::hex << std::uppercase;
  ss << PC << ": " << opcodeObj->label << " | ";
  ss << std::hex << std::uppercase
     << (int)memory.read(PC) << " "
     << (int)memory.read(PC + 2) << " " << (int)memory.read(PC + 1);
  instructionStack.push_back(ss.str());
  if (instructionStack.size() >= 20)
    instructionStack.pop_front();
  
  // DEBUG: Track instruction execution frequency
  if (instr_iters[currentOpcode])
    instr_iters[currentOpcode]->frequency++;
  else
    instr_iters[currentOpcode] = new Instr_Iterations({ 1, opcodeObj });

  // Exec Opcode
  opcodeObj->exec();

  // Set new PC
  this->PC += opcodeObj->length;

  // Keep track of CPU's State
  uint8_t cyclesTaken = this->extraCycles + opcodeObj->machineCycles;

  // Decrement CyclesLeft & Additional Cycles taken by Opcode
  totalInstructions++;
  totalCycles += cyclesTaken;
  cyclesLeft -= cyclesTaken;
  this->extraCycles = 0;

  // Update Timers
  updateTimers(cyclesTaken);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1a0, %rsp            # imm = 0x1A0
cmpb $0x1, 0x12b(%rdi)
jne 0x31130
movq %rdi, %rbx
callq 0x2ffc8
leaq 0x130(%rbx), %r14
movzwl 0x12c(%rbx), %esi
movq %r14, %rdi
callq 0x3c974
movb %al, -0x21(%rbp)
cmpb $-0x35, %al
jne 0x30ea6
leaq 0xe0(%rbx), %r15
movl 0x12c(%rbx), %eax
incl %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x3c974
movzbl %al, %eax
leaq -0x1c0(%rbp), %rsi
movw %ax, (%rsi)
movq %r15, %rdi
jmp 0x30eba
leaq 0xa8(%rbx), %rdi
movzbl %al, %eax
leaq -0x1c0(%rbp), %rsi
movw %ax, (%rsi)
callq 0x3a27a
movq (%rax), %r12
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
callq 0xc800
movq (%r15), %rax
movq -0x18(%rax), %rcx
movl -0x1a8(%rbp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, -0x1a8(%rbp,%rcx)
movq -0x18(%rax), %rax
orl $0x4000, -0x1a8(%rbp,%rax) # imm = 0x4000
movzwl 0x12c(%rbx), %esi
movq %r15, %rdi
callq 0xc460
movq %rax, %r15
leaq 0x80172(%rip), %rsi     # 0xb1086
movl $0x2, %edx
movq %rax, %rdi
callq 0xc830
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
movq %r15, %rdi
callq 0xc830
leaq 0x8023a(%rip), %rsi     # 0xb1174
movl $0x3, %edx
movq %rax, %rdi
callq 0xc830
movq -0x1c0(%rbp), %rax
movq -0x18(%rax), %rcx
movl -0x1a8(%rbp,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, -0x1a8(%rbp,%rcx)
movq -0x18(%rax), %rax
orl $0x4000, -0x1a8(%rbp,%rax) # imm = 0x4000
movzwl 0x12c(%rbx), %esi
movq %r14, %rdi
callq 0x3c974
movzbl %al, %esi
leaq -0x1c0(%rbp), %rdi
callq 0xc270
movq %rax, %r15
leaq 0x88263(%rip), %rsi     # 0xb9200
movl $0x1, %edx
movq %rax, %rdi
callq 0xc830
movl 0x12c(%rbx), %eax
addl $0x2, %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x3c974
movzbl %al, %esi
movq %r15, %rdi
callq 0xc270
movq %rax, %r15
leaq 0x8822d(%rip), %rsi     # 0xb9200
movl $0x1, %edx
movq %rax, %rdi
callq 0xc830
movl 0x12c(%rbx), %eax
incl %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x3c974
movzbl %al, %esi
movq %r15, %rdi
callq 0xc270
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xc330
leaq 0x10(%rbx), %r14
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x3a7b8
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x31037
movq -0x38(%rbp), %rsi
incq %rsi
callq 0xc0d0
movq 0x58(%rbx), %rax
movq %rax, %rcx
subq 0x38(%rbx), %rcx
shrq $0x3, %rcx
cmpq $0x1, %rax
adcq $-0x1, %rcx
shlq $0x4, %rcx
movq 0x40(%rbx), %rax
subq 0x48(%rbx), %rax
movq 0x30(%rbx), %rdx
sarq $0x5, %rax
subq 0x20(%rbx), %rdx
sarq $0x5, %rdx
addq %rax, %rdx
addq %rcx, %rdx
cmpq $0x14, %rdx
jb 0x3107e
movq %r14, %rdi
callq 0x39eee
leaq 0x70(%rbx), %r14
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x3aaf0
cmpq $0x0, (%rax)
je 0x310a7
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x3aaf0
movq (%rax), %rax
incl (%rax)
jmp 0x310cd
movl $0x10, %edi
callq 0xc120
movq %rax, %r15
movl $0x1, (%rax)
movq %r12, 0x8(%rax)
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x3aaf0
movq %r15, (%rax)
cmpq $0x0, 0x40(%r12)
je 0x31140
leaq 0x30(%r12), %rdi
callq *0x48(%r12)
movzwl 0x2a(%r12), %eax
addw %ax, 0x12c(%rbx)
movl 0xc(%rbx), %eax
addl 0x28(%r12), %eax
incq 0x60(%rbx)
movzbl %al, %esi
addq %rsi, 0x68(%rbx)
subl %esi, 0x8(%rbx)
movl $0x0, 0xc(%rbx)
movq %rbx, %rdi
callq 0x31192
movq 0xc5e10(%rip), %rsi     # 0xf6f28
leaq -0x1c0(%rbp), %rdi
callq 0xc610
leaq -0x150(%rbp), %rdi
callq 0xc5e0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
callq 0xc8f0
jmp 0x31167
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3116a
movq -0x38(%rbp), %rsi
incq %rsi
callq 0xc0d0
jmp 0x3116a
jmp 0x31167
movq %rax, %rbx
movq 0xc5db7(%rip), %rsi     # 0xf6f28
leaq -0x1c0(%rbp), %rdi
callq 0xc610
leaq -0x150(%rbp), %rdi
callq 0xc5e0
movq %rbx, %rdi
callq 0xc560
nop