// File: /workspace/github_c_makefile_star2_O1/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 0x9510
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x8(%rdi), %rax
movq %rax, 0x18(%rsp)
leaq 0x7127(%rip), %rbp      # 0x101e0
leaq 0x70f4(%rip), %rbx      # 0x101b4
movq %rdi, 0x20(%rsp)
movl 0x178(%rdi), %eax
addl 0x17c(%rdi), %eax
movl 0x174(%rdi), %r8d
movl %eax, %ecx
subl 0x41b8(%rdi), %ecx
movq %rsi, 0x28(%rsp)
jae 0x90f4
movl %r8d, 0xc(%rsp)
movl %eax, 0x178(%rdi)
jmp 0x9102
movl %ecx, 0x178(%rdi)
incl %r8d
movl %r8d, 0xc(%rsp)
decq %rdx
movq %rdx, 0x30(%rsp)
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 0x9244
addq 0x18(%rsp), %rcx
movl 0x20(%rcx), %esi
movl %esi, 0x24(%rcx)
movl %edx, %r10d
imull 0xc(%rsp), %r10d
addl %esi, %r10d
movl %r10d, 0x20(%rcx)
movq 0x18(%rcx), %r8
cmpl $0x0, 0x70(%r8)
je 0x917b
btl $0x17, %esi
jb 0x917b
movl %r10d, %r9d
andl $0x800000, %r9d         # imm = 0x800000
je 0x917b
andl $0x7fffff, %r10d        # imm = 0x7FFFFF
movl %r10d, 0x20(%r8)
btl $0x13, %esi
leal (%rsi,%rdx), %esi
jb 0x922c
movl %esi, %r8d
andl $0x80000, %r8d          # imm = 0x80000
je 0x922c
movl 0x28(%rcx), %r15d
movl %r15d, %r8d
shrl $0x16, %r8d
movl %r15d, %r9d
shrl $0x11, %r9d
xorl %r8d, %r9d
andl $0x1, %r9d
leal (%r9,%r15,2), %r8d
movl %r8d, 0x28(%rcx)
movl %r15d, %r8d
shrl $0xa, %r8d
andl $0x800, %r8d            # imm = 0x800
movl %r15d, %r9d
shrl $0x9, %r9d
andl $0x400, %r9d            # imm = 0x400
movl %r15d, %r10d
shrl $0x6, %r10d
andl $0x200, %r10d           # imm = 0x200
movl %r15d, %r11d
shrl $0x4, %r11d
andl $0x100, %r11d           # imm = 0x100
movl %r15d, %r14d
shrl $0x3, %r14d
andl $0x80, %r14d
movl %r15d, %r12d
andl $0x40, %r12d
movl %r15d, %r13d
andl $0x8, %r13d
andl $0x2, %r15d
leal (%r12,%r13,4), %r12d
leal (%r12,%r15,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 0x917b
andl $0xffffff, %r8d         # imm = 0xFFFFFF
movl %r8d, 0x20(%rcx)
subq $0x1, %rax
jae 0x910f
movl $0x0, 0x8(%rsp)
movl $0x2, %r13d
movq $0x0, 0x10(%rsp)
movl 0xc(%rsp), %r8d
imulq $0x78, %r13, %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r12
cmpb $0x0, 0x6c(%rcx,%rax)
movl $0x0, %eax
jne 0x9290
movq %r12, %rdi
callq 0x954a
movl 0xc(%rsp), %r8d
movl 0x4c(%r12), %r15d
movl 0x54(%r12), %edi
movl 0x40(%r12), %ecx
movl 0x8(%r12), %r10d
movl 0x3c(%r12), %edx
movl 0x50(%r12), %r11d
movl 0x48(%r12), %esi
movl %edi, %r14d
andl $0xf, %r14d
cmpl (%rbp,%r14,4), %r15d
jne 0x9328
movl $0x7fff, 0x4c(%r12)     # imm = 0x7FFF
cmpl $0x1, %r10d
je 0x92f3
incl %r11d
movl %r11d, 0x50(%r12)
movzbl %sil, %r9d
leaq 0x6f3d(%rip), %r14      # 0x10220
movzbl (%r9,%r14), %r9d
movl $0x7fff, %r15d          # imm = 0x7FFF
cmpl %r9d, %r11d
jne 0x934d
movl $0x0, 0x50(%r12)
movl $0x7fff, %r15d          # imm = 0x7FFF
xorl %r11d, %r11d
cmpl $0x3, %r10d
ja 0x934d
movl %r10d, %r9d
movslq (%rbx,%r9,4), %r9
addq %rbx, %r9
jmpq *%r9
movl $0x0, 0x48(%r12)
xorl %esi, %esi
xorl %r11d, %r11d
jmp 0x934d
movl %r15d, %r14d
shrl $0xe, %r14d
movl %r15d, %r9d
shrl $0xd, %r9d
xorl %r14d, %r9d
andl $0x1, %r9d
andl $0x3fff, %r15d          # imm = 0x3FFF
leal (%r9,%r15,2), %r15d
movl %r15d, 0x4c(%r12)
decl %r8d
jne 0x92b3
jmp 0x93a1
cmpl %ecx, %esi
je 0x934d
decl %esi
andl $0xff, %esi
movl %esi, 0x48(%r12)
jne 0x934d
movl $0x0, 0x8(%r12)
xorl %r11d, %r11d
xorl %r10d, %r10d
jmp 0x934d
incl %esi
movl %esi, 0x48(%r12)
cmpb $-0x1, %sil
jne 0x934d
movl $0x2, 0x8(%r12)
movl %edx, 0x54(%r12)
movl $0x2, %r10d
movl %edx, %edi
jmp 0x934d
movzbl 0x48(%r12), %ecx
cmpl $0x0, 0x64(%r12)
je 0x93d6
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), %r8d
jmp 0x941a
cmpl $0x0, 0x68(%r12)
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %r8d
je 0x9401
imull 0x188(%rdi), %ecx
movl 0x8(%rsp), %eax
addl 0x184(%rdi), %eax
subl %ecx, %eax
movl %eax, 0x8(%rsp)
jmp 0x941a
subl 0x188(%rdi), %eax
imull %ecx, %eax
movl 0x8(%rsp), %ecx
addl 0x184(%rdi), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rsp)
subq $0x1, %r13
jae 0x926a
movl 0x41a0(%rdi), %esi
movl 0x41a4(%rdi), %r9d
sarl $0x6, %r9d
movq 0x10(%rsp), %r11
sarl $0x7, %r11d
movl 0x41ac(%rdi), %edx
movl 0x41b0(%rdi), %eax
movl 0x41a8(%rdi), %ecx
movl %r8d, %r10d
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 %r11d, %ecx
movl %r10d, %r8d
decl %r8d
jne 0x9450
movl %edx, 0x41ac(%rdi)
movl %eax, 0x41b0(%rdi)
movl %ecx, 0x41a8(%rdi)
movzbl 0x194(%rdi), %esi
decl %esi
cmpl $0x6, %esi
ja 0x94bb
leaq 0x6d1e(%rip), %r8       # 0x101c4
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
movl %edx, %eax
jmp 0x94c9
addl %eax, %ecx
jmp 0x94c1
addl %edx, %eax
jmp 0x94c9
xorl %eax, %eax
jmp 0x94c9
addl %edx, %ecx
movl %ecx, %eax
jmp 0x94c9
addl %edx, %eax
addl %ecx, %eax
movl 0x8(%rsp), %ecx
shll $0x7, %eax
addl %ecx, %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
movq 0x30(%rsp), %rdx
testq %rdx, %rdx
jne 0x90c5
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop