// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %r12d
imulq $0x70, %r12, %rax
leaq 0xa09d(%rip), %r14      # 0x125b0
cmpb $0x1, 0x1(%rax,%r14)
jne 0x86db
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 0x4d59(%rip), %xmm0     # 0xd298
divss %xmm1, %xmm0
movq %r14, %rdi
callq 0x8275
movss 0x14(%r14), %xmm0
mulss 0x4d43(%rip), %xmm0     # 0xd29c
movss %xmm0, 0x14(%r14)
xorl %r13d, %r13d
movq 0xa207(%rip), %rbp      # 0x12770
leaq 0x14(%rsp), %r15
cmpq %r13, %rbp
jbe 0x86db
movq %r14, %rdi
callq 0x83ad
cmpb $0x1, (%r14)
jne 0x86d2
movq %r14, %rdi
callq 0x8350
testb %bl, %bl
jne 0x85a4
movq %r14, %rdi
callq 0x843b
movq 0xa1cc(%rip), %rbp      # 0x12770
andl $0x0, 0x14(%rsp)
xorps %xmm0, %xmm0
movss %xmm0, 0xc(%rsp)
movss %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x83f3
testb %al, %al
je 0x8621
movl 0x5c(%r14), %eax
movl 0x60(%r14), %ecx
incl %ecx
andl $0x7, %ecx
movl %ecx, 0x60(%r14)
movss 0x14(%rsp), %xmm0
movaps %xmm0, %xmm1
subss 0xc(%rsp), %xmm1
divss 0x14(%r14), %xmm1
cvtsi2ssl 0x18(%r14), %xmm2
mulss %xmm1, %xmm2
btl %ecx, %eax
movl $0x0, %eax
adcl $-0x1, %eax
movss 0x10(%rsp), %xmm1
addss %xmm2, %xmm1
movss %xmm1, 0x10(%rsp)
orl $0x1, %eax
movl %eax, 0x18(%r14)
movss %xmm0, 0xc(%rsp)
jmp 0x85b8
movss 0x14(%rsp), %xmm0
subss 0xc(%rsp), %xmm0
divss 0x14(%r14), %xmm0
cvtsi2ssl 0x18(%r14), %xmm1
mulss %xmm0, %xmm1
cvtsi2ssl 0x4(%r14), %xmm2
addss 0x10(%rsp), %xmm1
divss 0x4c4f(%rip), %xmm2     # 0xd2a0
mulss %xmm1, %xmm2
movaps %xmm2, %xmm0
subss 0x58(%r14), %xmm0
movss 0x9f3a(%rip), %xmm1     # 0x125a0
mulss %xmm0, %xmm1
subss %xmm1, %xmm2
movss %xmm2, 0x58(%r14)
leaq 0x9f2d(%rip), %rax      # 0x125a8
cmpb $0x0, (%r12,%rax)
jne 0x86d2
mulss 0x4c1a(%rip), %xmm0     # 0xd2a4
movq 0xa0ef(%rip), %rax      # 0x12780
movzwl 0x2(%r14), %ecx
movd %ecx, %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
movsd (%rax,%r13,4), %xmm1
movss 0xa0cc(%rip), %xmm2     # 0x12788
movss 0xa0b4(%rip), %xmm3     # 0x12778
unpcklps %xmm2, %xmm3            # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
mulps %xmm0, %xmm3
addps %xmm1, %xmm3
movlps %xmm3, (%rax,%r13,4)
addq $0x2, %r13
jmp 0x856e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq