// File: /workspace/github_c_makefile_star2_O1/qad114[P]yobeboy/src/gpu.c
// Function: GPU_update
void GPU_update(CPU* cpu, GPU* gpu, Memory* mem) {
    uint8_t* IF = &(mem->logicalMemory[REG_IF]);
    uint8_t* LYC = &(mem->logicalMemory[REG_LYC]);
    uint8_t* LY = &(mem->logicalMemory[REG_LY]);
    uint8_t* STAT = &(mem->logicalMemory[REG_STAT]);

    if (*LY < 144) {
        // Cycle through modes 2, 3, 0
        switch (gpu->machineCycleCounter) {
            case 0: // Mode 2
                *STAT = (*STAT & 0xFC) | 2;
                break;

            case 19: // Mode 3
                *STAT = (*STAT & 0xFC) | 3;
                break;

            case 61: // Mode 0 (HBlank)
                *STAT = (*STAT & 0xFC) | 0;
                updateBackgroundMap(gpu, mem);
                renderBgScanline(gpu, mem, *LY);
                break;

            case 113: // Go to next line
                ++*LY;
                *STAT = (*STAT & 0xFC) | 2;
                break;

            default:
                break;
        }

    } else if (*LY == 144) {
        // Mode 1 (VBlank)
        *STAT = (*STAT & 0xFC) | 1;
        if (gpu->machineCycleCounter == 113) {
            GPU_renderToFrameBuffer(gpu, mem);
            *IF |= 0x1;
            ++*LY;
        }

    } else if (*LY == 154) {
        // VBlank end
        if (gpu->machineCycleCounter == 113) {
            *LY = 0;
        }

    } else {
        if (gpu->machineCycleCounter == 113) {
            ++*LY;
        }
    }

    // Check for LYC=LY
    if (*LYC == *LY) {
        *STAT = setBit(*STAT, 2, 1);
        *IF = setBit(*IF, 1, 1);
    } else {
        *STAT = setBit(*STAT, 2, 0);
    }

    if (gpu->machineCycleCounter == 113) {
        gpu->machineCycleCounter = 0;
    } else {
        ++(gpu->machineCycleCounter);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movzbl 0xff44(%rdx), %ecx
cmpl $0x8f, %ecx
ja 0x5b28
movl (%rbx), %eax
cmpl $0x3c, %eax
jg 0x5b72
testl %eax, %eax
je 0x5b89
cmpl $0x13, %eax
jne 0x5df4
orb $0x3, 0xff41(%r14)
jmp 0x5df4
cmpl $0x9a, %ecx
je 0x5ba0
cmpl $0x90, %ecx
jne 0x5bb6
movb 0xff41(%r14), %al
andb $-0x4, %al
incb %al
movb %al, 0xff41(%r14)
cmpl $0x71, (%rbx)
jne 0x5df4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5e45
orb $0x1, 0xff0f(%r14)
incb 0xff44(%r14)
jmp 0x5df4
cmpl $0x3d, %eax
je 0x5bcd
cmpl $0x71, %eax
jne 0x5df4
incb %cl
movb %cl, 0xff44(%r14)
movb 0xff41(%r14), %al
andb $-0x4, %al
orb $0x2, %al
movb %al, 0xff41(%r14)
jmp 0x5df4
cmpl $0x71, (%rbx)
jne 0x5df4
movb $0x0, 0xff44(%r14)
jmp 0x5df4
cmpl $0x71, (%rbx)
jne 0x5df4
incb %cl
movb %cl, 0xff44(%r14)
jmp 0x5df4
andb $-0x4, 0xff41(%r14)
movb 0xff40(%r14), %al
movzbl 0xff42(%r14), %edx
addl %ecx, %edx
movl %edx, %ecx
shrl $0x3, %ecx
leal -0x20(%rcx), %esi
cmpl $0x100, %edx            # imm = 0x100
cmovbl %ecx, %esi
movl %eax, %edx
andb $0x8, %dl
shrb $0x3, %dl
movl %esi, %ecx
shll $0x9, %ecx
shll $0x5, %esi
movzbl %dl, %edx
shll $0xa, %edx
addl %esi, %edx
addl $0xffff9800, %edx       # imm = 0xFFFF9800
movzwl %dx, %edx
addq %r14, %rdx
xorl %esi, %esi
movzbl (%rdx,%rsi), %edi
movsbl %dil, %r8d
shll $0x4, %edi
orl $0x8000, %edi           # imm = 0x8000
shll $0x4, %r8d
addl $0x9000, %r8d           # imm = 0x9000
testb $0x10, %al
cmovneq %rdi, %r8
movzwl %r8w, %edi
movq 0x18(%rbx), %r8
movups (%r14,%rdi), %xmm0
movups %xmm0, (%r8,%rcx)
incq %rsi
addq $0x10, %rcx
cmpq $0x20, %rsi
jne 0x5c1d
movzbl 0xff44(%r14), %eax
cmpl $0x8f, %eax
ja 0x5df4
movzbl 0xff42(%r14), %r10d
movzbl 0xff43(%r14), %r8d
movl %r8d, %edx
shrl $0x3, %edx
addl %eax, %r10d
andl $0x7, %r8d
movq 0x8(%rbx), %rcx
shll $0x7, %eax
leaq (%rax,%rax,4), %rdi
movl %r10d, %eax
shll $0x6, %eax
andl $0x7e00, %eax           # imm = 0x7E00
leal -0x4000(%rax), %esi
cmpl $0x100, %r10d           # imm = 0x100
cmovbl %eax, %esi
movq %rcx, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
leaq (%rcx,%rdi), %r15
addq 0x18(%rbx), %rsi
movl %edx, %eax
shll $0x4, %eax
addq %rsi, %rax
addl %r10d, %r10d
andl $0xe, %r10d
movzbl (%r10,%rax), %ebp
movzbl 0x1(%r10,%rax), %r12d
leaq 0x28(%rbx), %rdi
movl %r8d, %eax
xorl $0x7, %eax
movl $0x8, %r13d
subq %r8, %r13
xorl %r9d, %r9d
movl %r12d, %r11d
movl %eax, %ecx
shrl %cl, %r11d
andl $0x1, %r11d
xorl %ecx, %ecx
btl %eax, %ebp
setb %cl
leal (%rcx,%r11,2), %ecx
movzbl 0xff47(%r14), %r11d
addl %ecx, %ecx
shrl %cl, %r11d
andl $0x3, %r11d
movb 0x2(%rdi,%r11,4), %cl
movb %cl, 0x2(%r15,%r9,4)
movzwl (%rdi,%r11,4), %ecx
movw %cx, (%r15,%r9,4)
movb $-0x1, 0x3(%r15,%r9,4)
decl %eax
incq %r9
cmpq %r9, %r13
jne 0x5cf6
addq %r10, %rsi
shll $0x2, %r8d
movl $0x20, %r10d
subl %r8d, %r10d
movq 0x8(%rsp), %rcx
addq %r10, %rcx
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %r8
addq $0x3, %r8
xorl %r9d, %r9d
incl %edx
cmpl $0x20, %edx
cmovel %r9d, %edx
movslq %edx, %r11
shlq $0x4, %r11
movl $0x7, %eax
xorl %r15d, %r15d
movzbl 0x1(%rsi,%r11), %r12d
movl %eax, %ecx
shrl %cl, %r12d
andl $0x1, %r12d
movzbl (%rsi,%r11), %ecx
xorl %r13d, %r13d
btl %eax, %ecx
setb %r13b
leal (%r13,%r12,2), %ecx
movzbl 0xff47(%r14), %r12d
addl %ecx, %ecx
shrl %cl, %r12d
andl $0x3, %r12d
movb 0x2(%rdi,%r12,4), %cl
movb %cl, -0x1(%r8,%r15,4)
movzwl (%rdi,%r12,4), %ecx
movw %cx, -0x3(%r8,%r15,4)
movb $-0x1, (%r8,%r15,4)
incq %r15
decl %eax
cmpq $0x8, %r15
jne 0x5d83
leaq 0x20(%r10), %rax
addq $0x20, %r8
cmpq $0x260, %r10            # imm = 0x260
movq %rax, %r10
jb 0x5d6b
movb 0xff41(%r14), %al
movb 0xff45(%r14), %cl
cmpb 0xff44(%r14), %cl
jne 0x5e1e
orb $0x4, %al
movb %al, 0xff41(%r14)
orb $0x2, 0xff0f(%r14)
jmp 0x5e27
andb $-0x5, %al
movb %al, 0xff41(%r14)
movl (%rbx), %eax
leal 0x1(%rax), %ecx
xorl %edx, %edx
cmpl $0x71, %eax
cmovnel %ecx, %edx
movl %edx, (%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq