// File: 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 %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movb %dl, %al
movb %sil, %cl
movb %dil, %dl
movb %dl, -0x1(%rbp)
andb $0x1, %cl
movb %cl, -0x2(%rbp)
andb $0x1, %al
movb %al, -0x3(%rbp)
movzbl -0x1(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x14d98
testb $0x1, -0x2(%rbp)
je 0x14cfd
movb $0x1, 0x1a8aa(%rip)     # 0x2f5a5
jmp 0x14d04
movb $0x1, 0x1a8a0(%rip)     # 0x2f5a4
testb $0x1, -0x2(%rbp)
je 0x14d13
movb -0x1(%rbp), %al
movb %al, 0x1a894(%rip)      # 0x2f5a7
testb $0x1, -0x2(%rbp)
je 0x14d91
leaq 0x3a999(%rip), %rax     # 0x4f6b9
testb $0x1, (%rax)
je 0x14d91
movzbl 0x1a87a(%rip), %eax     # 0x2f5a6
andl $0x1, %eax
cmpl $0x0, %eax
je 0x14d91
movzbl 0x1a86c(%rip), %eax     # 0x2f5a7
andl $-0x2, %eax
movb %al, 0x1a863(%rip)      # 0x2f5a7
testb $0x1, -0x3(%rbp)
je 0x14d5e
movzwl 0x1a84d(%rip), %eax     # 0x2f59e
addl $0x1, %eax
movzwl %ax, %edi
callq 0x14360
jmp 0x14d6a
movzwl 0x1a839(%rip), %edi     # 0x2f59e
callq 0x14360
testb $0x1, 0x1a618(%rip)     # 0x2f389
je 0x14d75
jmp 0x14d75
movb $0x0, 0x1a60d(%rip)     # 0x2f389
movw $0x40, 0x1a819(%rip)    # 0x2f59e
leaq 0x3a92d(%rip), %rax     # 0x4f6b9
movb $0x0, (%rax)
jmp 0x14d93
jmp 0x14d93
jmp 0x14f3e
movzbl -0x1(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x14e3c
movzbl -0x1(%rbp), %ecx
movzbl 0x1a7f4(%rip), %eax     # 0x2f5a7
orl %ecx, %eax
movb %al, 0x1a7ec(%rip)      # 0x2f5a7
testb $0x1, -0x2(%rbp)
je 0x14e37
leaq 0x3a8f1(%rip), %rax     # 0x4f6b9
testb $0x1, (%rax)
je 0x14e37
movzbl 0x1a7d2(%rip), %eax     # 0x2f5a6
andl $0x2, %eax
cmpl $0x0, %eax
je 0x14e37
movzbl 0x1a7c4(%rip), %eax     # 0x2f5a7
andl $-0x3, %eax
movb %al, 0x1a7bb(%rip)      # 0x2f5a7
testb $0x1, -0x3(%rbp)
je 0x14e06
movzwl 0x1a7a5(%rip), %eax     # 0x2f59e
addl $0x1, %eax
movzwl %ax, %edi
callq 0x14360
jmp 0x14e12
movzwl 0x1a791(%rip), %edi     # 0x2f59e
callq 0x14360
testb $0x1, 0x1a570(%rip)     # 0x2f389
je 0x14e1d
jmp 0x14e1d
movb $0x0, 0x1a565(%rip)     # 0x2f389
movw $0x48, 0x1a771(%rip)    # 0x2f59e
leaq 0x3a885(%rip), %rax     # 0x4f6b9
movb $0x0, (%rax)
jmp 0x14f3c
movzbl -0x1(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x14edd
movzbl -0x1(%rbp), %ecx
movzbl 0x1a750(%rip), %eax     # 0x2f5a7
orl %ecx, %eax
movb %al, 0x1a748(%rip)      # 0x2f5a7
testb $0x1, -0x2(%rbp)
je 0x14edb
leaq 0x3a84d(%rip), %rax     # 0x4f6b9
testb $0x1, (%rax)
je 0x14edb
movzbl 0x1a72e(%rip), %eax     # 0x2f5a6
andl $0x4, %eax
cmpl $0x0, %eax
je 0x14edb
movzbl 0x1a720(%rip), %eax     # 0x2f5a7
andl $-0x5, %eax
movb %al, 0x1a717(%rip)      # 0x2f5a7
testb $0x1, -0x3(%rbp)
je 0x14eaa
movzwl 0x1a701(%rip), %eax     # 0x2f59e
addl $0x1, %eax
movzwl %ax, %edi
callq 0x14360
jmp 0x14eb6
movzwl 0x1a6ed(%rip), %edi     # 0x2f59e
callq 0x14360
testb $0x1, 0x1a4cc(%rip)     # 0x2f389
je 0x14ec1
jmp 0x14ec1
movb $0x0, 0x1a4c1(%rip)     # 0x2f389
movw $0x50, 0x1a6cd(%rip)    # 0x2f59e
leaq 0x3a7e1(%rip), %rax     # 0x4f6b9
movb $0x0, (%rax)
jmp 0x14f3a
cmpb $0x0, -0x1(%rbp)
je 0x14f2f
movq 0x1a0e6(%rip), %rdi     # 0x2efd0
leaq 0xf221(%rip), %rsi      # 0x24112
callq 0x32c0
movq %rax, -0x10(%rbp)
leaq -0x4(%rbp), %rdi
movzbl -0x1(%rbp), %esi
callq 0x13860
movq -0x10(%rbp), %rdi
movzbl -0x4(%rbp), %esi
callq 0x13720
movq %rax, %rdi
movq 0x1a092(%rip), %rsi     # 0x2efb0
callq 0x3310
movl $0xa, %edi
callq 0x3210
jmp 0x14f38
movb -0x1(%rbp), %al
movb %al, 0x1a66f(%rip)      # 0x2f5a7
jmp 0x14f3a
jmp 0x14f3c
jmp 0x14f3e
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)