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

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

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

		if(c->enabled){
			update_env(c);
			if(!ch2) update_sweep(c);

			float pos = 0.0f;
			float prev_pos = 0.0f;
			float sample = 0.0f;

			while(update_freq(c, &pos)){
				c->duty_counter = (c->duty_counter + 1) & 7;
				sample += ((pos - prev_pos) / c->freq_inc) * (float)c->val;
				c->val = (c->duty & (1 << c->duty_counter)) ? 1 : -1;
				prev_pos = pos;
			}
			sample += ((pos - prev_pos) / c->freq_inc) * (float)c->val;
			sample = hipass(c, sample * (c->volume / 15.0f));

			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:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movl %edi, %r15d
imulq $0x70, %r15, %rax
leaq 0x8d72(%rip), %r14      # 0x115b0
cmpb $0x1, 0x1(%rax,%r14)
jne 0x8a8a
movl %edi, %ebx
addq %rax, %r14
movzwl 0xc(%r14), %eax
shll $0x5, %eax
movl $0x10000, %ecx          # imm = 0x10000
subl %eax, %ecx
cvtsi2ss %ecx, %xmm1
movss 0x49fe(%rip), %xmm0     # 0xd268
divss %xmm1, %xmm0
movaps %xmm0, %xmm1
divss 0x49e3(%rip), %xmm1     # 0xd25c
movss %xmm1, 0x14(%r14)
divss 0x49d9(%rip), %xmm0     # 0xd260
callq 0x51f0
divss 0x8d10(%rip), %xmm0     # 0x115a4
callq 0x50d0
cvttss2si %xmm0, %eax
cmpl $-0x2f, %eax
movl $0xffffffd0, %ecx       # imm = 0xFFFFFFD0
cmovgel %eax, %ecx
addl $0x30, %ecx
movl %ecx, 0x1c(%r14)
movss 0x14(%r14), %xmm0
mulss 0x49af(%rip), %xmm0     # 0xd26c
movss %xmm0, 0x14(%r14)
cmpq $0x0, 0x8ea5(%rip)      # 0x11770
je 0x8a8a
xorl %r12d, %r12d
leaq 0x8ccd(%rip), %r13      # 0x115a8
movq %r14, %rdi
callq 0x863a
cmpb $0x1, (%r14)
jne 0x8a79
movq %r14, %rdi
callq 0x85cd
testb %bl, %bl
jne 0x8901
movq %r14, %rdi
callq 0x8710
movss 0x14(%r14), %xmm0
movss 0x10(%r14), %xmm1
addss %xmm0, %xmm1
movss 0x36f3(%rip), %xmm7     # 0xc00c
ucomiss %xmm7, %xmm1
movaps %xmm1, %xmm3
movss 0x493c(%rip), %xmm8     # 0xd264
addss %xmm8, %xmm3
movaps %xmm0, %xmm2
jbe 0x8936
subss %xmm3, %xmm2
movaps %xmm7, %xmm4
cmpltss %xmm1, %xmm4
andnps %xmm1, %xmm4
xorps %xmm3, %xmm3
jbe 0x89c3
movl 0x18(%r14), %edx
movl 0x5c(%r14), %ecx
movl 0x60(%r14), %eax
xorps %xmm1, %xmm1
xorps %xmm5, %xmm5
movaps %xmm2, %xmm3
incl %eax
andl $0x7, %eax
btl %eax, %ecx
movl $0x0, %esi
adcl $-0x1, %esi
orl $0x1, %esi
movaps %xmm0, %xmm6
subss %xmm2, %xmm6
addss %xmm4, %xmm6
ucomiss %xmm7, %xmm6
movaps %xmm6, %xmm4
addss %xmm8, %xmm4
movaps %xmm0, %xmm2
jbe 0x898d
subss %xmm4, %xmm2
movaps %xmm3, %xmm4
subss %xmm5, %xmm4
xorps %xmm5, %xmm5
cvtsi2ss %edx, %xmm5
divss %xmm0, %xmm4
mulss %xmm4, %xmm5
addss %xmm5, %xmm1
movaps %xmm7, %xmm4
cmpltss %xmm6, %xmm4
andnps %xmm6, %xmm4
movl %esi, %edx
movaps %xmm3, %xmm5
ja 0x8958
movl %eax, 0x60(%r14)
movl %esi, 0x18(%r14)
jmp 0x89c6
xorps %xmm1, %xmm1
subss %xmm3, %xmm2
movss %xmm4, 0x10(%r14)
divss %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2ssl 0x18(%r14), %xmm0
mulss %xmm2, %xmm0
xorps %xmm2, %xmm2
cvtsi2ssl 0x4(%r14), %xmm2
addss %xmm1, %xmm0
divss 0x487a(%rip), %xmm2     # 0xd270
mulss %xmm0, %xmm2
movaps %xmm2, %xmm0
subss 0x58(%r14), %xmm0
movss 0x8b95(%rip), %xmm1     # 0x115a0
mulss %xmm0, %xmm1
subss %xmm1, %xmm2
movss %xmm2, 0x58(%r14)
cmpb $0x0, (%r15,%r13)
jne 0x8a79
movsbl 0x2(%r14), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
mulss 0x4840(%rip), %xmm0     # 0xd274
mulss %xmm0, %xmm1
mulss 0x8d38(%rip), %xmm1     # 0x11778
movq 0x8d39(%rip), %rax      # 0x11780
addss (%rax,%r12,4), %xmm1
movss %xmm1, (%rax,%r12,4)
movsbl 0x3(%r14), %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
mulss %xmm0, %xmm1
mulss 0x8d1d(%rip), %xmm1     # 0x11788
addss 0x4(%rax,%r12,4), %xmm1
movss %xmm1, 0x4(%rax,%r12,4)
addq $0x2, %r12
cmpq %r12, 0x8cec(%rip)      # 0x11770
ja 0x88db
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq