// File: /workspace/github_c_makefile_star2_O2/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 0x52f2
movl (%rbx), %ecx
cmpl $0x71, %ecx
je 0x54f6
cmpl $0x13, %ecx
je 0x536c
cmpl $0x3d, %ecx
je 0x5379
testl %ecx, %ecx
je 0x54ff
jmp 0x5513
cmpl $0x9a, %eax
je 0x533b
cmpl $0x90, %eax
jne 0x5355
movb 0xff41(%r14), %al
andb $-0x4, %al
incb %al
movb %al, 0xff41(%r14)
movb $-0x70, %al
cmpl $0x71, (%rbx)
jne 0x5513
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5614
orb $0x1, 0xff0f(%r14)
movb 0xff44(%r14), %al
incb %al
jmp 0x5360
movb $-0x66, %al
cmpl $0x71, (%rbx)
jne 0x5513
movb $0x0, 0xff44(%r14)
xorl %eax, %eax
jmp 0x5513
cmpl $0x71, (%rbx)
jne 0x5513
incl %eax
movb %al, 0xff44(%r14)
jmp 0x5513
orb $0x3, 0xff41(%r14)
jmp 0x5513
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
cmpq $0x20, %rsi
je 0x540d
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
jmp 0x53c9
movzbl 0xff44(%r14), %eax
cmpl $0x8f, %eax
ja 0x5513
movzbl 0xff42(%r14), %r8d
movzbl 0xff43(%r14), %r9d
movl %r9d, %edx
shrl $0x3, %edx
addl %eax, %r8d
andl $0x7, %r9d
movq 0x8(%rbx), %rcx
imulq $0x280, %rax, %rdi      # imm = 0x280
movl %r8d, %eax
shll $0x6, %eax
andl $0x7e00, %eax           # imm = 0x7E00
leal -0x4000(%rax), %esi
cmpl $0x100, %r8d            # 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 %r8d, %r8d
andl $0xe, %r8d
movzbl (%r8,%rax), %ebp
movzbl 0x1(%r8,%rax), %r12d
leaq 0x28(%rbx), %rdi
movl %r9d, %eax
xorl $0x7, %eax
pushq $0x8
popq %r13
subq %r9, %r13
xorl %r10d, %r10d
cmpq %r10, %r13
je 0x5558
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,%r10,4)
movzwl (%rdi,%r11,4), %ecx
movw %cx, (%r15,%r10,4)
movb $-0x1, 0x3(%r15,%r10,4)
incq %r10
decl %eax
jmp 0x54a3
incl %eax
movb %al, 0xff44(%r14)
movb 0xff41(%r14), %cl
andb $-0x4, %cl
orb $0x2, %cl
movb %cl, 0xff41(%r14)
movb 0xff41(%r14), %cl
cmpb %al, 0xff45(%r14)
jne 0x5530
orb $0x4, %cl
orb $0x2, 0xff0f(%r14)
jmp 0x5533
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
shll $0x2, %r9d
pushq $0x20
popq %r15
subl %r9d, %r15d
addq %r8, %rsi
movq 0x8(%rsp), %rcx
addq %r15, %rcx
movq 0x10(%rsp), %rax
leaq (%rax,%rcx), %r8
addq $0x3, %r8
xorl %r9d, %r9d
pushq $0x7
popq %r10
cmpq $0x27f, %r15            # imm = 0x27F
ja 0x5608
incl %edx
cmpl $0x20, %edx
cmovel %r9d, %edx
movslq %edx, %r11
shlq $0x4, %r11
movl %r10d, %eax
xorl %r12d, %r12d
cmpq $0x8, %r12
je 0x55fb
movzbl 0x1(%rsi,%r11), %r13d
movl %eax, %ecx
shrl %cl, %r13d
andl $0x1, %r13d
movzbl (%rsi,%r11), %ecx
xorl %ebp, %ebp
btl %eax, %ecx
setb %bpl
leal (%rbp,%r13,2), %ecx
movzbl 0xff47(%r14), %r13d
addl %ecx, %ecx
shrl %cl, %r13d
andl $0x3, %r13d
movb 0x2(%rdi,%r13,4), %cl
movb %cl, -0x1(%r8,%r12,4)
movzwl (%rdi,%r13,4), %ecx
movw %cx, -0x3(%r8,%r12,4)
movb $-0x1, (%r8,%r12,4)
incq %r12
decl %eax
jmp 0x55a1
addq $0x20, %r15
addq $0x20, %r8
jmp 0x5582
movb 0xff44(%r14), %al
jmp 0x5513