// File: /workspace/github_c_makefile_star2_O3/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 0xd6bb
movl 0x19744(%rip), %eax     # 0x26dd4
cmpl %eax, 0x190e2(%rip)     # 0x26778
jbe 0xd73e
addb %dil, 0x17b6e(%rip)     # 0x25211
addb %dil, 0x17b87(%rip)     # 0x25231
addb %dil, 0x17b90(%rip)     # 0x25241
addl %edi, %eax
movl %eax, 0x1971b(%rip)     # 0x26dd4
jmp 0xd6e3
movl 0x19713(%rip), %eax     # 0x26dd4
cmpl $0x2, %eax
jb 0xd73e
addb %dil, 0x17b44(%rip)     # 0x25211
addb %dil, 0x17b5d(%rip)     # 0x25231
addl %edi, %eax
movl %eax, 0x196f8(%rip)     # 0x26dd4
addb %dil, 0x17b5e(%rip)     # 0x25241
movl 0x196c3(%rip), %ecx     # 0x26dac
cmpl $0x1, %ecx
je 0xd704
cmpl $0x2, %ecx
jne 0xd784
cmpl $0x0, 0x19082(%rip)     # 0x26780
jne 0xd784
movq 0x196e5(%rip), %rax     # 0x26df0
xorl %ecx, %ecx
leaq 0x17b1c(%rip), %rdx     # 0x25230
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x8, %rcx
jne 0xd714
movq 0x296d0(%rip), %rdi     # 0x36e00
movl $0xfe00, %esi           # imm = 0xFE00
callq 0x448e
movb $0x1, %al
jmp 0xd782
movq 0x78ab(%rip), %rbx      # 0x14ff0
leaq 0x3524(%rip), %rsi      # 0x10c70
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, %ecx
jne 0xd7b3
movq 0x19660(%rip), %rax     # 0x26df0
xorl %ecx, %ecx
leaq 0x17aa7(%rip), %rdx     # 0x25240
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x25, %rcx
jne 0xd799
jmp 0xd729
movl 0x1962f(%rip), %ecx     # 0x26de8
decb %al
movzbl %al, %eax
btl %eax, %ecx
movl $0x4546, %eax           # imm = 0x4546
movl $0x39ba, %ecx           # imm = 0x39BA
cmovael %eax, %ecx
movb %cl, 0x17a77(%rip)      # 0x2524b
movb %ch, 0x17a76(%rip)      # 0x25250
movq 0x1960f(%rip), %rax     # 0x26df0
xorl %ecx, %ecx
leaq 0x17a56(%rip), %rdx     # 0x25240
movb (%rcx,%rdx), %sil
movb %sil, 0xb6fb8(%rax,%rcx)
incq %rcx
cmpq $0x25, %rcx
jne 0xd7ea
jmp 0xd729