// File: /workspace/github_cpp_makefile_star2_O1/klyushinmisha[P]GBEmu/./src/Interrupts.cpp
// Function: Interrupts::Drawing()
bool Interrupts::Drawing()
{
    if (gb->DMATransfer)
    {
        ushort addr = (ushort)((ushort)RAM[0xFF46] * 0x100);
        gb->DMATransfer = false;
        for (int i = 0; i < 160; i++)
            RAM[0xFE00 + i] = RAM[addr + i];
    }

    switch (gb->getMode())
    {
        case 0:
            if (interruptCounter >= 204)
            {
                interruptCounter %= 204;
                gb->DrawLine();
                if (gb->getYCoordinate() == 144)
                {
                    gb->setMode(1);
                    RAM[0xFF0F] |= 3;
                }
                else
                {
                    //gb->FindSprites();
                    gb->setMode(2);
                    RAM[0xFF0F] |= 2;
                }
                
            }
            break;
        case 2:
            if (interruptCounter >= 80)
            {
                interruptCounter %= 80;
                gb->setMode(3);
                RAM[0xFF0F] |= 2;
            }
            break;
        case 3:
            if (interruptCounter >= 172)
            {
                interruptCounter %= 172;
                gb->setMode(0);
                RAM[0xFF0F] |= 2;
            }
            break;
        case 1:
            if (interruptCounter >= 456)
            {
                gb->setYCoordinate(gb->getYCoordinate() + 1);
                interruptCounter %= 456;
            }
            if (gb->getYCoordinate() == 154)
            {
                gb->setYCoordinate(0);
                interruptCounter = 0;
                gb->setMode(2);
                RAM[0xFF0F] |= 2;
                return false;
            }
            break;
    }
    if (gb->getLYCompare() == gb->getYCoordinate())
        gb->setCoincidenceFlag(true);
    else
        gb->setCoincidenceFlag(false);
    if (gb->getCoincidenceFlag() & gb->getLYCLYCoincidenceInterrupt())
        RAM[0xFF0F] |= 2;
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq (%rdi), %rcx
cmpb $0x1, 0x58(%rcx)
jne 0x5b40
movq 0x8(%rbx), %rax
movzbl 0xff46(%rax), %eax
shll $0x8, %eax
movb $0x0, 0x58(%rcx)
xorl %ecx, %ecx
movq 0x8(%rbx), %rdx
leaq (%rdx,%rcx), %rsi
movb (%rax,%rsi), %sil
movb %sil, 0xfe00(%rdx,%rcx)
incq %rcx
cmpq $0xa0, %rcx
jne 0x5b20
movq (%rbx), %rdi
movq 0x48(%rdi), %rcx
movzbl 0xff41(%rcx), %edx
andl $0x3, %edx
movl 0x10(%rbx), %eax
leaq 0xdbd(%rip), %rsi       # 0x6918
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpl $0xcc, %eax
jl 0x5cae
movl %eax, %ecx
movl $0xa0a0a0a1, %edx       # imm = 0xA0A0A0A1
imulq %rcx, %rdx
shrq $0x27, %rdx
imull $0xcc, %edx, %ecx
subl %ecx, %eax
movl %eax, 0x10(%rbx)
callq 0x2bec
movq (%rbx), %rax
movq 0x48(%rax), %rcx
movb 0xff44(%rcx), %dl
andb $-0x4, 0xff41(%rcx)
movq 0x48(%rax), %rax
movb 0xff41(%rax), %cl
cmpb $-0x70, %dl
jne 0x5c99
orb $0x1, %cl
movb %cl, 0xff41(%rax)
movb $0x3, %al
jmp 0x5ca4
cmpl $0x50, %eax
jl 0x5cae
movl %eax, %edx
movl $0xcccccccd, %esi       # imm = 0xCCCCCCCD
imulq %rdx, %rsi
shrq $0x26, %rsi
shll $0x4, %esi
leal (%rsi,%rsi,4), %edx
subl %edx, %eax
movl %eax, 0x10(%rbx)
andb $-0x4, 0xff41(%rcx)
movq 0x48(%rdi), %rax
orb $0x3, 0xff41(%rax)
jmp 0x5ca2
cmpl $0xac, %eax
jl 0x5cae
movl %eax, %edx
imulq $0x2fa0be83, %rdx, %rdx # imm = 0x2FA0BE83
shrq $0x25, %rdx
imull $0xac, %edx, %edx
subl %edx, %eax
movl %eax, 0x10(%rbx)
andb $-0x4, 0xff41(%rcx)
jmp 0x5ca2
cmpl $0x1c8, %eax            # imm = 0x1C8
jl 0x5c5e
incb 0xff44(%rcx)
movslq 0x10(%rbx), %rax
imulq $-0x7047dc11, %rax, %rcx # imm = 0x8FB823EF
shrq $0x20, %rcx
addl %eax, %ecx
movl %ecx, %edx
shrl $0x1f, %edx
sarl $0x8, %ecx
addl %edx, %ecx
imull $0x1c8, %ecx, %ecx      # imm = 0x1C8
subl %ecx, %eax
movl %eax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x48(%rax), %rax
cmpb $-0x66, 0xff44(%rax)
jne 0x5cae
movb $0x0, 0xff44(%rax)
movl $0x0, 0x10(%rbx)
movq (%rbx), %rax
movq 0x48(%rax), %rcx
andb $-0x4, 0xff41(%rcx)
movq 0x48(%rax), %rax
orb $0x2, 0xff41(%rax)
xorl %eax, %eax
jmp 0x5cee
orb $0x2, %cl
movb %cl, 0xff41(%rax)
movb $0x2, %al
movq 0x8(%rbx), %rcx
orb %al, 0xff0f(%rcx)
movq (%rbx), %rax
movq 0x48(%rax), %rax
movb 0xff45(%rax), %cl
cmpb 0xff44(%rax), %cl
movb 0xff41(%rax), %cl
sete %dl
andb $-0x5, %cl
shlb $0x2, %dl
orb %cl, %dl
movb %dl, 0xff41(%rax)
movq (%rbx), %rax
movq 0x48(%rax), %rax
movb 0xff41(%rax), %cl
notb %cl
movb $0x1, %al
testb $0x44, %cl
jne 0x5cf9
movq 0x8(%rbx), %rcx
orb $0x2, 0xff0f(%rcx)
addq $0x8, %rsp
popq %rbx
popq %rbp
retq