// File: /workspace/github_c_makefile_star2_O0/calmopyrin[P]tedplay/Sid.cpp
// Function: SIDsound::write(unsigned int, unsigned char)
void SIDsound::write(unsigned int adr, unsigned char value)
{
	lastByteWritten = value;

	SIDVoice &v = voice[adr/7];
	switch (adr) {
		case 0:
		case 7:
		case 14:
			v.freq = (unsigned short)((v.freq & 0xff00) | value);
			break;

		case 1:
		case 8:
		case 15:
			v.freq = (unsigned short)((v.freq & 0xff) | (value << 8));
			break;

		case 2:
		case 9:
		case 16:
			v.pw = (v.pw & 0xf00000) | (value << 12);
			break;

		case 3:
		case 10:
		case 17:
			v.pw = (v.pw & 0x0ff000) | ((value & 0xf) << 20);
			break;

		case 4:
		case 11:
		case 18:
			if ((value ^ v.gate) & 1) {
				if (value & 1) {
					// gate on
					v.egState = EG_ATTACK;
					v.envCounterCompare = v.envAttackAdd;
				} else {
					// gate off
					v.egState = EG_RELEASE;
					v.envCounterCompare = v.envReleaseSub;
				}
				v.gate = value & 1;
			}
			v.sync = value & 2;
			v.ring = value & 4;
			if ((value & 8) && !v.test) {
				v.accu = 0; //(model_ >= SID8580) ? 0 : 0;
				unsigned int bit19 = (v.shiftReg >> 19) & 1;
				v.shiftReg = (v.shiftReg & 0x7ffffd) | ((bit19^1) << 1);
				v.test = 0xFFF;
			} else if (v.test && !(value & 8)) {
				unsigned int bit0 = ((v.shiftReg >> 22) ^ (v.shiftReg >> 17)) & 0x1;
				v.shiftReg <<= 1;
				v.shiftReg &= 0x7fffff;
				v.shiftReg |= bit0;
				v.test = 0x000;
			}
			v.wave = (value >> 4) & 0x0F;
			if (v.wave > 8) {
				v.shiftReg &= 0x7fffff^(1<<22)^(1<<20)^(1<<16)^(1<<13)^(1<<11)^(1<<7)^(1<<4)^(1<<2);
				v.waveNoiseOut = 0;
			}
			break;

		case 5:
		case 12:
		case 19:
			v.envAttackAdd = value >> 4;
			v.envDecaySub = value & 0x0F;
			if (v.egState == EG_ATTACK)
				v.envCounterCompare = v.envAttackAdd;
			else if (v.egState == EG_DECAY)
				v.envCounterCompare = v.envDecaySub;
			break;

		case 6:
		case 13:
		case 20:
			v.envSustainLevel = (value >> 4) * 0x11;
			v.envReleaseSub = value & 0x0F;
			if (v.egState == EG_RELEASE)
				v.envCounterCompare = v.envReleaseSub;
			break;

		case 21:
			if ((value ^ filterCutoff) & 7) {
				filterCutoff = (value & 7) | (filterCutoff & 0x7F8);
				setFilterCutoff();
			}
			break;

		case 22:
			filterCutoff = (value << 3) | (filterCutoff & 7);
			setFilterCutoff();
			break;

		case 23:
			voice[0].filter = value & 1;
			voice[1].filter = value & 2;
			voice[2].filter = value & 4;
			filterResonance = (unsigned char)(value >> 4);
			setResonance();
			break;

		case 24:
			volume = value & 0x0F;
			voice[2].muted = value & 0x80;
			filterType = (unsigned char)((value >> 4) & 7);
			break;

		case 30: // Digiblaster DAC
			if (enableDigiBlaster && model_ == SID8580)
			{
				dcDigiBlaster = (value ^ 0x00) << DIGIBLASTER_MULT;
			}
			break;

		case 31: // Digiblaster ADC
			break;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movb %dl, %al
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movb %al, -0xd(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x30(%rbp)
movb -0xd(%rbp), %cl
movb %cl, 0x41b4(%rax)
movl -0xc(%rbp), %ecx
movl %ecx, %edx
movl %edx, %ecx
imulq $0x24924925, %rdx, %rdx # imm = 0x24924925
shrq $0x20, %rdx
subl %edx, %ecx
shrl %ecx
addl %edx, %ecx
shrl $0x2, %ecx
imulq $0x78, %rcx, %rcx
leaq 0x8(%rax,%rcx), %rax
movq %rax, -0x18(%rbp)
movl -0xc(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x1f, %rax
ja 0x14e31
movq -0x28(%rbp), %rax
leaq 0xa91f(%rip), %rcx      # 0x1f3a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
andl $0xff00, %eax           # imm = 0xFF00
movzbl -0xd(%rbp), %ecx
orl %ecx, %eax
movzwl %ax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
jmp 0x14e31
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
andl $0xff, %eax
movzbl -0xd(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzwl %ax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
jmp 0x14e31
movq -0x18(%rbp), %rax
movl 0x34(%rax), %ecx
andl $0xf00000, %ecx         # imm = 0xF00000
movzbl -0xd(%rbp), %eax
shll $0xc, %eax
orl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x34(%rax)
jmp 0x14e31
movq -0x18(%rbp), %rax
movl 0x34(%rax), %ecx
andl $0xff000, %ecx          # imm = 0xFF000
movzbl -0xd(%rbp), %eax
andl $0xf, %eax
shll $0x14, %eax
orl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x34(%rax)
jmp 0x14e31
movzbl -0xd(%rbp), %eax
movq -0x18(%rbp), %rcx
xorl 0x58(%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x14b7f
movzbl -0xd(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x14b58
movq -0x18(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x14b71
movq -0x18(%rbp), %rax
movl $0x3, 0x8(%rax)
movq -0x18(%rbp), %rax
movl 0x44(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
movzbl -0xd(%rbp), %ecx
andl $0x1, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x58(%rax)
movzbl -0xd(%rbp), %ecx
andl $0x2, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x70(%rax)
movzbl -0xd(%rbp), %ecx
andl $0x4, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x5c(%rax)
movzbl -0xd(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x14bf7
movq -0x18(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jne 0x14bf7
movq -0x18(%rbp), %rax
movl $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
andl $0x7ffffd, %ecx         # imm = 0x7FFFFD
movl -0x1c(%rbp), %eax
xorl $0x1, %eax
shll %eax
orl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0xfff, 0x60(%rax)      # imm = 0xFFF
jmp 0x14c5f
movq -0x18(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0x14c5d
movzbl -0xd(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
jne 0x14c5d
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
shrl $0x16, %eax
movq -0x18(%rbp), %rcx
movl 0x28(%rcx), %ecx
shrl $0x11, %ecx
xorl %ecx, %eax
andl $0x1, %eax
movl %eax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
shll %ecx
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
andl $0x7fffff, %ecx         # imm = 0x7FFFFF
movl %ecx, 0x28(%rax)
movl -0x20(%rbp), %ecx
movq -0x18(%rbp), %rax
orl 0x28(%rax), %ecx
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x60(%rax)
jmp 0x14c5f
movzbl -0xd(%rbp), %ecx
sarl $0x4, %ecx
andl $0xf, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
cmpl $0x8, 0x4(%rax)
jle 0x14c95
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
andl $0x2ed76b, %ecx         # imm = 0x2ED76B
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x2c(%rax)
jmp 0x14e31
movzbl -0xd(%rbp), %ecx
sarl $0x4, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x38(%rax)
movzbl -0xd(%rbp), %ecx
andl $0xf, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x3c(%rax)
movq -0x18(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x14cd0
movq -0x18(%rbp), %rax
movl 0x38(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x14cea
movq -0x18(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x14ce8
movq -0x18(%rbp), %rax
movl 0x3c(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x14cea
jmp 0x14e31
movzbl -0xd(%rbp), %eax
sarl $0x4, %eax
imull $0x11, %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x40(%rax)
movzbl -0xd(%rbp), %ecx
andl $0xf, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x44(%rax)
movq -0x18(%rbp), %rax
cmpl $0x3, 0x8(%rax)
jne 0x14d26
movq -0x18(%rbp), %rax
movl 0x44(%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x14e31
movq -0x30(%rbp), %rcx
movzbl -0xd(%rbp), %eax
xorl 0x198(%rcx), %eax
andl $0x7, %eax
cmpl $0x0, %eax
je 0x14d65
movq -0x30(%rbp), %rdi
movzbl -0xd(%rbp), %eax
andl $0x7, %eax
movl 0x198(%rdi), %ecx
andl $0x7f8, %ecx            # imm = 0x7F8
orl %ecx, %eax
movl %eax, 0x198(%rdi)
callq 0x15170
jmp 0x14e31
movq -0x30(%rbp), %rdi
movzbl -0xd(%rbp), %eax
shll $0x3, %eax
movl 0x198(%rdi), %ecx
andl $0x7, %ecx
orl %ecx, %eax
movl %eax, 0x198(%rdi)
callq 0x15170
jmp 0x14e31
movq -0x30(%rbp), %rdi
movzbl -0xd(%rbp), %eax
andl $0x1, %eax
movl %eax, 0x6c(%rdi)
movzbl -0xd(%rbp), %eax
andl $0x2, %eax
movl %eax, 0xe4(%rdi)
movzbl -0xd(%rbp), %eax
andl $0x4, %eax
movl %eax, 0x15c(%rdi)
movzbl -0xd(%rbp), %eax
sarl $0x4, %eax
movb %al, 0x19c(%rdi)
callq 0x15240
jmp 0x14e31
movq -0x30(%rbp), %rax
movzbl -0xd(%rbp), %ecx
andl $0xf, %ecx
movl %ecx, 0x170(%rax)
movzbl -0xd(%rbp), %ecx
andl $0x80, %ecx
movl %ecx, 0x160(%rax)
movzbl -0xd(%rbp), %ecx
sarl $0x4, %ecx
andl $0x7, %ecx
movb %cl, 0x194(%rax)
jmp 0x14e31
movq -0x30(%rbp), %rax
testb $0x1, 0x41b5(%rax)
je 0x14e2d
movq -0x30(%rbp), %rax
cmpl $0x1, 0x41bc(%rax)
jne 0x14e2d
movq -0x30(%rbp), %rax
movzbl -0xd(%rbp), %ecx
xorl $0x0, %ecx
shll $0xe, %ecx
movl %ecx, 0x18c(%rax)
jmp 0x14e31
jmp 0x14e31
addq $0x30, %rsp
popq %rbp
retq
nopw (%rax,%rax)