// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movl %edi, -0x8(%rbp)
movl $0x1, -0xc(%rbp)
cmpl $0x0, -0x8(%rbp)
jle 0x1b8ee
movl 0x1a3e6(%rip), %eax     # 0x35c28
cmpl 0x1aa3c(%rip), %eax     # 0x36284
jbe 0x1b8be
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18a37(%rip), %rax     # 0x34290
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18a28(%rip), %rax     # 0x34290
movb %dl, (%rax,%rcx)
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18a36(%rip), %rax     # 0x342b0
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18a27(%rip), %rax     # 0x342b0
movb %dl, (%rax,%rcx)
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18a25(%rip), %rax     # 0x342c0
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18a16(%rip), %rax     # 0x342c0
movb %dl, (%rax,%rcx)
movl -0x8(%rbp), %eax
addl 0x1a9ce(%rip), %eax     # 0x36284
movl %eax, 0x1a9c8(%rip)     # 0x36284
jmp 0x1b8e9
movq 0x872b(%rip), %rdi      # 0x23ff0
leaq 0x441b(%rip), %rsi      # 0x1fce7
callq 0x3250
movq %rax, %rdi
movq 0x86dd(%rip), %rsi      # 0x23fb8
callq 0x3290
movb $0x0, -0x1(%rbp)
jmp 0x1ba85
jmp 0x1b99c
movl $0x1, %eax
cmpl 0x1a98b(%rip), %eax     # 0x36284
jae 0x1b96f
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18986(%rip), %rax     # 0x34290
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18977(%rip), %rax     # 0x34290
movb %dl, (%rax,%rcx)
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18985(%rip), %rax     # 0x342b0
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18976(%rip), %rax     # 0x342b0
movb %dl, (%rax,%rcx)
movl -0x8(%rbp), %eax
addl 0x1a93e(%rip), %eax     # 0x36284
movl %eax, 0x1a938(%rip)     # 0x36284
movl -0x8(%rbp), %edx
movl -0xc(%rbp), %eax
movl %eax, %ecx
leaq 0x18965(%rip), %rax     # 0x342c0
movzbl (%rax,%rcx), %eax
addl %edx, %eax
movb %al, %dl
leaq 0x18956(%rip), %rax     # 0x342c0
movb %dl, (%rax,%rcx)
jmp 0x1b99a
movq 0x867a(%rip), %rdi      # 0x23ff0
leaq 0x436a(%rip), %rsi      # 0x1fce7
callq 0x3250
movq %rax, %rdi
movq 0x862c(%rip), %rsi      # 0x23fb8
callq 0x3290
movb $0x0, -0x1(%rbp)
jmp 0x1ba85
jmp 0x1b99c
cmpl $0x1, 0x1a8b9(%rip)     # 0x3625c
je 0x1b9b7
cmpl $0x2, 0x1a8b0(%rip)     # 0x3625c
jne 0x1b9d9
cmpl $0x0, 0x1a27b(%rip)     # 0x35c30
jne 0x1b9d9
movq 0x1a8e2(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x188e6(%rip), %rdx     # 0x342b0
movl $0x8, %ecx
callq 0x1d550
jmp 0x1ba70
cmpl $0x3, 0x1a87c(%rip)     # 0x3625c
jne 0x1ba01
movq 0x1a8b7(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x188cb(%rip), %rdx     # 0x342c0
movl $0x25, %ecx
callq 0x1d550
jmp 0x1ba6e
movl 0x1a891(%rip), %edx     # 0x36298
movl 0x1a877(%rip), %ecx     # 0x36284
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
andl %eax, %edx
movl $0x32, %ecx
movl $0x3c, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movl $0xd87c0, %eax          # imm = 0xD87C0
cltd
idivl %ecx
movw %ax, -0xe(%rbp)
movzwl -0xe(%rbp), %eax
andl $0xff, %eax
movb %al, 0x18887(%rip)      # 0x342cb
movzwl -0xe(%rbp), %eax
sarl $0x8, %eax
movb %al, 0x1887f(%rip)      # 0x342d0
movq 0x1a848(%rip), %rdi     # 0x362a0
movl $0xfe00, %esi           # imm = 0xFE00
leaq 0x1885c(%rip), %rdx     # 0x342c0
movl $0x25, %ecx
callq 0x1d550
jmp 0x1ba70
movq 0x2a839(%rip), %rdi     # 0x462b0
movl $0xfe00, %esi           # imm = 0xFE00
callq 0x4ff0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x10, %rsp
popq %rbp
retq