// File: /workspace/github_cpp_makefile_star2_O2/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 %rax
leaq 0x6(%rsp), %rsi
movw %di, (%rsi)
leaq 0xac72(%rip), %rdi      # 0x16528
callq 0xbec0
leaq 0xac6e(%rip), %rcx      # 0x16530
cmpq %rcx, %rax
je 0xb8cf
callq *0x28(%rax)
jmp 0xb98f
movzwl 0x6(%rsp), %eax
leal -0xff01(%rax), %ecx
cmpl $0x4, %ecx
jb 0xb98d
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xb93e
cmpl $0xff0f, %eax           # imm = 0xFF0F
je 0xb94f
cmpl $0xff44, %eax           # imm = 0xFF44
je 0xb945
cmpl $0xff00, %eax           # imm = 0xFF00
jne 0xb959
movb $-0x31, %al
cmpb $0x0, 0xac57(%rip)      # 0x1655f
jne 0xb928
movb $-0x39, %al
cmpb $0x0, 0xac45(%rip)      # 0x16558
jne 0xb928
movb $-0x35, %al
cmpb $0x0, 0xac3c(%rip)      # 0x1655a
jne 0xb928
movb 0xac33(%rip), %al       # 0x16559
xorb $-0x31, %al
cmpb $0x0, 0xac31(%rip)      # 0x16560
jne 0xb98f
cmpb $0x1, 0xac24(%rip)      # 0x1655c
jne 0xb991
andb $-0x33, %al
jmp 0xb98f
callq 0xb272
jmp 0xb8ca
callq 0xecc2
jmp 0xb8ca
callq 0xb279
jmp 0xb8ca
leaq 0xabb9(%rip), %rax      # 0x16519
cmpb $0x1, (%rax)
jne 0xb98d
movq 0xa664(%rip), %rdi      # 0x15fd0
leaq 0x47bd(%rip), %rsi      # 0x10130
callq 0x3260
movzwl 0x6(%rsp), %esi
movq %rax, %rdi
callq 0xb2b0
movq %rax, %rdi
callq 0x3130
xorl %eax, %eax
popq %rcx
retq
cmpb $0x1, 0xabc3(%rip)      # 0x1655b
jne 0xb99e
andb $-0x32, %al
jmp 0xb98f
cmpb $0x1, 0xabb8(%rip)      # 0x1655d
jne 0xb9ab
andb $-0x35, %al
jmp 0xb98f
movzbl %al, %ecx
andb $-0x39, %al
movzbl %al, %eax
cmpb $0x0, 0xaba4(%rip)      # 0x1655e
cmovel %ecx, %eax
jmp 0xb98f