// File: /workspace/github_cpp_makefile_star2_O3/khedoros[P]khednes/mem.cpp
// Function: mem::write(unsigned int, unsigned char)
void mem::write(unsigned int address, unsigned char val) {
        address&=0xFFFF;
        written_to[address]=true;
        if(address>=0x2000&&address<0x4000) {
                //cout<<"Writing to PPU "<<endl;
                pu.reg_write((address&7)+0x2000,val,cycle);
        }
        else if(address<=0x1FFF) {
                ram[address&0x7FF]=val;
        }
        else if(address==0x4014) {
                for(int ii=0;ii<256;ii++) {
                        pu.dma(this->read(((int(val))<<(8))+ii),cycle);
                }
        }
        else if((address>=0x4000&&address<0x4014)||address==0x4015||address==0x4017) {
                //util::debug(ORIGIN,"TODO: unimplemented audio stuff.\n");
                snd.reg_write(frame, cycle, address,val);
        }
        else if(address==0x4016) {
                //util::debug(ORIGIN,"TODO: unimplemented joypad handling.\n");
                if((val&0x01)==1) {
                                        joy1_strobe=true;
                                        joy1_bit=0;
                                        joy2_bit=0;
                                }
                                else {
                                        joy1_strobe=false;
                                }
        }
        else if(address>=0x6000&&address<=0x7fff) {
            //std::cout<<"Writing SRAM address "<<std::hex<<(address&0x1fff)<<std::endl;
            sram[address&0x1fff]=val;
        }
        else if(address>=0x8000&&address<=0xffff) {
            if(pu.phase(cycle) == ppu::RENDER) {
                //cout<<dec<<cycle<<": 0x"<<hex<<address<<" = "<<int(val)<<dec<<endl;
            }
            cart.put_pbyte(cycle, val,address);
        }
        else {
                if(cart.isNSF()) {
                    cart.put_pbyte(cycle, val, address);
                }
                else {
                    printf("Hey! Don't try to write %02x to %04x!!\n",val,address);
                    util::debug(ORIGIN,"I don't know what writing %02x to %04x does.\n",val,address);
                }
        }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
movl %edx, %ebp
movq %rdi, %rbx
movzwl %si, %r14d
movq 0x98(%rdi), %rax
movl $0x1, (%rax,%r14,4)
movl %esi, %eax
andl $0xe000, %eax           # imm = 0xE000
cmpl $0x2000, %eax           # imm = 0x2000
jne 0xab92
movq 0x8(%rbx), %rdi
andl $0x2007, %esi           # imm = 0x2007
movl 0x18(%rbx), %ecx
movzbl %bpl, %edx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe162
cmpl $0x1fff, %r14d          # imm = 0x1FFF
ja 0xabab
andl $0x7ff, %esi            # imm = 0x7FF
movq 0x20(%rbx), %rax
movb %bpl, (%rax,%rsi)
jmp 0xabe9
cmpl $0x4014, %r14d          # imm = 0x4014
jne 0xabf2
movzbl %bpl, %r15d
shll $0x8, %r15d
xorl %r12d, %r12d
movq 0x8(%rbx), %r14
leal (%r15,%r12), %esi
movq %rbx, %rdi
callq 0xa836
movl 0x18(%rbx), %edx
movzbl %al, %esi
movq %r14, %rdi
callq 0xe148
incl %r12d
cmpl $0x100, %r12d           # imm = 0x100
jne 0xabbf
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leal -0x4000(%r14), %ecx
cmpl $0x14, %ecx
setae %cl
movl %esi, %edx
andl $0xfffd, %edx           # imm = 0xFFFD
cmpl $0x4015, %edx           # imm = 0x4015
setne %dl
testb %cl, %dl
jne 0xac32
movq 0x10(%rbx), %rdi
movl 0x50(%rbx), %esi
movl 0x18(%rbx), %edx
movzbl %bpl, %r8d
movl %r14d, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x11a4a
cmpl $0x4016, %r14d          # imm = 0x4016
jne 0xac47
testb $0x1, %bpl
jne 0xac5d
movb $0x0, 0x54(%rbx)
jmp 0xabe9
cmpl $0x6000, %eax           # imm = 0x6000
jne 0xac6e
andl $0x1fff, %esi           # imm = 0x1FFF
movq 0x38(%rbx), %rax
jmp 0xaba5
movb $0x1, 0x54(%rbx)
movq $0x0, 0x58(%rbx)
jmp 0xabe9
cmpl $0x8000, %r14d          # imm = 0x8000
jb 0xac85
movq 0x8(%rbx), %rdi
movl 0x18(%rbx), %esi
callq 0xe8a6
jmp 0xac91
movq (%rbx), %rdi
callq 0x9f72
testb %al, %al
je 0xacab
movq (%rbx), %rdi
movl 0x18(%rbx), %esi
movzbl %bpl, %edx
movl %r14d, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xa04a
movzbl %bpl, %ebx
leaq 0x9d9b(%rip), %rdi      # 0x14a51
movl %ebx, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x4050
leaq 0x9db0(%rip), %rsi      # 0x14a79
movl $0x3, %edi
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xfdca