// File: /workspace/github_c_makefile_star2_O1/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 *0x8a01b(%rip)          # 0x9c390
xorl %ebx, %ebx
leaq 0x1552(%rip), %r14      # 0x138d0
xorl %r15d, %r15d
movl (%r14,%rbx,8), %ecx
xorl %edi, %edi
movl $0x1, %esi
xorl %edx, %edx
callq *0x8a004(%rip)          # 0x9c398
testw %ax, %ax
je 0x1239e
orb 0x4(%r14,%rbx,8), %r15b
incq %rbx
cmpq $0x5, %rbx
jne 0x12381
movzbl %r15b, %esi
leaq 0x9d6e(%rip), %rdi      # 0x1c120
callq 0x31c0
movl $0x1, %ebx
leaq 0x153d(%rip), %r14      # 0x13900
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r13d, %r13d
movl (%r14,%r13,4), %ecx
xorl %edi, %edi
movl $0x3, %esi
xorl %edx, %edx
callq *0x89fb9(%rip)          # 0x9c398
testw %ax, %ax
movl $0x0, %eax
cmovneq %rbx, %rax
orq %rax, %r15
incq %r13
addq %rbx, %rbx
cmpq $0x5, %r13
jne 0x123cc
incq %r12
addq $0x14, %r14
cmpq $0x8, %r12
jne 0x123c9
movq 0x39f42(%rip), %r12     # 0x4c350
xorq %r15, %r12
movl $0x1, %r13d
movl $0x80, %r14d
leaq 0x9cfc(%rip), %rbx      # 0x1c120
testq %r12, %r13
je 0x12440
movq %rbx, %rdi
movl %r14d, %esi
testq %r15, %r13
je 0x1243b
callq 0x3190
jmp 0x12440
callq 0x3100
incl %r14d
addq %r13, %r13
cmpl $0xa8, %r14d
jne 0x12424
movq %r15, 0x39efa(%rip)     # 0x4c350
movq %rbx, %rdi
movl $0x4e20, %esi           # imm = 0x4E20
callq 0x3130
xorl %eax, %eax
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
cmpq $0x14000, %rax          # imm = 0x14000
jne 0x12465
movl 0x89ebc(%rip), %esi     # 0x9c358
movl 0x89eba(%rip), %edx     # 0x9c35c
leal (,%rsi,4), %ecx
leaq -0x50030(%rbp), %rdi
callq *0x89eca(%rip)          # 0x9c380
addq $0x50008, %rsp          # imm = 0x50008
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq