// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %esi, %ebx
movl %edi, %ebp
movq 0xaca5(%rip), %rcx      # 0x18558
testq %rcx, %rcx
je 0xd8ec
leaq 0xac91(%rip), %rax      # 0x18550
xorl %edx, %edx
cmpw %bp, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0xd8bf
leaq 0xac73(%rip), %rcx      # 0x18550
cmpq %rcx, %rax
je 0xd8ec
cmpw %bp, 0x20(%rax)
jbe 0xda1a
leaq 0xac46(%rip), %r12      # 0x18539
cmpb $0x1, (%r12)
jne 0xd9b7
movq 0xa6cb(%rip), %r14      # 0x17fd0
leaq 0x5836(%rip), %rsi      # 0x13142
movl $0xf, %edx
movq %r14, %rdi
callq 0x3300
movq (%r14), %rax
movq -0x18(%rax), %rcx
movl 0x18(%r14,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%r14,%rcx)
movq -0x18(%rax), %rcx
leaq (%r14,%rcx), %r15
cmpb $0x0, 0xe1(%r14,%rcx)
jne 0xd95b
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r15)
movq (%r14), %rax
movb $0x30, 0xe0(%r15)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%r14,%rax)
movzwl %bp, %esi
movq %r14, %rdi
callq 0x3190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x31a0
movzwl %bp, %r14d
leal -0xff00(%r14), %eax
cmpl $0x4, %eax
ja 0xd9f7
leaq 0x574e(%rip), %rcx      # 0x1311c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl %ebx, %eax
andb $0x20, %al
movb %al, 0xab9e(%rip)       # 0x1857f
movzbl %bl, %esi
andb $0x10, %bl
movb %bl, 0xab93(%rip)       # 0x18580
movl $0xff00, %edi           # imm = 0xFF00
jmp 0xdb49
cmpl $0xff0f, %r14d          # imm = 0xFF0F
je 0xda34
cmpl $0xffff, %r14d          # imm = 0xFFFF
jne 0xda64
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xd1bf
movl $0xffff, %edi           # imm = 0xFFFF
jmp 0xda43
movzbl %bl, %ebx
movl %ebx, %edi
callq *0x30(%rax)
movzwl %bp, %edi
jmp 0xda43
movzbl %bl, %esi
movl $0xff04, %edi           # imm = 0xFF04
jmp 0xdb49
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xd1c7
movl $0xff0f, %edi           # imm = 0xFF0F
movl %ebx, %esi
jmp 0xdb49
movzbl %bl, %esi
movl $0xff02, %edi           # imm = 0xFF02
jmp 0xdb49
movzbl %bl, %esi
movl $0xff03, %edi           # imm = 0xFF03
jmp 0xdb49
cmpl $0xff01, %r14d          # imm = 0xFF01
jb 0xda78
movzbl %bl, %esi
movl %r14d, %edi
callq 0xe389
cmpb $0x1, (%r12)
jne 0xdb56
movq 0xa546(%rip), %rbx      # 0x17fd0
leaq 0x56c1(%rip), %rsi      # 0x13152
movl $0x12, %edx
movq %rbx, %rdi
callq 0x3300
movq (%rbx), %rax
movq -0x18(%rax), %rcx
movl 0x18(%rbx,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rbx,%rcx)
movq -0x18(%rax), %rcx
leaq (%rbx,%rcx), %r15
cmpb $0x0, 0xe1(%rbx,%rcx)
jne 0xdadd
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb $0x1, 0xe1(%r15)
movq (%rbx), %rax
movb $0x30, 0xe0(%r15)
movq -0x18(%rax), %rax
movq $0x4, 0x10(%rbx,%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3190
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x31a0
movzbl %bl, %esi
movl $0xff01, %edi           # imm = 0xFF01
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe389
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop