// File: /workspace/github_c_makefile_star2_O1/calmopyrin[P]tedplay/tedplay.cpp
// Function: psidChangeTrack(int)
bool psidChangeTrack(int direction)
{
#ifdef NEWPLAYER
	unsigned int tuneOffset = 0x22;
#else
	unsigned int tuneOffset = 1;
#endif
	// SID
	if (direction > 0) {
		if (psidHdr.tracks > psidHdr.current) {
			psidPlayer[tuneOffset] += direction;
			rsidPlayer[tuneOffset] += direction;
			tmfPlayer[tuneOffset] += direction;
			psidHdr.current += direction;
		}
		else {
			std::cerr << "No more tracks." << std::endl;
			return false;
		}
	}
	else {
		if (1 < psidHdr.current) {
			psidPlayer[tuneOffset] += direction;
			rsidPlayer[tuneOffset] += direction;
			psidHdr.current += direction;
			tmfPlayer[tuneOffset] += direction;
		}
		else {
			std::cerr << "No more tracks." << std::endl;
			return false;
		}
	}
	if (psidHdr.type == 1 || (psidHdr.type == 2 && !psidHdr.replayAddress)) {
		ted->writeProtectedPlayerMemory(playerStartAddress, rsidPlayer, sizeof(rsidPlayer));
	}
	else if (psidHdr.type == 3) {
		ted->writeProtectedPlayerMemory(playerStartAddress, tmfPlayer, sizeof(tmfPlayer));
	}
	else {
		//ted->writeProtectedPlayerMemory(playerStartAddress, psidPlayer, sizeof(psidPlayer));
		unsigned short timer = (TED_SOUND_CLOCK * 4) / (psidHdr.speed & (1 << (psidHdr.current - 1)) ? 60 : 50);
		tmfPlayer[11] = timer & 0xFF;
		tmfPlayer[16] = timer >> 8;
		ted->writeProtectedPlayerMemory(playerStartAddress, tmfPlayer, sizeof(tmfPlayer));
	}
	cpu->setPC(playerStartAddress);
	return true;
}

// Assembly:
pushq %rbx
testl %edi, %edi
jle 0xd1cd
movl 0x18c22(%rip), %eax     # 0x25dc4
cmpl %eax, 0x185c0(%rip)     # 0x25768
jbe 0xd250
addb %dil, 0x1705c(%rip)     # 0x24211
addb %dil, 0x17075(%rip)     # 0x24231
addb %dil, 0x1707e(%rip)     # 0x24241
addl %edi, %eax
movl %eax, 0x18bf9(%rip)     # 0x25dc4
jmp 0xd1f5
movl 0x18bf1(%rip), %eax     # 0x25dc4
cmpl $0x2, %eax
jb 0xd250
addb %dil, 0x17032(%rip)     # 0x24211
addb %dil, 0x1704b(%rip)     # 0x24231
addl %edi, %eax
movl %eax, 0x18bd6(%rip)     # 0x25dc4
addb %dil, 0x1704c(%rip)     # 0x24241
movl 0x18ba1(%rip), %eax     # 0x25d9c
cmpl $0x1, %eax
je 0xd216
cmpl $0x2, %eax
jne 0xd296
cmpl $0x0, 0x18560(%rip)     # 0x25770
jne 0xd296
movq 0x18bc3(%rip), %rax     # 0x25de0
xorl %ecx, %ecx
leaq 0x1700a(%rip), %rdx     # 0x24230
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x8, %rcx
jne 0xd226
movq 0x28bae(%rip), %rdi     # 0x35df0
movl $0xfe00, %esi           # imm = 0xFE00
callq 0x44aa
movb $0x1, %al
jmp 0xd294
movq 0x6d99(%rip), %rbx      # 0x13ff0
leaq 0x37ea(%rip), %rsi      # 0x10a48
movl $0xf, %edx
movq %rbx, %rdi
callq 0x3260
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3200
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3050
movq %rax, %rdi
callq 0x3150
xorl %eax, %eax
popq %rbx
retq
cmpl $0x3, %eax
jne 0xd2c5
movq 0x18b3e(%rip), %rax     # 0x25de0
xorl %ecx, %ecx
leaq 0x16f95(%rip), %rdx     # 0x24240
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x25, %rcx
jne 0xd2ab
jmp 0xd23b
movl 0x18b0d(%rip), %eax     # 0x25dd8
movb 0x18af3(%rip), %cl      # 0x25dc4
decb %cl
movzbl %cl, %ecx
btl %ecx, %eax
movl $0x4546, %eax           # imm = 0x4546
movl $0x39ba, %ecx           # imm = 0x39BA
cmovael %eax, %ecx
movb %cl, 0x16f5f(%rip)      # 0x2424b
movb %ch, 0x16f5e(%rip)      # 0x24250
movq 0x18ae7(%rip), %rax     # 0x25de0
xorl %ecx, %ecx
leaq 0x16f3e(%rip), %rdx     # 0x24240
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x25, %rcx
jne 0xd302
jmp 0xd23b