// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x204f0
cmpq $0x0, -0x10(%rbp)
je 0x204f0
jmp 0x2050f
leaq 0x4d67(%rip), %rdi      # 0x2525e
leaq 0x4d6c(%rip), %rsi      # 0x2526a
movl $0xd9, %edx
leaq 0x4d69(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x20529
movq -0x10(%rbp), %rax
cmpl $0x50000, 0x10(%rax)    # imm = 0x50000
jl 0x20529
jmp 0x20548
leaq 0x4d6b(%rip), %rdi      # 0x2529b
leaq 0x4d33(%rip), %rsi      # 0x2526a
movl $0xda, %edx
leaq 0x4d30(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x8(%rbp), %rdi
xorl %esi, %esi
movl $0x30230, %edx          # imm = 0x30230
callq 0x31c0
movq -0x8(%rbp), %rax
movb $0x1, 0x1110(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1114(%rax)
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1118(%rax)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x3210(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x3218(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x3220(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x205d3
movq -0x10(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x5c(%rbp)
jmp 0x205dd
movl $0x80, %eax
movl %eax, -0x5c(%rbp)
jmp 0x205dd
movl -0x5c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x3228(%rax)
movq -0x8(%rbp), %rax
cmpl $0x400, 0x3228(%rax)    # imm = 0x400
jg 0x205fc
jmp 0x2061b
leaq 0x4cdb(%rip), %rdi      # 0x252de
leaq 0x4c60(%rip), %rsi      # 0x2526a
movl $0xe4, %edx
leaq 0x4c5d(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x8(%rbp), %rax
movl $0xff000000, 0x1140(%rax) # imm = 0xFF000000
movq -0x8(%rbp), %rcx
movl $0x1, %eax
cmpl 0x1114(%rcx), %eax
jne 0x2072e
movq -0x10(%rbp), %rax
cmpq $0x0, 0x48(%rax)
je 0x20658
movq -0x10(%rbp), %rax
cmpl $0x4000, 0x58(%rax)     # imm = 0x4000
jne 0x20658
jmp 0x20677
leaq 0x4c9a(%rip), %rdi      # 0x252f9
leaq 0x4c04(%rip), %rsi      # 0x2526a
movl $0xe9, %edx
leaq 0x4c01(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x20691
movq -0x10(%rbp), %rax
cmpl $0x4000, 0x5c(%rax)     # imm = 0x4000
jne 0x20691
jmp 0x206b0
leaq 0x4c99(%rip), %rdi      # 0x25331
leaq 0x4bcb(%rip), %rsi      # 0x2526a
movl $0xea, %edx
leaq 0x4bc8(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x8(%rbp), %rdi
addq $0x24230, %rdi          # imm = 0x24230
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x32e0
movq -0x8(%rbp), %rdi
addq $0x24230, %rdi          # imm = 0x24230
addq $0x4000, %rdi           # imm = 0x4000
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x32e0
movq -0x8(%rbp), %rax
movl $0x5, 0x113c(%rax)
movq -0x8(%rbp), %rax
movl $0x137, 0x1128(%rax)    # imm = 0x137
movq -0x8(%rbp), %rax
movl $0x3f, 0x112c(%rax)
movq -0x8(%rbp), %rax
movl $0xe4, 0x1130(%rax)
jmp 0x207bc
movq -0x10(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x20748
movq -0x10(%rbp), %rax
cmpl $0x4000, 0x40(%rax)     # imm = 0x4000
jne 0x20748
jmp 0x20767
leaq 0x4c1a(%rip), %rdi      # 0x25369
leaq 0x4b14(%rip), %rsi      # 0x2526a
movl $0xf3, %edx
leaq 0x4b11(%rip), %rcx      # 0x25273
callq 0x3180
movq -0x8(%rbp), %rdi
addq $0x24230, %rdi          # imm = 0x24230
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x32e0
movq -0x8(%rbp), %rax
movl $0x0, 0x113c(%rax)
movq -0x8(%rbp), %rax
movl $0x138, 0x1128(%rax)    # imm = 0x138
movq -0x8(%rbp), %rax
movl $0x40, 0x112c(%rax)
movq -0x8(%rbp), %rax
movl $0xe0, 0x1130(%rax)
movq -0x8(%rbp), %rax
movl 0x1130(%rax), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1134(%rax)
movq -0x8(%rbp), %rax
movl 0x1114(%rax), %edx
movl $0x361f0e, %eax         # imm = 0x361F0E
movl $0x3567e0, %ecx         # imm = 0x3567E0
cmpl $0x0, %edx
cmovel %ecx, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
addq $0x1148, %rdi           # imm = 0x1148
movl -0x14(%rbp), %esi
callq 0x3500
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31c0
leaq 0x149(%rip), %rax       # 0x20960
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x3380
movq -0x10(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x20846
movq -0x10(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x60(%rbp)
jmp 0x20850
movl $0xac44, %eax           # imm = 0xAC44
movl %eax, -0x60(%rbp)
jmp 0x20850
movl -0x60(%rbp), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movss 0x30(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x2086b
jp 0x2086b
jmp 0x2087b
movq -0x10(%rbp), %rax
movss 0x30(%rax), %xmm0
movss %xmm0, -0x64(%rbp)
jmp 0x2088a
movss 0x3781(%rip), %xmm0     # 0x24004
movss %xmm0, -0x64(%rbp)
jmp 0x2088a
movss -0x64(%rbp), %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
addq $0x50, %rdi
movl -0x14(%rbp), %esi
movl -0x2c(%rbp), %edx
movss -0x30(%rbp), %xmm0
callq 0x3140
movq -0x8(%rbp), %rcx
movl $0x1, %eax
cmpl 0x1114(%rcx), %eax
jne 0x2093a
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x31c0
movl $0x1, -0x58(%rbp)
movl -0x14(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x54(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movss 0x34(%rax), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jne 0x208fd
jp 0x208fd
jmp 0x2090d
movq -0x10(%rbp), %rax
movss 0x34(%rax), %xmm0
movss %xmm0, -0x68(%rbp)
jmp 0x2091c
movss 0x36f3(%rip), %xmm0     # 0x24008
movss %xmm0, -0x68(%rbp)
jmp 0x2091c
movss -0x68(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
addq $0x870, %rdi            # imm = 0x870
leaq -0x58(%rbp), %rsi
callq 0x3200
movq -0x8(%rbp), %rdi
callq 0x20f50
movq -0x8(%rbp), %rdi
callq 0x210d0
movq -0x8(%rbp), %rdi
xorl %esi, %esi
callq 0x30d0
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)