// File: mdpkh[P]retro6k/src/main.cpp
// Function: InitMainWindow()
int InitMainWindow()
{
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
	SDL_DisplayMode desktopmode;
	SDL_GetDesktopDisplayMode(0, &desktopmode);
	config.screen.pixwidth = config.screen.specpixwidth;
	config.screen.pixheight = config.screen.specpixheight;
	switch (config.screen.specaspectratio)
	{
	case aspectratiocat::AR_WIDE:
		config.screen.reportedaspectratio = aspectratiocat::AR_WIDE;
		videomode |= 0x80;
		if (config.screen.pixwidth == -1)
		{
			if (config.screen.pixheight == -1)
			{
				int autowidth = (desktopmode.w - 64) / 256;
				int autoheight = (desktopmode.h - 64) / 144;
				if (autowidth >= autoheight)
				{
					config.screen.pixheight = autoheight;
					config.screen.pixwidth = autoheight;
				}
				else
				{
					config.screen.pixwidth = autowidth;
					config.screen.pixheight = autowidth;
				}
			}
			else
			{
				config.screen.pixwidth = config.screen.pixheight;
			}
		}
		else
		{
			if (config.screen.pixheight == -1)
			{
				config.screen.pixheight = config.screen.pixwidth;
			}
		}
		break;
	case aspectratiocat::AR_CLASSIC:
		config.screen.reportedaspectratio = aspectratiocat::AR_CLASSIC;
		videomode &= 0x7f;
		if (config.screen.pixwidth == -1)
		{
			if (config.screen.pixheight == -1)
			{
				int autowidth = (desktopmode.w - 64) / 256;
				int autoheight = (desktopmode.h - 64) / 144;
				if (autowidth * 4 >= autoheight * 3)
				{
					config.screen.pixheight = autoheight;
					config.screen.pixwidth = (autoheight * 3 + 2) / 4;
				}
				else
				{
					config.screen.pixwidth = autowidth;
					config.screen.pixheight = (autowidth * 4 + 1) / 3;
				}
			}
			else
			{
				config.screen.pixwidth = (config.screen.pixheight * 3 + 2) / 4;
			}
		}
		else
		{
			if (config.screen.pixheight == -1)
			{
				config.screen.pixheight = (config.screen.pixwidth * 4 + 1) / 3;
			}
		}
		break;
	case aspectratiocat::AR_FREE:
	default:
		if (config.screen.pixwidth == -1)
			config.screen.pixwidth = (desktopmode.w - 64) / 256;
		if (config.screen.pixheight == -1)
			config.screen.pixheight = (desktopmode.h - 64) / 144;
		if (config.screen.pixwidth * config.screen.pixwidth * 4
			> config.screen.pixheight* config.screen.pixheight * 3)
		{
			config.screen.reportedaspectratio = aspectratiocat::AR_WIDE;
			videomode |= 0x80;
		}
		else
		{
			config.screen.reportedaspectratio = aspectratiocat::AR_CLASSIC;
			videomode &= 0x7f;
		}
		break;
	}
	if (config.screen.pixwidth < 1)
		config.screen.pixwidth = 1;
	if (config.screen.pixheight < 1)
		config.screen.pixheight = 1;
	mainwindow = SDL_CreateWindow("Retro 6k Emulator",
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		config.screen.pixwidth * 256,
		config.screen.pixheight * 144,
		0);
	if (!mainwindow)
	{
		SDL_Log("Could not create a window: %s", SDL_GetError());
		return -1;
	}
	SDL_Surface* mwsurface = SDL_GetWindowSurface(mainwindow);
	framebuffer = SDL_CreateRGBSurfaceWithFormat(
		0,
		config.screen.pixwidth * 256,
		config.screen.pixheight * 144,
		mwsurface->format->BitsPerPixel,
		SDL_MasksToPixelFormatEnum(
			mwsurface->format->BitsPerPixel,
			mwsurface->format->Rmask,
			mwsurface->format->Gmask,
			mwsurface->format->Bmask,
			mwsurface->format->Amask));
	if (!framebuffer)
	{
		SDL_Log("Could not create frame buffer: %s", SDL_GetError());
		return -1;
	}
	cellcanvas = SDL_CreateRGBSurface(0, config.screen.pixwidth * 8, config.screen.pixheight * 8, 8, 0, 0, 0, 0);
	if (!cellcanvas)
	{
		SDL_Log("Could not create cell canvas: %s", SDL_GetError());
		return -1;
	}
	SetHWPalette(cellcanvas->format->palette);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x31, %edi
callq 0x73f0
xorl %edi, %edi
leaq -0x20(%rbp), %rsi
callq 0x76a0
movl 0x1f145(%rip), %eax     # 0x2d548
movl %eax, 0x1f147(%rip)     # 0x2d550
movl 0x1f13d(%rip), %eax     # 0x2d54c
movl %eax, 0x1f13f(%rip)     # 0x2d554
movl 0x1f13d(%rip), %eax     # 0x2d558
movl %eax, -0x3c(%rbp)
testl %eax, %eax
je 0xe5d4
jmp 0xe428
movl -0x3c(%rbp), %eax
subl $0x1, %eax
je 0xe4ee
jmp 0xe436
movl -0x3c(%rbp), %eax
subl $0x2, %eax
jne 0xe5d6
jmp 0xe444
movl $0x2, 0x1f10e(%rip)     # 0x2d55c
movzbl 0x1f36e(%rip), %eax     # 0x2d7c3
orl $0x80, %eax
movb %al, 0x1f363(%rip)      # 0x2d7c3
cmpl $-0x1, 0x1f0e9(%rip)    # 0x2d550
jne 0xe4d2
cmpl $-0x1, 0x1f0e4(%rip)    # 0x2d554
jne 0xe4c4
movl -0x1c(%rbp), %eax
subl $0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, -0x24(%rbp)
movl -0x18(%rbp), %eax
subl $0x40, %eax
movl $0x90, %ecx
cltd
idivl %ecx
movl %eax, -0x28(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
jl 0xe4b0
movl -0x28(%rbp), %eax
movl %eax, 0x1f0af(%rip)     # 0x2d554
movl -0x28(%rbp), %eax
movl %eax, 0x1f0a2(%rip)     # 0x2d550
jmp 0xe4c2
movl -0x24(%rbp), %eax
movl %eax, 0x1f097(%rip)     # 0x2d550
movl -0x24(%rbp), %eax
movl %eax, 0x1f092(%rip)     # 0x2d554
jmp 0xe4d0
movl 0x1f08a(%rip), %eax     # 0x2d554
movl %eax, 0x1f080(%rip)     # 0x2d550
jmp 0xe4e9
cmpl $-0x1, 0x1f07b(%rip)    # 0x2d554
jne 0xe4e7
movl 0x1f06f(%rip), %eax     # 0x2d550
movl %eax, 0x1f06d(%rip)     # 0x2d554
jmp 0xe4e9
jmp 0xe66e
movl $0x1, 0x1f064(%rip)     # 0x2d55c
movzbl 0x1f2c4(%rip), %eax     # 0x2d7c3
andl $0x7f, %eax
movb %al, 0x1f2bb(%rip)      # 0x2d7c3
cmpl $-0x1, 0x1f041(%rip)    # 0x2d550
jne 0xe5aa
cmpl $-0x1, 0x1f038(%rip)    # 0x2d554
jne 0xe590
movl -0x1c(%rbp), %eax
subl $0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, -0x2c(%rbp)
movl -0x18(%rbp), %eax
subl $0x40, %eax
movl $0x90, %ecx
cltd
idivl %ecx
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
shll $0x2, %eax
imull $0x3, -0x30(%rbp), %ecx
cmpl %ecx, %eax
jl 0xe56e
movl -0x30(%rbp), %eax
movl %eax, 0x1effd(%rip)     # 0x2d554
imull $0x3, -0x30(%rbp), %eax
addl $0x2, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, 0x1efe4(%rip)     # 0x2d550
jmp 0xe58e
movl -0x2c(%rbp), %eax
movl %eax, 0x1efd9(%rip)     # 0x2d550
movl -0x2c(%rbp), %eax
shll $0x2, %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, 0x1efc6(%rip)     # 0x2d554
jmp 0xe5a8
imull $0x3, 0x1efbd(%rip), %eax # 0x2d554
addl $0x2, %eax
movl $0x4, %ecx
cltd
idivl %ecx
movl %eax, 0x1efa8(%rip)     # 0x2d550
jmp 0xe5cf
cmpl $-0x1, 0x1efa3(%rip)    # 0x2d554
jne 0xe5cd
movl 0x1ef97(%rip), %eax     # 0x2d550
shll $0x2, %eax
addl $0x1, %eax
movl $0x3, %ecx
cltd
idivl %ecx
movl %eax, 0x1ef87(%rip)     # 0x2d554
jmp 0xe5cf
jmp 0xe66e
jmp 0xe5d6
cmpl $-0x1, 0x1ef73(%rip)    # 0x2d550
jne 0xe5f3
movl -0x1c(%rbp), %eax
subl $0x40, %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, 0x1ef5d(%rip)     # 0x2d550
cmpl $-0x1, 0x1ef5a(%rip)    # 0x2d554
jne 0xe610
movl -0x18(%rbp), %eax
subl $0x40, %eax
movl $0x90, %ecx
cltd
idivl %ecx
movl %eax, 0x1ef44(%rip)     # 0x2d554
movl 0x1ef3a(%rip), %eax     # 0x2d550
imull 0x1ef33(%rip), %eax     # 0x2d550
shll $0x2, %eax
movl 0x1ef2e(%rip), %ecx     # 0x2d554
imull 0x1ef27(%rip), %ecx     # 0x2d554
imull $0x3, %ecx, %ecx
cmpl %ecx, %eax
jle 0xe652
movl $0x2, 0x1ef1e(%rip)     # 0x2d55c
movzbl 0x1f17e(%rip), %eax     # 0x2d7c3
orl $0x80, %eax
movb %al, 0x1f173(%rip)      # 0x2d7c3
jmp 0xe66c
movl $0x1, 0x1ef00(%rip)     # 0x2d55c
movzbl 0x1f160(%rip), %eax     # 0x2d7c3
andl $0x7f, %eax
movb %al, 0x1f157(%rip)      # 0x2d7c3
jmp 0xe66e
cmpl $0x1, 0x1eedb(%rip)     # 0x2d550
jge 0xe681
movl $0x1, 0x1eecf(%rip)     # 0x2d550
cmpl $0x1, 0x1eecc(%rip)     # 0x2d554
jge 0xe694
movl $0x1, 0x1eec0(%rip)     # 0x2d554
movl 0x1eeb6(%rip), %ecx     # 0x2d550
shll $0x8, %ecx
imull $0x90, 0x1eeac(%rip), %r8d # 0x2d554
leaq 0x151b6(%rip), %rdi     # 0x23865
movl $0x1fff0000, %edx       # imm = 0x1FFF0000
xorl %r9d, %r9d
movl %edx, %esi
callq 0x7050
movq %rax, 0x822d23(%rip)    # 0x8313e8
cmpq $0x0, 0x822d1b(%rip)    # 0x8313e8
jne 0xe6f1
callq 0x7370
movq %rax, %rsi
leaq 0x15199(%rip), %rdi     # 0x23877
movb $0x0, %al
callq 0x75c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe829
movq 0x822cf0(%rip), %rdi    # 0x8313e8
callq 0x75a0
movq %rax, -0x38(%rbp)
movl 0x1ee49(%rip), %eax     # 0x2d550
shll $0x8, %eax
movl %eax, -0x48(%rbp)
imull $0x90, 0x1ee3d(%rip), %eax # 0x2d554
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x10(%rax), %eax
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movzbl 0x10(%rax), %edi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x14(%rax), %esi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x18(%rax), %edx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x1c(%rax), %ecx
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x20(%rax), %r8d
callq 0x7690
movl -0x48(%rbp), %esi
movl -0x44(%rbp), %edx
movl -0x40(%rbp), %ecx
movl %eax, %r8d
xorl %edi, %edi
callq 0x70b0
movq %rax, 0x822c77(%rip)    # 0x8313f8
cmpq $0x0, 0x822c6f(%rip)    # 0x8313f8
jne 0xe7aa
callq 0x7370
movq %rax, %rsi
leaq 0x150fb(%rip), %rdi     # 0x23895
movb $0x0, %al
callq 0x75c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe829
movl 0x1eda0(%rip), %esi     # 0x2d550
shll $0x3, %esi
movl 0x1ed9b(%rip), %edx     # 0x2d554
shll $0x3, %edx
xorl %r9d, %r9d
movl $0x8, %ecx
movl %r9d, %edi
movl %r9d, %r8d
movl $0x0, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x7600
movq %rax, 0x822c0b(%rip)    # 0x8313f0
cmpq $0x0, 0x822c03(%rip)    # 0x8313f0
jne 0xe80e
callq 0x7370
movq %rax, %rsi
leaq 0x150b9(%rip), %rdi     # 0x238b7
movb $0x0, %al
callq 0x75c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xe829
movq 0x822bdb(%rip), %rax    # 0x8313f0
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x8a60
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)