// File: klyushinmisha[P]GBEmu/./src/GameBoy.cpp
// Function: GameBoy::KeyDown(SDL_Event)
void GameBoy::KeyDown(SDL_Event e)
{
    byte key = 0;
    
    if (((RAM[0xFF00] >> 4) & 3) == 3 | ((RAM[0xFF00] >> 4) & 3) == 0)
        return;
    
    if (RAM[0xFF00] >> 4 == 0b1101)
        switch (e.key.keysym.sym)
        {
            case SDL_SCANCODE_1:
                key = 0b111;
                break;
            case SDL_SCANCODE_2:
                key = 0b1011;
                break;
            case SDL_SCANCODE_K:
                key = 0b1101;
                break;
            case SDL_SCANCODE_L:
                key = 0b1110;
                break;
            default:
                return;
        }
    else if (RAM[0xFF00] >> 4 == 0b1110)
        switch (e.key.keysym.sym)
        {
            case SDL_SCANCODE_W:
                key = 0b1011;
                break;
            case SDL_SCANCODE_A:
                key = 0b1101;
                break;
            case SDL_SCANCODE_S:
                key = 0b111;
                break;
            case SDL_SCANCODE_D:
                key = 0b1110;
                break;
            default:
                return;
        }
    
    RAM[0xFF0F] |= 0b10000;
    RAM[0xFF00] &= 0xF0;
    RAM[0xFF00] |= key;
    Stop = false;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
leaq 0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x18(%rbp)
movb $0x0, -0x9(%rbp)
movq 0x48(%rcx), %rax
movzbl 0xff00(%rax), %eax
sarl $0x4, %eax
andl $0x3, %eax
cmpl $0x3, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movq 0x48(%rcx), %rcx
movzbl 0xff00(%rcx), %ecx
sarl $0x4, %ecx
andl $0x3, %ecx
cmpl $0x0, %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl %ecx, %eax
cmpl $0x0, %eax
je 0x2e71
jmp 0x2f78
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movzbl 0xff00(%rax), %eax
sarl $0x4, %eax
cmpl $0xd, %eax
jne 0x2ed0
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
addl $-0xe, %eax
movl %eax, %ecx
movq %rcx, -0x28(%rbp)
subl $0x11, %eax
ja 0x2ec9
movq -0x28(%rbp), %rax
leaq 0x81b8(%rip), %rcx      # 0xb060
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x7, -0x9(%rbp)
jmp 0x2ece
movb $0xb, -0x9(%rbp)
jmp 0x2ece
movb $0xd, -0x9(%rbp)
jmp 0x2ece
movb $0xe, -0x9(%rbp)
jmp 0x2ece
jmp 0x2f78
jmp 0x2f2e
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movzbl 0xff00(%rax), %eax
sarl $0x4, %eax
cmpl $0xe, %eax
jne 0x2f2c
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
addl $-0x4, %eax
movl %eax, %ecx
movq %rcx, -0x30(%rbp)
subl $0x16, %eax
ja 0x2f28
movq -0x30(%rbp), %rax
leaq 0x80fd(%rip), %rcx      # 0xb004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0xb, -0x9(%rbp)
jmp 0x2f2a
movb $0xd, -0x9(%rbp)
jmp 0x2f2a
movb $0x7, -0x9(%rbp)
jmp 0x2f2a
movb $0xe, -0x9(%rbp)
jmp 0x2f2a
jmp 0x2f78
jmp 0x2f2c
jmp 0x2f2e
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movzbl 0xff0f(%rcx), %edx
orl $0x10, %edx
movb %dl, 0xff0f(%rcx)
movq 0x48(%rax), %rcx
movzbl 0xff00(%rcx), %edx
andl $0xf0, %edx
movb %dl, 0xff00(%rcx)
movzbl -0x9(%rbp), %esi
movq 0x48(%rax), %rcx
movzbl 0xff00(%rcx), %edx
orl %esi, %edx
movb %dl, 0xff00(%rcx)
movb $0x0, 0x5b(%rax)
popq %rbp
retq
nopw (%rax,%rax)