// File: /workspace/github_cpp_makefile_star2_O1/khedoros[P]khednes/apu.cpp
// Function: apu::get_sample(int)
int apu::get_sample(int voice) {
    if(!enabled[voice] || length[voice] == 0 || wavelength_count_reset[voice] == 0) {
        //cout<<"Returning 0"<<endl;
        //cout<<"Enabled: "<<enabled[voice]<<" wavelength: "<<wavelength_count[voice]<<endl;
        return 0;
    }
    //cout<<"Generating something."<<endl;
    int to_gen = 0;
    float interleave = float(CLK_PER_FRAME * 20) / float(SAMPLE_RATE);
    if(interleave - int(interleave) >= 0.5) to_gen = int(interleave) + 1;
    else to_gen = int(interleave);
    //cout<<"to_gen: "<<to_gen<<endl;
    int accum = 0;
    int vol = 0;
    while(to_gen > 0) {
        //cout<<to_gen<<" left to go!"<<endl;
        int this_one = 0;
        if(wavelength_count[voice] == 0) clock_duty(voice);
        if(wavelength_count[voice] >= to_gen) {
            this_one = to_gen;
        }
        else this_one = wavelength_count[voice];
            
        //cout<<"Doing "<<this_one<<" for this iteration"<<endl;
        switch(voice) {
            case SQ1:
                vol = sq1_duty();
                break;
            case SQ2:
                vol = sq2_duty();
                break;
            case TRI:
                vol = tri_duty();
                break;
            case NOISE:
                vol = noise_duty();
                break;
        }
        //if(vol != 0) cout<<"Vol: "<<vol<<endl;
        to_gen -= this_one;
        wavelength_count[voice] -= this_one;
        accum += (vol * this_one);
        if(wavelength_count[voice] == 0) {
            //clock the channel's duty thingie
            clock_duty(voice);
        }
    }
    //cout<<"Accum: "<<accum<<" returning: "<<accum/41<<endl;
    return accum / 41;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movslq %esi, %r15
xorl %eax, %eax
cmpb $0x1, 0xb4(%rdi,%r15)
jne 0x11b48
movq %rdi, %r14
cmpl $0x0, 0x174(%rdi,%r15,4)
je 0x11b48
cmpl $0x0, 0x188(%r14,%r15,4)
je 0x11b48
movl $0x29, %ebp
xorl %ebx, %ebx
movl %esi, %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movl %esi, 0xc(%rsp)
cmpl $0x0, 0x19c(%r14,%r15,4)
jne 0x11979
movq %r14, %rdi
callq 0x11c4c
movl 0xc(%rsp), %esi
movl 0x19c(%r14,%r15,4), %eax
cmpl %ebp, %eax
movl %ebp, %r12d
cmovll %eax, %r12d
cmpl $0x3, %esi
ja 0x11b02
movq 0x10(%rsp), %rcx
leaq 0x43e1(%rip), %rdx      # 0x15d80
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x1, 0xe4(%r14)
jne 0x11ab0
movl 0x104(%r14), %ecx
movslq 0x1b0(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1cb91(%rip), %rdi     # 0x2e560
addq %rdi, %rcx
movzbl (%rdx,%rcx), %ebx
imull 0xf0(%r14), %ebx
jmp 0x11b02
movslq 0x1b8(%r14), %rcx
leaq 0x1cb4f(%rip), %rdx     # 0x2e540
movzbl (%rcx,%rdx), %ebx
jmp 0x11b02
cmpb $0x1, 0xe7(%r14)
jne 0x11a6f
movl 0x110(%r14), %ecx
movslq 0x1bc(%r14), %rdx
movq %rcx, %rdi
shlq $0xf, %rdi
subq %rcx, %rdi
leaq 0xcadd(%rip), %rcx      # 0x1e500
addq %rcx, %rdi
movzbl (%rdx,%rdi), %ecx
imull 0xfc(%r14), %ecx
jmp 0x11a9d
cmpb $0x1, 0xe5(%r14)
jne 0x11ada
movl 0x108(%r14), %ecx
movslq 0x1b4(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1cb05(%rip), %rdi     # 0x2e560
addq %rdi, %rcx
movzbl (%rdx,%rcx), %ebx
imull 0xf4(%r14), %ebx
jmp 0x11b02
movl 0x110(%r14), %ecx
movslq 0x1bc(%r14), %rdx
movq %rcx, %rdi
shlq $0xf, %rdi
subq %rcx, %rdi
leaq 0xca72(%rip), %rcx      # 0x1e500
addq %rcx, %rdi
movzbl (%rdx,%rdi), %ecx
imull 0xc8(%r14), %ecx
leal (%rcx,%rcx,2), %edx
leal (%rcx,%rcx,2), %ebx
addl $0x3, %ebx
testl %edx, %edx
cmovnsl %edx, %ebx
sarl $0x2, %ebx
jmp 0x11b02
movl 0x104(%r14), %ecx
movslq 0x1b0(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1ca97(%rip), %rdi     # 0x2e560
addq %rdi, %rcx
movzbl (%rdx,%rcx), %ebx
imull 0xbc(%r14), %ebx
jmp 0x11b02
movl 0x108(%r14), %ecx
movslq 0x1b4(%r14), %rdx
shlq $0x4, %rcx
leaq 0x1ca6d(%rip), %rdi     # 0x2e560
addq %rdi, %rcx
movzbl (%rdx,%rcx), %ebx
imull 0xc0(%r14), %ebx
movl %eax, %ecx
subl %r12d, %ecx
movl %ecx, 0x19c(%r14,%r15,4)
cmpl %ebp, %eax
jg 0x11b1f
movq %r14, %rdi
callq 0x11c4c
movl 0xc(%rsp), %esi
subl %r12d, %ebp
imull %ebx, %r12d
addl %r12d, %r13d
testl %ebp, %ebp
jg 0x11962
movslq %r13d, %rax
imulq $0x63e7063f, %rax, %rax # imm = 0x63E7063F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x24, %rax
addl %ecx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop