// File: /workspace/github_cpp_makefile_star2_O3/mdpkh[P]retro6k/src/main.cpp
// Function: bool LoadCartridge<char const*>(char const*)
bool LoadCartridge(T infilename)
{
	std::ifstream infile(infilename, std::ios::binary);
	if (!infile.is_open())
	{
		infile.close();
		return false;
	}
	char readbuf[0x100];
	std::streamsize readsize = 0x8;
	char* readptr = readbuf;
	// read 8-byte signature
	while (readsize && infile.good())
	{
		infile.read(readptr, readsize);
		readsize -= infile.gcount();
		readptr += infile.gcount();
	}
	if (infile.eof() || !infile.good())
	{
		infile.close();
		return false;
	}
	if ( !((readbuf[0] == (char)0x47) // magic signature for R6k cartridge format 1986c
		&& (readbuf[1] == (char)0xa9)
		&& (readbuf[2] == (char)0x02)
		&& (readbuf[3] == (char)0x6a)
		&& (readbuf[4] == (char)0xbb)
		&& (readbuf[5] == (char)0x47)
		&& (readbuf[6] == (char)0xf3)
		&& (readbuf[7] == (char)0xa7)))
	{
		infile.close();
		return false;
	}
	// read offsets to (base address space) page flags start,
	// (base address space) ROM/NonVol data start (4-byte LE)
	readsize = 0x8;
	readptr = readbuf;
	while (readsize && infile.good())
	{
		infile.read(readptr, readsize);
		readsize -= infile.gcount();
		readptr += infile.gcount();
	}
	int pfstart = (unsigned char)readbuf[0] | (unsigned char)readbuf[1] << 8 | (unsigned char)readbuf[2] << 16 | (unsigned char)readbuf[3] << 24;
	int datastart = (unsigned char)readbuf[4] | (unsigned char)readbuf[5] << 8 | (unsigned char)readbuf[6] << 16 | (unsigned char)readbuf[7] << 24;
	// TODO: read extension data
	// read BAS page flags
	infile.seekg(pfstart);
	readsize = 0xa0;
	readptr = (char*)syspflags + 0x20;
	while (readsize && infile.good())
	{
		infile.read(readptr, readsize);
		readsize -= infile.gcount();
		readptr += infile.gcount();
	}
	// read BAS ROM & factory-state non-volatile memory
	infile.seekg(datastart);
	for (unsigned char p = 0x20; p < 0xc0; ++p)
	{
		if ((char)syspflags[p] & (char)pageflags::PF_TROM) // ROM or NVM
		{
			readsize = 0x100;
			readptr = (char*)sysram + ((long long)p << 8);
			while (readsize && infile.good())
			{
				infile.read(readptr, readsize);
				readsize -= infile.gcount();
				readptr += infile.gcount();
			}
		}
	}
	infile.close();
	cartridgeinserted = true;
	return true;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rdi, %rsi
leaq 0x8(%rsp), %rdi
movl $0x4, %edx
callq 0x6590
leaq 0x80(%rsp), %rdi
callq 0x60a0
testb %al, %al
je 0x10034
movl $0x8, %ebx
leaq 0x210(%rsp), %r14
leaq 0x8(%rsp), %r15
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xfeef
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %rbx
jne 0xfebb
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0x10040
movabsq $-0x580cb84495fd56b9, %rax # imm = 0xA7F347BB6A02A947
cmpq %rax, 0x210(%rsp)
jne 0x10040
movl $0x8, %ebx
leaq 0x210(%rsp), %r14
leaq 0x8(%rsp), %r15
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xff4f
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %rbx
jne 0xff24
movslq 0x210(%rsp), %rsi
movslq 0x214(%rsp), %rbx
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x6430
movq 0xc4f6(%rip), %r14      # 0x1c468
addq $0x20, %r14
movl $0xa0, %r15d
leaq 0x8(%rsp), %r12
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xffac
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %r15
jne 0xff81
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x6430
movl $0x20, %r12d
leaq 0x8(%rsp), %rbx
movq 0xc49b(%rip), %rax      # 0x1c468
testb $0x1, (%rax,%r12)
je 0x10013
movq %r12, %r14
shlq $0x8, %r14
addq 0xc47e(%rip), %r14      # 0x1c460
movl $0x100, %r15d           # imm = 0x100
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0x10013
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %r15
jne 0xffe8
incq %r12
cmpq $0xc0, %r12
jne 0xffc6
leaq 0x8(%rsp), %rdi
callq 0x6080
movb $0x1, 0xc440(%rip)      # 0x1c470
movb $0x1, %bl
jmp 0x1004c
leaq 0x8(%rsp), %rdi
callq 0x6080
jmp 0x1004a
leaq 0x8(%rsp), %rdi
callq 0x6080
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x60d0
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x10073
jmp 0x10073
jmp 0x10073
jmp 0x10073
jmp 0x10073
jmp 0x10073
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x60d0
movq %rbx, %rdi
callq 0x6550