// File: /workspace/github_cpp_makefile_star2_O1/Ciaxur[P]yagb-emu/src/CPU.cc
// Function: CPU::CPU(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
CPU::CPU(std::string ROMPath): memory(ROMPath) {
  // Default Values
  cyclesLeft = 114;
  extraCycles = 0;
  halted = false;
  running = true;

  // Timer Cycles Initial State
  timerCycles.div = 0;
  timerCycles.tima = 0;

  // CPU Initial State
  IME = false;
  PC = 0x0100;
  SP = 0xFFFE;

  // Register Init
  //reg.A = 0x01; // Default for commercial roms
  reg.A = 0x11;   // Default for test roms
  reg.B = 0x00;
  reg.C = 0x13;
  reg.D = 0x00;
  reg.E = 0xD8;
  reg.F = 0xB0;
  reg.H = 0x01;
  reg.L = 0x4D;

  // Initialize the Opcode & Prefix Map
  this->initOpcodes(this);
  this->initPrefixed(this);

  ppu = new PPU(this, &this->memory);

  // Give memory access to LCD Mode
  this->memory.LCDMode = &(ppu->LCDMode);

  // Memory Power Up Sequence Init Values
  this->memory.memory[0xFF10] = 0x80; // NR10
  this->memory.memory[0xFF11] = 0xBF; // NR11
  this->memory.memory[0xFF12] = 0xF3; // NR12
  this->memory.memory[0xFF14] = 0xBF; // NR14
  this->memory.memory[0xFF16] = 0x3F; // NR21
  this->memory.memory[0xFF17] = 0x00; // NR22
  this->memory.memory[0xFF19] = 0xBF; // NR24
  this->memory.memory[0xFF1A] = 0x7F; // NR30
  this->memory.memory[0xFF1B] = 0xFF; // NR31
  this->memory.memory[0xFF1C] = 0x9F; // NR32
  this->memory.memory[0xFF1E] = 0xBF; // NR34
  this->memory.memory[0xFF20] = 0xFF; // NR41
  this->memory.memory[0xFF21] = 0x00; // NR42
  this->memory.memory[0xFF22] = 0x00; // NR43
  this->memory.memory[0xFF23] = 0xBF; // NR44
  this->memory.memory[0xFF24] = 0x77; // NR50
  this->memory.memory[0xFF25] = 0xF3; // NR51
  this->memory.memory[0xFF26] = 0xF1; // NR52 (Gameboy = 0xF1)
  this->memory.memory[0xFF40] = 0x91; // LCDC
  this->memory.memory[0xFF47] = 0xFC; // BGP
  this->memory.memory[0xFF48] = 0xFF; // OBP0
  this->memory.memory[0xFF49] = 0xFF; // OBP1
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x10(%rdi), %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rdi)
movups %xmm0, 0x40(%rdi)
movups %xmm0, 0x30(%rdi)
movups %xmm0, 0x20(%rdi)
movups %xmm0, 0x10(%rdi)
movq %r14, %rdi
xorl %esi, %esi
callq 0x3a730
leaq 0xa0(%rbx), %rax
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
movq %rax, 0x70(%rbx)
movl $0x1, %eax
movq %rax, 0x78(%rbx)
movups %xmm0, 0x80(%rbx)
movl $0x3f800000, %ecx       # imm = 0x3F800000
movl %ecx, 0x90(%rbx)
movups %xmm0, 0x98(%rbx)
leaq 0xd8(%rbx), %rdx
movq %rdx, 0xa8(%rbx)
movq %rax, 0xb0(%rbx)
movups %xmm0, 0xb8(%rbx)
movl %ecx, 0xc8(%rbx)
movups %xmm0, 0xd0(%rbx)
leaq 0x110(%rbx), %rdx
movq %rdx, 0xe0(%rbx)
movq %rax, 0xe8(%rbx)
movups %xmm0, 0xf0(%rbx)
movl %ecx, 0x100(%rbx)
movups %xmm0, 0x108(%rbx)
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
movq (%r15), %rsi
movq 0x8(%r15), %rdx
addq %rsi, %rdx
leaq -0x48(%rbp), %rdi
callq 0x3a9cc
leaq 0x130(%rbx), %r15
leaq -0x48(%rbp), %rsi
movq %r15, %rdi
callq 0x3cd7c
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x30462
movq -0x38(%rbp), %rsi
incq %rsi
callq 0xc0d0
leaq 0x10158(%rbx), %r12
movq %r12, %rdi
callq 0xc1c0
movq $0x72, 0x8(%rbx)
movw $0x100, 0x12a(%rbx)     # imm = 0x100
movq $0x0, 0x120(%rbx)
movb $0x0, 0x128(%rbx)
movl $0xfffe0100, 0x12c(%rbx) # imm = 0xFFFE0100
movabsq $0x4d01b0d800130011, %rax # imm = 0x4D01B0D800130011
movq %rax, 0x118(%rbx)
movq %rbx, %rdi
callq 0xd0cc
movq %rbx, %rdi
callq 0x1e354
movl $0x28, %edi
callq 0xc120
movq %rax, %r13
movq %rax, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x3d654
movq %r13, (%rbx)
movq %r13, 0x10150(%rbx)
movw $0xbf80, 0x10060(%rbx)  # imm = 0xBF80
movb $-0xd, 0x10062(%rbx)
movb $-0x41, %al
movb %al, 0x10064(%rbx)
movw $0x3f, 0x10066(%rbx)
movl $0x9fff7fbf, 0x10069(%rbx) # imm = 0x9FFF7FBF
movb %al, 0x1006e(%rbx)
movl $0xbf0000ff, 0x10070(%rbx) # imm = 0xBF0000FF
movw $0xf377, 0x10074(%rbx)  # imm = 0xF377
movb $-0xf, 0x10076(%rbx)
movb $-0x6f, 0x10090(%rbx)
movw $0xfffc, 0x10097(%rbx)  # imm = 0xFFFC
movb $-0x1, 0x10099(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0x28, %esi
movq %r13, %rdi
callq 0xc0d0
jmp 0x30589
movq %rax, %r15
jmp 0x30591
movq %rax, %r15
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x305ac
movq -0x38(%rbp), %rsi
incq %rsi
jmp 0x305a7
movq %rax, %r15
jmp 0x305ac
movq %rax, %r15
movq %r12, %rdi
callq 0xc6d0
movq 0x138(%rbx), %rdi
testq %rdi, %rdi
je 0x305ac
movq 0x148(%rbx), %rsi
subq %rdi, %rsi
callq 0xc0d0
leaq 0x70(%rbx), %r12
leaq 0xa8(%rbx), %r13
addq $0xe0, %rbx
movq %rbx, %rdi
callq 0x3a8bc
movq %r13, %rdi
callq 0x3a8bc
movq %r12, %rdi
callq 0x3a944
movq %r14, %rdi
callq 0x3a566
movq %r15, %rdi
callq 0xc560