// File: mdpkh[P]retro6k/src/main.cpp
// Function: InstallROM()
void InstallROM()
{
	const char* infilename[4] = {
		"bankc.rom",
		"bankd.rom",
		"banke.rom",
		"bankf.rom" 
	};
	for (int i = 0; i < 4; ++i)
	{
		std::filesystem::path romfile;
		for (auto& path : config.path.rompath)
		{
			if (std::filesystem::exists(path / std::filesystem::path(infilename[i])))
			{
				romfile = path / std::filesystem::path(infilename[i]);
				break;
			}
		}
		for (int p = 0; p < 16; ++p)
			syspflags[0xc0 | (i << 4) | p] = pageflags::PF_TROM;
		std::ifstream infile(romfile, std::ios::binary);
		if (infile.is_open())
		{
			std::streamsize readsize = 0x1000;
			char* romptr = (char*)sysram + 0xC000ll + 0x1000ll * i;
			while (readsize && infile.good())
			{
				infile.read(romptr, readsize);
				readsize -= infile.gcount();
				romptr += infile.gcount();
			}
			infile.close();
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x370, %rsp            # imm = 0x370
movq 0x1d36e(%rip), %rax     # 0x2bd90
movq %rax, -0x20(%rbp)
movq 0x1d36b(%rip), %rax     # 0x2bd98
movq %rax, -0x18(%rbp)
movq 0x1d368(%rip), %rax     # 0x2bda0
movq %rax, -0x10(%rbp)
movq 0x1d365(%rip), %rax     # 0x2bda8
movq %rax, -0x8(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x4, -0x24(%rbp)
jge 0xee03
leaq -0x50(%rbp), %rdi
callq 0x137e0
leaq 0x1e9b8(%rip), %rax     # 0x2d420
addq $0x68, %rax
movq %rax, -0x58(%rbp)
leaq 0x1e9a9(%rip), %rdi     # 0x2d420
addq $0x68, %rdi
callq 0x12da0
movq %rax, -0x60(%rbp)
leaq 0x1e995(%rip), %rdi     # 0x2d420
addq $0x68, %rdi
callq 0x12d30
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x12dd0
testb $0x1, %al
jne 0xeaae
jmp 0xec36
leaq -0x60(%rbp), %rdi
callq 0x12e40
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x348(%rbp)
movslq -0x24(%rbp), %rax
leaq -0x20(%rbp,%rax,8), %rsi
leaq -0xc0(%rbp), %rdi
movl $0x2, %edx
callq 0x14ba0
jmp 0xeae2
movq -0x348(%rbp), %rsi
leaq -0x98(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x146c0
jmp 0xeafe
leaq -0x98(%rbp), %rdi
callq 0x12e10
movb %al, -0x349(%rbp)
jmp 0xeb12
leaq -0x98(%rbp), %rdi
callq 0x12ed0
leaq -0xc0(%rbp), %rdi
callq 0x12ed0
movb -0x349(%rbp), %al
testb $0x1, %al
jne 0xeb39
jmp 0xec26
movq -0x70(%rbp), %rax
movq %rax, -0x358(%rbp)
movslq -0x24(%rbp), %rax
leaq -0x20(%rbp,%rax,8), %rsi
leaq -0x120(%rbp), %rdi
movl $0x2, %edx
callq 0x14ba0
jmp 0xeb60
movq -0x358(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
leaq -0x120(%rbp), %rdx
callq 0x146c0
jmp 0xeb7c
leaq -0x50(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x12e60
leaq -0xf8(%rbp), %rdi
callq 0x12ed0
leaq -0x120(%rbp), %rdi
callq 0x12ed0
jmp 0xec36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xedf8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
jmp 0xebf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x12ed0
leaq -0xc0(%rbp), %rdi
callq 0x12ed0
jmp 0xedf8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x12ed0
jmp 0xedf8
jmp 0xec28
leaq -0x60(%rbp), %rdi
callq 0x12f00
jmp 0xea98
movl $0x0, -0x124(%rbp)
cmpl $0x10, -0x124(%rbp)
jge 0xec7a
movq 0x1e918(%rip), %rax     # 0x2d568
movl -0x24(%rbp), %ecx
shll $0x4, %ecx
orl $0xc0, %ecx
orl -0x124(%rbp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0xec40
leaq -0x330(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x4, %edx
callq 0x14c80
jmp 0xec91
leaq -0x330(%rbp), %rdi
callq 0x7680
movb %al, -0x359(%rbp)
jmp 0xeca5
movb -0x359(%rbp), %al
testb $0x1, %al
jne 0xecb4
jmp 0xedd5
movq $0x1000, -0x338(%rbp) # imm = 0x1000
movq 0x1e89a(%rip), %rax     # 0x2d560
addq $0xc000, %rax           # imm = 0xC000
movslq -0x24(%rbp), %rcx
shlq $0xc, %rcx
addq %rcx, %rax
movq %rax, -0x340(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x338(%rbp)
movb %al, -0x35a(%rbp)
je 0xed1e
movq -0x330(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x330(%rbp,%rax), %rdi
callq 0x76f0
movb %al, -0x35b(%rbp)
jmp 0xed10
movb -0x35b(%rbp), %al
movb %al, -0x35a(%rbp)
jmp 0xed1e
movb -0x35a(%rbp), %al
testb $0x1, %al
jne 0xed2d
jmp 0xedc5
movq -0x340(%rbp), %rsi
movq -0x338(%rbp), %rdx
leaq -0x330(%rbp), %rdi
callq 0x7550
jmp 0xed49
leaq -0x330(%rbp), %rdi
callq 0x77c0
movq %rax, -0x368(%rbp)
jmp 0xed5e
movq -0x368(%rbp), %rcx
movq -0x338(%rbp), %rax
subq %rcx, %rax
movq %rax, -0x338(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x77c0
movq %rax, -0x370(%rbp)
jmp 0xed8b
movq -0x370(%rbp), %rax
addq -0x340(%rbp), %rax
movq %rax, -0x340(%rbp)
jmp 0xecde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x330(%rbp), %rdi
callq 0x70f0
jmp 0xedf8
leaq -0x330(%rbp), %rdi
callq 0x70a0
jmp 0xedd3
jmp 0xedd5
leaq -0x330(%rbp), %rdi
callq 0x70f0
leaq -0x50(%rbp), %rdi
callq 0x12ed0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xea4e
leaq -0x50(%rbp), %rdi
callq 0x12ed0
jmp 0xee0c
addq $0x370, %rsp            # imm = 0x370
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x7710
nopl (%rax,%rax)