// File: RegulusImpact[P]sadboy/src/Graphics/XGPU.cpp
// Function: XGPU::init_palette()
void XGPU::init_palette() {
    Visual* visual = DefaultVisual(display, screen);

    cmap = XCreateColormap(
        display,
        RootWindow(display, screen),
        visual,
        AllocNone
    );

    for (int ii = 0; ii < 4; ii++) {
        uint8_t mmuColor = mmu->palette[ii];
        Status rc;

        switch (mmuColor) {
            XColor xc;
            case 0:
            {
                rc = XAllocNamedColor(display, cmap, "Gray100", &palette[ii], &xc);
            }
                break;
            case 1:
            {
                rc = XAllocNamedColor(display, cmap, "Gray85", &palette[ii], &xc);
            }
                break;
            case 2:
            {
                rc = XAllocNamedColor(display, cmap, "Gray55", &palette[ii], &xc);
            }
                break;
            case 3:
            {
                rc = XAllocNamedColor(display, cmap, "Gray0", &palette[ii], &xc);
            }
                break;
        }

        if (rc == 0) {
            printf("Color Alloc [%d] failed.\n", ii);
        }
    }

    XSetWindowColormap(display, window, cmap);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movq 0x5a20(%rcx), %rax
movq 0xe8(%rax), %rax
movslq 0x5a28(%rcx), %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movq 0x40(%rax), %rax
movq %rax, -0x10(%rbp)
movq 0x5a20(%rcx), %rdi
movq 0x5a20(%rcx), %rax
movq 0xe8(%rax), %rax
movslq 0x5a28(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
movq -0x10(%rbp), %rdx
xorl %ecx, %ecx
callq 0x5300
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x5a40(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x4, -0x14(%rbp)
jge 0x34da4
movq -0x38(%rbp), %rax
movq 0x5a98(%rax), %rax
movslq -0x14(%rbp), %rcx
movb 0xe39a(%rax,%rcx), %al
movb %al, -0x15(%rbp)
movzbl -0x15(%rbp), %eax
movq %rax, -0x40(%rbp)
subq $0x3, %rax
ja 0x34d7d
movq -0x40(%rbp), %rax
leaq 0x4f49(%rip), %rcx      # 0x39bd8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rcx
movq 0x5a20(%rcx), %rdi
movq 0x5a40(%rcx), %rsi
addq $0x5a50, %rcx           # imm = 0x5A50
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
leaq 0x4fbc(%rip), %rdx      # 0x39c7f
leaq -0x30(%rbp), %r8
callq 0x55c0
movl %eax, -0x1c(%rbp)
jmp 0x34d7d
movq -0x38(%rbp), %rcx
movq 0x5a20(%rcx), %rdi
movq 0x5a40(%rcx), %rsi
addq $0x5a50, %rcx           # imm = 0x5A50
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
leaq 0x4f88(%rip), %rdx      # 0x39c87
leaq -0x30(%rbp), %r8
callq 0x55c0
movl %eax, -0x1c(%rbp)
jmp 0x34d7d
movq -0x38(%rbp), %rcx
movq 0x5a20(%rcx), %rdi
movq 0x5a40(%rcx), %rsi
addq $0x5a50, %rcx           # imm = 0x5A50
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
leaq 0x4f56(%rip), %rdx      # 0x39c8e
leaq -0x30(%rbp), %r8
callq 0x55c0
movl %eax, -0x1c(%rbp)
jmp 0x34d7d
movq -0x38(%rbp), %rcx
movq 0x5a20(%rcx), %rdi
movq 0x5a40(%rcx), %rsi
addq $0x5a50, %rcx           # imm = 0x5A50
movslq -0x14(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rcx
leaq 0x4f24(%rip), %rdx      # 0x39c95
leaq -0x30(%rbp), %r8
callq 0x55c0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x34d94
movl -0x14(%rbp), %esi
leaq 0x4f0e(%rip), %rdi      # 0x39c9b
movb $0x0, %al
callq 0x5070
jmp 0x34d96
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x34c4f
movq -0x38(%rbp), %rax
movq 0x5a20(%rax), %rdi
movq 0x5a30(%rax), %rsi
movq 0x5a40(%rax), %rdx
callq 0x56f0
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)