// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
pushq %rax
movl %edi, %ebx
testb $0x1, %bl
jne 0xb149
testb $0x2, %bl
jne 0xb1ae
testb $0x4, %bl
jne 0xb212
testb %bl, %bl
je 0xb269
movq 0xaeb5(%rip), %rdi      # 0x15fd0
leaq 0x4fc8(%rip), %rsi      # 0x100ea
callq 0x3260
movzbl %bl, %esi
movq %rax, %rdi
callq 0xaba6
movq %rax, %rdi
callq 0x3130
pushq $0xa
popq %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x31e0
testb %sil, %sil
je 0xb203
movb $0x1, 0xb38c(%rip)      # 0x164e5
movb %bl, 0xb388(%rip)       # 0x164e7
leaq 0x2b493(%rip), %r14     # 0x365f9
cmpb $0x1, (%r14)
jne 0xb20a
testb $0x1, 0xb36f(%rip)      # 0x164e6
je 0xb20a
andb $-0x2, %bl
movb %bl, 0xb361(%rip)       # 0x164e7
movzbl %dl, %eax
addw 0xb34e(%rip), %ax       # 0x164de
movzwl %ax, %edi
callq 0xac08
xorl %eax, %eax
movb %al, 0xb129(%rip)       # 0x162c9
movw $0x40, 0xb335(%rip)     # 0x164de
movb %al, (%r14)
jmp 0xb20a
movb 0xb333(%rip), %al       # 0x164e7
orb %bl, %al
movb %al, 0xb32b(%rip)       # 0x164e7
testb %sil, %sil
je 0xb20a
leaq 0x2b431(%rip), %rbx     # 0x365f9
cmpb $0x1, (%rbx)
jne 0xb20a
testb $0x2, 0xb312(%rip)      # 0x164e6
je 0xb20a
andb $-0x3, %al
movb %al, 0xb309(%rip)       # 0x164e7
movzbl %dl, %eax
addw 0xb2f6(%rip), %ax       # 0x164de
movzwl %ax, %edi
callq 0xac08
xorl %eax, %eax
movb %al, 0xb0d1(%rip)       # 0x162c9
movw $0x48, 0xb2dd(%rip)     # 0x164de
jmp 0xb265
movb $0x1, 0xb2da(%rip)      # 0x164e4
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movb 0xb2cf(%rip), %al       # 0x164e7
orb %bl, %al
movb %al, 0xb2c7(%rip)       # 0x164e7
testb %sil, %sil
je 0xb20a
leaq 0x2b3cd(%rip), %rbx     # 0x365f9
cmpb $0x1, (%rbx)
jne 0xb20a
testb $0x4, 0xb2ae(%rip)      # 0x164e6
je 0xb20a
andb $-0x5, %al
movb %al, 0xb2a5(%rip)       # 0x164e7
movzbl %dl, %eax
addw 0xb292(%rip), %ax       # 0x164de
movzwl %ax, %edi
callq 0xac08
xorl %eax, %eax
movb %al, 0xb06d(%rip)       # 0x162c9
movw $0x50, 0xb279(%rip)     # 0x164de
movb %al, (%rbx)
jmp 0xb20a
movb $0x0, 0xb277(%rip)      # 0x164e7
jmp 0xb20a