// File: /workspace/github_c_makefile_star2_O0/baines[P]MiniGBS/audio.c
// Function: chan_trigger
void chan_trigger(int i){
	struct chan* c = chans + i;

	if(cfg.debug_mode){
		static const char* cname[] = { "sq1", "sq2", "wave", "noise" };
		debug_msg("Trigger %s", cname[i]);
	}

	chan_enable(i, 1);
	c->volume = c->volume_init;

	// volume envelope
	{
		uint8_t val = mem[0xFF12 + (i*5)];

		c->env.step    = val & 0x07;
		c->env.up      = val & 0x08;
		c->env.inc     = c->env.step ? (64.0f / (float)c->env.step) / FREQ : 8.0f / FREQ;
		c->env.counter = 0.0f;
	}

	// freq sweep
	if(i == 0){
		uint8_t val = mem[0xFF10];

		c->sweep.freq    = c->freq;
		c->sweep.rate    = (val >> 4) & 0x07;
		c->sweep.up      = !(val & 0x08);
		c->sweep.shift   = (val & 0x07);
		c->sweep.inc     = c->sweep.rate ? (128.0f / (float)(c->sweep.rate)) / FREQ : 0;
		c->sweep.counter = nexttowardf(1.0f, 1.1f);
	}

	if(i == 2){ // wave
		c->val = 0;
	} else if(i == 3){ // noise
		c->lfsr_reg = 0xFFFF;
		c->val = -1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl %edi, -0x4(%rbp)
movslq -0x4(%rbp), %rcx
leaq 0xa2da(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
leaq 0xa160(%rip), %rax      # 0x16918
cmpl $0x0, (%rax)
je 0xc7da
movslq -0x4(%rbp), %rcx
leaq 0x9e98(%rip), %rax      # 0x16660
movq (%rax,%rcx,8), %rsi
leaq 0x51f9(%rip), %rdi      # 0x119cc
movb $0x0, %al
callq 0xacd0
movl -0x4(%rbp), %edi
movl $0x1, %esi
callq 0xaf40
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
leaq 0xa114(%rip), %rax      # 0x16910
movq (%rax), %rax
imull $0x5, -0x4(%rbp), %ecx
addl $0xff12, %ecx           # imm = 0xFF12
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x11(%rbp)
movzbl -0x11(%rbp), %ecx
andl $0x7, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movzbl -0x11(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %cl
movq -0x10(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x34(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
je 0xc869
movq -0x10(%rbp), %rax
cvtsi2ssl 0x30(%rax), %xmm1
movss 0x505a(%rip), %xmm0     # 0x118ac
divss %xmm1, %xmm0
movss 0x5022(%rip), %xmm1     # 0x11880
divss %xmm1, %xmm0
movss %xmm0, -0x18(%rbp)
jmp 0xc878
movss 0x5037(%rip), %xmm0     # 0x118a8
movss %xmm0, -0x18(%rbp)
jmp 0xc878
movss -0x18(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x3c(%rax)
movq -0x10(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, 0x38(%rax)
cmpl $0x0, -0x4(%rbp)
jne 0xc963
leaq 0xa06d(%rip), %rax      # 0x16910
movq (%rax), %rax
movb 0xff10(%rax), %al
movb %al, -0x12(%rbp)
movq -0x10(%rbp), %rax
movw 0xc(%rax), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x40(%rax)
movzbl -0x12(%rbp), %ecx
sarl $0x4, %ecx
andl $0x7, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x44(%rax)
movzbl -0x12(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %cl
xorb $-0x1, %cl
movq -0x10(%rbp), %rax
andb $0x1, %cl
movb %cl, 0x48(%rax)
movzbl -0x12(%rbp), %ecx
andl $0x7, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4c(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x44(%rax)
je 0xc92a
movq -0x10(%rbp), %rax
cvtsi2ssl 0x44(%rax), %xmm1
movss 0x4f9d(%rip), %xmm0     # 0x118b0
divss %xmm1, %xmm0
movss 0x4f61(%rip), %xmm1     # 0x11880
divss %xmm1, %xmm0
movss %xmm0, -0x1c(%rbp)
jmp 0xc934
xorps %xmm0, %xmm0
movss %xmm0, -0x1c(%rbp)
jmp 0xc934
movss -0x1c(%rbp), %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x54(%rax)
movq %rsp, %rax
flds 0x4f69(%rip)            # 0x118b4
fstpt (%rax)
movss 0x36af(%rip), %xmm0     # 0x10004
callq 0x5360
movq -0x10(%rbp), %rax
movss %xmm0, 0x50(%rax)
cmpl $0x2, -0x4(%rbp)
jne 0xc976
movq -0x10(%rbp), %rax
movl $0x0, 0x18(%rax)
jmp 0xc993
cmpl $0x3, -0x4(%rbp)
jne 0xc991
movq -0x10(%rbp), %rax
movw $0xffff, 0x64(%rax)     # imm = 0xFFFF
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
jmp 0xc993
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)