// File: /workspace/github_c_makefile_star2_O3/leiradel[P]zx48k-libretro/src/zx.h
// Function: zx_init
void zx_init(zx_t* sys, const zx_desc_t* desc) {
    CHIPS_ASSERT(sys && desc);
    CHIPS_ASSERT(desc->pixel_buffer && (desc->pixel_buffer_size >= _ZX_DISPLAY_SIZE));

    memset(sys, 0, sizeof(zx_t));
    sys->valid = true;
    sys->type = desc->type;
    sys->joystick_type = desc->joystick_type;
    sys->pixel_buffer = (uint32_t*) desc->pixel_buffer;
    sys->user_data = desc->user_data;
    sys->audio_cb = desc->audio_cb;
    sys->num_samples = _ZX_DEFAULT(desc->audio_num_samples, ZX_DEFAULT_AUDIO_SAMPLES);
    CHIPS_ASSERT(sys->num_samples <= ZX_MAX_AUDIO_SAMPLES);

    /* initalize the hardware */
    sys->border_color = 0xFF000000;
    if (ZX_TYPE_128 == sys->type) {
        CHIPS_ASSERT(desc->rom_zx128_0 && (desc->rom_zx128_0_size == 0x4000));
        CHIPS_ASSERT(desc->rom_zx128_1 && (desc->rom_zx128_1_size == 0x4000));
        memcpy(sys->rom[0], desc->rom_zx128_0, 0x4000);
        memcpy(sys->rom[1], desc->rom_zx128_1, 0x4000);
        sys->display_ram_bank = 5;
        sys->frame_scan_lines = 311;
        sys->top_border_scanlines = 63;
        sys->scanline_period = 228;
    }
    else {
        CHIPS_ASSERT(desc->rom_zx48k && (desc->rom_zx48k_size == 0x4000));
        memcpy(sys->rom[0], desc->rom_zx48k, 0x4000);
        sys->display_ram_bank = 0;
        sys->frame_scan_lines = 312;
        sys->top_border_scanlines = 64;
        sys->scanline_period = 224;
    }
    sys->scanline_counter = sys->scanline_period;

    const int cpu_freq = (sys->type == ZX_TYPE_48K) ? _ZX_48K_FREQUENCY : _ZX_128_FREQUENCY;
    clk_init(&sys->clk, cpu_freq);

    z80_desc_t cpu_desc;
    _ZX_CLEAR(cpu_desc);
    cpu_desc.tick_cb = _zx_tick;
    cpu_desc.user_data = sys;
    z80_init(&sys->cpu, &cpu_desc);

    const int audio_hz = _ZX_DEFAULT(desc->audio_sample_rate, 44100);
    const float beeper_vol = _ZX_DEFAULT(desc->audio_beeper_volume, 0.25f);
    beeper_init(&sys->beeper, cpu_freq, audio_hz, beeper_vol);
    if (ZX_TYPE_128 == sys->type) {
        ay38910_desc_t ay_desc;
        _ZX_CLEAR(ay_desc);
        ay_desc.type = AY38910_TYPE_8912;
        ay_desc.tick_hz = cpu_freq / 2;
        ay_desc.sound_hz = audio_hz;
        ay_desc.magnitude = _ZX_DEFAULT(desc->audio_ay_volume, 0.5f);
        ay38910_init(&sys->ay, &ay_desc);
    }
    _zx_init_memory_map(sys);
    _zx_init_keyboard_matrix(sys);
    
    z80_set_pc(&sys->cpu, 0x0000);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
testq %rdi, %rdi
je 0x109e7
movq %rsi, %r14
testq %rsi, %rsi
je 0x109e7
cmpq $0x0, 0x8(%r14)
je 0x109c8
cmpl $0x4ffff, 0x10(%r14)    # imm = 0x4FFFF
jle 0x109c8
movq %rdi, %rbx
movl $0x30230, %edx          # imm = 0x30230
xorl %esi, %esi
callq 0x30d0
movb $0x1, 0x1110(%rbx)
movl (%r14), %r15d
movl %r15d, 0x1114(%rbx)
movl 0x4(%r14), %eax
movl %eax, 0x1118(%rbx)
movq 0x8(%r14), %rax
movq %rax, 0x3210(%rbx)
movq 0x18(%r14), %rax
movq %rax, 0x3218(%rbx)
movq 0x20(%r14), %rax
movq %rax, 0x3220(%rbx)
movl 0x28(%r14), %eax
testl %eax, %eax
movl $0x80, %ecx
cmovnel %eax, %ecx
movl %ecx, 0x3228(%rbx)
cmpl $0x401, %ecx            # imm = 0x401
jge 0x10a06
movl $0xff000000, 0x1140(%rbx) # imm = 0xFF000000
cmpl $0x1, %r15d
jne 0x10789
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x10a25
cmpl $0x4000, 0x58(%r14)     # imm = 0x4000
jne 0x10a25
cmpq $0x0, 0x50(%r14)
je 0x10a63
cmpl $0x4000, 0x5c(%r14)     # imm = 0x4000
jne 0x10a63
leaq 0x24230(%rbx), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3140
leaq 0x28230(%rbx), %rdi
movq 0x50(%r14), %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3140
movl $0xe4, %eax
movl $0x3f, %ecx
movl $0x137, %edx            # imm = 0x137
movl $0x5, %esi
jmp 0x107c6
movq 0x38(%r14), %rsi
testq %rsi, %rsi
je 0x10a44
cmpl $0x4000, 0x40(%r14)     # imm = 0x4000
jne 0x10a44
leaq 0x24230(%rbx), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3140
movl $0xe0, %eax
movl $0x40, %ecx
movl $0x138, %edx            # imm = 0x138
xorl %esi, %esi
movl %esi, 0x113c(%rbx)
movl %edx, 0x1128(%rbx)
movl %ecx, 0x112c(%rbx)
movl %eax, 0x1130(%rbx)
movl %eax, 0x1134(%rbx)
testl %r15d, %r15d
movl $0x3567e0, %eax         # imm = 0x3567E0
movl $0x361f0e, %r15d        # imm = 0x361F0E
cmovel %eax, %r15d
movq $0x0, 0x1150(%rbx)
movq %r15, 0x1148(%rbx)
leaq 0x273(%rip), %rax       # 0x10a82
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
movq %rbx, %rdi
callq 0x3180
movl 0x2c(%r14), %eax
testl %eax, %eax
movl $0xac44, %r12d          # imm = 0xAC44
cmovnel %eax, %r12d
movss 0x30(%r14), %xmm1
xorps %xmm2, %xmm2
movaps %xmm1, %xmm0
cmpneqss %xmm2, %xmm0
andps %xmm0, %xmm1
movss 0x27b6(%rip), %xmm2     # 0x13004
andnps %xmm2, %xmm0
orps %xmm1, %xmm0
leaq 0x50(%rbx), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x3080
cmpl $0x1, 0x1114(%rbx)
jne 0x108c0
xorps %xmm0, %xmm0
leaq -0x78(%rbp), %rsi
movups %xmm0, 0x10(%rsi)
movq $0x0, 0x20(%rsi)
movl $0x1, (%rsi)
shrl %r15d
movl %r15d, 0x4(%rsi)
movl %r12d, 0x8(%rsi)
movss 0x34(%r14), %xmm0
xorps %xmm2, %xmm2
cmpneqss %xmm0, %xmm2
andps %xmm2, %xmm0
movss 0x275f(%rip), %xmm1     # 0x13008
andnps %xmm1, %xmm2
orps %xmm0, %xmm2
movss %xmm2, 0xc(%rsi)
leaq 0x870(%rbx), %rdi
callq 0x30e0
movq %rbx, %rdi
callq 0x1104d
leaq 0x1158(%rbx), %r14
movl $0x4b8, %edx            # imm = 0x4B8
movq %r14, %rdi
xorl %esi, %esi
callq 0x30d0
movl $0x411b, 0x1160(%rbx)   # imm = 0x411B
movabsq $0x208000201001001, %rax # imm = 0x208000201001001
movq %rbx, -0x30(%rbp)
movq %rax, 0x1568(%rbx)
leaq 0x3bdb(%rip), %rbx      # 0x144df
xorl %eax, %eax
leal -0x1(%rax), %ecx
movl $0x1, %r15d
shll %cl, %r15d
movq %rax, -0x38(%rbp)
testq %rax, %rax
movl $0x0, %eax
cmovel %eax, %r15d
movq %rbx, -0x40(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movzbl (%rbx,%r13), %esi
cmpl $0x20, %esi
je 0x10947
movq %r14, %rdi
movl %r12d, %edx
movl %r13d, %ecx
movl %r15d, %r8d
callq 0x3160
incq %r13
cmpq $0x5, %r13
jne 0x1092c
incq %r12
addq $0x5, %rbx
cmpq $0x8, %r12
jne 0x10929
movq -0x38(%rbp), %rax
incq %rax
movq -0x40(%rbp), %rbx
addq $0x28, %rbx
cmpq $0x3, %rax
jne 0x10906
movq -0x30(%rbp), %rcx
movl $0x80001, 0x11e8(%rcx)  # imm = 0x80001
movl $0x80002, 0x11a4(%rcx)  # imm = 0x80002
movl $0x1010008, 0x1194(%rcx) # imm = 0x1010008
movaps 0x31c5(%rip), %xmm0     # 0x13b60
movups %xmm0, 0x1184(%rcx)
movabsq $0x4000101010001, %rax  # imm = 0x4000101010001
movq %rax, 0x1198(%rcx)
movw $0x0, 0x22(%rcx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x37bc(%rip), %rdi      # 0x1418b
leaq 0x3784(%rip), %rsi      # 0x1415a
leaq 0x3786(%rip), %rcx      # 0x14163
movl $0xda, %edx
callq 0x30b0
leaq 0x3760(%rip), %rdi      # 0x1414e
leaq 0x3765(%rip), %rsi      # 0x1415a
leaq 0x3767(%rip), %rcx      # 0x14163
movl $0xd9, %edx
callq 0x30b0
leaq 0x37c1(%rip), %rdi      # 0x141ce
leaq 0x3746(%rip), %rsi      # 0x1415a
leaq 0x3748(%rip), %rcx      # 0x14163
movl $0xe4, %edx
callq 0x30b0
leaq 0x37bd(%rip), %rdi      # 0x141e9
leaq 0x3727(%rip), %rsi      # 0x1415a
leaq 0x3729(%rip), %rcx      # 0x14163
movl $0xe9, %edx
callq 0x30b0
leaq 0x380e(%rip), %rdi      # 0x14259
leaq 0x3708(%rip), %rsi      # 0x1415a
leaq 0x370a(%rip), %rcx      # 0x14163
movl $0xf3, %edx
callq 0x30b0
leaq 0x37b7(%rip), %rdi      # 0x14221
leaq 0x36e9(%rip), %rsi      # 0x1415a
leaq 0x36eb(%rip), %rcx      # 0x14163
movl $0xea, %edx
callq 0x30b0