// File: /workspace/github_c_makefile_star2_O0/calmopyrin[P]tedplay/Tedsound.cpp
// Function: TED::writeSoundReg(unsigned int, unsigned char)
void TED::writeSoundReg(unsigned int reg, unsigned char value)
{
#if defined(_DEBUG) && 0
	static FILE *f = std::fopen("freqlog.txt", "a");
	if (f)
		std::fprintf(f, "%04X <- %02X in cycle %llu", 0xff0e + reg, value, CycleCounter);
	fprintf(f, "\n");
#endif

	switch (reg) {
		case 0:
			Freq1 = (Freq1 & 0x300) | value;
			setFreq(0, Freq1);
			break;
		case 1:
			Freq2 = (Freq2 & 0x300) | value;
			setFreq(1, Freq2);
			break;
		case 2:
			Freq2 = (Freq2 & 0xFF) | (value << 8);
			setFreq(1, Freq2);
			break;
		case 3:
			if ((DAStatus = value & 0x80)) {
				FlipFlop[0] = 1;
				FlipFlop[1] = 1;
				oscCount[0] = OscReload[0];
				oscCount[1] = OscReload[1];
				NoiseCounter = 0;
			}
			Volume = value & 0x0F;
			if (Volume > 8) Volume = 8;
			Volume = (Volume << 9);
			Snd1Status = value & 0x10;
			Snd2Status = value & 0x20;
			SndNoiseStatus = value & 0x40;
			break;
		case 4:
			Freq1 = (Freq1 & 0xFF) | (value << 8);
			setFreq(0, Freq1);
			break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movb %al, -0xd(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movl -0xc(%rbp), %eax
movq %rax, -0x18(%rbp)
subq $0x4, %rax
ja 0x19e0a
movq -0x18(%rbp), %rax
leaq 0x5c08(%rip), %rcx      # 0x1f8a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x20(%rbp), %rdi
movzwl 0x1bcf6(%rip), %eax     # 0x359a2
andl $0x300, %eax            # imm = 0x300
movzbl -0xd(%rbp), %ecx
orl %ecx, %eax
movw %ax, 0x1bce4(%rip)      # 0x359a2
movzwl 0x1bcdd(%rip), %edx     # 0x359a2
xorl %esi, %esi
callq 0x1a490
jmp 0x19e0a
movq -0x20(%rbp), %rdi
movzwl 0x1bcc4(%rip), %eax     # 0x359a0
andl $0x300, %eax            # imm = 0x300
movzbl -0xd(%rbp), %ecx
orl %ecx, %eax
movw %ax, 0x1bcb2(%rip)      # 0x359a0
movzwl 0x1bcab(%rip), %edx     # 0x359a0
movl $0x1, %esi
callq 0x1a490
jmp 0x19e0a
movq -0x20(%rbp), %rdi
movzwl 0x1bc91(%rip), %eax     # 0x359a0
andl $0xff, %eax
movzbl -0xd(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, 0x1bc7c(%rip)      # 0x359a0
movzwl 0x1bc75(%rip), %edx     # 0x359a0
movl $0x1, %esi
callq 0x1a490
jmp 0x19e0a
movzbl -0xd(%rbp), %eax
andl $0x80, %eax
movl %eax, 0x1bc67(%rip)     # 0x359b0
cmpl $0x0, %eax
je 0x19d84
movl $0x1, 0x1bc34(%rip)     # 0x3598c
movl $0x1, 0x1bc2e(%rip)     # 0x35990
movl 0x1be5c(%rip), %eax     # 0x35bc4
movl %eax, 0x1bc26(%rip)     # 0x35994
movl 0x1be54(%rip), %eax     # 0x35bc8
movl %eax, 0x1bc1e(%rip)     # 0x35998
movl $0x0, 0x1bc18(%rip)     # 0x3599c
movzbl -0xd(%rbp), %eax
andl $0xf, %eax
movl %eax, 0x1be3b(%rip)     # 0x35bcc
cmpl $0x8, 0x1be34(%rip)     # 0x35bcc
jle 0x19da4
movl $0x8, 0x1be28(%rip)     # 0x35bcc
movl 0x1be22(%rip), %eax     # 0x35bcc
shll $0x9, %eax
movl %eax, 0x1be19(%rip)     # 0x35bcc
movzbl -0xd(%rbp), %eax
andl $0x10, %eax
movl %eax, 0x1bbec(%rip)     # 0x359ac
movzbl -0xd(%rbp), %eax
andl $0x20, %eax
movl %eax, 0x1bbdb(%rip)     # 0x359a8
movzbl -0xd(%rbp), %eax
andl $0x40, %eax
movl %eax, 0x1bbca(%rip)     # 0x359a4
jmp 0x19e0a
movq -0x20(%rbp), %rdi
movzwl 0x1bbbb(%rip), %eax     # 0x359a2
andl $0xff, %eax
movzbl -0xd(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movw %ax, 0x1bba6(%rip)      # 0x359a2
movzwl 0x1bb9f(%rip), %edx     # 0x359a2
xorl %esi, %esi
callq 0x1a490
addq $0x20, %rsp
popq %rbp
retq