// File: /workspace/github_cpp_makefile_star2_O2/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
pushq $0x4
popq %rdx
callq 0x65c0
leaq 0x80(%rsp), %rdi
callq 0x60b0
testb %al, %al
je 0xe017
leaq 0x210(%rsp), %rbx
pushq $0x8
popq %r14
leaq 0x8(%rsp), %r15
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
testq %r14, %r14
je 0xe023
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xe023
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
subq %rax, %r14
addq %rax, %rbx
jmp 0xdfe7
leaq 0x8(%rsp), %rdi
callq 0x6090
jmp 0xe089
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xe07f
movabsq $-0x580cb84495fd56b9, %rax # imm = 0xA7F347BB6A02A947
cmpq %rax, 0x210(%rsp)
jne 0xe07f
leaq 0x210(%rsp), %rbx
pushq $0x8
popq %r14
leaq 0x8(%rsp), %r15
testq %r14, %r14
je 0xe0a6
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xe0a6
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
subq %rax, %r14
addq %rax, %rbx
jmp 0xe04f
leaq 0x8(%rsp), %rdi
callq 0x6090
xorl %ebx, %ebx
leaq 0x8(%rsp), %rdi
callq 0x60e0
movl %ebx, %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r14
popq %r15
retq
movslq 0x210(%rsp), %rsi
movslq 0x214(%rsp), %rbx
leaq 0x8(%rsp), %rdi
xorl %edx, %edx
callq 0x6430
movq 0xd3af(%rip), %r14      # 0x1b478
addq $0x20, %r14
movl $0xa0, %r15d
leaq 0x8(%rsp), %r12
testq %r15, %r15
je 0xe108
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xe108
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
subq %rax, %r15
addq %rax, %r14
jmp 0xe0d8
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x6430
pushq $0x20
popq %r12
leaq 0x8(%rsp), %rbx
cmpq $0xc0, %r12
je 0xe180
movq 0xd348(%rip), %rax      # 0x1b478
testb $0x1, (%rax,%r12)
jne 0xe13c
incq %r12
jmp 0xe120
movq %r12, %r14
shlq $0x8, %r14
addq 0xd326(%rip), %r14      # 0x1b470
movl $0x100, %r15d           # imm = 0x100
testq %r15, %r15
je 0xe137
movq 0x8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x28(%rsp,%rax)
jne 0xe137
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6410
movq 0x10(%rsp), %rax
subq %rax, %r15
addq %rax, %r14
jmp 0xe150
leaq 0x8(%rsp), %rdi
callq 0x6090
movb $0x1, 0xd2ef(%rip)      # 0x1b480
movb $0x1, %bl
jmp 0xe08b
jmp 0xe1a4
jmp 0xe1a4
jmp 0xe1a4
jmp 0xe1a4
jmp 0xe1a4
jmp 0xe1a4
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x60e0
movq %rbx, %rdi
callq 0x6580