// File: /workspace/github_cpp_makefile_star2_O2/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 %r14
pushq %rbx
pushq %rax
movl %esi, %ebx
leaq 0x6(%rsp), %rsi
movw %di, (%rsi)
leaq 0xab54(%rip), %rdi      # 0x16528
callq 0xbec0
leaq 0xab50(%rip), %rcx      # 0x16530
cmpq %rcx, %rax
je 0xba01
movzbl %bl, %ebx
movl %ebx, %edi
callq *0x30(%rax)
movzwl 0x6(%rsp), %edi
movl %ebx, %esi
callq 0xc2ed
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0xab11(%rip), %r14      # 0x16519
cmpb $0x1, (%r14)
jne 0xba36
movq 0xa5bb(%rip), %rdi      # 0x15fd0
leaq 0x4726(%rip), %rsi      # 0x10142
callq 0x3260
movzwl 0x6(%rsp), %esi
movq %rax, %rdi
callq 0xb2b0
movq %rax, %rdi
callq 0x3130
movzwl 0x6(%rsp), %eax
movzwl %ax, %edi
leal -0xff00(%rdi), %ecx
cmpl $0x4, %ecx
ja 0xba79
leaq 0x46cc(%rip), %rax      # 0x1011c
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl %ebx, %eax
andb $0x20, %al
movb %al, 0xaafc(%rip)       # 0x1655f
movzbl %bl, %esi
andb $0x10, %bl
movb %bl, 0xaaf1(%rip)       # 0x16560
movl $0xff00, %edi           # imm = 0xFF00
jmp 0xb9f4
cmpl $0xff0f, %edi           # imm = 0xFF0F
je 0xbaa5
cmpl $0xffff, %edi           # imm = 0xFFFF
jne 0xbace
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xb280
jmp 0xb9ed
movzbl %bl, %esi
movl $0xff04, %edi           # imm = 0xFF04
jmp 0xb9f4
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xb288
jmp 0xb9ed
movzbl %bl, %esi
movl $0xff02, %edi           # imm = 0xFF02
jmp 0xb9f4
movzbl %bl, %esi
movl $0xff03, %edi           # imm = 0xFF03
jmp 0xb9f4
cmpw $0xff01, %ax            # imm = 0xFF01
jb 0xbadc
movzbl %bl, %esi
callq 0xc2ed
cmpb $0x1, (%r14)
jne 0xb9f9
movq 0xa4e3(%rip), %rdi      # 0x15fd0
leaq 0x465e(%rip), %rsi      # 0x10152
callq 0x3260
movzwl 0x6(%rsp), %esi
movq %rax, %rdi
callq 0xb2b0
movq %rax, %rdi
callq 0x3130
jmp 0xb9f9
movzbl %bl, %esi
movl $0xff01, %edi           # imm = 0xFF01
jmp 0xb9f4