// File: /workspace/github_cpp_makefile_star2_O2/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), %rdi
cmpb $0x1, 0x58(%rdi)
jne 0x5816
movq 0x8(%rbx), %rax
movzbl 0xff46(%rax), %eax
shll $0x8, %eax
movb $0x0, 0x58(%rdi)
movq $-0xa0, %rcx
testq %rcx, %rcx
je 0x5813
movq 0x8(%rbx), %rdx
leaq (%rdx,%rcx), %rsi
movb 0xa0(%rax,%rsi), %sil
movb %sil, 0xfea0(%rdx,%rcx)
incq %rcx
jmp 0x57f1
movq (%rbx), %rdi
movq 0x48(%rdi), %rcx
movzbl 0xff41(%rcx), %edx
andl $0x3, %edx
movl 0x10(%rbx), %eax
leaq 0x10c2(%rip), %rsi      # 0x68f0
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpl $0xcc, %eax
jl 0x5935
movl $0xcc, %ecx
xorl %edx, %edx
divl %ecx
movl %edx, 0x10(%rbx)
callq 0x2b18
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 0x5920
orb $0x1, %cl
movb %cl, 0xff41(%rax)
movb $0x3, %al
jmp 0x592b
cmpl $0x50, %eax
jl 0x5935
pushq $0x50
popq %rsi
xorl %edx, %edx
divl %esi
movl %edx, 0x10(%rbx)
andb $-0x4, 0xff41(%rcx)
movq 0x48(%rdi), %rax
orb $0x3, 0xff41(%rax)
jmp 0x5929
cmpl $0xac, %eax
jl 0x5935
movl $0xac, %esi
xorl %edx, %edx
divl %esi
movl %edx, 0x10(%rbx)
andb $-0x4, 0xff41(%rcx)
jmp 0x5929
cmpl $0x1c8, %eax            # imm = 0x1C8
jl 0x58ef
incb 0xff44(%rcx)
movl 0x10(%rbx), %eax
movl $0x1c8, %ecx            # imm = 0x1C8
cltd
idivl %ecx
movl %edx, 0x10(%rbx)
movq (%rbx), %rax
movq 0x48(%rax), %rcx
cmpb $-0x66, 0xff44(%rcx)
jne 0x5935
movb $0x0, 0xff44(%rcx)
andl $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 0x5975
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 0x5980
movq 0x8(%rbx), %rcx
orb $0x2, 0xff0f(%rcx)
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
nop