// File: /workspace/github_c_makefile_star2_O2/leiradel[P]zx48k-libretro/src/main.c
// Function: retro_run
void retro_run(void) {
    zx48k.input_poll_cb();

    /* Update the joystick */
    static struct {unsigned id; uint8_t mask;} const joy_map[] = {
        {RETRO_DEVICE_ID_JOYPAD_UP, ZX_JOYSTICK_UP},
        {RETRO_DEVICE_ID_JOYPAD_DOWN, ZX_JOYSTICK_DOWN},
        {RETRO_DEVICE_ID_JOYPAD_LEFT, ZX_JOYSTICK_LEFT},
        {RETRO_DEVICE_ID_JOYPAD_RIGHT, ZX_JOYSTICK_RIGHT},
        {RETRO_DEVICE_ID_JOYPAD_B, ZX_JOYSTICK_BTN}
    };

    uint8_t joy_mask = 0;

    for (int i = 0; i < sizeof(joy_map) / sizeof(joy_map[0]); i++) {
        int16_t const pressed = zx48k.input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, joy_map[i].id);

        if (pressed != 0) {
            joy_mask |= joy_map[i].mask;
        }
    }

    zx_joystick(&zx48k.zx, joy_mask);

    /* Update the keyboard */
    static unsigned const kbd_map[8][5] = {
        {RETROK_LSHIFT, RETROK_z,     RETROK_x, RETROK_c, RETROK_v},
        {RETROK_a,      RETROK_s,     RETROK_d, RETROK_f, RETROK_g},
        {RETROK_q,      RETROK_w,     RETROK_e, RETROK_r, RETROK_t},
        {RETROK_1,      RETROK_2,     RETROK_3, RETROK_4, RETROK_5},
        {RETROK_0,      RETROK_9,     RETROK_8, RETROK_7, RETROK_6},
        {RETROK_p,      RETROK_o,     RETROK_i, RETROK_u, RETROK_y},
        {RETROK_RETURN, RETROK_l,     RETROK_k, RETROK_j, RETROK_h},
        {RETROK_SPACE,  RETROK_LCTRL, RETROK_m, RETROK_n, RETROK_b}
    };

    uint64_t current_key_states = 0;
    uint64_t key = 1;

    for (int col = 0; col < 8; col++) {
        for (int row = 0; row < 5; row++, key <<= 1) {
            uint64_t const pressed = -(zx48k.input_state_cb(0, RETRO_DEVICE_KEYBOARD, 0, kbd_map[col][row]) != 0);
            current_key_states |= pressed & key;
        }
    }

    uint64_t const changed_keys = current_key_states ^ zx48k.key_states;
    key = 1;

    for (int code = 128; code < 168; code++, key <<= 1) {
        if ((changed_keys & key) != 0) {
            if ((current_key_states & key) != 0) {
                zx_key_down(&zx48k.zx, code);
            }
            else {
                zx_key_up(&zx48k.zx, code);
            }
        }
    }

    zx48k.key_states = current_key_states;

    zx_exec(&zx48k.zx, ZX48K_US_PER_FRAME);

    uint32_t pixel_buffer[320 * 256];

    for (size_t i = 0; i < sizeof(pixel_buffer) / sizeof(pixel_buffer[0]); i++) {
        uint32_t const pixel = zx48k.pixel_buffer[i];
        pixel_buffer[i] = (pixel & 0xff00ff00) | ((pixel & 0x00ff0000) >> 16) | ((pixel & 0x000000ff) << 16);
    }

    zx48k.video_cb(pixel_buffer, zx48k.width, zx48k.height, zx48k.width * 4);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x50008, %rsp          # imm = 0x50008
callq *0x8a77a(%rip)          # 0x9c3c0
xorl %r14d, %r14d
leaq 0x1c80(%rip), %r15      # 0x138d0
pushq $0x1
popq %rbx
xorl %r12d, %r12d
cmpq $0x5, %r14
je 0x11c7b
movl (%r15,%r14,8), %ecx
xorl %edi, %edi
movl %ebx, %esi
xorl %edx, %edx
callq *0x8a75c(%rip)          # 0x9c3c8
testw %ax, %ax
je 0x11c76
orb 0x4(%r15,%r14,8), %r12b
incq %r14
jmp 0x11c56
movzbl %r12b, %esi
leaq 0xa4ca(%rip), %rdi      # 0x1c150
callq 0x3220
pushq $0x1
popq %r14
leaq 0x1c6a(%rip), %r12      # 0x13900
pushq $0x3
popq %rbx
xorl %eax, %eax
xorl %r15d, %r15d
cmpq $0x8, %rax
je 0x11ce8
movq %rax, -0x30(%rbp)
xorl %r13d, %r13d
cmpq $0x5, %r13
je 0x11cdb
movl (%r12,%r13,4), %ecx
xorl %edi, %edi
movl %ebx, %esi
xorl %edx, %edx
callq *0x8a707(%rip)          # 0x9c3c8
testw %ax, %ax
movq %r14, %rax
movl $0x0, %ecx
cmoveq %rcx, %rax
orq %rax, %r15
incq %r13
addq %r14, %r14
jmp 0x11cab
movq -0x30(%rbp), %rax
incq %rax
addq $0x14, %r12
jmp 0x11c9e
movq 0x3a691(%rip), %r12     # 0x4c380
xorq %r15, %r12
movl $0x80, %r14d
pushq $0x1
popq %r13
leaq 0xa44d(%rip), %rbx      # 0x1c150
cmpl $0xa8, %r14d
je 0x11d30
testq %r12, %r13
je 0x11d28
movq %rbx, %rdi
movl %r14d, %esi
testq %r15, %r13
je 0x11d23
callq 0x31e0
jmp 0x11d28
callq 0x3120
incl %r14d
addq %r13, %r13
jmp 0x11d03
movq %r15, 0x3a649(%rip)     # 0x4c380
movq %rbx, %rdi
movl $0x4e20, %esi           # imm = 0x4E20
callq 0x3170
xorl %eax, %eax
cmpq $0x14000, %rax          # imm = 0x14000
je 0x11d79
movl 0x30238(%rbx,%rax,4), %ecx
movl %ecx, %edx
movzbl %cl, %esi
andl $0xff00ff00, %ecx       # imm = 0xFF00FF00
shrl $0x10, %edx
movzbl %dl, %edx
orl %ecx, %edx
shll $0x10, %esi
orl %edx, %esi
movl %esi, -0x50030(%rbp,%rax,4)
incq %rax
jmp 0x11d46
movl 0x8a609(%rip), %esi     # 0x9c388
movl 0x8a607(%rip), %edx     # 0x9c38c
leal (,%rsi,4), %ecx
leaq -0x50030(%rbp), %rdi
callq *0x8a617(%rip)          # 0x9c3b0
addq $0x50008, %rsp          # imm = 0x50008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq