// File: /workspace/github_c_makefile_star2_O0/leiradel[P]zx48k-libretro/src/mem.h
// Function: mem_map
static void _mem_map(mem_t* m, int layer, uint16_t addr, uint32_t size, const uint8_t* read_ptr, uint8_t* write_ptr) {
    CHIPS_ASSERT(m);
    CHIPS_ASSERT((layer >= 0) && (layer < MEM_NUM_LAYERS));
    CHIPS_ASSERT((addr & MEM_PAGE_MASK) == 0);
    CHIPS_ASSERT((size & MEM_PAGE_MASK) == 0);
    CHIPS_ASSERT(size <= MEM_ADDR_RANGE);
    const int num = size>>MEM_PAGE_SHIFT;
    CHIPS_ASSERT(num <= MEM_NUM_PAGES);
    for (int i = 0; i < num; i++) {
        const uint16_t offset = i * MEM_PAGE_SIZE;
        /* the page_index will wrap-around */
        const uint16_t page_index = ((addr+offset) & MEM_ADDR_MASK) >> MEM_PAGE_SHIFT;
        CHIPS_ASSERT(page_index <= MEM_NUM_PAGES);
        mem_page_t* page = &m->layers[layer][page_index];
        page->read_ptr = read_ptr + offset;
        if (0 != write_ptr) {
            page->write_ptr = write_ptr + offset;
        }
        else {
            page->write_ptr = m->junk_page;
        }
        _mem_update_page_table(m, page_index);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movw %dx, %ax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movw %ax, -0xe(%rbp)
movl %ecx, -0x14(%rbp)
movq %r8, -0x20(%rbp)
movq %r9, -0x28(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x4f2a
jmp 0x4f49
leaq 0x200e0(%rip), %rdi     # 0x25011
leaq 0x200db(%rip), %rsi     # 0x25013
movl $0x12a, %edx            # imm = 0x12A
leaq 0x20671(%rip), %rcx     # 0x255b5
callq 0x3180
cmpl $0x0, -0xc(%rbp)
jl 0x4f57
cmpl $0x4, -0xc(%rbp)
jge 0x4f57
jmp 0x4f76
leaq 0x1ff76(%rip), %rdi     # 0x24ed4
leaq 0x200ae(%rip), %rsi     # 0x25013
movl $0x12b, %edx            # imm = 0x12B
leaq 0x20644(%rip), %rcx     # 0x255b5
callq 0x3180
movzwl -0xe(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x0, %eax
jne 0x4f86
jmp 0x4fa5
leaq 0x20674(%rip), %rdi     # 0x25601
leaq 0x2007f(%rip), %rsi     # 0x25013
movl $0x12c, %edx            # imm = 0x12C
leaq 0x20615(%rip), %rcx     # 0x255b5
callq 0x3180
movl -0x14(%rbp), %eax
andl $0x3ff, %eax            # imm = 0x3FF
cmpl $0x0, %eax
jne 0x4fb4
jmp 0x4fd3
leaq 0x20662(%rip), %rdi     # 0x2561d
leaq 0x20051(%rip), %rsi     # 0x25013
movl $0x12d, %edx            # imm = 0x12D
leaq 0x205e7(%rip), %rcx     # 0x255b5
callq 0x3180
cmpl $0x10000, -0x14(%rbp)   # imm = 0x10000
ja 0x4fde
jmp 0x4ffd
leaq 0x20654(%rip), %rdi     # 0x25639
leaq 0x20027(%rip), %rsi     # 0x25013
movl $0x12e, %edx            # imm = 0x12E
leaq 0x205bd(%rip), %rcx     # 0x255b5
callq 0x3180
movl -0x14(%rbp), %eax
shrl $0xa, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x40, -0x2c(%rbp)
jg 0x500e
jmp 0x502d
leaq 0x20634(%rip), %rdi     # 0x25649
leaq 0x1fff7(%rip), %rsi     # 0x25013
movl $0x130, %edx            # imm = 0x130
leaq 0x2058d(%rip), %rcx     # 0x255b5
callq 0x3180
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x5109
movl -0x30(%rbp), %eax
shll $0xa, %eax
movw %ax, -0x32(%rbp)
movzwl -0xe(%rbp), %eax
movzwl -0x32(%rbp), %ecx
addl %ecx, %eax
andl $0xffff, %eax           # imm = 0xFFFF
sarl $0xa, %eax
movw %ax, -0x34(%rbp)
movzwl -0x34(%rbp), %eax
cmpl $0x40, %eax
jg 0x506b
jmp 0x508a
leaq 0x205f4(%rip), %rdi     # 0x25666
leaq 0x1ff9a(%rip), %rsi     # 0x25013
movl $0x135, %edx            # imm = 0x135
leaq 0x20530(%rip), %rcx     # 0x255b5
callq 0x3180
movq -0x8(%rbp), %rax
movslq -0xc(%rbp), %rcx
shlq $0xa, %rcx
addq %rcx, %rax
movzwl -0x34(%rbp), %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rcx
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
xorl %eax, %eax
cmpq -0x28(%rbp), %rax
je 0x50db
movq -0x28(%rbp), %rcx
movzwl -0x32(%rbp), %eax
cltq
addq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x50ee
movq -0x8(%rbp), %rcx
addq $0x1800, %rcx           # imm = 0x1800
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movzwl -0x34(%rbp), %esi
callq 0x52d0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x5034
addq $0x40, %rsp
popq %rbp
retq
nop