// File: /workspace/github_cpp_makefile_star2_O2/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 0x9a32
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 0xc8fa
cmpl $0x1fff, %r14d          # imm = 0x1FFF
ja 0x9a52
andl $0x7ff, %esi            # imm = 0x7FF
movq 0x20(%rbx), %rax
movb %bpl, (%rax,%rsi)
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x4014, %r14d          # imm = 0x4014
jne 0x9a92
movzbl %bpl, %r15d
shll $0x8, %r15d
xorl %r12d, %r12d
cmpl $0x100, %r12d           # imm = 0x100
je 0x9a49
movq 0x8(%rbx), %r14
leal (%r15,%r12), %esi
movq %rbx, %rdi
callq 0x96dc
movl 0x18(%rbx), %edx
movzbl %al, %esi
movq %r14, %rdi
callq 0xc8e0
incl %r12d
jmp 0x9a66
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 0x9ad2
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 0xf924
cmpl $0x4016, %r14d          # imm = 0x4016
jne 0x9aea
testb $0x1, %bpl
jne 0x9b00
movb $0x0, 0x54(%rbx)
jmp 0x9a49
cmpl $0x6000, %eax           # imm = 0x6000
jne 0x9b0e
andl $0x1fff, %esi           # imm = 0x1FFF
movq 0x38(%rbx), %rax
jmp 0x9a45
movb $0x1, 0x54(%rbx)
andq $0x0, 0x58(%rbx)
jmp 0x9a49
cmpl $0x8000, %r14d          # imm = 0x8000
jb 0x9b25
movq 0x8(%rbx), %rdi
movl 0x18(%rbx), %esi
callq 0xce48
jmp 0x9b31
movq (%rbx), %rdi
callq 0x8e62
testb %al, %al
je 0x9b4b
movq (%rbx), %rdi
movl 0x18(%rbx), %esi
movzbl %bpl, %edx
movl %r14d, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8f26
movzbl %bpl, %ebx
leaq 0x8ea7(%rip), %rdi      # 0x129fd
movl %ebx, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x4040
leaq 0x8ebc(%rip), %rsi      # 0x12a25
pushq $0x3
popq %rdi
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xe2fc