// File: /workspace/github_c_makefile_star2_O2/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 0x1073d
movq %rsi, %r14
testq %rsi, %rsi
je 0x1073d
cmpq $0x0, 0x8(%r14)
je 0x1071e
cmpl $0x4ffff, 0x10(%r14)    # imm = 0x4FFFF
jle 0x1071e
movq %rdi, %rbx
movl $0x30230, %edx          # imm = 0x30230
xorl %esi, %esi
callq 0x3100
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 0x1075c
movl $0xff000000, 0x1140(%rbx) # imm = 0xFF000000
cmpl $0x1, %r15d
jne 0x104e1
movq 0x48(%r14), %rsi
testq %rsi, %rsi
je 0x1077b
cmpl $0x4000, 0x58(%r14)     # imm = 0x4000
jne 0x1077b
cmpq $0x0, 0x50(%r14)
je 0x107b9
cmpl $0x4000, 0x5c(%r14)     # imm = 0x4000
jne 0x107b9
leaq 0x24230(%rbx), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3180
leaq 0x28230(%rbx), %rdi
movq 0x50(%r14), %rsi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3180
movl $0xe4, %eax
pushq $0x3f
popq %rcx
movl $0x137, %edx            # imm = 0x137
pushq $0x5
popq %rsi
jmp 0x1051c
movq 0x38(%r14), %rsi
testq %rsi, %rsi
je 0x1079a
cmpl $0x4000, 0x40(%r14)     # imm = 0x4000
jne 0x1079a
leaq 0x24230(%rbx), %rdi
movl $0x4000, %edx           # imm = 0x4000
callq 0x3180
movl $0xe0, %eax
pushq $0x40
popq %rcx
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
leaq 0x1148(%rbx), %rdi
movl %r15d, %esi
callq 0x3210
leaq 0x276(%rip), %rax       # 0x107d8
leaq -0x50(%rbp), %rsi
movq %rax, (%rsi)
movq %rbx, 0x8(%rsi)
movq %rbx, %rdi
callq 0x31c0
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 0x2a63(%rip), %xmm2     # 0x13004
andnps %xmm2, %xmm0
orps %xmm1, %xmm0
leaq 0x50(%rbx), %rdi
movl %r15d, %esi
movl %r12d, %edx
callq 0x30a0
cmpl $0x1, 0x1114(%rbx)
jne 0x10610
xorps %xmm0, %xmm0
leaq -0x78(%rbp), %rsi
movups %xmm0, 0x10(%rsi)
andq $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 0x2a0f(%rip), %xmm1     # 0x13008
andnps %xmm1, %xmm2
orps %xmm0, %xmm2
movss %xmm2, 0xc(%rsi)
leaq 0x870(%rbx), %rdi
callq 0x3110
movq %rbx, %rdi
callq 0x10d3a
leaq 0x1158(%rbx), %r14
movl $0x4b8, %edx            # imm = 0x4B8
movq %r14, %rdi
xorl %esi, %esi
callq 0x3100
movl $0x411b, 0x1160(%rbx)   # imm = 0x411B
movabsq $0x208000201001001, %rax # imm = 0x208000201001001
movq %rax, 0x1568(%rbx)
leaq 0x3e9f(%rip), %rdx      # 0x144ef
xorl %eax, %eax
movq %rbx, -0x30(%rbp)
cmpq $0x3, %rax
je 0x106cd
leal -0x1(%rax), %ecx
pushq $0x1
popq %r15
shll %cl, %r15d
movq %rax, -0x38(%rbp)
testq %rax, %rax
movl $0x0, %eax
cmovel %eax, %r15d
movq %rdx, -0x40(%rbp)
movq %rdx, %rbx
xorl %r12d, %r12d
cmpq $0x8, %r12
je 0x106b8
xorl %r13d, %r13d
cmpq $0x5, %r13
je 0x106af
movzbl (%rbx,%r13), %esi
cmpl $0x20, %esi
je 0x106aa
movq %r14, %rdi
movl %r12d, %edx
movl %r13d, %ecx
movl %r15d, %r8d
callq 0x31a0
incq %r13
jmp 0x10689
incq %r12
addq $0x5, %rbx
jmp 0x10680
movq -0x38(%rbp), %rax
incq %rax
movq -0x40(%rbp), %rdx
addq $0x28, %rdx
movq -0x30(%rbp), %rbx
jmp 0x10656
movl $0x80001, 0x11e8(%rbx)  # imm = 0x80001
movl $0x80002, 0x11a4(%rbx)  # imm = 0x80002
movl $0x1010008, 0x1194(%rbx) # imm = 0x1010008
movaps 0x293e(%rip), %xmm0     # 0x13030
movups %xmm0, 0x1184(%rbx)
movabsq $0x4000101010001, %rax  # imm = 0x4000101010001
movq %rax, 0x1198(%rbx)
andw $0x0, 0x22(%rbx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3a76(%rip), %rdi      # 0x1419b
leaq 0x3a3e(%rip), %rsi      # 0x1416a
leaq 0x3a40(%rip), %rcx      # 0x14173
movl $0xda, %edx
callq 0x30d0
leaq 0x3a1a(%rip), %rdi      # 0x1415e
leaq 0x3a1f(%rip), %rsi      # 0x1416a
leaq 0x3a21(%rip), %rcx      # 0x14173
movl $0xd9, %edx
callq 0x30d0
leaq 0x3a7b(%rip), %rdi      # 0x141de
leaq 0x3a00(%rip), %rsi      # 0x1416a
leaq 0x3a02(%rip), %rcx      # 0x14173
movl $0xe4, %edx
callq 0x30d0
leaq 0x3a77(%rip), %rdi      # 0x141f9
leaq 0x39e1(%rip), %rsi      # 0x1416a
leaq 0x39e3(%rip), %rcx      # 0x14173
movl $0xe9, %edx
callq 0x30d0
leaq 0x3ac8(%rip), %rdi      # 0x14269
leaq 0x39c2(%rip), %rsi      # 0x1416a
leaq 0x39c4(%rip), %rcx      # 0x14173
movl $0xf3, %edx
callq 0x30d0
leaq 0x3a71(%rip), %rdi      # 0x14231
leaq 0x39a3(%rip), %rsi      # 0x1416a
leaq 0x39a5(%rip), %rcx      # 0x14173
movl $0xea, %edx
callq 0x30d0