// File: mindbeast[P]rnes/apu.cpp
// Function: Rnes::Apu::restore(Rnes::ApuState const&)
void Apu::restore(const ApuState &pb) {
  // Restore sub-units.
  pulseA->restore(pb.pulsea());
  pulseB->restore(pb.pulseb());
  triangle->restore(pb.triangle());
  noise->restore(pb.noise());

  /*
  // Apu register file.
  repeated uint32 reg = 1;
  optional uint32 sampleRate = 2;
  optional uint32 fourFrameCount = 3;
  optional uint32 fiveFrameCount = 4;
  optional uint32 frameDivider = 5;
  optional uint32 step = 6;
  optional uint32 halfTimerDivider = 7;
  optional uint32 samplerDivider = 8;
  optional float clksPerSample = 9;
  optional float currentSampleClk = 10;
  optional uint32 nextSampleCountdown = 11;
  */

  for (int i = 0; i < pb.reg_size(); i++) {
    regs[i] = pb.reg(i);
  }

  sampleRate = pb.samplerate();
  fourFrameCount = pb.fourframecount();
  fiveFrameCount = pb.fiveframecount();
  frameDivider = pb.framedivider();
  step = pb.step();
  halfTimerDivider = pb.halftimerdivider();
  samplerDivider = pb.samplerdivider();
  clksPerSample = pb.clkspersample();
  currentSampleClk = pb.currentsampleclk();
  nextSampleCountdown = pb.nextsamplecountdown();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x28(%rbp)
addq $0xe8, %rdi
callq 0x27370
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x27b00
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x2c050
movq -0x28(%rbp), %rdi
addq $0xf0, %rdi
callq 0x27370
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x27b20
movq -0x38(%rbp), %rdi
movq %rax, %rsi
callq 0x2c050
movq -0x28(%rbp), %rdi
addq $0xf8, %rdi
callq 0x27390
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x27b40
movq -0x30(%rbp), %rdi
movq %rax, %rsi
callq 0x2c420
movq -0x28(%rbp), %rdi
addq $0x100, %rdi            # imm = 0x100
callq 0x273b0
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
callq 0x27b60
movq -0x20(%rbp), %rdi
movq %rax, %rsi
callq 0x2c7f0
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
callq 0x27b80
movl %eax, %ecx
movl -0x44(%rbp), %eax
cmpl %ecx, %eax
jge 0x26b01
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x27ba0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movb %cl, %dl
movslq -0x14(%rbp), %rcx
movb %dl, 0xb8(%rax,%rcx)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x26abf
movq -0x10(%rbp), %rdi
callq 0x27bd0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0xd0(%rax)
movq -0x10(%rbp), %rdi
callq 0x27bf0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
movq %rcx, 0xd8(%rax)
movq -0x10(%rbp), %rdi
callq 0x27c10
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, %ecx
movq %rcx, 0xe0(%rax)
movq -0x10(%rbp), %rdi
callq 0x27c30
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x10(%rbp), %rdi
callq 0x27c50
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0x10(%rbp), %rdi
callq 0x27c70
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x10(%rbp), %rdi
callq 0x27c90
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rdi
callq 0x27cb0
movq -0x28(%rbp), %rax
movss %xmm0, 0x20(%rax)
movq -0x10(%rbp), %rdi
callq 0x27cd0
movq -0x28(%rbp), %rax
movss %xmm0, 0x24(%rax)
movq -0x10(%rbp), %rdi
callq 0x27cf0
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x28(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)