// File: /workspace/github_cpp_makefile_star2_O3/rockytriton[P]dreamsys_emu_gb/src/cpu.cpp
// Function: dsemu::cpu::handleInterrupt(unsigned char, bool, bool)
void handleInterrupt(byte flag, bool request, bool pcp1) {
    if (flag & 1) {
        if (request) vbEnabled = true; else vbRequested = true;

        //cout << "V-Blank " << (request ? "requested" : "enabled") << endl;

        if (request) {
            intRequestFlag = flag;
        }

        if (request && interruptsEnabled && (intEnableFlag & 1)) {
            //cout << "CPU:> HANDLING VBLANK" << endl;
            intRequestFlag &= ~1;
            //cout << "Disabling VBlank: " << Byte(intRequestFlag) << endl;

            if (pcp1) {
                push((ushort)(regPC + 1));
            } else {
                push(regPC);
            }

            if (haltWaitingForInterrupt) {
                //cout << "RESUMING" << endl;
            }

            haltWaitingForInterrupt = false;
            regPC = 0x40;
            interruptsEnabled = false;
            //changePC(0x40);
            //cpuSpeed = 500;
        } else {
           // cout << "DENIED" << endl;
        }
    } else if (flag & 2) {
        intRequestFlag |= flag;

        if (request && interruptsEnabled && (intEnableFlag & 2)) {
            intRequestFlag &= ~2;

            if (pcp1) {
                push((ushort)(regPC + 1));
            } else {
                push(regPC);
            }

            if (haltWaitingForInterrupt) {
                //cout << "RESUMING" << endl;
            }

            haltWaitingForInterrupt = false;
            regPC = 0x48;
            interruptsEnabled = false;
        }
    }else if (flag & 4) {
        intRequestFlag |= flag;

        if (request && interruptsEnabled && (intEnableFlag & 4)) {
            intRequestFlag &= ~4;

            if (pcp1) {
                push((ushort)(regPC + 1));
            } else {
                push(regPC);
            }

            if (haltWaitingForInterrupt) {
                //cout << "RESUMING" << endl;
            }

            haltWaitingForInterrupt = false;
            regPC = 0x50;
            interruptsEnabled = false;
        }
    }
    else if (flag) {
        cout << "OK ANOTHER INTERRUPT: " << Byte(flag) << endl;
        sleep(10);
    } else {
        intRequestFlag = flag;
    }
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movl %edi, %ebx
testb $0x1, %bl
jne 0xd08a
testb $0x2, %bl
jne 0xd0ef
testb $0x4, %bl
jne 0xd151
testb %bl, %bl
je 0xd1a8
movq 0xb006(%rip), %r14      # 0x17fd0
leaq 0x6119(%rip), %rsi      # 0x130ea
movl $0x16, %edx
movq %r14, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r14,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rcx
leaq (%r14,%rcx), %r15
cmpb $0x0, 0xe1(%r14,%rcx)
jne 0xd020
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r15)
movq (%r14), %rax
movb $0x30, 0xe0(%r15)
movq -0x18(%rax), %rax
movq $0x2, 0x10(%r14,%rax)
movq %r14, %rdi
movl %ebx, %esi
callq 0x3420
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movl $0xa, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x3210
testb %sil, %sil
je 0xd144
movb $0x1, 0xb46b(%rip)      # 0x18505
movb %bl, 0xb467(%rip)       # 0x18507
leaq 0x2b572(%rip), %r14     # 0x38619
cmpb $0x1, (%r14)
jne 0xd14b
testb $0x1, 0xb44e(%rip)      # 0x18506
je 0xd14b
andb $-0x2, %bl
movb %bl, 0xb440(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb42d(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc490
xorl %eax, %eax
movb %al, 0xb208(%rip)       # 0x182e9
movw $0x40, 0xb414(%rip)     # 0x184fe
movb %al, (%r14)
jmp 0xd14b
movb 0xb412(%rip), %al       # 0x18507
orb %bl, %al
movb %al, 0xb40a(%rip)       # 0x18507
testb %sil, %sil
je 0xd14b
leaq 0x2b510(%rip), %rbx     # 0x38619
cmpb $0x1, (%rbx)
jne 0xd14b
testb $0x2, 0xb3f1(%rip)      # 0x18506
je 0xd14b
andb $-0x3, %al
movb %al, 0xb3e8(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb3d5(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc490
xorl %eax, %eax
movb %al, 0xb1b0(%rip)       # 0x182e9
movw $0x48, 0xb3bc(%rip)     # 0x184fe
jmp 0xd1a4
movb $0x1, 0xb3b9(%rip)      # 0x18504
popq %rbx
popq %r14
popq %r15
retq
movb 0xb3b0(%rip), %al       # 0x18507
orb %bl, %al
movb %al, 0xb3a8(%rip)       # 0x18507
testb %sil, %sil
je 0xd14b
leaq 0x2b4ae(%rip), %rbx     # 0x38619
cmpb $0x1, (%rbx)
jne 0xd14b
testb $0x4, 0xb38f(%rip)      # 0x18506
je 0xd14b
andb $-0x5, %al
movb %al, 0xb386(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb373(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc490
xorl %eax, %eax
movb %al, 0xb14e(%rip)       # 0x182e9
movw $0x50, 0xb35a(%rip)     # 0x184fe
movb %al, (%rbx)
jmp 0xd14b
movb $0x0, 0xb358(%rip)      # 0x18507
jmp 0xd14b