// File: 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
subq $0x490, %rsp            # imm = 0x490
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x3d0(%rbp)
testb $0x1, 0x12b(%rax)
jne 0x4c0c8
jmp 0x4c6a8
movq -0x3d0(%rbp), %rdi
callq 0x4a730
movq -0x3d0(%rbp), %rax
movzwl 0x12c(%rax), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jg 0x4c100
movq -0x3d0(%rbp), %rax
movzwl 0x12c(%rax), %eax
cmpl $0x0, %eax
jge 0x4c1f8
movq 0x1c1de9(%rip), %rdi    # 0x20def0
movq %rdi, -0x3e8(%rbp)
leaq 0x125c9b(%rip), %rsi    # 0x171db0
callq 0xd5c0
movq -0x3e8(%rbp), %rdi
leaq 0x125ca1(%rip), %rsi    # 0x171dc9
callq 0xd5c0
movq %rax, %rdi
movq -0x3d0(%rbp), %rax
movzwl 0x12c(%rax), %esi
callq 0xd320
movq %rax, %rdi
movl $0xa, %esi
movl %esi, -0x3d4(%rbp)
callq 0xd630
movq -0x3e8(%rbp), %rdi
movq -0x3d0(%rbp), %rax
movb $0x0, 0x12b(%rax)
leaq 0x125c65(%rip), %rsi    # 0x171dd7
callq 0xd5c0
leaq 0x125c80(%rip), %rsi    # 0x171dfe
leaq -0x208(%rbp), %rdi
movq %rdi, -0x3e0(%rbp)
movl $0x10, %edx
callq 0xd300
movq -0x3d0(%rbp), %rdi
movq -0x3e0(%rbp), %rsi
movl -0x3d4(%rbp), %ecx
movl $0x1, %edx
callq 0x4ac90
jmp 0x4c1b6
leaq -0x208(%rbp), %rdi
callq 0xd880
jmp 0x4c1c4
leaq -0x208(%rbp), %rdi
callq 0xd780
jmp 0x4c6a8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x208(%rbp), %rdi
callq 0xd780
jmp 0x4c6bd
movq -0x3d0(%rbp), %rax
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movzwl 0x12c(%rax), %esi
callq 0xbc0c0
movb %al, -0x215(%rbp)
movzbl -0x215(%rbp), %eax
cmpl $0xcb, %eax
je 0x4c25d
movq -0x3d0(%rbp), %rdi
addq $0xa8, %rdi
movzbl -0x215(%rbp), %eax
movw %ax, -0x222(%rbp)
leaq -0x222(%rbp), %rsi
callq 0xb3ef0
movq (%rax), %rax
movq %rax, -0x220(%rbp)
jmp 0x4c2b8
movq -0x3d0(%rbp), %rax
movq %rax, %rcx
addq $0xe0, %rcx
movq %rcx, -0x3f0(%rbp)
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movzwl 0x12c(%rax), %eax
addl $0x1, %eax
movzwl %ax, %esi
callq 0xbc0c0
movq -0x3f0(%rbp), %rdi
movzbl %al, %eax
movw %ax, -0x224(%rbp)
leaq -0x224(%rbp), %rsi
callq 0xb3ef0
movq (%rax), %rax
movq %rax, -0x220(%rbp)
leaq -0x3a0(%rbp), %rdi
movq %rdi, -0x400(%rbp)
callq 0xd8f0
movq -0x400(%rbp), %rdi
leaq 0x68007(%rip), %rsi     # 0xb42e0
callq 0xdb40
movq %rax, -0x3f8(%rbp)
jmp 0x4c2e7
movq -0x3f8(%rbp), %rdi
leaq 0x67fbb(%rip), %rsi     # 0xb42b0
callq 0xdb40
jmp 0x4c2fc
movq -0x3d0(%rbp), %rax
movzwl 0x12c(%rax), %esi
leaq -0x3a0(%rbp), %rdi
callq 0xd320
movq %rax, -0x408(%rbp)
jmp 0x4c31f
movq -0x408(%rbp), %rdi
leaq 0x125995(%rip), %rsi    # 0x171cc2
callq 0xd5c0
movq %rax, -0x410(%rbp)
jmp 0x4c33b
movq -0x410(%rbp), %rdi
movq -0x220(%rbp), %rsi
addq $0x8, %rsi
callq 0xd290
movq %rax, -0x418(%rbp)
jmp 0x4c35b
movq -0x418(%rbp), %rdi
leaq 0x125aa4(%rip), %rsi    # 0x171e0d
callq 0xd5c0
jmp 0x4c370
leaq 0x67f69(%rip), %rsi     # 0xb42e0
leaq -0x3a0(%rbp), %rdi
callq 0xdb40
movq %rax, -0x420(%rbp)
jmp 0x4c38c
movq -0x420(%rbp), %rdi
leaq 0x67f16(%rip), %rsi     # 0xb42b0
callq 0xdb40
movq %rax, -0x428(%rbp)
jmp 0x4c3a8
movq -0x3d0(%rbp), %rax
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movzwl 0x12c(%rax), %esi
callq 0xbc0c0
movb %al, -0x429(%rbp)
jmp 0x4c3cd
movq -0x428(%rbp), %rdi
movb -0x429(%rbp), %al
movzbl %al, %esi
callq 0xd310
movq %rax, -0x438(%rbp)
jmp 0x4c3eb
movq -0x438(%rbp), %rdi
leaq 0x129e77(%rip), %rsi    # 0x176270
callq 0xd5c0
movq %rax, -0x440(%rbp)
jmp 0x4c407
movq -0x3d0(%rbp), %rax
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movl 0x12c(%rax), %eax
addl $0x2, %eax
movzwl %ax, %esi
callq 0xbc0c0
movb %al, -0x441(%rbp)
jmp 0x4c431
movq -0x440(%rbp), %rdi
movb -0x441(%rbp), %al
movzbl %al, %esi
callq 0xd310
movq %rax, -0x450(%rbp)
jmp 0x4c44f
movq -0x450(%rbp), %rdi
leaq 0x129e13(%rip), %rsi    # 0x176270
callq 0xd5c0
movq %rax, -0x458(%rbp)
jmp 0x4c46b
movq -0x3d0(%rbp), %rax
movq %rax, %rdi
addq $0x130, %rdi            # imm = 0x130
movl 0x12c(%rax), %eax
incl %eax
movzwl %ax, %esi
callq 0xbc0c0
movb %al, -0x459(%rbp)
jmp 0x4c494
movq -0x458(%rbp), %rdi
movb -0x459(%rbp), %al
movzbl %al, %esi
callq 0xd310
jmp 0x4c4ab
movq -0x3d0(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x468(%rbp)
leaq -0x3c0(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0xdaa0
jmp 0x4c4d2
movq -0x468(%rbp), %rdi
leaq -0x3c0(%rbp), %rsi
callq 0xb44b0
jmp 0x4c4e7
leaq -0x3c0(%rbp), %rdi
callq 0xd730
movq -0x3d0(%rbp), %rdi
addq $0x10, %rdi
callq 0xb44e0
cmpq $0x14, %rax
jb 0x4c555
movq -0x3d0(%rbp), %rdi
addq $0x10, %rdi
callq 0xb4510
jmp 0x4c555
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
jmp 0x4c6b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x210(%rbp)
movl %eax, -0x214(%rbp)
leaq -0x3c0(%rbp), %rdi
callq 0xd730
jmp 0x4c6b1
movq -0x3d0(%rbp), %rdi
addq $0x70, %rdi
leaq -0x215(%rbp), %rsi
callq 0xb4580
movq %rax, -0x470(%rbp)
jmp 0x4c575
movq -0x470(%rbp), %rax
cmpq $0x0, (%rax)
je 0x4c5b5
movq -0x3d0(%rbp), %rdi
addq $0x70, %rdi
leaq -0x215(%rbp), %rsi
callq 0xb4580
movq %rax, -0x478(%rbp)
jmp 0x4c5a2
movq -0x478(%rbp), %rax
movq (%rax), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x4c611
movl $0x10, %edi
callq 0xd160
movq %rax, -0x480(%rbp)
jmp 0x4c5c8
movq -0x3d0(%rbp), %rdi
movq -0x480(%rbp), %rax
movl $0x1, (%rax)
movq -0x220(%rbp), %rcx
movq %rcx, 0x8(%rax)
addq $0x70, %rdi
leaq -0x215(%rbp), %rsi
callq 0xb4580
movq %rax, -0x488(%rbp)
jmp 0x4c600
movq -0x488(%rbp), %rax
movq -0x480(%rbp), %rcx
movq %rcx, (%rax)
movq -0x220(%rbp), %rdi
addq $0x30, %rdi
callq 0xb45b0
jmp 0x4c623
movq -0x3d0(%rbp), %rdi
movq -0x220(%rbp), %rax
movzwl 0x2a(%rax), %ecx
movl 0x12c(%rdi), %eax
addl %ecx, %eax
movw %ax, 0x12c(%rdi)
movl 0xc(%rdi), %eax
movq -0x220(%rbp), %rcx
movl 0x28(%rcx), %ecx
addl %ecx, %eax
movb %al, -0x3c1(%rbp)
movq 0x60(%rdi), %rax
incq %rax
movq %rax, 0x60(%rdi)
movzbl -0x3c1(%rbp), %eax
movl %eax, %ecx
movq 0x68(%rdi), %rax
addq %rcx, %rax
movq %rax, 0x68(%rdi)
movzbl -0x3c1(%rbp), %ecx
movl 0x8(%rdi), %eax
subl %ecx, %eax
movl %eax, 0x8(%rdi)
movl $0x0, 0xc(%rdi)
movzbl -0x3c1(%rbp), %esi
callq 0x4c6d0
jmp 0x4c69c
leaq -0x3a0(%rbp), %rdi
callq 0xd840
addq $0x490, %rsp            # imm = 0x490
popq %rbp
retq
leaq -0x3a0(%rbp), %rdi
callq 0xd840
movq -0x210(%rbp), %rdi
callq 0xd620
nopl (%rax)