// File: /workspace/github_c_makefile_star2_O0/baines[P]MiniGBS/minigbs.c
// Function: mem_read
static inline uint8_t mem_read(uint16_t addr){
	uint8_t val = mem[addr];

	static uint8_t ortab[] = {
		0x80, 0x3f, 0x00, 0xff, 0xbf,
		0xff, 0x3f, 0x00, 0xff, 0xbf,
		0x7f, 0xff, 0x9f, 0xff, 0xbf,
		0xff, 0xff, 0x00, 0x00, 0xbf,
		0x00, 0x00, 0x70
	};

	if(addr >= 0xFF10 && addr <= 0xFF26){
		val |= ortab[addr - 0xFF10];
	}

	if(cfg.debug_mode){
		switch(addr){
			case 0xFF10 ... 0xFF26: {
				int i = (addr - 0xFF10)/5;
				int j = (addr - 0xFF10)%5;
				debug_msg("Audio read : %4x / NR%1d%1d -> %2x", addr, i+1, j, val);
				break;
			}
			case 0xFF27 ... 0xFF40:
				debug_msg("Audio read : %4x -> %2x", addr, val);
				break;
			case 0xFF04: debug_msg("DIV read: %2x", val); break;
			case 0xFF05: debug_msg("TIMA read: %2x", val); break;
			case 0xFF06: debug_msg("TMA read: %2x", val); break;
			case 0xFF07: debug_msg("TAC read: %2x", val); break;
			case 0xFF0F: debug_msg("IF read: %2x", val); break;
			case 0xFF41: debug_msg("STAT read: %2x", val); break;
			case 0xFF46: debug_msg("DMA read: %2x", val); break;
			case 0xFFFF: debug_msg("IE read: %2x", val); break;
		}
	}

	return val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movw %di, %ax
movw %ax, -0x2(%rbp)
movq 0xca5a(%rip), %rax      # 0x16910
movzwl -0x2(%rbp), %ecx
movb (%rax,%rcx), %al
movb %al, -0x3(%rbp)
movzwl -0x2(%rbp), %eax
cmpl $0xff10, %eax           # imm = 0xFF10
jl 0x9ef6
movzwl -0x2(%rbp), %eax
cmpl $0xff26, %eax           # imm = 0xFF26
jg 0x9ef6
movzwl -0x2(%rbp), %eax
subl $0xff10, %eax           # imm = 0xFF10
movslq %eax, %rcx
leaq 0xbe07(%rip), %rax      # 0x15cf0
movzbl (%rax,%rcx), %ecx
movzbl -0x3(%rbp), %eax
orl %ecx, %eax
movb %al, -0x3(%rbp)
cmpl $0x0, 0xca1b(%rip)      # 0x16918
je 0xa03c
movzwl -0x2(%rbp), %eax
addl $0xffff00fc, %eax       # imm = 0xFFFF00FC
movl %eax, %ecx
movq %rcx, -0x18(%rbp)
subl $0xfb, %eax
ja 0xa03a
movq -0x18(%rbp), %rax
leaq 0x6160(%rip), %rcx      # 0x10088
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl -0x2(%rbp), %eax
subl $0xff10, %eax           # imm = 0xFF10
movl $0x5, %ecx
cltd
idivl %ecx
movl %eax, -0x8(%rbp)
movzwl -0x2(%rbp), %eax
subl $0xff10, %eax           # imm = 0xFF10
movl $0x5, %ecx
cltd
idivl %ecx
movl %edx, -0xc(%rbp)
movzwl -0x2(%rbp), %esi
movl -0x8(%rbp), %edx
addl $0x1, %edx
movl -0xc(%rbp), %ecx
movzbl -0x3(%rbp), %r8d
leaq 0x67ea(%rip), %rdi      # 0x1075c
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzwl -0x2(%rbp), %esi
movzbl -0x3(%rbp), %edx
leaq 0x67f2(%rip), %rdi      # 0x1077f
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67f3(%rip), %rdi      # 0x10797
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67ea(%rip), %rdi      # 0x107a5
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67e5(%rip), %rdi      # 0x107b4
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67df(%rip), %rdi      # 0x107c2
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67d9(%rip), %rdi      # 0x107d0
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67d2(%rip), %rdi      # 0x107dd
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67cd(%rip), %rdi      # 0x107ec
movb $0x0, %al
callq 0xacd0
jmp 0xa03a
movzbl -0x3(%rbp), %esi
leaq 0x67c7(%rip), %rdi      # 0x107fa
movb $0x0, %al
callq 0xacd0
jmp 0xa03c
movb -0x3(%rbp), %al
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)