// File: 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
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rcx
movq %rcx, (%rax)
movq -0x18(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x0, 0x10(%rax)
movl $0x0, 0x14(%rax)
movl $0x0, 0x18(%rax)
movl $0x0, 0x1c(%rax)
movq %rax, %rcx
addq $0x2c, %rcx
xorps %xmm0, %xmm0
movss %xmm0, 0x2c(%rax)
movq %rcx, %rax
addq $0x4, %rax
addq $0x80, %rcx
movq %rcx, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x60(%rbp), %rcx
xorps %xmm0, %xmm0
movss %xmm0, (%rax)
addq $0x4, %rax
cmpq %rcx, %rax
movq %rax, -0x58(%rbp)
jne 0x26c3f
movq -0x68(%rbp), %rdi
movq $0x0, 0xb0(%rdi)
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
movups %xmm0, 0xb8(%rdi)
movq $0x0, 0xc8(%rdi)
movq $0x0, 0xd8(%rdi)
movq $0x0, 0xe0(%rdi)
addq $0xe8, %rdi
movq %rdi, -0xb8(%rbp)
callq 0x27d10
movq -0x68(%rbp), %rdi
addq $0xf0, %rdi
movq %rdi, -0xb0(%rbp)
callq 0x27d10
movq -0x68(%rbp), %rdi
addq $0xf8, %rdi
movq %rdi, -0xa8(%rbp)
callq 0x27d40
movq -0x68(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
movq %rdi, -0xa0(%rbp)
callq 0x27d70
movq -0x68(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
movq %rdi, -0x98(%rbp)
callq 0x27da0
movaps -0x90(%rbp), %xmm0
movq -0x68(%rbp), %rax
movq %rax, %rdi
addq $0x118, %rdi            # imm = 0x118
movq %rdi, -0x78(%rbp)
movups %xmm0, 0x118(%rax)
movq $0x0, 0x128(%rax)
callq 0x27dc0
movl $0xb8, %edi
callq 0xe200
movq %rax, -0x70(%rbp)
jmp 0x26d4b
movq -0x70(%rbp), %rdi
movl $0x1000, %esi           # imm = 0x1000
callq 0x27de0
jmp 0x26d5b
movq -0x70(%rbp), %rsi
leaq -0x20(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x27f20
movq -0x68(%rbp), %rdi
movq -0xc0(%rbp), %rsi
addq $0x108, %rdi            # imm = 0x108
callq 0x27f50
jmp 0x26d88
movl $0x20, %edi
callq 0xe200
movq %rax, -0xc8(%rbp)
jmp 0x26d9b
movq -0xc8(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0xb8, %rsi
movl $0x1, %edx
callq 0x27f80
jmp 0x26db9
movq -0xc8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0xd8(%rbp)
callq 0x27ff0
movq -0x68(%rbp), %rdi
movq -0xd8(%rbp), %rsi
addq $0xe8, %rdi
callq 0x28020
movl $0x20, %edi
callq 0xe200
movq %rax, -0xd0(%rbp)
jmp 0x26dfa
movq -0xd0(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0xbc, %rsi
xorl %edx, %edx
callq 0x27f80
jmp 0x26e15
movq -0xd0(%rbp), %rsi
leaq -0x40(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
callq 0x27ff0
movq -0x68(%rbp), %rdi
movq -0xe8(%rbp), %rsi
addq $0xf0, %rdi
callq 0x28020
movl $0x38, %edi
callq 0xe200
movq %rax, -0xe0(%rbp)
jmp 0x26e56
movq -0xe0(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0xc0, %rsi
callq 0x28050
jmp 0x26e6f
movq -0xe0(%rbp), %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0xf8(%rbp)
callq 0x280c0
movq -0x68(%rbp), %rdi
movq -0xf8(%rbp), %rsi
addq $0xf8, %rdi
callq 0x280f0
movl $0x38, %edi
callq 0xe200
movq %rax, -0xf0(%rbp)
jmp 0x26eb0
movq -0xf0(%rbp), %rdi
movq -0x68(%rbp), %rsi
addq $0xc4, %rsi
callq 0x28120
jmp 0x26ec9
movq -0xf0(%rbp), %rsi
leaq -0x50(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x28190
movq -0x68(%rbp), %rdi
movq -0x108(%rbp), %rsi
addq $0x100, %rdi            # imm = 0x100
callq 0x281c0
movq -0x18(%rbp), %rdi
callq 0x414b0
movl %eax, -0xfc(%rbp)
jmp 0x26f08
movq -0x68(%rbp), %rdi
movl -0xfc(%rbp), %eax
movl %eax, 0xd0(%rdi)
movq -0x18(%rbp), %rax
movq %rax, -0x110(%rbp)
addq $0x108, %rdi            # imm = 0x108
callq 0x281f0
movq -0x110(%rbp), %rdi
movq %rax, %rdx
leaq -0x1190(%rip), %rsi     # 0x25db0
callq 0x41430
jmp 0x26f47
movq -0x68(%rbp), %rax
movl 0xd0(%rax), %ecx
cvtsi2ss %rcx, %xmm1
movss 0x4250a(%rip), %xmm0    # 0x69468
divss %xmm1, %xmm0
movss %xmm0, 0x20(%rax)
movl $0x1, 0x28(%rax)
xorps %xmm0, %xmm0
movss %xmm0, 0x24(%rax)
leaq -0x50(%rbp), %rdi
callq 0x28210
leaq -0x48(%rbp), %rdi
callq 0x28260
leaq -0x40(%rbp), %rdi
callq 0x282b0
leaq -0x38(%rbp), %rdi
callq 0x282b0
leaq -0x20(%rbp), %rdi
callq 0x28300
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x270d5
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0xb8, %esi
callq 0xe170
jmp 0x270d5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x270cc
movq -0xc8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x20, %esi
callq 0xe170
jmp 0x270cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x270c3
movq -0xd0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x20, %esi
callq 0xe170
jmp 0x270c3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x270ba
movq -0xe0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0xe170
jmp 0x270ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x270b1
movq -0xf0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
movl $0x38, %esi
callq 0xe170
jmp 0x270b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x28210
leaq -0x48(%rbp), %rdi
callq 0x28260
leaq -0x40(%rbp), %rdi
callq 0x282b0
leaq -0x38(%rbp), %rdi
callq 0x282b0
leaq -0x20(%rbp), %rdi
callq 0x28300
movq -0x78(%rbp), %rdi
callq 0x28360
movq -0x98(%rbp), %rdi
callq 0x283c0
movq -0xa0(%rbp), %rdi
callq 0x28210
movq -0xa8(%rbp), %rdi
callq 0x28260
movq -0xb0(%rbp), %rdi
callq 0x282b0
movq -0xb8(%rbp), %rdi
callq 0x282b0
movq -0x28(%rbp), %rdi
callq 0xe700
nopw %cs:(%rax,%rax)