// File: /workspace/github_c_makefile_star2_O2/calmopyrin[P]tedplay/Tedsound.cpp
// Function: TED::renderSound(unsigned int, short*)
void TED::renderSound(unsigned int nrsamples, short *buffer)
{
	// Calculate the buffer...
	if (DAStatus) {// digi?
		int sample = (volumeTable[Snd1Status | Snd2Status | (Volume >> 9)] & channelMask[2]) * masterVolume / 1000;
		for (;nrsamples--;) {
			*buffer++ = short(sample);
		}
	} else {
		unsigned int result;
		for (;nrsamples--;) {
			// Channel 1
			if (dcOutput[0]) {
				FlipFlop[0] = 1;
			} else if ((oscCount[0] += oscStep) >= OSCRELOADVAL) {
				FlipFlop[0] ^= 1;
				oscCount[0] = OscReload[0] + (oscCount[0] - OSCRELOADVAL);
			}
			// Channel 2
			static bool channelOverFlow = false;
			if (dcOutput[1]) {
				FlipFlop[1] = 1;
			}
			else {
				if (channelOverFlow)
					NoiseCounter = (NoiseCounter + 1) % 255;
				if ((oscCount[1] += oscStep) >= OSCRELOADVAL) {
					channelOverFlow = true;
					FlipFlop[1] ^= 1;
					oscCount[1] = OscReload[1] + (oscCount[1] - OSCRELOADVAL);
				}
				else
					channelOverFlow = false;
			}
			int s1on = (Snd1Status && FlipFlop[0]);
			int s2on = (Snd2Status && FlipFlop[1]);
			int non = SndNoiseStatus && noise[NoiseCounter];
			result = s1on ? (getWaveSample(0, waveForm[0]) & channelMask[0]) : 0;
			if (s2on) {
				result += getWaveSample(1, waveForm[1]) & channelMask[1];
			} else if (non) {
				result += volumeTable[0x20 | (Volume >> 9)] & channelMask[2];
			}
			if ((s2on || non) && s1on && Volume) {
				unsigned int vol = Volume >> 9;
				result = (result * volumeTable[0x30 | vol]) / (volumeTable[0x10 | vol] * 2);
			}

			*buffer++ = result * masterVolume / 1000;
		}   // for
	}
}

// Assembly:
movq %rdx, %r8
cmpl $0x0, 0x19a56(%rip)     # 0x25524
je 0xbb1d
movl 0x19a46(%rip), %eax     # 0x2551c
orl 0x19a44(%rip), %eax     # 0x25520
movl 0x19c5e(%rip), %ecx     # 0x25740
shrl $0x9, %ecx
orl %eax, %ecx
leaq 0x19b42(%rip), %rdx     # 0x25630
movl 0xb71c4(%rdi), %eax
andl (%rdx,%rcx,4), %eax
imull 0x199f2(%rip), %eax     # 0x254f0
xorl %ecx, %ecx
movl $0x3e8, %edi            # imm = 0x3E8
xorl %edx, %edx
divl %edi
movl %esi, %edx
cmpl %ecx, %edx
je 0xbd54
movw %ax, (%r8,%rcx,2)
incq %rcx
jmp 0xbb0b
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %eax
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq %r8, 0x8(%rsp)
movq %rdi, (%rsp)
cmpl %r13d, 0x10(%rsp)
je 0xbd46
cmpl $0x0, 0x199a4(%rip)     # 0x254f4
je 0xbb5e
movl $0x1, 0x199a0(%rip)     # 0x254fc
jmp 0xbb92
movzbl 0x19bcb(%rip), %eax     # 0x25730
addl 0x1999d(%rip), %eax     # 0x25508
movl %eax, 0x19997(%rip)     # 0x25508
cmpl $0x400, %eax            # imm = 0x400
jl 0xbb92
xorb $0x1, 0x1997d(%rip)     # 0x254fc
movl 0x19bb3(%rip), %ecx     # 0x25738
addl %ecx, %eax
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
movl %eax, 0x19976(%rip)     # 0x25508
cmpl $0x0, 0x1995f(%rip)     # 0x254f8
je 0xbbaa
movl $0x1, 0x1995b(%rip)     # 0x25500
pushq $0x1
popq %rax
jmp 0xbc1d
cmpb $0x1, 0x19b9f(%rip)     # 0x25750
jne 0xbbc9
movl 0x19957(%rip), %eax     # 0x25510
incl %eax
cltd
movl $0xff, %ecx
idivl %ecx
movl %edx, 0x19947(%rip)     # 0x25510
movzbl 0x19b60(%rip), %ecx     # 0x25730
addl 0x19936(%rip), %ecx     # 0x2550c
movl %ecx, 0x19930(%rip)     # 0x2550c
cmpl $0x400, %ecx            # imm = 0x400
jl 0xbc10
movb $0x1, 0x19b65(%rip)     # 0x25750
movl 0x1990f(%rip), %eax     # 0x25500
xorl $0x1, %eax
movl %eax, 0x19906(%rip)     # 0x25500
movl 0x19b3c(%rip), %edx     # 0x2573c
addl %edx, %ecx
addl $0xfffffc00, %ecx       # imm = 0xFFFFFC00
movl %ecx, 0x198fe(%rip)     # 0x2550c
jmp 0xbc1d
movb $0x0, 0x19b39(%rip)     # 0x25750
movl 0x198e3(%rip), %eax     # 0x25500
cmpl $0x0, 0x198fc(%rip)     # 0x25520
setne %bpl
cmpl $0x0, 0x198cd(%rip)     # 0x254fc
setne %cl
cmpl $0x0, 0x198e3(%rip)     # 0x2551c
setne %r12b
testl %eax, %eax
setne %bl
xorl %r15d, %r15d
cmpl $0x0, 0x198cc(%rip)     # 0x25518
je 0xbc66
movslq 0x198bb(%rip), %rax     # 0x25510
leaq 0x198d4(%rip), %rdx     # 0x25530
cmpb $0x0, (%rax,%rdx)
setne %r14b
jmp 0xbc69
xorl %r14d, %r14d
andb %cl, %bpl
je 0xbc92
movl 0xb61b0(%rdi), %edx
movq (%rsp), %rdi
xorl %esi, %esi
callq 0xbe74
movq (%rsp), %rdi
movq 0x8(%rsp), %r8
movl %eax, %r15d
andl 0xb71bc(%rdi), %r15d
andb %bl, %r12b
je 0xbcc0
movl 0xb61b4(%rdi), %edx
movq (%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xbe74
movq (%rsp), %rdi
movq 0x8(%rsp), %r8
andl 0xb71c0(%rdi), %eax
movl 0x19a82(%rip), %ecx     # 0x25740
jmp 0xbce9
testb %r14b, %r14b
je 0xbd25
movl 0x19a75(%rip), %ecx     # 0x25740
movl %ecx, %edx
shrl $0x7, %edx
orl $0x80, %edx
andl $-0x4, %edx
movl 0xb71c4(%rdi), %eax
leaq 0x1994a(%rip), %rsi     # 0x25630
andl (%rdx,%rsi), %eax
addl %eax, %r15d
testl %ecx, %ecx
setne %al
andb %al, %bpl
cmpb $0x1, %bpl
jne 0xbd25
shrl $0x7, %ecx
andl $-0x4, %ecx
movl %ecx, %eax
orl $0xc0, %eax
leaq 0x19922(%rip), %rdx     # 0x25630
imull (%rax,%rdx), %r15d
orl $0x40, %ecx
movl (%rcx,%rdx), %ecx
addl %ecx, %ecx
movl %r15d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
imull 0x197c3(%rip), %r15d    # 0x254f0
movl %r15d, %eax
xorl %edx, %edx
movl $0x3e8, %ecx            # imm = 0x3E8
divl %ecx
movw %ax, (%r8,%r13,2)
incq %r13
jmp 0xbb3e
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop