// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r15
leaq 0x8(%rdi), %r13
leaq 0xf8(%rdi), %rax
movq %rax, 0x8(%rsp)
movq %r13, 0x10(%rsp)
subq $0x1, %rdx
jb 0x8e65
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %r15, %rdi
callq 0x8f9c
movl %eax, 0x4(%rsp)
pushq $0x2
popq %rbx
imulq $0x78, %rbx, %r12
movl 0x30(%r13,%r12), %r14d
cmpl $0x0, 0x60(%r13,%r12)
sete %al
testl %r14d, %r14d
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x8d8e
addq %r13, %r12
movl 0x20(%r12), %ebp
movl %ebp, 0x24(%r12)
movl %r14d, %eax
imull 0x4(%rsp), %eax
addl %ebp, %eax
movl %eax, 0x20(%r12)
movq 0x18(%r12), %rcx
cmpl $0x0, 0x70(%rcx)
je 0x8d5b
btl $0x17, %ebp
jb 0x8d5b
movl %eax, %edx
andl $0x800000, %edx         # imm = 0x800000
je 0x8d5b
andl $0x7fffff, %eax         # imm = 0x7FFFFF
movl %eax, 0x20(%rcx)
movl %ebp, %eax
btl $0x13, %ebp
leal (%rax,%r14), %ebp
jb 0x8d7b
movl %ebp, %eax
andl $0x80000, %eax          # imm = 0x80000
je 0x8d7b
movq %r15, %rdi
movq %r12, %rsi
callq 0x8fc8
movl 0x20(%r12), %eax
cmpl %eax, %ebp
jb 0x8d5b
andl $0xffffff, %eax         # imm = 0xFFFFFF
movl %eax, 0x20(%r12)
subq $0x1, %rbx
jae 0x8cfd
xorl %ebp, %ebp
movq 0x8(%rsp), %r13
pushq $-0x3
popq %rbx
xorl %r12d, %r12d
cmpb $0x0, 0x6c(%r13)
movl $0x0, %r14d
jne 0x8dbd
movq %r13, %rdi
callq 0x8f46
movl %eax, %r14d
movq %r15, %rdi
movl 0x4(%rsp), %esi
movq %r13, %rdx
callq 0x8ff2
cmpl $0x0, 0x64(%r13)
je 0x8dea
subl 0x188(%r15), %r14d
imull %eax, %r14d
addl 0x184(%r15), %r12d
addl %r14d, %r12d
jmp 0x8e17
cmpl $0x0, 0x68(%r13)
movl 0x188(%r15), %ecx
je 0x8e06
imull %eax, %ecx
addl 0x184(%r15), %ebp
subl %ecx, %ebp
jmp 0x8e17
subl %ecx, %r14d
imull %eax, %r14d
addl 0x184(%r15), %ebp
addl %r14d, %ebp
addq $-0x78, %r13
incq %rbx
jne 0x8da5
movq %r15, %rdi
movl 0x4(%rsp), %esi
movl %r12d, %edx
callq 0x9150
addl %ebp, %eax
addl 0x180(%r15), %eax
addl 0x18c(%r15), %eax
imull 0x170(%r15), %eax
shrl $0xd, %eax
movq 0x18(%rsp), %rsi
movw %ax, (%rsi)
addq $0x2, %rsi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %r13
jmp 0x8cda
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq