// File: /workspace/github_c_makefile_star2_O0/leiradel[P]zx48k-libretro/src/zx.h
// Function: zx_init_keyboard_matrix
static void _zx_init_keyboard_matrix(zx_t* sys) {
    /* setup keyboard matrix */
    kbd_init(&sys->kbd, 1);
    /* caps-shift is column 0, line 0 */
    kbd_register_modifier(&sys->kbd, 0, 0, 0);
    /* sym-shift is column 7, line 1 */
    kbd_register_modifier(&sys->kbd, 1, 7, 1);
    /* alpha-numeric keys */
    const char* keymap =
        /* no shift */
        " zxcv"         /* A8       shift,z,x,c,v */
        "asdfg"         /* A9       a,s,d,f,g */
        "qwert"         /* A10      q,w,e,r,t */
        "12345"         /* A11      1,2,3,4,5 */
        "09876"         /* A12      0,9,8,7,6 */
        "poiuy"         /* A13      p,o,i,u,y */
        " lkjh"         /* A14      enter,l,k,j,h */
        "  mnb"         /* A15      space,symshift,m,n,b */

        /* shift */
        " ZXCV"         /* A8   */
        "ASDFG"         /* A9   */
        "QWERT"         /* A10  */
        "     "         /* A11  */
        "     "         /* A12  */
        "POIUY"         /* A13  */
        " LKJH"         /* A14  */
        "  MNB"         /* A15  */

        /* symshift */
        " : ?/"         /* A8   */
        "     "         /* A9   */
        "   <>"         /* A10  */
        "!@#$%"         /* A11  */
        "_)('&"         /* A12  */
        "\";   "        /* A13  */
        " =+-^"         /* A14  */
        "  .,*";        /* A15  */
    for (int layer = 0; layer < 3; layer++) {
        for (int column = 0; column < 8; column++) {
            for (int line = 0; line < 5; line++) {
                const uint8_t c = keymap[layer*40 + column*5 + line];
                if (c != 0x20) {
                    kbd_register_key(&sys->kbd, c, column, line, (layer>0) ? (1<<(layer-1)) : 0);
                }
            }
        }
    }

    /* special keys */
    kbd_register_key(&sys->kbd, ' ', 7, 0, 0);  /* Space */
    kbd_register_key(&sys->kbd, 0x0F, 7, 1, 0); /* SymShift */
    kbd_register_key(&sys->kbd, 0x08, 3, 4, 1); /* Cursor Left (Shift+5) */
    kbd_register_key(&sys->kbd, 0x0A, 4, 4, 1); /* Cursor Down (Shift+6) */
    kbd_register_key(&sys->kbd, 0x0B, 4, 3, 1); /* Cursor Up (Shift+7) */
    kbd_register_key(&sys->kbd, 0x09, 4, 2, 1); /* Cursor Right (Shift+8) */
    kbd_register_key(&sys->kbd, 0x07, 3, 0, 1); /* Edit (Shift+1) */
    kbd_register_key(&sys->kbd, 0x0C, 4, 0, 1); /* Delete (Shift+0) */
    kbd_register_key(&sys->kbd, 0x0D, 6, 0, 0); /* Enter */
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x1, %esi
callq 0x32a0
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
xorl %ecx, %ecx
movl %ecx, %esi
movl %ecx, %edx
callq 0x3460
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x1, %ecx
movl $0x7, %edx
movl %ecx, %esi
callq 0x3460
leaq 0x4560(%rip), %rax      # 0x2568a
movq %rax, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x3, -0x14(%rbp)
jge 0x2120a
movl $0x0, -0x18(%rbp)
cmpl $0x8, -0x18(%rbp)
jge 0x211fa
movl $0x0, -0x1c(%rbp)
cmpl $0x5, -0x1c(%rbp)
jge 0x211ea
movq -0x10(%rbp), %rax
imull $0x28, -0x14(%rbp), %ecx
imull $0x5, -0x18(%rbp), %edx
addl %edx, %ecx
addl -0x1c(%rbp), %ecx
movslq %ecx, %rcx
movb (%rax,%rcx), %al
movb %al, -0x1d(%rbp)
movzbl -0x1d(%rbp), %eax
cmpl $0x20, %eax
je 0x211da
movq -0x8(%rbp), %rax
addq $0x1158, %rax           # imm = 0x1158
movq %rax, -0x38(%rbp)
movzbl -0x1d(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x14(%rbp)
jle 0x211bd
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, -0x3c(%rbp)
jmp 0x211c4
xorl %eax, %eax
movl %eax, -0x3c(%rbp)
jmp 0x211c4
movl -0x24(%rbp), %ecx
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %esi
movq -0x38(%rbp), %rdi
movl -0x3c(%rbp), %r8d
callq 0x3350
jmp 0x211dc
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x21157
jmp 0x211ec
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x21146
jmp 0x211fc
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x21135
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x20, %esi
movl $0x7, %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0xf, %esi
movl $0x7, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x8, %esi
movl $0x3, %edx
movl $0x4, %ecx
movl $0x1, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0xa, %esi
movl $0x4, %ecx
movl $0x1, %r8d
movl %ecx, %edx
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0xb, %esi
movl $0x4, %edx
movl $0x3, %ecx
movl $0x1, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x9, %esi
movl $0x4, %edx
movl $0x2, %ecx
movl $0x1, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0x7, %esi
movl $0x3, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0xc, %esi
movl $0x4, %edx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x3350
movq -0x8(%rbp), %rdi
addq $0x1158, %rdi           # imm = 0x1158
movl $0xd, %esi
movl $0x6, %edx
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x3350
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)