// File: rockytriton[P]dreamsys_emu_gb/src/io.cpp
// Function: dsemu::io::read(unsigned short)
byte read(ushort address) {

    HANDLER_MAP::iterator it = handlerMap.find(address);
    
    if (it != handlerMap.end()) {
        return it->second.first();
    }

    if (address == 0xFF00) {
        byte output = 0xCF;


        if (!selButtons) {
            if (startDown) {
                output &= ~(1 << 3);
            } else if (selectDown) {
                output &= ~(1 << 2);
            } else if (aDown) {
                output &= ~(1 << 0);
            }
        }

        if (!selDirs) {
            if (leftDown) {
                output &= ~(1 << 1);
            } else if (rightDown) {
                output &= ~(1 << 0);
            } else if (upDown) {
                output &= ~(1 << 2);
            } else if (downDown) {
                output &= ~(1 << 3);
            }
        }

        return output; //0xC0 | (0xF^moreBit) | (selDirs | selButtons);
    } else if (address == 0xFF01) {
        //cout << endl << "SERIAL READ: " << endl;
        //sleep(2);
        return 0;
    } else if (address == 0xFF02) {
        //cout << endl << "SERIAL IO: " << endl;
        //sleep(2);
        return 0;
    } else if (address == 0xFF03) {
        //cout << endl << "DIV IO: " << endl;
        //sleep(2);
        return 0;
    } else if (address == 0xFF04) {
        //cout << endl << "TIMA IO: " << endl;
        //sleep(2);
        return 0;
    } else if (address == 0xFF44) {
        return (byte)ppu::getCurrentLine();
        //cout << endl << "READ LCD: " << endl;
        //sleep(2);
    } else if (address == 0xFF0F) {
        return cpu::getInterruptsRequestsFlag();
    } else if (address == 0xFFFF) {
        return cpu::getInterruptsEnableFlag();
    }

    if (DEBUG) cout << "UNKNOWN IO READ: " << Short(address) << endl;
    //sleep(5);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movw %di, %ax
movw %ax, -0x4(%rbp)
leaq 0x18bf2(%rip), %rdi     # 0x2f5e8
leaq -0x4(%rbp), %rsi
callq 0x174c0
movq %rax, -0x10(%rbp)
leaq 0x18bde(%rip), %rdi     # 0x2f5e8
callq 0x17520
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x174f0
testb $0x1, %al
jne 0x16a26
jmp 0x16a3a
leaq -0x10(%rbp), %rdi
callq 0x17550
callq *0x8(%rax)
movb %al, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
jne 0x16b07
movb $-0x31, -0x19(%rbp)
cmpb $0x0, 0x18bcb(%rip)     # 0x2f61f
jne 0x16a99
testb $0x1, 0x18bbb(%rip)     # 0x2f618
je 0x16a6b
movzbl -0x19(%rbp), %eax
andl $-0x9, %eax
movb %al, -0x19(%rbp)
jmp 0x16a97
testb $0x1, 0x18ba8(%rip)     # 0x2f61a
je 0x16a80
movzbl -0x19(%rbp), %eax
andl $-0x5, %eax
movb %al, -0x19(%rbp)
jmp 0x16a95
testb $0x1, 0x18b92(%rip)     # 0x2f619
je 0x16a93
movzbl -0x19(%rbp), %eax
andl $-0x2, %eax
movb %al, -0x19(%rbp)
jmp 0x16a95
jmp 0x16a97
jmp 0x16a99
cmpb $0x0, 0x18b80(%rip)     # 0x2f620
jne 0x16afc
testb $0x1, 0x18b73(%rip)     # 0x2f61c
je 0x16ab7
movzbl -0x19(%rbp), %eax
andl $-0x3, %eax
movb %al, -0x19(%rbp)
jmp 0x16afa
testb $0x1, 0x18b5d(%rip)     # 0x2f61b
je 0x16acc
movzbl -0x19(%rbp), %eax
andl $-0x2, %eax
movb %al, -0x19(%rbp)
jmp 0x16af8
testb $0x1, 0x18b4a(%rip)     # 0x2f61d
je 0x16ae1
movzbl -0x19(%rbp), %eax
andl $-0x5, %eax
movb %al, -0x19(%rbp)
jmp 0x16af6
testb $0x1, 0x18b36(%rip)     # 0x2f61e
je 0x16af4
movzbl -0x19(%rbp), %eax
andl $-0x9, %eax
movb %al, -0x19(%rbp)
jmp 0x16af6
jmp 0x16af8
jmp 0x16afa
jmp 0x16afc
movb -0x19(%rbp), %al
movb %al, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff01, %eax           # imm = 0xFF01
jne 0x16b1b
movb $0x0, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff02, %eax           # imm = 0xFF02
jne 0x16b2f
movb $0x0, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff03, %eax           # imm = 0xFF03
jne 0x16b43
movb $0x0, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff04, %eax           # imm = 0xFF04
jne 0x16b57
movb $0x0, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff44, %eax           # imm = 0xFF44
jne 0x16b6f
callq 0x21d50
movb %al, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xff0f, %eax           # imm = 0xFF0F
jne 0x16b84
callq 0x14f60
movb %al, -0x1(%rbp)
jmp 0x16bf7
movzwl -0x4(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x16b99
callq 0x14f50
movb %al, -0x1(%rbp)
jmp 0x16bf7
jmp 0x16b9b
jmp 0x16b9d
jmp 0x16b9f
jmp 0x16ba1
jmp 0x16ba3
jmp 0x16ba5
jmp 0x16ba7
leaq 0x18a2b(%rip), %rax     # 0x2f5d9
testb $0x1, (%rax)
je 0x16bf3
movq 0x18416(%rip), %rdi     # 0x2efd0
leaq 0xd582(%rip), %rsi      # 0x24143
callq 0x32c0
movq %rax, -0x28(%rbp)
leaq -0x1c(%rbp), %rdi
movzwl -0x4(%rbp), %esi
callq 0x13840
movq -0x28(%rbp), %rdi
movzwl -0x1c(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
movq 0x183c2(%rip), %rsi     # 0x2efb0
callq 0x3310
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x30, %rsp
popq %rbp
retq