// File: 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
movq %rsp, %rbp
subq $0x3e0, %rsp            # imm = 0x3E0
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x218(%rbp), %rdi
movq %rdi, -0x378(%rbp)
movl $0x4, %edx
callq 0x7760
movq -0x378(%rbp), %rdi
callq 0x7680
movb %al, -0x36a(%rbp)
jmp 0x157cf
movb -0x36a(%rbp), %al
testb $0x1, %al
jne 0x1581f
jmp 0x157db
leaq -0x218(%rbp), %rdi
callq 0x70a0
jmp 0x157e9
movb $0x0, -0x1(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0x15d8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x70f0
jmp 0x15da4
movq $0x8, -0x338(%rbp)
leaq -0x330(%rbp), %rax
movq %rax, -0x340(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x338(%rbp)
movb %al, -0x379(%rbp)
je 0x15878
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x37a(%rbp)
jmp 0x1586a
movb -0x37a(%rbp), %al
movb %al, -0x379(%rbp)
jmp 0x15878
movb -0x379(%rbp), %al
testb $0x1, %al
jne 0x15884
jmp 0x158fc
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7550
jmp 0x158a0
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x388(%rbp)
jmp 0x158b5
movq -0x388(%rbp), %rcx
movq -0x338(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x338(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x390(%rbp)
jmp 0x158e2
movq -0x390(%rbp), %rax
addq -0x340(%rbp), %rax
movq %rax, -0x340(%rbp)
jmp 0x15838
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x7770
movb %al, -0x391(%rbp)
jmp 0x1591c
movb -0x391(%rbp), %al
testb $0x1, %al
jne 0x15954
jmp 0x15928
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x392(%rbp)
jmp 0x15948
movb -0x392(%rbp), %al
testb $0x1, %al
jne 0x15975
jmp 0x15954
leaq -0x218(%rbp), %rdi
callq 0x70a0
jmp 0x15962
movb $0x0, -0x1(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0x15d8a
movsbl -0x330(%rbp), %eax
cmpl $0x47, %eax
jne 0x159d5
movsbl -0x32f(%rbp), %eax
cmpl $-0x57, %eax
jne 0x159d5
movsbl -0x32e(%rbp), %eax
cmpl $0x2, %eax
jne 0x159d5
movsbl -0x32d(%rbp), %eax
cmpl $0x6a, %eax
jne 0x159d5
movsbl -0x32c(%rbp), %eax
cmpl $-0x45, %eax
jne 0x159d5
movsbl -0x32b(%rbp), %eax
cmpl $0x47, %eax
jne 0x159d5
movsbl -0x32a(%rbp), %eax
cmpl $-0xd, %eax
jne 0x159d5
movsbl -0x329(%rbp), %eax
cmpl $-0x59, %eax
je 0x159f6
leaq -0x218(%rbp), %rdi
callq 0x70a0
jmp 0x159e3
movb $0x0, -0x1(%rbp)
movl $0x1, -0x228(%rbp)
jmp 0x15d8a
movq $0x8, -0x338(%rbp)
leaq -0x330(%rbp), %rax
movq %rax, -0x340(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x338(%rbp)
movb %al, -0x393(%rbp)
je 0x15a4f
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x394(%rbp)
jmp 0x15a41
movb -0x394(%rbp), %al
movb %al, -0x393(%rbp)
jmp 0x15a4f
movb -0x393(%rbp), %al
testb $0x1, %al
jne 0x15a5b
jmp 0x15ad3
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7550
jmp 0x15a77
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3a0(%rbp)
jmp 0x15a8c
movq -0x3a0(%rbp), %rcx
movq -0x338(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x338(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3a8(%rbp)
jmp 0x15ab9
movq -0x3a8(%rbp), %rax
addq -0x340(%rbp), %rax
movq %rax, -0x340(%rbp)
jmp 0x15a0f
movl -0x330(%rbp), %eax
movl %eax, -0x344(%rbp)
movl -0x32c(%rbp), %eax
movl %eax, -0x348(%rbp)
movslq -0x344(%rbp), %rsi
leaq -0x358(%rbp), %rdi
callq 0x1c860
jmp 0x15b00
movq -0x358(%rbp), %rsi
movq -0x350(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7570
jmp 0x15b1c
movq $0xa0, -0x338(%rbp)
movq 0x17a3a(%rip), %rax     # 0x2d568
addq $0x20, %rax
movq %rax, -0x340(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x338(%rbp)
movb %al, -0x3a9(%rbp)
je 0x15b79
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x3aa(%rbp)
jmp 0x15b6b
movb -0x3aa(%rbp), %al
movb %al, -0x3a9(%rbp)
jmp 0x15b79
movb -0x3a9(%rbp), %al
testb $0x1, %al
jne 0x15b85
jmp 0x15bfd
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7550
jmp 0x15ba1
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3b8(%rbp)
jmp 0x15bb6
movq -0x3b8(%rbp), %rcx
movq -0x338(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x338(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3c0(%rbp)
jmp 0x15be3
movq -0x3c0(%rbp), %rax
addq -0x340(%rbp), %rax
movq %rax, -0x340(%rbp)
jmp 0x15b39
movslq -0x348(%rbp), %rsi
leaq -0x368(%rbp), %rdi
callq 0x1c860
jmp 0x15c12
movq -0x368(%rbp), %rsi
movq -0x360(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7570
jmp 0x15c2e
movb $0x20, -0x369(%rbp)
movzbl -0x369(%rbp), %eax
cmpl $0xc0, %eax
jge 0x15d67
movq 0x1791a(%rip), %rax     # 0x2d568
movzbl -0x369(%rbp), %ecx
movsbl (%rax,%rcx), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x15d52
movq $0x100, -0x338(%rbp) # imm = 0x100
movq 0x178e9(%rip), %rax     # 0x2d560
movzbl -0x369(%rbp), %ecx
shlq $0x8, %rcx
addq %rcx, %rax
movq %rax, -0x340(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x338(%rbp)
movb %al, -0x3c1(%rbp)
je 0x15ccc
movq -0x218(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x218(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x3c2(%rbp)
jmp 0x15cbe
movb -0x3c2(%rbp), %al
movb %al, -0x3c1(%rbp)
jmp 0x15ccc
movb -0x3c1(%rbp), %al
testb $0x1, %al
jne 0x15cd8
jmp 0x15d50
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x7550
jmp 0x15cf4
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3d0(%rbp)
jmp 0x15d09
movq -0x3d0(%rbp), %rcx
movq -0x338(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x338(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x77c0
movq %rax, -0x3d8(%rbp)
jmp 0x15d36
movq -0x3d8(%rbp), %rax
addq -0x340(%rbp), %rax
movq %rax, -0x340(%rbp)
jmp 0x15c8c
jmp 0x15d52
jmp 0x15d54
movb -0x369(%rbp), %al
addb $0x1, %al
movb %al, -0x369(%rbp)
jmp 0x15c35
leaq -0x218(%rbp), %rdi
callq 0x70a0
jmp 0x15d75
movb $0x1, 0x177f4(%rip)     # 0x2d570
movb $0x1, -0x1(%rbp)
movl $0x1, -0x228(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x70f0
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x3e0, %rsp            # imm = 0x3E0
popq %rbp
retq
movq -0x220(%rbp), %rdi
callq 0x7710