// File: /workspace/github_cpp_makefile_star2_O1/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 0xabcd(%rip), %rdx      # 0x18558
leaq 0xabbe(%rip), %rax      # 0x18550
movq %rax, %rcx
testq %rdx, %rdx
je 0xd9b9
leaq 0xabaf(%rip), %rcx      # 0x18550
xorl %esi, %esi
cmpw %bp, 0x20(%rdx)
setb %sil
cmovaeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0xd9a1
movq %rax, %rdx
cmpq %rax, %rcx
je 0xd9d0
cmpw %bp, 0x20(%rcx)
leaq 0xab84(%rip), %rdx      # 0x18550
cmovbeq %rcx, %rdx
cmpq %rax, %rdx
je 0xd9e7
movzbl %bl, %ebx
movl %ebx, %edi
callq *0x30(%rdx)
movzwl %bp, %edi
movl %ebx, %esi
jmp 0xdb15
leaq 0xab4b(%rip), %r12      # 0x18539
cmpb $0x1, (%r12)
jne 0xdab9
movq 0xa5d0(%rip), %r14      # 0x17fd0
leaq 0x573b(%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), %rax
leaq (%r14,%rax), %r15
cmpb $0x0, 0xe1(%r14,%rax)
jne 0xda5a
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x30, 0xe0(%r15)
movq (%r14), %rax
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 0xdade
leaq 0x564c(%rip), %rcx      # 0x1311c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzbl %bl, %esi
jmp 0xdb12
cmpl $0xff0f, %r14d          # imm = 0xFF0F
je 0xdb22
cmpl $0xffff, %r14d          # imm = 0xFFFF
jne 0xdb34
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xd281
jmp 0xdb2c
movl %ebx, %eax
andb $0x20, %al
movb %al, 0xaa79(%rip)       # 0x1857f
movzbl %bl, %esi
andb $0x10, %bl
movb %bl, 0xaa6e(%rip)       # 0x18580
movl %r14d, %edi
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe4cf
movzbl %bl, %ebx
movl %ebx, %edi
callq 0xd289
movl %r14d, %edi
jmp 0xd9e0
cmpl $0xff01, %r14d          # imm = 0xFF01
jb 0xdb48
movzbl %bl, %esi
movl %r14d, %edi
callq 0xe4cf
cmpb $0x1, (%r12)
jne 0xdc18
movq 0xa476(%rip), %rbx      # 0x17fd0
leaq 0x55f1(%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), %rax
leaq (%rbx,%rax), %r15
cmpb $0x0, 0xe1(%rbx,%rax)
jne 0xdbb1
movq %r15, %rdi
movl $0x20, %esi
callq 0x3280
movb %al, 0xe0(%r15)
movb $0x1, 0xe1(%r15)
movb $0x30, 0xe0(%r15)
movq (%rbx), %rax
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
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop