// File: klyushinmisha[P]GBEmu/./src/GameBoy.cpp
// Function: GameBoy::Run()
void GameBoy::Run()
{
    scale = 2;
    pixels = new Uint32[scale * scale * 160 * 144];
    SDL_Init(SDL_INIT_EVERYTHING);
    
    SDL_Window* w = SDL_CreateWindow("GBEmu", 100, 100, scale * 160, scale * 144, 0);

    SDL_Event e;
    ren = SDL_CreateRenderer(w, -1, SDL_RENDERER_ACCELERATED);

    texture = SDL_CreateTexture(ren,
        SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, scale * 160, scale * 144);

    bool isRunning = true;
    SDL_ShowWindow(w);
    while (isRunning)
    {
        if (Drawing())
            Manager();
        else
        {
            SDL_RenderClear(ren);
            SDL_RenderCopy(ren, texture, nullptr, nullptr);
            SDL_RenderPresent(ren);
            SDL_UpdateTexture(texture, nullptr, pixels, scale * 160 * sizeof(Uint32));
            SDL_PollEvent(&e);
            switch (e.type)
            {
                case SDL_QUIT:
                    isRunning = false;
                    break;
                case SDL_KEYDOWN:
                    KeyDown(e);
                    break;
                case SDL_KEYUP:
                    KeyUp();
                    break;
            }
        }
    }

    delete pixels;
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(w);
    SDL_Quit();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movl $0x2, 0x40(%rcx)
movl 0x40(%rcx), %eax
imull 0x40(%rcx), %eax
imull $0xa0, %eax, %eax
imull $0x90, %eax, %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x2040
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x38(%rax)
movl $0xf231, %edi           # imm = 0xF231
callq 0x2210
movq -0x90(%rbp), %rax
imull $0xa0, 0x40(%rax), %ecx
imull $0x90, 0x40(%rax), %r8d
leaq 0x84ec(%rip), %rdi      # 0xb0a8
movl $0x64, %edx
xorl %r9d, %r9d
movl %edx, %esi
callq 0x2060
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x2, %edx
callq 0x2050
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x28(%rax)
movq 0x28(%rax), %rdi
imull $0xa0, 0x40(%rax), %ecx
imull $0x90, 0x40(%rax), %r8d
movl $0x16362004, %esi       # imm = 0x16362004
xorl %edx, %edx
callq 0x23e0
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x30(%rax)
movb $0x1, -0x49(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2140
testb $0x1, -0x49(%rbp)
je 0x2d59
movq -0x90(%rbp), %rdi
callq 0x2dc0
testb $0x1, %al
jne 0x2c46
jmp 0x2c57
movq -0x90(%rbp), %rdi
callq 0x2df0
jmp 0x2d54
movq -0x90(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2230
movq -0x90(%rbp), %rax
movq 0x28(%rax), %rdi
movq 0x30(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rdx
callq 0x22c0
movq -0x90(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x2090
movq -0x90(%rbp), %rax
movq -0xa0(%rbp), %rsi
movq 0x30(%rax), %rdi
movq 0x38(%rax), %rdx
movl 0x40(%rax), %ecx
shll $0x7, %ecx
movl %ecx, %eax
leal (%rax,%rax,4), %ecx
callq 0x21b0
leaq -0x48(%rbp), %rdi
callq 0x2150
movl -0x48(%rbp), %eax
movl %eax, -0x94(%rbp)
subl $0x100, %eax            # imm = 0x100
je 0x2cf8
jmp 0x2cda
movl -0x94(%rbp), %eax
subl $0x300, %eax            # imm = 0x300
je 0x2cfe
jmp 0x2ce9
movl -0x94(%rbp), %eax
subl $0x301, %eax            # imm = 0x301
je 0x2d46
jmp 0x2d52
movb $0x0, -0x49(%rbp)
jmp 0x2d52
leaq -0x88(%rbp), %rdi
leaq -0x48(%rbp), %rsi
movl $0x38, %edx
callq 0x21e0
movq -0x90(%rbp), %rdi
movq -0x58(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x30(%rax)
movups -0x88(%rbp), %xmm0
movups -0x78(%rbp), %xmm1
movups -0x68(%rbp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x2e10
jmp 0x2d52
movq -0x90(%rbp), %rdi
callq 0x2f80
jmp 0x2d54
jmp 0x2c2a
movq -0x90(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x2d82
movq -0xa8(%rbp), %rdi
movl $0x4, %esi
callq 0x2260
movq -0x90(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x2190
movq -0x90(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x20f0
movq -0x10(%rbp), %rdi
callq 0x2030
callq 0x2080
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)