// File: /workspace/github_c_makefile_star2_O0/baines[P]MiniGBS/audio.c
// Function: audio_write
void audio_write(uint16_t addr, uint8_t val){

	if(!cfg.subdued && mem[addr] != val){
		ui_regs_set(addr, audio_rate / 8);
	}

	int i = (addr - 0xFF10)/5;

	if(cfg.debug_mode){
		if(addr <= 0xFF26){
			int j = (addr - 0xFF10)%5;
			debug_msg("Audio write: %4x / NR%1d%1d <- %2x", addr, i+1, j, val);
		} else {
			debug_msg("Audio write: %4x <- %2x", addr, val);
		}
	}

	switch(addr){

		case 0xFF12:
		case 0xFF17:
		case 0xFF21: {
			chans[i].volume_init = val >> 4;
			chans[i].powered = val >> 3;

			// "zombie mode" stuff, needed for Prehistorik Man and probably others
			if(chans[i].powered && chans[i].enabled){

				if((chans[i].env.step == 0 && chans[i].env.inc != 0)){
					if(val & 0x08){
						debug_msg("(zombie vol++)");
						chans[i].volume++;
					} else {
						debug_msg("(zombie vol+=2)");
						chans[i].volume+=2;
					}
				} else if(chans[i].env.step != (val & 0x07)) {
					debug_msg("(zombie swap)");
					chans[i].volume = 16 - chans[i].volume;
				}

				chans[i].volume &= 0x0F;
				chans[i].env.step = val & 0x07;
			}
		} break;

		case 0xFF1C:
			chans[i].volume = chans[i].volume_init = (val >> 5) & 0x03;
			break;

		case 0xFF11:
		case 0xFF16:
		case 0xFF20:
			chans[i].len.load = val & 0x3f;
			chans[i].duty = duty_lookup[val >> 6];
			chan_update_len(i);
			break;

		case 0xFF1B:
			chans[i].len.load = val;
			chan_update_len(i);
			break;

		case 0xFF13:
		case 0xFF18:
		case 0xFF1D:
			chans[i].freq &= 0xFF00;
			chans[i].freq |= val;
			break;

		case 0xFF1A:
			chans[i].powered = val & 0x80;
			chan_enable(i, val & 0x80);
			break;

		case 0xFF14:
		case 0xFF19:
		case 0xFF1E:
			chans[i].freq &= 0x00FF;
			chans[i].freq |= ((val & 0x07) << 8);
		case 0xFF23:
			chans[i].len.enabled = val & 0x40;
			if(val & 0x80){
				chan_trigger(i);
			}
			break;

		case 0xFF22:
			chans[3].freq = val >> 4;
			chans[3].lfsr_wide = !(val & 0x08);
			chans[3].lfsr_div = val & 0x07;
			break;

		case 0xFF24:
			vol_l = ((val >> 4) & 0x07) / 7.0f;
			vol_r = (val & 0x07) / 7.0f;
			break;

		case 0xFF25: {
			for(int i = 0; i < 4; ++i){
				chans[i].on_left  = (val >> (4 + i)) & 1;
				chans[i].on_right = (val >> i) & 1;
			}
		} break;
	}

	mem[addr] = val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %sil, %al
movw %di, %cx
movw %cx, -0x2(%rbp)
movb %al, -0x3(%rbp)
leaq 0x9eec(%rip), %rax      # 0x16918
testb $0x1, 0x6(%rax)
jne 0xca70
leaq 0x9ed7(%rip), %rax      # 0x16910
movq (%rax), %rax
movzwl -0x2(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movzbl -0x3(%rbp), %ecx
cmpl %ecx, %eax
je 0xca70
movw -0x2(%rbp), %ax
movss 0xa238(%rip), %xmm0     # 0x16c90
movss 0x4e24(%rip), %xmm1     # 0x11884
divss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movzwl %ax, %edi
callq 0xd790
movzwl -0x2(%rbp), %eax
subl $0xff10, %eax           # imm = 0xFF10
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, -0x8(%rbp)
leaq 0x9e8d(%rip), %rax      # 0x16918
cmpl $0x0, (%rax)
je 0xcae9
movzwl -0x2(%rbp), %eax
cmpl $0xff26, %eax           # imm = 0xFF26
jg 0xcad1
movzwl -0x2(%rbp), %eax
subl $0xff10, %eax           # imm = 0xFF10
movl $0x5, %ecx
cltd
idivl %ecx
movl %edx, -0xc(%rbp)
movzwl -0x2(%rbp), %esi
movl -0x8(%rbp), %edx
addl $0x1, %edx
movl -0xc(%rbp), %ecx
movzbl -0x3(%rbp), %r8d
leaq 0x4f0f(%rip), %rdi      # 0x119d7
movb $0x0, %al
callq 0xacd0
jmp 0xcae7
movzwl -0x2(%rbp), %esi
movzbl -0x3(%rbp), %edx
leaq 0x4f1a(%rip), %rdi      # 0x119fa
movb $0x0, %al
callq 0xacd0
jmp 0xcae9
movzwl -0x2(%rbp), %eax
addl $0xffff00ef, %eax       # imm = 0xFFFF00EF
movl %eax, %ecx
movq %rcx, -0x18(%rbp)
subl $0x14, %eax
ja 0xcf79
movq -0x18(%rbp), %rax
leaq 0x4dd4(%rip), %rcx      # 0x118e0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl -0x3(%rbp), %ecx
sarl $0x4, %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9f59(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movzbl -0x3(%rbp), %eax
sarl $0x3, %eax
cmpl $0x0, %eax
setne %cl
movslq -0x8(%rbp), %rdx
leaq 0x9f37(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
andb $0x1, %cl
movb %cl, 0x1(%rax)
movslq -0x8(%rbp), %rcx
leaq 0x9f1f(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
testb $0x1, 0x1(%rax)
je 0xccc4
movslq -0x8(%rbp), %rcx
leaq 0x9f03(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
testb $0x1, (%rax)
je 0xccc4
movslq -0x8(%rbp), %rcx
leaq 0x9ee8(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x30(%rax)
jne 0xcc2e
movslq -0x8(%rbp), %rcx
leaq 0x9ecc(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movss 0x3c(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0xcbcc
jp 0xcbcc
jmp 0xcc2e
movzbl -0x3(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0xcc03
leaq 0x4e33(%rip), %rdi      # 0x11a12
movb $0x0, %al
callq 0xacd0
movslq -0x8(%rbp), %rcx
leaq 0x9e8f(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0xcc2c
leaq 0x4e17(%rip), %rdi      # 0x11a21
movb $0x0, %al
callq 0xacd0
movslq -0x8(%rbp), %rcx
leaq 0x9e64(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
addl $0x2, %ecx
movl %ecx, 0x4(%rax)
jmp 0xcc8d
movslq -0x8(%rbp), %rcx
leaq 0x9e47(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %eax
movzbl -0x3(%rbp), %ecx
andl $0x7, %ecx
cmpl %ecx, %eax
je 0xcc8b
leaq 0x4ddc(%rip), %rdi      # 0x11a31
movb $0x0, %al
callq 0xacd0
movslq -0x8(%rbp), %rcx
leaq 0x9e19(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl $0x10, %ecx
subl 0x4(%rax), %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9dff(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0xcc8d
movslq -0x8(%rbp), %rcx
leaq 0x9de8(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %ecx
andl $0xf, %ecx
movl %ecx, 0x4(%rax)
movzbl -0x3(%rbp), %ecx
andl $0x7, %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9dc6(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x30(%rax)
jmp 0xcf79
movzbl -0x3(%rbp), %ecx
sarl $0x5, %ecx
andl $0x3, %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9da2(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movslq -0x8(%rbp), %rdx
leaq 0x9d8d(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
jmp 0xcf79
movzbl -0x3(%rbp), %ecx
andl $0x3f, %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9d6c(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movzbl -0x3(%rbp), %eax
sarl $0x6, %eax
movslq %eax, %rcx
leaq 0x4c51(%rip), %rax      # 0x11980
movl (%rax,%rcx,4), %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9d43(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x5c(%rax)
movl -0x8(%rbp), %edi
callq 0xc9a0
jmp 0xcf79
movzbl -0x3(%rbp), %ecx
movslq -0x8(%rbp), %rdx
leaq 0x9d1d(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x20(%rax)
movl -0x8(%rbp), %edi
callq 0xc9a0
jmp 0xcf79
movslq -0x8(%rbp), %rcx
leaq 0x9cfb(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movzwl 0xc(%rax), %ecx
andl $0xff00, %ecx           # imm = 0xFF00
movw %cx, 0xc(%rax)
movzbl -0x3(%rbp), %edx
movslq -0x8(%rbp), %rcx
leaq 0x9cd7(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movzwl 0xc(%rax), %ecx
orl %edx, %ecx
movw %cx, 0xc(%rax)
jmp 0xcf79
movzbl -0x3(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %cl
movslq -0x8(%rbp), %rdx
leaq 0x9ca7(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
andb $0x1, %cl
movb %cl, 0x1(%rax)
movl -0x8(%rbp), %edi
movzbl -0x3(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
movzbl %al, %esi
andl $0x1, %esi
callq 0xaf40
jmp 0xcf79
movslq -0x8(%rbp), %rcx
leaq 0x9c6d(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movzwl 0xc(%rax), %ecx
andl $0xff, %ecx
movw %cx, 0xc(%rax)
movzbl -0x3(%rbp), %edx
andl $0x7, %edx
shll $0x8, %edx
movslq -0x8(%rbp), %rcx
leaq 0x9c43(%rip), %rax      # 0x16a80
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movzwl 0xc(%rax), %ecx
orl %edx, %ecx
movw %cx, 0xc(%rax)
movzbl -0x3(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
setne %cl
movslq -0x8(%rbp), %rdx
leaq 0x9c1a(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
andb $0x1, %cl
movb %cl, 0x24(%rax)
movzbl -0x3(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0xce89
movl -0x8(%rbp), %edi
callq 0xc790
jmp 0xcf79
movzbl -0x3(%rbp), %eax
sarl $0x4, %eax
movw %ax, 0x9d40(%rip)       # 0x16bdc
movzbl -0x3(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, 0x9d83(%rip)       # 0x16c36
movzbl -0x3(%rbp), %eax
andl $0x7, %eax
movl %eax, 0x9d78(%rip)      # 0x16c38
jmp 0xcf79
movzbl -0x3(%rbp), %eax
sarl $0x4, %eax
andl $0x7, %eax
cvtsi2ss %eax, %xmm0
movss 0x49e1(%rip), %xmm1     # 0x118bc
divss %xmm1, %xmm0
movss %xmm0, 0x9d61(%rip)     # 0x16c48
movzbl -0x3(%rbp), %eax
andl $0x7, %eax
cvtsi2ss %eax, %xmm0
movss 0x49c2(%rip), %xmm1     # 0x118bc
divss %xmm1, %xmm0
movss %xmm0, 0x9d52(%rip)     # 0x16c58
jmp 0xcf79
movl $0x0, -0x10(%rbp)
cmpl $0x4, -0x10(%rbp)
jge 0xcf77
movzbl -0x3(%rbp), %eax
movl -0x10(%rbp), %ecx
addl $0x4, %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %cl
movslq -0x10(%rbp), %rdx
leaq 0x9b4b(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
andb $0x1, %cl
movb %cl, 0x2(%rax)
movzbl -0x3(%rbp), %eax
movl -0x10(%rbp), %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %cl
movslq -0x10(%rbp), %rdx
leaq 0x9b21(%rip), %rax      # 0x16a80
imulq $0x70, %rdx, %rdx
addq %rdx, %rax
andb $0x1, %cl
movb %cl, 0x3(%rax)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xcf0f
jmp 0xcf79
movb -0x3(%rbp), %dl
leaq 0x998d(%rip), %rax      # 0x16910
movq (%rax), %rax
movzwl -0x2(%rbp), %ecx
movb %dl, (%rax,%rcx)
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)