// File: /workspace/github_cpp_makefile_star2_O2/khedoros[P]khednes/mem.cpp
// Function: mem::read(unsigned int)
const unsigned int mem::read(unsigned int address) {
        address&=0xFFFF;
        read_from[address] = true;
        if(address<=0x1FFF) { 
                return ram[address&0x7ff];
        }
        if(address>=0x8000) {
                return cart.get_pbyte(address);
        }
        if(address>=0x2000&&address<0x4000) {
                //util::debug(ORIGIN, "Reading ppu register address %04x\n",address);
                return pu.reg_read((address&7)+0x2000, cycle);
        }
        if((address>=0x4000&&address<0x4014)||address==0x4015) {
                //util::debug(ORIGIN, "TODO: unimplemented audio stuff.\n");
                //if(address == 0x4015) cout<<"Read sound status"<<endl;
        return snd.read_status();
                //return 0;
        }
        if(address==0x4016) {//||address==0x4017) {
                //util::debug(ORIGIN, "TODO: unimplemented joypad handling.\n");
                if(joy1_strobe) {
                        //util::debug(ORIGIN,"A (strobe is on)\n");
                        //printf("Strobe is on!\n");
                        return joy1_buttons[0];
                }
                else if(joy1_bit<8) {
                        int retval=(joy1_buttons[joy1_bit])?1:0;
                        joy1_bit++;
                        return retval;
                }
                else {
                        return 1;
                }
                return 0;
        }
        else if(address==0x4017) {
                uint32_t color = pu.get_buffer_color(mouse_x, mouse_y);
                color |= pu.get_buffer_color(mouse_x+5, mouse_y+5);
                color |= pu.get_buffer_color(mouse_x+5, mouse_y-5);
                color |= pu.get_buffer_color(mouse_x-5, mouse_y+5);
                color |= pu.get_buffer_color(mouse_x-5, mouse_y-5);
                color |= pu.get_buffer_color(mouse_x, mouse_y+5);
                color |= pu.get_buffer_color(mouse_x, mouse_y-5);
                color |= pu.get_buffer_color(mouse_x-5, mouse_y);
                color |= pu.get_buffer_color(mouse_x-5, mouse_y);
                uint8_t r = color>>(16);
                uint8_t g = (color&0xFF00)>>(8);
                uint8_t b = (color&0xFF);
                if(r > 0x80 || g > 0x80 || b > 0x80) joy2_light = 0;
                else                                 joy2_light = 8;
                //cout<<"joy2_light: "<<((joy2_light)?"yes":"no")<<endl;
                if(joy1_strobe) {
                    return joy2_buttons[0];
                }
                else if(joy2_bit<8) {
                    int retval=(joy2_buttons[joy2_bit])?1:0;
                    joy2_bit++;
                    return retval|joy2_trigger|joy2_light;
                }
                else {
                    return 1|joy2_trigger|joy2_light;
                }
                return 0;
        }
        else if(address>=0x6000&&address<=0x7fff) {
            return sram[address&0x1fff];
        }
        util::debug(ORIGIN, "mem read of %04x unhandled. I don't know what the address does.\n",address);
        return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movzwl %si, %edx
movq 0x80(%rdi), %rax
movl $0x1, (%rax,%rdx,4)
cmpl $0x1fff, %edx           # imm = 0x1FFF
ja 0x971a
andl $0x7ff, %esi            # imm = 0x7FF
movq 0x20(%rbx), %rax
movzbl (%rax,%rsi), %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x8000, %edx           # imm = 0x8000
jb 0x9736
movq (%rbx), %rdi
movl %edx, %esi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x8e52
cmpl $0x3fff, %edx           # imm = 0x3FFF
ja 0x9758
movq 0x8(%rbx), %rdi
andl $0x7, %esi
orl $0x2000, %esi           # imm = 0x2000
movl 0x18(%rbx), %edx
callq 0xd268
movzbl %al, %ebp
jmp 0x970d
cmpl $0x4014, %edx           # imm = 0x4014
setae %al
cmpl $0x4015, %edx           # imm = 0x4015
setne %cl
testb %cl, %al
jne 0x9781
movq 0x10(%rbx), %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xf954
cmpl $0x4017, %edx           # imm = 0x4017
je 0x97a7
cmpl $0x4016, %edx           # imm = 0x4016
jne 0x98c6
cmpb $0x1, 0x54(%rbx)
jne 0x98dd
movl 0x70(%rbx), %ebp
jmp 0x98be
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
callq 0xdfa6
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $0x5, %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xdfa6
movl %eax, %r14d
orl %ebp, %r14d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $0x5, %esi
movl 0x6c(%rbx), %edx
addl $-0x5, %edx
callq 0xdfa6
movl %eax, %r15d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xdfa6
movl %eax, %ebp
orl %r15d, %ebp
orl %r14d, %ebp
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
addl $-0x5, %edx
callq 0xdfa6
movl %eax, %r14d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xdfa6
movl %eax, %r15d
orl %r14d, %r15d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
addl $-0x5, %edx
callq 0xdfa6
movl %eax, %r14d
orl %r15d, %r14d
orl %ebp, %r14d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
callq 0xdfa6
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
callq 0xdfa6
orl %ebp, %eax
orl %r14d, %eax
movl %eax, %edx
movl %eax, %esi
andl $0xff0000, %esi         # imm = 0xFF0000
andl $0xff00, %edx           # imm = 0xFF00
xorl %ecx, %ecx
cmpl $0x8001, %edx           # imm = 0x8001
setb %cl
shll $0x3, %ecx
xorl %edx, %edx
cmpl $0x800001, %esi         # imm = 0x800001
cmovael %edx, %ecx
movzbl %al, %eax
cmpl $0x81, %eax
cmovael %edx, %ecx
movl %ecx, 0x64(%rbx)
cmpb $0x1, 0x54(%rbx)
jne 0x98fd
movl 0x78(%rbx), %ebp
andl $0x1, %ebp
jmp 0x970d
cmpl $0x6000, %edx           # imm = 0x6000
jb 0x9922
andl $0x1fff, %esi           # imm = 0x1FFF
movq 0x38(%rbx), %rax
jmp 0x9709
movl 0x58(%rbx), %eax
cmpl $0x7, %eax
jg 0x993a
movq 0x70(%rbx), %rcx
xorl %ebp, %ebp
btq %rax, %rcx
setb %bpl
incl %eax
movl %eax, 0x58(%rbx)
jmp 0x970d
movl 0x5c(%rbx), %eax
cmpl $0x7, %eax
jg 0x9942
movq 0x78(%rbx), %rdx
xorl %ebp, %ebp
btq %rax, %rdx
setb %bpl
incl %eax
movl %eax, 0x5c(%rbx)
orl 0x60(%rbx), %ebp
orl %ecx, %ebp
jmp 0x970d
leaq 0x904f(%rip), %rsi      # 0x12978
xorl %ebp, %ebp
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0xe2fc
jmp 0x970d
pushq $0x1
popq %rbp
jmp 0x970d
orl 0x60(%rbx), %ecx
orl $0x1, %ecx
movl %ecx, %ebp
jmp 0x970d
nop