// File: /workspace/github_cpp_makefile_star2_O1/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 0xb130
movq %rax, %r15
movq %rax, %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x1ad3e
leaq 0x108(%rbx), %rdi
leaq -0x20(%rbp), %rsi
movq %r15, (%rsi)
callq 0x1b152
movl $0x20, %edi
callq 0xb130
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 0x1a892
movl $0x20, %esi
callq 0xb0e0
movl $0x20, %edi
callq 0xb130
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 0x1a8e5
movl $0x20, %esi
callq 0xb0e0
movl $0x38, %edi
callq 0xb130
leaq 0xc0(%rbx), %rcx
movups 0x1a793(%rip), %xmm0    # 0x35090
movups %xmm0, 0x10(%rax)
movups 0x1a778(%rip), %xmm0    # 0x35080
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 0x1a93b
movl $0x38, %esi
callq 0xb0e0
movl $0x38, %edi
callq 0xb130
leaq 0xc4(%rbx), %rcx
movq %rcx, (%rax)
movb $0x0, 0x8(%rax)
movw $0x1, 0xa(%rax)
movq $0x10000, 0xc(%rax)     # imm = 0x10000
movups 0x1a738(%rip), %xmm0    # 0x350a0
movups %xmm0, 0x14(%rax)
movups 0x1a73d(%rip), %xmm0    # 0x350b0
movups %xmm0, 0x24(%rax)
movq 0x100(%rbx), %rdi
movq %rax, 0x100(%rbx)
testq %rdi, %rdi
je 0x1a994
movl $0x38, %esi
callq 0xb0e0
movq %r14, %rdi
callq 0x228ba
movl %eax, 0xd0(%rbx)
movq 0x108(%rbx), %rdx
leaq -0xb98(%rip), %rsi      # 0x19e18
movq %r14, %rdi
callq 0x22884
movl 0xd0(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %rax, %xmm0
movss 0x1a662(%rip), %xmm1    # 0x35030
divss %xmm0, %xmm1
movss %xmm1, 0x20(%rbx)
movabsq $0x100000000, %rax      # imm = 0x100000000
movq %rax, 0x24(%rbx)
leaq -0x20(%rbp), %rdi
callq 0x1adc8
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1aa1a
jmp 0x1aa1a
jmp 0x1aa1a
jmp 0x1aa1a
jmp 0x1aa1a
movq %rax, %r14
movl $0xb8, %esi
movq %r15, %rdi
callq 0xb0e0
jmp 0x1aa26
movq %rax, %r14
jmp 0x1aa26
movq %rax, %r14
leaq -0x20(%rbp), %rdi
callq 0x1adc8
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0x1aa41
movq 0x128(%rbx), %rsi
subq %rdi, %rsi
callq 0xb0e0
movq 0x110(%rbx), %rdi
testq %rdi, %rdi
je 0x1aa52
callq 0x1adfc
movq 0x100(%rbx), %rdi
testq %rdi, %rdi
je 0x1aa68
movl $0x38, %esi
callq 0xb0e0
movq $0x0, 0x100(%rbx)
movq 0xf8(%rbx), %rdi
testq %rdi, %rdi
je 0x1aa89
movl $0x38, %esi
callq 0xb0e0
movq $0x0, 0xf8(%rbx)
movq 0xf0(%rbx), %rdi
testq %rdi, %rdi
je 0x1aaaa
movl $0x20, %esi
callq 0xb0e0
movq $0x0, 0xf0(%rbx)
movq 0xe8(%rbx), %rdi
testq %rdi, %rdi
je 0x1aacb
movl $0x20, %esi
callq 0xb0e0
movq $0x0, 0xe8(%rbx)
movq %r14, %rdi
callq 0xb4d0