// File: /workspace/github_cpp_makefile_star2_O1/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 0xd138
testb $0x2, %bl
jne 0xd1ae
testb $0x4, %bl
jne 0xd20f
testb %bl, %bl
je 0xd266
movq 0xaf5f(%rip), %r14      # 0x17fd0
leaq 0x6072(%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), %rax
leaq (%r14,%rax), %r15
cmpb $0x0, 0xe1(%r14,%rax)
jne 0xd0cb
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x30, 0xe0(%r15)
movq (%r14), %rax
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
leaq 0xb3c6(%rip), %rax      # 0x18505
leaq 0xb3be(%rip), %rcx      # 0x18504
testb %sil, %sil
cmovneq %rax, %rcx
movb $0x1, (%rcx)
je 0xd26d
movb %bl, 0xb3ab(%rip)       # 0x18507
leaq 0x2b4b6(%rip), %r14     # 0x38619
cmpb $0x1, (%r14)
jne 0xd26d
testb $0x1, 0xb392(%rip)      # 0x18506
je 0xd26d
andb $-0x2, %bl
movb %bl, 0xb384(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb371(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc4e4
xorl %eax, %eax
movb %al, 0xb14c(%rip)       # 0x182e9
movw $0x40, 0xb358(%rip)     # 0x184fe
movb %al, (%r14)
jmp 0xd26d
movb 0xb353(%rip), %al       # 0x18507
orb %bl, %al
movb %al, 0xb34b(%rip)       # 0x18507
testb %sil, %sil
je 0xd26d
leaq 0x2b44d(%rip), %rbx     # 0x38619
cmpb $0x1, (%rbx)
jne 0xd26d
testb $0x2, 0xb32a(%rip)      # 0x18506
je 0xd26d
andb $-0x3, %al
movb %al, 0xb31d(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb30a(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc4e4
xorl %eax, %eax
movb %al, 0xb0e5(%rip)       # 0x182e9
movw $0x48, 0xb2f1(%rip)     # 0x184fe
jmp 0xd262
movb 0xb2f2(%rip), %al       # 0x18507
orb %bl, %al
movb %al, 0xb2ea(%rip)       # 0x18507
testb %sil, %sil
je 0xd26d
leaq 0x2b3f0(%rip), %rbx     # 0x38619
cmpb $0x1, (%rbx)
jne 0xd26d
testb $0x4, 0xb2d1(%rip)      # 0x18506
je 0xd26d
andb $-0x5, %al
movb %al, 0xb2c8(%rip)       # 0x18507
movzbl %dl, %eax
addw 0xb2b5(%rip), %ax       # 0x184fe
movzwl %ax, %edi
callq 0xc4e4
xorl %eax, %eax
movb %al, 0xb090(%rip)       # 0x182e9
movw $0x50, 0xb29c(%rip)     # 0x184fe
movb %al, (%rbx)
jmp 0xd26d
movb $0x0, 0xb29a(%rip)      # 0x18507
popq %rbx
popq %r14
popq %r15
retq