// File: mdpkh[P]retro6k/src/main.cpp
// Function: StepSoundSchedules()
void StepSoundSchedules()
{
	bool shiftregister[4]{ false, false, false, false };
	bool scheduleisempty[2]{ false, false };
	for (int s = 0; s < 2; ++s)
	{
		if (!soundcndcounter[s])
		{
			for (int v = 0; v < 4; ++v)
			{
				if (soundscvmapregister & (1 << (4 * s + v)))
				{
					soundvolregister[v] = soundqueue[v][0].vol;
					soundfreqregister[v] = soundqueue[v][0].freq;
					soundcndcounter[s] = soundqueuedur[s][0];
					shiftregister[v] = true;
				}
			}
			for (int i = 0; i < 15; ++i)
				soundqueuedur[s][i] = soundqueuedur[s][i + 1];
			soundqueuedur[s][15] = 0xffff;
			++soundncounter[s];
			if (!soundncounter[s])
				scheduleisempty[s] = true;
		}
		--soundcndcounter[s];
	}
	for (int v = 0; v < 4; ++v)
	{
		if (shiftregister[v])
		{
			for (int i = 0; i < 15; ++i)
				soundqueue[v][i] = soundqueue[v][i + 1];
			soundqueue[v][15] = { 0, 0 };
		}
	}
	if (scheduleisempty[0] || scheduleisempty[1])
		; // TODO: trigger interrupt if appropriate flag (?) is set
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
leaq -0x4(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x7260
leaq -0x6(%rbp), %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x7260
movl $0x0, -0xc(%rbp)
cmpl $0x2, -0xc(%rbp)
jge 0xdfd0
movslq -0xc(%rbp), %rcx
leaq 0x1fadc(%rip), %rax     # 0x2d920
cmpw $0x0, (%rax,%rcx,2)
jne 0xdfa4
movl $0x0, -0x10(%rbp)
cmpl $0x4, -0x10(%rbp)
jge 0xdf04
movzbl 0x1f96c(%rip), %eax     # 0x2d7d3
movl -0xc(%rbp), %ecx
shll $0x2, %ecx
addl -0x10(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xdef4
movslq -0x10(%rbp), %rcx
leaq 0x1f995(%rip), %rax     # 0x2d820
shlq $0x6, %rcx
addq %rcx, %rax
movb (%rax), %dl
movslq -0x10(%rbp), %rcx
leaq 0x1f92f(%rip), %rax     # 0x2d7ce
movb %dl, (%rax,%rcx)
movslq -0x10(%rbp), %rcx
leaq 0x1f973(%rip), %rax     # 0x2d820
shlq $0x6, %rcx
addq %rcx, %rax
movw 0x2(%rax), %dx
movslq -0x10(%rbp), %rcx
leaq 0x1f903(%rip), %rax     # 0x2d7c6
movw %dx, (%rax,%rcx,2)
movslq -0xc(%rbp), %rcx
leaq 0x1f90e(%rip), %rax     # 0x2d7e0
shlq $0x5, %rcx
addq %rcx, %rax
movw (%rax), %dx
movslq -0xc(%rbp), %rcx
leaq 0x1fa39(%rip), %rax     # 0x2d920
movw %dx, (%rax,%rcx,2)
movslq -0x10(%rbp), %rax
movb $0x1, -0x4(%rbp,%rax)
jmp 0xdef6
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xde56
movl $0x0, -0x14(%rbp)
cmpl $0xf, -0x14(%rbp)
jge 0xdf55
movslq -0xc(%rbp), %rcx
leaq 0x1f8c4(%rip), %rax     # 0x2d7e0
shlq $0x5, %rcx
addq %rcx, %rax
movl -0x14(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movw (%rax,%rcx,2), %dx
movslq -0xc(%rbp), %rcx
leaq 0x1f8a5(%rip), %rax     # 0x2d7e0
shlq $0x5, %rcx
addq %rcx, %rax
movslq -0x14(%rbp), %rcx
movw %dx, (%rax,%rcx,2)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xdf0b
movslq -0xc(%rbp), %rcx
leaq 0x1f880(%rip), %rax     # 0x2d7e0
shlq $0x5, %rcx
addq %rcx, %rax
movw $0xffff, 0x1e(%rax)     # imm = 0xFFFF
movslq -0xc(%rbp), %rcx
leaq 0x1f9ac(%rip), %rax     # 0x2d924
movb (%rax,%rcx), %dl
addb $0x1, %dl
leaq 0x1f99f(%rip), %rax     # 0x2d924
movb %dl, (%rax,%rcx)
movslq -0xc(%rbp), %rcx
leaq 0x1f991(%rip), %rax     # 0x2d924
cmpb $0x0, (%rax,%rcx)
jne 0xdfa2
movslq -0xc(%rbp), %rax
movb $0x1, -0x6(%rbp,%rax)
jmp 0xdfa4
movslq -0xc(%rbp), %rcx
leaq 0x1f971(%rip), %rax     # 0x2d920
movw (%rax,%rcx,2), %dx
addw $-0x1, %dx
leaq 0x1f962(%rip), %rax     # 0x2d920
movw %dx, (%rax,%rcx,2)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xde2f
movl $0x0, -0x18(%rbp)
cmpl $0x4, -0x18(%rbp)
jge 0xe06d
movslq -0x18(%rbp), %rax
testb $0x1, -0x4(%rbp,%rax)
je 0xe05d
movl $0x0, -0x1c(%rbp)
cmpl $0xf, -0x1c(%rbp)
jge 0xe03b
movslq -0x18(%rbp), %rax
leaq 0x1f81c(%rip), %rdx     # 0x2d820
shlq $0x6, %rax
addq %rax, %rdx
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
movslq -0x18(%rbp), %rcx
leaq 0x1f801(%rip), %rax     # 0x2d820
shlq $0x6, %rcx
addq %rcx, %rax
movslq -0x1c(%rbp), %rcx
movl (%rdx,%rsi,4), %edx
movl %edx, (%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xdff3
movb $0x0, -0x20(%rbp)
movw $0x0, -0x1e(%rbp)
movslq -0x18(%rbp), %rcx
leaq 0x1f7d0(%rip), %rax     # 0x2d820
shlq $0x6, %rcx
addq %rcx, %rax
movl -0x20(%rbp), %ecx
movl %ecx, 0x3c(%rax)
jmp 0xe05f
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0xdfd7
testb $0x1, -0x6(%rbp)
jne 0xe079
testb $0x1, -0x5(%rbp)
je 0xe07b
jmp 0xe07b
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)