// File: /workspace/github_c_makefile_star2_O3/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), %eax
cmpl $0x8f, %eax
ja 0x5af3
movl (%rbx), %ecx
cmpl $0x3c, %ecx
jg 0x5b40
testl %ecx, %ecx
je 0x5b57
cmpl $0x13, %ecx
jne 0x5dd0
orb $0x3, 0xff41(%r14)
jmp 0x5dd0
cmpl $0x9a, %eax
je 0x5b70
cmpl $0x90, %eax
jne 0x5b8a
movb 0xff41(%r14), %al
andb $-0x4, %al
incb %al
movb %al, 0xff41(%r14)
movb $-0x70, %al
cmpl $0x71, (%rbx)
jne 0x5dd0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5e15
orb $0x1, 0xff0f(%r14)
movb 0xff44(%r14), %al
incb %al
jmp 0x5b95
cmpl $0x3d, %ecx
je 0x5ba1
cmpl $0x71, %ecx
jne 0x5dd0
incl %eax
movb %al, 0xff44(%r14)
movb 0xff41(%r14), %cl
andb $-0x4, %cl
orb $0x2, %cl
movb %cl, 0xff41(%r14)
jmp 0x5dd0
movb $-0x66, %al
cmpl $0x71, (%rbx)
jne 0x5dd0
movb $0x0, 0xff44(%r14)
xorl %eax, %eax
jmp 0x5dd0
cmpl $0x71, (%rbx)
jne 0x5dd0
incl %eax
movb %al, 0xff44(%r14)
jmp 0x5dd0
andb $-0x4, 0xff41(%r14)
movb 0xff40(%r14), %cl
movzbl 0xff42(%r14), %edx
addl %eax, %edx
movl %edx, %eax
shrl $0x3, %eax
leal -0x20(%rax), %esi
cmpl $0x100, %edx            # imm = 0x100
cmovbl %eax, %esi
movl %ecx, %edx
andb $0x8, %dl
shrb $0x3, %dl
movl %esi, %eax
shll $0x9, %eax
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, %cl
cmovneq %rdi, %r8
movzwl %r8w, %edi
movq 0x18(%rbx), %r8
movups (%r14,%rdi), %xmm0
movups %xmm0, (%r8,%rax)
incq %rsi
addq $0x10, %rax
cmpq $0x20, %rsi
jne 0x5bf1
movzbl 0xff44(%r14), %eax
cmpl $0x8f, %eax
ja 0x5dd0
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 $0x8, %r13d
subq %r8, %r13
movl %r8d, %eax
xorl $0x7, %eax
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)
incq %r9
decl %eax
cmpq %r9, %r13
jne 0x5ccb
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 0x5d58
leaq 0x20(%r10), %rax
addq $0x20, %r8
cmpq $0x260, %r10            # imm = 0x260
movq %rax, %r10
jb 0x5d40
movb 0xff44(%r14), %al
movb 0xff41(%r14), %cl
cmpb %al, 0xff45(%r14)
jne 0x5ded
orb $0x4, %cl
orb $0x2, 0xff0f(%r14)
jmp 0x5df0
andb $-0x5, %cl
movb %cl, 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