// File: /workspace/github_cpp_makefile_star2_O3/mindbeast[P]rnes/apu.cpp
// Function: Rnes::Apu::Apu(Rnes::Nes*, Rnes::Sdl*)
Apu::Apu(Nes *parent, Sdl *audio)
    : nes{parent}, audio{audio}, frameDivider{0}, step{0}, halfTimerDivider{0},
      samplerDivider{0}, regs{0}, fourFrameCount{0}, fiveFrameCount{0}, sampleBuffer{} {
  // Create audio ringbuffer.
  std::unique_ptr<RingBuffer<uint16_t>> rbLocal(new RingBuffer<uint16_t>(1 << 12));
  rb = std::move(rbLocal);

  // Create apu units.
  std::unique_ptr<Pulse> pulseALocal(new Pulse{&regs[CHANNEL1_VOLUME_DECAY], true});
  pulseA = std::move(pulseALocal);

  std::unique_ptr<Pulse> pulseBLocal(new Pulse{&regs[CHANNEL2_VOLUME_DECAY], false});
  pulseB = std::move(pulseBLocal);

  std::unique_ptr<Triangle> triangleLocal(new Triangle{&regs[CHANNEL3_LINEAR_COUNTER]});
  triangle = std::move(triangleLocal);

  std::unique_ptr<Noise> noiseLocal(new Noise{&regs[CHANNEL4_VOLUME_DECAY]});
  noise = std::move(noiseLocal);

  // Get Current sample rate
  sampleRate = audio->getSampleRate();

  // Start audio callbacks.
  audio->registerAudioCallback(apuSdlCallback, rb.get());

  // compute clocks per sample
  clksPerSample = float(cpuClk) / float(sampleRate);

  nextSampleCountdown = 1;
  currentSampleClk = 0.0f;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, (%rdi)
movq %rdx, 0x8(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movups %xmm0, 0x2c(%rdi)
movups %xmm0, 0x3c(%rdi)
movups %xmm0, 0x4c(%rdi)
movups %xmm0, 0x5c(%rdi)
movups %xmm0, 0x6c(%rdi)
movups %xmm0, 0x7c(%rdi)
movups %xmm0, 0x8c(%rdi)
movups %xmm0, 0x9c(%rdi)
movups %xmm0, 0xb0(%rdi)
movups %xmm0, 0xc0(%rdi)
movups %xmm0, 0xd8(%rdi)
movups %xmm0, 0xe8(%rdi)
movups %xmm0, 0xf8(%rdi)
movups %xmm0, 0x108(%rdi)
movups %xmm0, 0x118(%rdi)
movq $0x0, 0x128(%rdi)
movl $0xb8, %edi
callq 0xa130
movq %rax, %r15
movq %rax, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x19d02
leaq 0x108(%rbx), %rdi
leaq -0x20(%rbp), %rsi
movq %r15, (%rsi)
callq 0x1a116
movl $0x20, %edi
callq 0xa130
leaq 0xb8(%rbx), %rcx
movl $0xf91e0602, (%rax)     # imm = 0xF91E0602
movq %rcx, 0x8(%rax)
movl $0x1, 0x10(%rax)
movw $0x101, 0x14(%rax)      # imm = 0x101
movq $0x0, 0x16(%rax)
movw $0x0, 0x1e(%rax)
movq 0xe8(%rbx), %rdi
movq %rax, 0xe8(%rbx)
testq %rdi, %rdi
je 0x19848
movl $0x20, %esi
callq 0xa0e0
movl $0x20, %edi
callq 0xa130
leaq 0xbc(%rbx), %rcx
movl $0xf91e0602, (%rax)     # imm = 0xF91E0602
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movw $0x101, 0x14(%rax)      # imm = 0x101
movq $0x0, 0x16(%rax)
movw $0x0, 0x1e(%rax)
movq 0xf0(%rbx), %rdi
movq %rax, 0xf0(%rbx)
testq %rdi, %rdi
je 0x1989b
movl $0x20, %esi
callq 0xa0e0
movl $0x38, %edi
callq 0xa130
leaq 0xc0(%rbx), %rcx
movups 0x1a7dd(%rip), %xmm0    # 0x34090
movups %xmm0, 0x10(%rax)
movups 0x1a7c2(%rip), %xmm0    # 0x34080
movups %xmm0, (%rax)
movq %rcx, 0x20(%rax)
movq $0x0, 0x28(%rax)
movl $0x0, 0x30(%rax)
movq 0xf8(%rbx), %rdi
movq %rax, 0xf8(%rbx)
testq %rdi, %rdi
je 0x198f1
movl $0x38, %esi
callq 0xa0e0
movl $0x38, %edi
callq 0xa130
leaq 0xc4(%rbx), %rcx
movq %rcx, (%rax)
movb $0x0, 0x8(%rax)
movw $0x1, 0xa(%rax)
movq $0x10000, 0xc(%rax)     # imm = 0x10000
movups 0x1a782(%rip), %xmm0    # 0x340a0
movups %xmm0, 0x14(%rax)
movups 0x1a787(%rip), %xmm0    # 0x340b0
movups %xmm0, 0x24(%rax)
movq 0x100(%rbx), %rdi
movq %rax, 0x100(%rbx)
testq %rdi, %rdi
je 0x1994a
movl $0x38, %esi
callq 0xa0e0
movq %r14, %rdi
callq 0x218e6
movl %eax, 0xd0(%rbx)
movq 0x108(%rbx), %rdx
leaq -0xb9a(%rip), %rsi      # 0x18dcc
movq %r14, %rdi
callq 0x218b0
movl 0xd0(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
movss 0x1a6ac(%rip), %xmm1    # 0x34030
divss %xmm0, %xmm1
movss %xmm1, 0x20(%rbx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x24(%rbx)
leaq -0x20(%rbp), %rdi
callq 0x19d8c
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x199d0
jmp 0x199d0
jmp 0x199d0
jmp 0x199d0
jmp 0x199d0
movq %rax, %r14
movl $0xb8, %esi
movq %r15, %rdi
callq 0xa0e0
jmp 0x199dc
movq %rax, %r14
jmp 0x199f7
movq %rax, %r14
leaq -0x20(%rbp), %rdi
callq 0x19d8c
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0x199f7
movq 0x128(%rbx), %rsi
subq %rdi, %rsi
callq 0xa0e0
movq 0x110(%rbx), %rdi
testq %rdi, %rdi
je 0x19a08
callq 0x19dc0
movq 0x100(%rbx), %rdi
testq %rdi, %rdi
je 0x19a1e
movl $0x38, %esi
callq 0xa0e0
movq $0x0, 0x100(%rbx)
movq 0xf8(%rbx), %rdi
testq %rdi, %rdi
je 0x19a3f
movl $0x38, %esi
callq 0xa0e0
movq $0x0, 0xf8(%rbx)
movq 0xf0(%rbx), %rdi
testq %rdi, %rdi
je 0x19a60
movl $0x20, %esi
callq 0xa0e0
movq $0x0, 0xf0(%rbx)
movq 0xe8(%rbx), %rdi
testq %rdi, %rdi
je 0x19a81
movl $0x20, %esi
callq 0xa0e0
movq $0x0, 0xe8(%rbx)
movq %r14, %rdi
callq 0xa4d0