// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x18(%rbp)
cmpq $0x0, %rax
je 0x150ea
movq -0x60(%rbp), %rdi
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
callq 0x15370
movl %eax, -0x24(%rbp)
movl $0x2, -0x28(%rbp)
movq -0x60(%rbp), %rax
addq $0x8, %rax
movl -0x28(%rbp), %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x60(%rax)
jne 0x14f9f
cmpl $0x0, -0x34(%rbp)
je 0x14f9f
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x24(%rax)
movl -0x34(%rbp), %eax
imull -0x24(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %ecx
movq -0x30(%rbp), %rax
addl 0x20(%rax), %ecx
movl %ecx, 0x20(%rax)
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
cmpl $0x0, 0x70(%rax)
je 0x14f47
movq -0x30(%rbp), %rax
movl 0x24(%rax), %eax
andl $0x800000, %eax         # imm = 0x800000
cmpl $0x0, %eax
jne 0x14f47
movq -0x30(%rbp), %rax
movl 0x20(%rax), %eax
andl $0x800000, %eax         # imm = 0x800000
cmpl $0x0, %eax
je 0x14f47
movq -0x30(%rbp), %rax
movl 0x20(%rax), %ecx
subl $0x800000, %ecx         # imm = 0x800000
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movl %ecx, 0x20(%rax)
movl -0x38(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x34(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x38(%rbp), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
jne 0x14f7d
movl -0x40(%rbp), %eax
andl $0x80000, %eax          # imm = 0x80000
cmpl $0x0, %eax
je 0x14f7d
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x153e0
movl -0x40(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x40(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jb 0x14f4d
movq -0x30(%rbp), %rax
movl 0x20(%rax), %ecx
andl $0xffffff, %ecx         # imm = 0xFFFFFF
movl %ecx, 0x20(%rax)
jmp 0x14fa1
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, %eax
jne 0x14e96
movl $0x2, -0x28(%rbp)
movq -0x60(%rbp), %rax
addq $0x8, %rax
movl -0x28(%rbp), %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
testb $0x1, 0x6c(%rax)
je 0x14fe3
xorl %eax, %eax
movl %eax, -0x64(%rbp)
jmp 0x14fef
movq -0x48(%rbp), %rdi
callq 0x152a0
movl %eax, -0x64(%rbp)
movq -0x60(%rbp), %rdi
movl -0x64(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x24(%rbp), %esi
movq -0x48(%rbp), %rdx
callq 0x15460
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpl $0x0, 0x64(%rax)
je 0x15031
movq -0x60(%rbp), %rcx
movl -0x4c(%rbp), %eax
subl 0x188(%rcx), %eax
imull -0x50(%rbp), %eax
addl 0x184(%rcx), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x15078
movq -0x48(%rbp), %rax
cmpl $0x0, 0x68(%rax)
je 0x15059
movq -0x60(%rbp), %rcx
xorl %eax, %eax
subl 0x188(%rcx), %eax
imull -0x50(%rbp), %eax
addl 0x184(%rcx), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x15076
movq -0x60(%rbp), %rcx
movl -0x4c(%rbp), %eax
subl 0x188(%rcx), %eax
imull -0x50(%rbp), %eax
addl 0x184(%rcx), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x15078
jmp 0x1507a
movl -0x28(%rbp), %eax
movl %eax, %ecx
addl $-0x1, %ecx
movl %ecx, -0x28(%rbp)
cmpl $0x0, %eax
jne 0x14fbc
movq -0x60(%rbp), %rdi
movl -0x20(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x24(%rbp), %esi
movl -0x1c(%rbp), %edx
callq 0x15620
movq -0x60(%rbp), %rcx
movl %eax, %edx
movl -0x68(%rbp), %eax
addl %edx, %eax
addl 0x180(%rcx), %eax
addl 0x18c(%rcx), %eax
imull 0x170(%rcx), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
sarl $0xd, %eax
movl %eax, -0x58(%rbp)
movl -0x58(%rbp), %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movq %rax, %rdx
addq $0x2, %rdx
movq %rdx, -0x10(%rbp)
movw %cx, (%rax)
jmp 0x14e5c
addq $0x70, %rsp
popq %rbp
retq