// File: /workspace/github_c_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r8
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x19555(%rip)     # 0x25510
je 0xc018
testl %esi, %esi
je 0xc26a
movq 0x8(%rsp), %rax
movl 0xb71c4(%rax), %eax
movl 0x19532(%rip), %ecx     # 0x25508
orl 0x19530(%rip), %ecx     # 0x2550c
movl 0x1974a(%rip), %edx     # 0x2572c
shrl $0x9, %edx
orl %ecx, %edx
leaq 0x19632(%rip), %rcx     # 0x25620
andl (%rcx,%rdx,4), %eax
imull 0x194e8(%rip), %eax     # 0x254e0
imulq $0x10624dd3, %rax, %rax # imm = 0x10624DD3
shrq $0x26, %rax
movl %esi, %ecx
xorl %edx, %edx
movw %ax, (%r8,%rdx,2)
incq %rdx
cmpl %edx, %ecx
jne 0xc007
jmp 0xc26a
testl %esi, %esi
je 0xc26a
movl %esi, %r13d
xorl %esi, %esi
movq %r8, 0x18(%rsp)
movq %r13, 0x20(%rsp)
cmpl $0x0, 0x194ae(%rip)     # 0x254e4
je 0xc044
movl $0x1, 0x194aa(%rip)     # 0x254ec
jmp 0xc078
movzbl 0x196d5(%rip), %eax     # 0x25720
addl 0x194a3(%rip), %eax     # 0x254f4
movl %eax, 0x1949d(%rip)     # 0x254f4
cmpl $0x400, %eax            # imm = 0x400
jl 0xc078
xorb $0x1, 0x19487(%rip)     # 0x254ec
movl 0x196b9(%rip), %ecx     # 0x25724
addl %ecx, %eax
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
movl %eax, 0x1947c(%rip)     # 0x254f4
cmpl $0x0, 0x19469(%rip)     # 0x254e8
movq %rsi, 0x10(%rsp)
je 0xc095
movl $0x1, 0x19460(%rip)     # 0x254f0
jmp 0xc119
cmpb $0x1, 0x1969c(%rip)     # 0x25738
jne 0xc0d5
movl 0x19458(%rip), %eax     # 0x254fc
movl %eax, %ecx
incl %ecx
movslq %ecx, %rcx
imulq $-0x7f7f7f7f, %rcx, %rcx # imm = 0x80808081
shrq $0x20, %rcx
addl %eax, %ecx
incl %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x7, %ecx
addl %edx, %ecx
movl %ecx, %edx
shll $0x8, %edx
subl %edx, %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x19427(%rip)     # 0x254fc
movzbl 0x19644(%rip), %eax     # 0x25720
addl 0x19416(%rip), %eax     # 0x254f8
movl %eax, 0x19410(%rip)     # 0x254f8
cmpl $0x400, %eax            # imm = 0x400
jl 0xc112
xorb $0x1, 0x193fa(%rip)     # 0x254f0
movb $0x1, 0x1963b(%rip)     # 0x25738
movl 0x19625(%rip), %ecx     # 0x25728
addl %ecx, %eax
addl $0xfffffc00, %eax       # imm = 0xFFFFFC00
movl %eax, 0x193e8(%rip)     # 0x254f8
jmp 0xc119
movb $0x0, 0x1961f(%rip)     # 0x25738
cmpl $0x0, 0x193ec(%rip)     # 0x2550c
setne %bpl
cmpl $0x0, 0x193c1(%rip)     # 0x254ec
setne %al
cmpl $0x0, 0x193d3(%rip)     # 0x25508
setne %bl
cmpl $0x0, 0x193b1(%rip)     # 0x254f0
setne %r14b
xorl %r15d, %r15d
cmpl $0x0, 0x193b7(%rip)     # 0x25504
je 0xc167
movslq 0x193a6(%rip), %rcx     # 0x254fc
leaq 0x193c3(%rip), %rdx     # 0x25520
cmpb $0x0, (%rcx,%rdx)
setne %r12b
jmp 0xc16a
xorl %r12d, %r12d
andb %al, %bpl
je 0xc199
movq 0x8(%rsp), %r13
movl 0xb61b0(%r13), %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0xc39c
movq 0x18(%rsp), %r8
movl %eax, %r15d
andl 0xb71bc(%r13), %r15d
movq 0x20(%rsp), %r13
andb %r14b, %bl
je 0xc1cc
movq 0x8(%rsp), %rbx
movl 0xb61b4(%rbx), %edx
movq %rbx, %rdi
movl $0x1, %esi
callq 0xc39c
movq 0x18(%rsp), %r8
andl 0xb71c0(%rbx), %eax
addl %r15d, %eax
movb $0x1, %r12b
movl %eax, %r15d
jmp 0xc1fd
testb %r12b, %r12b
je 0xc1fd
movl 0x19555(%rip), %eax     # 0x2572c
shrl $0x7, %eax
orl $0x80, %eax
andl $-0x4, %eax
movq 0x8(%rsp), %rcx
movl 0xb71c4(%rcx), %ecx
leaq 0x1942c(%rip), %rdx     # 0x25620
andl (%rax,%rdx), %ecx
addl %r15d, %ecx
movl %ecx, %r15d
movq 0x10(%rsp), %rsi
movl 0x19524(%rip), %eax     # 0x2572c
testl %eax, %eax
setne %cl
andb %r12b, %cl
andb %bpl, %cl
cmpb $0x1, %cl
jne 0xc246
movl %eax, %eax
shrl $0x7, %eax
andl $-0x4, %eax
movl %eax, %ecx
orl $0xc0, %ecx
leaq 0x193f1(%rip), %rdx     # 0x25620
imull (%rcx,%rdx), %r15d
orl $0x40, %eax
movl (%rax,%rdx), %ecx
addl %ecx, %ecx
movl %r15d, %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r15d
imull 0x19292(%rip), %r15d    # 0x254e0
imulq $0x10624dd3, %r15, %rax # imm = 0x10624DD3
shrq $0x26, %rax
movw %ax, (%r8,%rsi,2)
incq %rsi
cmpl %esi, %r13d
jne 0xc02f
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop