// File: /workspace/github_cpp_makefile_star2_O1/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 0xacf6
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 0xe1f6
cmpl $0x1fff, %r14d          # imm = 0x1FFF
ja 0xad0f
andl $0x7ff, %esi            # imm = 0x7FF
movq 0x20(%rbx), %rax
movb %bpl, (%rax,%rsi)
jmp 0xad4d
cmpl $0x4014, %r14d          # imm = 0x4014
jne 0xad56
movzbl %bpl, %r15d
shll $0x8, %r15d
xorl %r12d, %r12d
movq 0x8(%rbx), %r14
leal (%r15,%r12), %esi
movq %rbx, %rdi
callq 0xa996
movl 0x18(%rbx), %edx
movzbl %al, %esi
movq %r14, %rdi
callq 0xe1dc
incl %r12d
cmpl $0x100, %r12d           # imm = 0x100
jne 0xad23
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 0xad96
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 0x11b58
cmpl $0x4016, %r14d          # imm = 0x4016
jne 0xadab
testb $0x1, %bpl
jne 0xadc1
movb $0x0, 0x54(%rbx)
jmp 0xad4d
cmpl $0x6000, %eax           # imm = 0x6000
jne 0xadd2
andl $0x1fff, %esi           # imm = 0x1FFF
movq 0x38(%rbx), %rax
jmp 0xad09
movb $0x1, 0x54(%rbx)
movq $0x0, 0x58(%rbx)
jmp 0xad4d
cmpl $0x8000, %r14d          # imm = 0x8000
jb 0xade9
movq 0x8(%rbx), %rdi
movl 0x18(%rbx), %esi
callq 0xe7f8
jmp 0xadf5
movq (%rbx), %rdi
callq 0xa084
testb %al, %al
je 0xae0f
movq (%rbx), %rdi
movl 0x18(%rbx), %esi
movzbl %bpl, %edx
movl %r14d, %ecx
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xa15c
movzbl %bpl, %ebx
leaq 0x9c5f(%rip), %rdi      # 0x14a79
movl %ebx, %esi
movl %r14d, %edx
xorl %eax, %eax
callq 0x4050
leaq 0x9c74(%rip), %rsi      # 0x14aa1
movl $0x3, %edi
movl %ebx, %edx
movl %r14d, %ecx
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0xfe5a