// File: /workspace/github_c_makefile_star2_O3/calmopyrin[P]tedplay/Sid.cpp
// Function: SIDsound::calcSamples(short*, long)
void SIDsound::calcSamples(short *buf, long accu)
{
	for (;accu--;) {
		// Outputs for normal and filtered sounds
		int sumFilteredOutput = 0;
		int sumOutput = 0;

		const unsigned int cyclesToDo = clock();
		// Loop for the three voices
		unsigned int j = 2;
		do {
			SIDVoice &v = voice[j];
			const unsigned int freq = v.freq;
			// Waveform generator
			if (!v.test && freq) {
				unsigned int accPrev = v.accu;
				v.accPrev = accPrev;
				// Update accumulator
				const unsigned int add = freq * cyclesToDo;
				v.accu += add;
				// FIXME Apply syncing
#if 1
				if (v.modulatesThis->sync && !(v.accPrev & 0x800000) && (v.accu & 0x800000)
           			)
					v.modulatesThis->accu = (v.accu - 0x800000) & 0xFFFFFF;
#else
				if (v.sync && !(accPrev & 0x800000) && (v.accu & 0x800000)
					/*&& !(v.modulatedBy->sync && !(v.modulatedBy->accPrev & 0x80000) &&
						((v.modulatedBy->accu + add) & 0x80000))*/
           			)
				{
					if (j > v.modulatesThis->index) {
						v.modulatesThis->accu = (accPrev - 0x8000000) & 0xFFFFFFF;
					} else {
						unsigned int addModulating = v.modulatesThis->accu > v.modulatesThis->accPrev ? 0 : 0x8000000;
						addModulating += v.modulatesThis->accu - v.modulatesThis->accPrev;
						v.modulatesThis->accu = (accPrev + addModulating - 0x8000000) & 0xFFFFFFF;
					}
				}
#endif
				// noise shift register is updating even when waveform is not selected
				unsigned int accNext = accPrev;
				do {
					accNext += freq;
					// Update noise shift register
					if (!(accPrev & 0x080000) && (accNext & 0x080000))
						updateShiftReg(v);
					accPrev = accNext;
				} while (accNext < v.accu);
				// accu is 24 bit
				v.accu &= 0xFFFFFF;
			}
		} while (j--);
		j = 2;
		do {
			SIDVoice &v = voice[j];
			int output = v.disabled ? 0x0000 : getWaveSample(v);
			int envelope = doEnvelopeGenerator(cyclesToDo, v);
			
			if (v.filter)
				sumFilteredOutput += (output - dcWave) * envelope + dcVoice;
			else {
				if (v.muted)
					sumOutput += (0x0000 - dcWave) * envelope + dcVoice;
				else
					sumOutput += (output - dcWave) * envelope + dcVoice;
			}
		} while (j--);

		int accu = (sumOutput + filterOutput(cyclesToDo, sumFilteredOutput)
			+ dcMixer + dcDigiBlaster) * volume;

#if 1
		int sample = accu >> 13;
#else
		unsigned int interPolationFac = (clockDeltaRemainder - sidCyclesPerSampleInt) & 0xFF;
		accu >>= 7;
		sample = (prevAccu * (0xFF ^ interPolationFac) + accu * (interPolationFac)) >> 12;
		prevAccu = accu;
#endif

		*buf++ = (short) sample;
	}
}

// Assembly:
testq %rdx, %rdx
je 0x95a6
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x8(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x7189(%rip), %rbp      # 0x102b0
movq %rdi, 0x20(%rsp)
movq %rsi, 0x28(%rsp)
movq %rdx, 0x30(%rsp)
movl 0x174(%rdi), %ebx
movl 0x178(%rdi), %eax
addl 0x17c(%rdi), %eax
movl 0x41b8(%rdi), %ecx
cmpl %ecx, %eax
movl $0x0, %edx
cmovbl %edx, %ecx
sbbl $-0x1, %ebx
subl %ecx, %eax
movl %eax, 0x178(%rdi)
movl $0x2, %eax
imulq $0x78, %rax, %rcx
movq 0x18(%rsp), %rsi
movl 0x30(%rsi,%rcx), %edx
cmpl $0x0, 0x60(%rsi,%rcx)
sete %sil
testl %edx, %edx
setne %r8b
andb %sil, %r8b
cmpb $0x1, %r8b
jne 0x929b
addq 0x18(%rsp), %rcx
movl 0x20(%rcx), %esi
movl %esi, 0x24(%rcx)
movl %edx, %r10d
imull %ebx, %r10d
addl %esi, %r10d
movl %r10d, 0x20(%rcx)
movq 0x18(%rcx), %r8
cmpl $0x0, 0x70(%r8)
je 0x91d2
btl $0x17, %esi
jb 0x91d2
movl %r10d, %r9d
andl $0x800000, %r9d         # imm = 0x800000
je 0x91d2
andl $0x7fffff, %r10d        # imm = 0x7FFFFF
movl %r10d, 0x20(%r8)
btl $0x13, %esi
leal (%rsi,%rdx), %esi
jb 0x9283
movl %esi, %r8d
andl $0x80000, %r8d          # imm = 0x80000
je 0x9283
movl 0x28(%rcx), %r13d
movl %r13d, %r8d
shrl $0x16, %r8d
movl %r13d, %r9d
shrl $0x11, %r9d
xorl %r8d, %r9d
andl $0x1, %r9d
leal (%r9,%r13,2), %r8d
movl %r8d, 0x28(%rcx)
movl %r13d, %r8d
shrl $0xa, %r8d
andl $0x800, %r8d            # imm = 0x800
movl %r13d, %r9d
shrl $0x9, %r9d
andl $0x400, %r9d            # imm = 0x400
movl %r13d, %r10d
shrl $0x6, %r10d
andl $0x200, %r10d           # imm = 0x200
movl %r13d, %r11d
shrl $0x4, %r11d
andl $0x100, %r11d           # imm = 0x100
movl %r13d, %r14d
shrl $0x3, %r14d
andl $0x80, %r14d
movl %r13d, %r15d
andl $0x40, %r15d
movl %r13d, %r12d
andl $0x8, %r12d
andl $0x2, %r13d
leal (%r15,%r12,4), %r15d
leal (%r15,%r13,8), %r15d
orl %r9d, %r15d
orl %r8d, %r15d
orl %r10d, %r15d
orl %r11d, %r15d
orl %r14d, %r15d
movl %r15d, 0x2c(%rcx)
movl 0x20(%rcx), %r8d
cmpl %r8d, %esi
jb 0x91d2
andl $0xffffff, %r8d         # imm = 0xFFFFFF
movl %r8d, 0x20(%rcx)
subq $0x1, %rax
jae 0x9168
movl $0x0, 0x8(%rsp)
movl $0x2, %r14d
movq $0x0, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
imulq $0x78, %r14, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r12
cmpb $0x0, 0x6c(%rcx,%rax)
movl $0x0, %eax
jne 0x92e1
movq %r12, %rdi
callq 0x95e0
movl 0x4c(%r12), %ecx
movl 0x54(%r12), %esi
movl 0x40(%r12), %ebx
movl 0x8(%r12), %r13d
movl 0x3c(%r12), %edx
movl 0x50(%r12), %r10d
movl 0x48(%r12), %r9d
movl %r9d, %r11d
movl 0xc(%rsp), %edi
movl %esi, %r15d
andl $0xf, %r15d
cmpl (%rbp,%r15,4), %ecx
jne 0x936c
movl $0x7fff, 0x4c(%r12)     # imm = 0x7FFF
cmpl $0x1, %r13d
jne 0x939d
movl $0x0, 0x50(%r12)
incl %r11d
movl %r11d, 0x48(%r12)
movl $0x7fff, %ecx           # imm = 0x7FFF
movl $0x1, %r13d
xorl %r10d, %r10d
cmpb $-0x1, %r11b
jne 0x93ea
movl $0x2, 0x8(%r12)
movl %edx, 0x54(%r12)
movl $0x2, %r13d
movl %r11d, %r9d
movl %edx, %esi
jmp 0x9390
movl %ecx, %r15d
shrl $0xe, %r15d
movl %ecx, %r8d
shrl $0xd, %r8d
xorl %r15d, %r8d
andl $0x1, %r8d
andl $0x3fff, %ecx           # imm = 0x3FFF
leal (%r8,%rcx,2), %ecx
movl %ecx, 0x4c(%r12)
decl %edi
jne 0x930b
jmp 0x9443
incl %r10d
movl %r10d, 0x50(%r12)
movzbl %r11b, %ecx
leaq 0x6f40(%rip), %r8       # 0x102f0
movzbl (%rcx,%r8), %r8d
movl $0x7fff, %ecx           # imm = 0x7FFF
cmpl %r8d, %r10d
jne 0x9390
movl $0x0, 0x50(%r12)
testl %r13d, %r13d
je 0x9402
xorl %r10d, %r10d
cmpl $0x2, %r13d
je 0x93ef
cmpl $0x3, %r13d
jne 0x9390
decl %r11d
movl %r11d, %r9d
movl $0x3, %r13d
jmp 0x9413
movl %r11d, %r9d
jmp 0x9390
movl %ebx, %r8d
movl $0x2, %r13d
cmpl %ebx, %r11d
jne 0x940d
movl %r8d, %r11d
jmp 0x9390
movl $0x0, 0x48(%r12)
jmp 0x9432
decl %r11d
movl %r11d, %r9d
andl $0xff, %r9d
movl %r9d, 0x48(%r12)
je 0x9429
movl %r9d, %r11d
jmp 0x9390
movl $0x0, 0x8(%r12)
xorl %r9d, %r9d
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %r13d, %r13d
jmp 0x9390
movzbl %r9b, %ecx
cmpl $0x0, 0x64(%r12)
je 0x9475
movq 0x20(%rsp), %rdi
subl 0x188(%rdi), %eax
imull %ecx, %eax
movq 0x10(%rsp), %rcx
addl 0x184(%rdi), %ecx
addl %eax, %ecx
movq %rcx, 0x10(%rsp)
movl 0xc(%rsp), %ebx
jmp 0x94b6
cmpl $0x0, 0x68(%r12)
movq 0x20(%rsp), %rdi
movl 0x188(%rdi), %edx
movl 0xc(%rsp), %ebx
je 0x94a1
imull %ecx, %edx
movl 0x8(%rsp), %eax
addl 0x184(%rdi), %eax
subl %edx, %eax
movl %eax, 0x8(%rsp)
jmp 0x94b6
subl %edx, %eax
imull %ecx, %eax
movl 0x8(%rsp), %ecx
addl 0x184(%rdi), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rsp)
subq $0x1, %r14
jae 0x92c0
movl 0x41a0(%rdi), %esi
movl 0x41a4(%rdi), %r9d
sarl $0x6, %r9d
movq 0x10(%rsp), %r10
sarl $0x7, %r10d
movl 0x41ac(%rdi), %edx
movl 0x41b0(%rdi), %eax
movl 0x41a8(%rdi), %ecx
movl %edx, %r8d
imull %r9d, %r8d
sarl $0xe, %r8d
subl %r8d, %eax
imull %r9d, %ecx
sarl $0xe, %ecx
subl %ecx, %edx
movl %edx, %ecx
imull %esi, %ecx
sarl $0xa, %ecx
subl %eax, %ecx
subl %r10d, %ecx
decl %ebx
jne 0x94ec
movl %edx, 0x41ac(%rdi)
movl %eax, 0x41b0(%rdi)
movl %ecx, 0x41a8(%rdi)
movzbl 0x194(%rdi), %esi
decl %esi
cmpl $0x6, %esi
ja 0x9550
leaq 0x6d59(%rip), %r8       # 0x10294
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
movl %edx, %eax
jmp 0x955e
addl %eax, %ecx
jmp 0x9556
addl %edx, %eax
jmp 0x955e
xorl %eax, %eax
jmp 0x955e
addl %edx, %ecx
movl %ecx, %eax
jmp 0x955e
addl %edx, %eax
addl %ecx, %eax
movq 0x30(%rsp), %rdx
decq %rdx
shll $0x7, %eax
addl 0x8(%rsp), %eax
addl 0x180(%rdi), %eax
addl 0x18c(%rdi), %eax
imull 0x170(%rdi), %eax
shrl $0xd, %eax
movq 0x28(%rsp), %rsi
movw %ax, (%rsi)
addq $0x2, %rsi
testq %rdx, %rdx
jne 0x912c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop