// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x310, %rsp            # imm = 0x310
movq %rdi, %rsi
leaq 0x8(%rsp), %rdi
movl $0x4, %edx
callq 0x6590
leaq 0x80(%rsp), %rdi
callq 0x60a0
testb %al, %al
je 0xf96f
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 0xf953
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %rbx
jne 0xf928
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
je 0xf997
leaq 0x8(%rsp), %rdi
callq 0x6080
jmp 0xf979
leaq 0x8(%rsp), %rdi
callq 0x6080
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x60d0
movl %ebx, %eax
addq $0x310, %rsp            # imm = 0x310
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x47, 0x210(%rsp)
jne 0xf963
cmpb $-0x57, 0x211(%rsp)
jne 0xf963
cmpb $0x2, 0x212(%rsp)
jne 0xf963
cmpb $0x6a, 0x213(%rsp)
jne 0xf963
cmpb $-0x45, 0x214(%rsp)
jne 0xf963
cmpb $0x47, 0x215(%rsp)
jne 0xf963
cmpb $-0xd, 0x216(%rsp)
jne 0xf963
cmpb $-0x59, 0x217(%rsp)
jne 0xf963
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 0xfa28
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %rbx
jne 0xf9fd
movslq 0x210(%rsp), %rsi
movl 0x214(%rsp), %ebp
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x6430
movq 0xca1e(%rip), %rbx      # 0x1c468
addq $0x20, %rbx
movl $0xa0, %r14d
leaq 0x8(%rsp), %r15
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xfa84
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %rbx
subq %rax, %r14
jne 0xfa59
movslq %ebp, %rsi
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x6430
movl $0x20, %r12d
leaq 0x8(%rsp), %rbx
movq 0xc9c3(%rip), %rax      # 0x1c468
testb $0x1, (%rax,%r12)
je 0xfaeb
movq %r12, %r14
shlq $0x8, %r14
addq 0xc9a6(%rip), %r14      # 0x1c460
movl $0x100, %r15d           # imm = 0x100
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xfaeb
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
addq %rax, %r14
subq %rax, %r15
jne 0xfac0
incq %r12
cmpq $0xc0, %r12
jne 0xfa9e
leaq 0x8(%rsp), %rdi
callq 0x6080
movb $0x1, 0xc968(%rip)      # 0x1c470
movb $0x1, %bl
jmp 0xf97b
jmp 0xfb1b
jmp 0xfb1b
jmp 0xfb1b
jmp 0xfb1b
jmp 0xfb1b
jmp 0xfb1b
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x60d0
movq %rbx, %rdi
callq 0x6550