// File: /workspace/github_cpp_makefile_star2_O1/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 0xa9d4
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 0xa9f0
movq (%rbx), %rdi
movl %edx, %esi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0xa074
cmpl $0x3fff, %edx           # imm = 0x3FFF
ja 0xaa12
movq 0x8(%rbx), %rdi
andl $0x7, %esi
orl $0x2000, %esi           # imm = 0x2000
movl 0x18(%rbx), %edx
callq 0xeca4
movzbl %al, %ebp
jmp 0xa9c7
cmpl $0x4014, %edx           # imm = 0x4014
setae %al
cmpl $0x4015, %edx           # imm = 0x4015
setne %cl
testb %cl, %al
jne 0xaa3b
movq 0x10(%rbx), %rdi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x11ba8
cmpl $0x4017, %edx           # imm = 0x4017
je 0xaa61
cmpl $0x4016, %edx           # imm = 0x4016
jne 0xab81
cmpb $0x1, 0x54(%rbx)
jne 0xab98
movl 0x70(%rbx), %ebp
jmp 0xab79
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
callq 0xfc4e
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $0x5, %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xfc4e
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 0xfc4e
movl %eax, %r15d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xfc4e
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 0xfc4e
movl %eax, %r14d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
addl $0x5, %edx
callq 0xfc4e
movl %eax, %r15d
orl %r14d, %r15d
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
movl 0x6c(%rbx), %edx
addl $-0x5, %edx
callq 0xfc4e
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 0xfc4e
movl %eax, %ebp
movq 0x8(%rbx), %rdi
movl 0x68(%rbx), %esi
addl $-0x5, %esi
movl 0x6c(%rbx), %edx
callq 0xfc4e
orl %ebp, %eax
orl %r14d, %eax
movl %eax, %ecx
movl %eax, %edx
andl $0xff0000, %edx         # imm = 0xFF0000
andl $0xff00, %ecx           # imm = 0xFF00
xorl %esi, %esi
cmpl $0x8001, %ecx           # imm = 0x8001
setb %sil
shll $0x3, %esi
xorl %ecx, %ecx
cmpl $0x800001, %edx         # imm = 0x800001
cmovael %ecx, %esi
movzbl %al, %eax
cmpl $0x81, %eax
cmovael %ecx, %esi
movl %esi, 0x64(%rbx)
cmpb $0x1, 0x54(%rbx)
jne 0xabc1
movl 0x78(%rbx), %ebp
andl $0x1, %ebp
jmp 0xa9c7
cmpl $0x6000, %edx           # imm = 0x6000
jb 0xabe7
andl $0x1fff, %esi           # imm = 0x1FFF
movq 0x38(%rbx), %rax
jmp 0xa9c3
movl 0x58(%rbx), %eax
movl $0x1, %ebp
cmpl $0x7, %eax
jg 0xa9c7
movq 0x70(%rbx), %rcx
xorl %ebp, %ebp
btq %rax, %rcx
setb %bpl
incl %eax
movl %eax, 0x58(%rbx)
jmp 0xa9c7
movl 0x5c(%rbx), %eax
cmpl $0x7, %eax
jg 0xac01
movq 0x78(%rbx), %rcx
xorl %ebp, %ebp
btq %rax, %rcx
setb %bpl
incl %eax
movl %eax, 0x5c(%rbx)
orl 0x60(%rbx), %ebp
orl 0x64(%rbx), %ebp
jmp 0xa9c7
leaq 0x9e06(%rip), %rsi      # 0x149f4
xorl %ebp, %ebp
movl $0x3, %edi
xorl %eax, %eax
callq 0xfe5a
jmp 0xa9c7
movl 0x60(%rbx), %ebp
orl 0x64(%rbx), %ebp
orl $0x1, %ebp
jmp 0xa9c7
nop