// File: RegulusImpact[P]sadboy/src/MMU.cpp
// Function: MMU::Read(unsigned short)
std::uint8_t MMU::Read(uint16_t addr) {
    // cartridge / bios
    if (addr <= 0x7FFF) {
        if (readBios && addr < 0xFF)
            return bios[addr];

        return cart[addr];
    }
    // sram
    else if (addr >= 0xA000 && addr <= 0xBFFF)
        return sram[addr - 0xA000];
    // video ram
    else if (addr >= 0x8000 && addr <= 0x9FFF) {
        return vram[addr - 0x8000];
    }

    // writable ram
    else if (addr >= 0xC000 && addr <= 0xDFFF)
        return wram[addr - 0xC000];

    // more writable ram
    else if (addr >= 0xE000 && addr <= 0xFDFF)
        return wram[addr - 0xE000];

    // oam
    else if (addr >= 0xFE00 && addr <= 0xFEFF)
        return oam[addr - 0xFE00];

    // io
    // this includes FFOO - user input joypad
    // timers
    // gpu

    else if (addr >= 0xFF80 && addr <= 0xFFFE)
        return hram[addr - 0xFF80];
    else if (addr >= 0xFF00 && addr <= 0xFF7F) {
        if (addr == 0xff00) return 1;
        return io[(uint8_t)(addr - 0xFF00)];
    }
    else if (addr == 0xFFFF)
        return interrupt;

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movzwl -0x12(%rbp), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jg 0x33524
movq -0x20(%rbp), %rax
testb $0x1, 0xe398(%rax)
je 0x33511
movzwl -0x12(%rbp), %eax
cmpl $0xff, %eax
jge 0x33511
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
movb 0xe281(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xa000, %eax           # imm = 0xA000
jl 0x3355a
movzwl -0x12(%rbp), %eax
cmpl $0xbfff, %eax           # imm = 0xBFFF
jg 0x3355a
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xa000, %ecx           # imm = 0xA000
movslq %ecx, %rcx
movb 0x8000(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jl 0x33590
movzwl -0x12(%rbp), %eax
cmpl $0x9fff, %eax           # imm = 0x9FFF
jg 0x33590
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0x8000, %ecx           # imm = 0x8000
movslq %ecx, %rcx
movb 0xa100(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xc000, %eax           # imm = 0xC000
jl 0x335c6
movzwl -0x12(%rbp), %eax
cmpl $0xdfff, %eax           # imm = 0xDFFF
jg 0x335c6
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xc000, %ecx           # imm = 0xC000
movslq %ecx, %rcx
movb 0xc200(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xe000, %eax           # imm = 0xE000
jl 0x335fc
movzwl -0x12(%rbp), %eax
cmpl $0xfdff, %eax           # imm = 0xFDFF
jg 0x335fc
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xe000, %ecx           # imm = 0xE000
movslq %ecx, %rcx
movb 0xc200(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xfe00, %eax           # imm = 0xFE00
jl 0x33632
movzwl -0x12(%rbp), %eax
cmpl $0xfeff, %eax           # imm = 0xFEFF
jg 0x33632
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xfe00, %ecx           # imm = 0xFE00
movslq %ecx, %rcx
movb 0xc100(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xff80, %eax           # imm = 0xFF80
jl 0x33665
movzwl -0x12(%rbp), %eax
cmpl $0xfffe, %eax           # imm = 0xFFFE
jg 0x33665
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xff80, %ecx           # imm = 0xFF80
movslq %ecx, %rcx
movb 0xe200(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
jl 0x336a9
movzwl -0x12(%rbp), %eax
cmpl $0xff7f, %eax           # imm = 0xFF7F
jg 0x336a9
movzwl -0x12(%rbp), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
jne 0x3368c
movb $0x1, -0x1(%rbp)
jmp 0x336d7
movq -0x20(%rbp), %rax
movzwl -0x12(%rbp), %ecx
subl $0xff00, %ecx           # imm = 0xFF00
movzbl %cl, %ecx
movb 0xa000(%rax,%rcx), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
movzwl -0x12(%rbp), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x336c3
movq -0x20(%rbp), %rax
movb 0xe280(%rax), %al
movb %al, -0x1(%rbp)
jmp 0x336d7
jmp 0x336c5
jmp 0x336c7
jmp 0x336c9
jmp 0x336cb
jmp 0x336cd
jmp 0x336cf
jmp 0x336d1
jmp 0x336d3
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
popq %rbp
retq
nopl (%rax)