// File: /workspace/github_c_makefile_star2_O2/baines[P]MiniGBS/audio.c
// Function: update_wave
void update_wave(void){
	struct chan* c = chans + 2;
	if(!c->powered) return;

	float freq = 4194304.0f / (float)((2048 - c->freq) << 5);
	set_note_freq(c, freq);

	c->freq_inc *= 16.0f;

	for(int i = 0; i < nsamples; i+=2){
		update_len(c);

		if(c->enabled){
			float pos = 0.0f;
			float prev_pos = 0.0f;
			float sample = 0.0f;

			c->sample = wave_sample(c->val, c->volume);

			while(update_freq(c, &pos)){
				c->val = (c->val + 1) & 31;
				sample += ((pos - prev_pos) / c->freq_inc) * (float)c->sample;
				c->sample = wave_sample(c->val, c->volume);
				prev_pos = pos;
			}
			sample += ((pos - prev_pos) / c->freq_inc) * (float)c->sample;

			if(c->volume > 0){
				float diff = (float[]){ 7.5f, 3.75f, 1.5f }[c->volume - 1];
				sample = hipass(c, (sample - diff) / 7.5f);

				if(!muted[c-chans]){
					samples[i+0] += sample * 0.25f * c->on_left * vol_l;
					samples[i+1] += sample * 0.25f * c->on_right * vol_r;
				}
			}
		}
	}
}

// Assembly:
cmpb $0x1, 0x9fa0(%rip)      # 0x12691
jne 0x8907
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movzwl 0x9f90(%rip), %eax      # 0x1269c
shll $0x5, %eax
movl $0x10000, %ecx          # imm = 0x10000
subl %eax, %ecx
cvtsi2ss %ecx, %xmm1
movss 0x4b76(%rip), %xmm0     # 0xd298
divss %xmm1, %xmm0
leaq 0x9f63(%rip), %rbx      # 0x12690
movq %rbx, %rdi
callq 0x8275
movss 0x9f67(%rip), %xmm0     # 0x126a4
mulss 0x4b63(%rip), %xmm0     # 0xd2a8
movss %xmm0, 0x9f57(%rip)     # 0x126a4
movq 0xa01c(%rip), %r15      # 0x12770
movss 0x9e44(%rip), %xmm0     # 0x125a0
movss %xmm0, 0xc(%rsp)
movb 0x9e41(%rip), %bpl      # 0x125aa
movq 0xa010(%rip), %r12      # 0x12780
movss 0xa010(%rip), %xmm0     # 0x12788
movss 0x9ff8(%rip), %xmm1     # 0x12778
unpcklps %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movaps %xmm1, 0x20(%rsp)
xorl %r13d, %r13d
leaq 0x8(%rsp), %r14
cmpq %r13, %r15
jbe 0x88f9
movq %rbx, %rdi
callq 0x83ad
cmpb $0x1, 0x9ee8(%rip)      # 0x12690
jne 0x88f0
andl $0x0, 0x8(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, (%rsp)
movl 0x9ee7(%rip), %edi      # 0x126a8
movss %xmm0, 0x4(%rsp)
movl 0x9ec7(%rip), %esi      # 0x12694
callq 0x8908
movb %al, 0x9f24(%rip)       # 0x126fc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x83f3
testb %al, %al
je 0x8834
movl 0x9ebb(%rip), %edi      # 0x126a8
incl %edi
andl $0x1f, %edi
movl %edi, 0x9eb0(%rip)      # 0x126a8
movss 0x8(%rsp), %xmm0
movaps %xmm0, %xmm1
subss (%rsp), %xmm1
divss 0x9e96(%rip), %xmm1     # 0x126a4
movzbl 0x9ee7(%rip), %eax      # 0x126fc
cvtsi2ss %eax, %xmm2
mulss %xmm1, %xmm2
movss 0x4(%rsp), %xmm1
addss %xmm2, %xmm1
movss %xmm1, 0x4(%rsp)
movss %xmm0, (%rsp)
jmp 0x87c7
movl 0x9e5a(%rip), %eax      # 0x12694
testl %eax, %eax
jle 0x88f0
movss 0x8(%rsp), %xmm0
subss (%rsp), %xmm0
divss 0x9e4f(%rip), %xmm0     # 0x126a4
movzbl 0x9ea0(%rip), %ecx      # 0x126fc
cvtsi2ss %ecx, %xmm1
mulss %xmm0, %xmm1
addss 0x4(%rsp), %xmm1
movsd 0x4a5e(%rip), %xmm0     # 0xd2d0
movsd %xmm0, 0x10(%rsp)
movl $0x3fc00000, 0x18(%rsp) # imm = 0x3FC00000
decl %eax
subss 0x10(%rsp,%rax,4), %xmm1
divss 0x4a1c(%rip), %xmm1     # 0xd2ac
movaps %xmm1, %xmm0
subss 0x9e4d(%rip), %xmm0     # 0x126e8
movaps %xmm0, %xmm2
mulss 0xc(%rsp), %xmm2
subss %xmm2, %xmm1
movss %xmm1, 0x9e38(%rip)     # 0x126e8
testb $0x1, %bpl
jne 0x88f0
mulss 0x49e6(%rip), %xmm0     # 0xd2a4
movzwl 0x9dcd(%rip), %eax      # 0x12692
movd %eax, %xmm1
xorps %xmm2, %xmm2
punpcklbw %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3],xmm1[4],xmm2[4],xmm1[5],xmm2[5],xmm1[6],xmm2[6],xmm1[7],xmm2[7]
punpcklwd %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1],xmm1[2],xmm2[2],xmm1[3],xmm2[3]
cvtdq2ps %xmm1, %xmm1
unpcklps %xmm0, %xmm0            # xmm0 = xmm0[0,0,1,1]
mulps %xmm1, %xmm0
mulps 0x20(%rsp), %xmm0
movsd (%r12,%r13,4), %xmm1
addps %xmm0, %xmm1
movlps %xmm1, (%r12,%r13,4)
addq $0x2, %r13
jmp 0x8790
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq