// File: rockytriton[P]dreamsys_emu_gb/src/io.cpp
// Function: dsemu::io::write(unsigned short, unsigned char)
void write(ushort address, byte b) {

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

    if (DEBUG) cout << " NO WRITE MAP: " << Short(address) << endl;
    //sleep(1);

    if (address == 0xFF00) {
        //cout << endl << "JOYPAD WRITE: " << Byte(b) << endl;
        //memory::write(address, b & (0x10 | 0x20));
        selButtons = b & 0x20;
        selDirs = b & 0x10;
        memory::write(address, b);
       // sleep(2);
        return;
    } else if (address == 0xFF01) {
       // cout << endl << "SERIAL WRITE: " << endl;
        //sleep(2);
        memory::write(address, b);
        return;
    } else if (address == 0xFF02) {
        //cout << endl << "SERIAL WRITE: " << endl;
        //sleep(2);
        memory::write(address, b);
        return;
    } else if (address == 0xFF03) {
       // cout << endl << "DIV WRITE: " << endl;
       // sleep(2);
        memory::write(address, b);
        return;
    } else if (address == 0xFF04) {
       // cout << endl << "TIMA WRITE: " << endl;
        //sleep(2);
        memory::write(address, b);
        return;
    } else if (address == 0xFF0F) {
        cpu::setInterruptsRequestsFlag(b);
        memory::write(address, b);
        return;
    } else if (address == 0xFFFF) {
        cpu::setInterruptsEnableFlag(b);
        memory::write(address, b);
        return;
    }

    if (address > 0xFF00) {
        memory::write(address, b);
    }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movb %sil, %al
movw %di, %cx
movw %cx, -0x2(%rbp)
movb %al, -0x3(%rbp)
leaq 0x189cc(%rip), %rdi     # 0x2f5e8
leaq -0x2(%rbp), %rsi
callq 0x174c0
movq %rax, -0x10(%rbp)
leaq 0x189b8(%rip), %rdi     # 0x2f5e8
callq 0x17520
movq %rax, -0x18(%rbp)
leaq -0x10(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x174f0
testb $0x1, %al
jne 0x16c4c
jmp 0x16c74
leaq -0x10(%rbp), %rdi
callq 0x17550
movq 0x10(%rax), %rax
movzbl -0x3(%rbp), %edi
callq *%rax
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
leaq 0x1895e(%rip), %rax     # 0x2f5d9
testb $0x1, (%rax)
je 0x16cc0
movq 0x18349(%rip), %rdi     # 0x2efd0
leaq 0xd4c7(%rip), %rsi      # 0x24155
callq 0x32c0
movq %rax, -0x28(%rbp)
leaq -0x1a(%rbp), %rdi
movzwl -0x2(%rbp), %esi
callq 0x13840
movq -0x28(%rbp), %rdi
movzwl -0x1a(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
movq 0x182f5(%rip), %rsi     # 0x2efb0
callq 0x3310
movzwl -0x2(%rbp), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
jne 0x16cfa
movzbl -0x3(%rbp), %eax
andl $0x20, %eax
movb %al, 0x18947(%rip)      # 0x2f61f
movzbl -0x3(%rbp), %eax
andl $0x10, %eax
movb %al, 0x1893b(%rip)      # 0x2f620
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xff01, %eax           # imm = 0xFF01
jne 0x16d1a
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xff02, %eax           # imm = 0xFF02
jne 0x16d3a
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xff03, %eax           # imm = 0xFF03
jne 0x16d5a
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xff04, %eax           # imm = 0xFF04
jne 0x16d7a
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xff0f, %eax           # imm = 0xFF0F
jne 0x16da3
movzbl -0x3(%rbp), %edi
callq 0x14f90
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
movzwl -0x2(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x16dc9
movzbl -0x3(%rbp), %edi
callq 0x14f70
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
jmp 0x16e3c
jmp 0x16dcb
jmp 0x16dcd
jmp 0x16dcf
jmp 0x16dd1
jmp 0x16dd3
jmp 0x16dd5
movzwl -0x2(%rbp), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
jle 0x16df0
movw -0x2(%rbp), %ax
movzwl %ax, %edi
movzbl -0x3(%rbp), %esi
callq 0x199f0
leaq 0x187e2(%rip), %rax     # 0x2f5d9
testb $0x1, (%rax)
je 0x16e3c
movq 0x181cd(%rip), %rdi     # 0x2efd0
leaq 0xd35b(%rip), %rsi      # 0x24165
callq 0x32c0
movq %rax, -0x30(%rbp)
leaq -0x1c(%rbp), %rdi
movzwl -0x2(%rbp), %esi
callq 0x13840
movq -0x30(%rbp), %rdi
movzwl -0x1c(%rbp), %esi
callq 0x137b0
movq %rax, %rdi
movq 0x18179(%rip), %rsi     # 0x2efb0
callq 0x3310
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)