// File: /workspace/github_cpp_makefile_star2_O2/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 0x2443b
movq %rdi, %rbx
callq 0x23542
leaq 0x130(%rbx), %r14
movzwl 0x12c(%rbx), %esi
movq %r14, %rdi
callq 0x2fc3a
movb %al, -0x21(%rbp)
cmpb $-0x35, %al
jne 0x2422e
leaq 0xe0(%rbx), %r15
movl 0x12c(%rbx), %eax
incl %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x2fc3a
movzbl %al, %eax
leaq -0x1c0(%rbp), %rsi
movw %ax, (%rsi)
movq %r15, %rdi
jmp 0x24242
leaq 0xa8(%rbx), %rdi
movzbl %al, %eax
leaq -0x1c0(%rbp), %rsi
movw %ax, (%rsi)
callq 0x2d656
movq (%rax), %r12
leaq -0x1c0(%rbp), %r15
movq %r15, %rdi
callq 0xc850
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 0xc290
leaq 0x73ded(%rip), %rsi     # 0x98086
movq %rax, %rdi
callq 0xc520
leaq 0x8(%r12), %rsi
movq %rax, %rdi
callq 0xc220
leaq 0x73ebf(%rip), %rsi     # 0x98174
movq %rax, %rdi
callq 0xc520
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 0x2fc3a
movzbl %al, %esi
leaq -0x1c0(%rbp), %rdi
callq 0xc280
leaq 0x7bf20(%rip), %rsi     # 0xa0230
movq %rax, %rdi
callq 0xc520
movq %rax, %r15
movl 0x12c(%rbx), %eax
addl $0x2, %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x2fc3a
movzbl %al, %esi
movq %r15, %rdi
callq 0xc280
leaq 0x7beef(%rip), %rsi     # 0xa0230
movq %rax, %rdi
callq 0xc520
movq %rax, %r15
movl 0x12c(%rbx), %eax
incl %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x2fc3a
movzbl %al, %esi
movq %r15, %rdi
callq 0xc280
leaq -0x1b8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0xc360
leaq 0x10(%rbx), %r14
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x2dc64
leaq -0x48(%rbp), %rdi
callq 0xc5d0
movq %r14, %rdi
callq 0x2d20e
cmpq $0x14, %rax
jb 0x243a9
movq %r14, %rdi
callq 0x2d224
leaq 0x70(%rbx), %r14
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x2df22
cmpq $0x0, (%rax)
je 0x243d2
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x2df22
movq (%rax), %rax
incl (%rax)
jmp 0x243f6
pushq $0x10
popq %rdi
callq 0xc120
movq %rax, %r15
movl $0x1, (%rax)
movq %r12, 0x8(%rax)
leaq -0x21(%rbp), %rsi
movq %r14, %rdi
callq 0x2df22
movq %r15, (%rax)
leaq 0x30(%r12), %rdi
callq 0x2d25e
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)
andl $0x0, 0xc(%rbx)
movq %rbx, %rdi
callq 0x24474
leaq -0x1c0(%rbp), %rdi
callq 0xc7c0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2445d
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0xc5d0
jmp 0x24460
jmp 0x2445d
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
callq 0xc7c0
movq %rbx, %rdi
callq 0xc580