// File: mindbeast[P]rnes/mmc.cpp
// Function: Rnes::Mmc3::getChrPointer(unsigned short)
uint8_t *Mmc3::getChrPointer(uint16_t addr) {
  bool a12Invert = isChrA12Inverted();
  assert(addr <= 0x1fff);

  if (a12Invert) {
    addr ^= (1 << 12);
  }
  if (addr <= 0x7ff) {
    return get2kChrBank(bankRegister[0]) + addr;
  } else if (addr >= 0x800 and addr <= 0xfff) {
    return get2kChrBank(bankRegister[1]) + addr - 0x800;
  } else if (addr >= 0x1000 and addr <= 0x13ff) {
    return get1kChrBank(bankRegister[2]) + addr - 0x1000;
  } else if (addr >= 0x1400 and addr <= 0x17ff) {
    return get1kChrBank(bankRegister[3]) + addr - 0x1400;
  } else if (addr >= 0x1800 and addr <= 0x1bff) {
    return get1kChrBank(bankRegister[4]) + addr - 0x1800;
  } else if (addr >= 0x1c00 and addr <= 0x1fff) {
    return get1kChrBank(bankRegister[5]) + addr - 0x1c00;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x20(%rbp)
callq 0x43940
andb $0x1, %al
movb %al, -0x13(%rbp)
movzwl -0x12(%rbp), %eax
cmpl $0x1fff, %eax           # imm = 0x1FFF
jg 0x43022
jmp 0x43041
leaq 0x26e37(%rip), %rdi     # 0x69e60
leaq 0x26b47(%rip), %rsi     # 0x69b77
movl $0x19e, %edx            # imm = 0x19E
leaq 0x26e33(%rip), %rcx     # 0x69e6f
callq 0xe240
testb $0x1, -0x13(%rbp)
je 0x43054
movzwl -0x12(%rbp), %eax
xorl $0x1000, %eax           # imm = 0x1000
movw %ax, -0x12(%rbp)
movzwl -0x12(%rbp), %eax
cmpl $0x7ff, %eax            # imm = 0x7FF
jg 0x4307f
movq -0x20(%rbp), %rdi
movzbl 0x53(%rdi), %esi
callq 0x43960
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x8(%rbp)
jmp 0x431b7
movzwl -0x12(%rbp), %eax
cmpl $0x800, %eax            # imm = 0x800
jl 0x430bb
movzwl -0x12(%rbp), %eax
cmpl $0xfff, %eax            # imm = 0xFFF
jg 0x430bb
movq -0x20(%rbp), %rdi
movzbl 0x54(%rdi), %esi
callq 0x43960
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $-0x800, %rax           # imm = 0xF800
movq %rax, -0x8(%rbp)
jmp 0x431b7
movzwl -0x12(%rbp), %eax
cmpl $0x1000, %eax           # imm = 0x1000
jl 0x430f7
movzwl -0x12(%rbp), %eax
cmpl $0x13ff, %eax           # imm = 0x13FF
jg 0x430f7
movq -0x20(%rbp), %rdi
movzbl 0x55(%rdi), %esi
callq 0x42c30
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $-0x1000, %rax          # imm = 0xF000
movq %rax, -0x8(%rbp)
jmp 0x431b7
movzwl -0x12(%rbp), %eax
cmpl $0x1400, %eax           # imm = 0x1400
jl 0x43133
movzwl -0x12(%rbp), %eax
cmpl $0x17ff, %eax           # imm = 0x17FF
jg 0x43133
movq -0x20(%rbp), %rdi
movzbl 0x56(%rdi), %esi
callq 0x42c30
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $-0x1400, %rax          # imm = 0xEC00
movq %rax, -0x8(%rbp)
jmp 0x431b7
movzwl -0x12(%rbp), %eax
cmpl $0x1800, %eax           # imm = 0x1800
jl 0x4316c
movzwl -0x12(%rbp), %eax
cmpl $0x1bff, %eax           # imm = 0x1BFF
jg 0x4316c
movq -0x20(%rbp), %rdi
movzbl 0x57(%rdi), %esi
callq 0x42c30
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $-0x1800, %rax          # imm = 0xE800
movq %rax, -0x8(%rbp)
jmp 0x431b7
movzwl -0x12(%rbp), %eax
cmpl $0x1c00, %eax           # imm = 0x1C00
jl 0x431a5
movzwl -0x12(%rbp), %eax
cmpl $0x1fff, %eax           # imm = 0x1FFF
jg 0x431a5
movq -0x20(%rbp), %rdi
movzbl 0x58(%rdi), %esi
callq 0x42c30
movzwl -0x12(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
addq $-0x1c00, %rax          # imm = 0xE400
movq %rax, -0x8(%rbp)
jmp 0x431b7
jmp 0x431a7
jmp 0x431a9
jmp 0x431ab
jmp 0x431ad
jmp 0x431af
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)