// File: /workspace/github_c_makefile_star2_O1/ughman[P]c2c/src/emu.c
// Function: EMU_ReadU16
uint16_t EMU_ReadU16(uint32_t address)
{
	if (address & 1)
		SDL_LogError(LOG_EMU,"Unaligned halfword load");
	if (address >= 0x80000000 && address <= 0x801FFFFF)
	{
normal:
		{
			uint32_t (*codehandler)(uint32_t) = EMU_codemap[(address & 0x1FFFFF) >> 2];
			if (codehandler != EMU_ExecuteUnrecognizedCode)
			{
				SDL_LogError(LOG_EMU,"Reading code as data");
			}
		}
		return EMU_ram[(address & 0x1FFFFF) >> 2] >> (address & 2) * 8;
	}
	else if (address >= 0x0 && address <= 0x1FFFFF)
	{
		goto normal;
	}
	else if (address >= 0xA0000000 && address <= 0xA01FFFFF)
	{
		goto normal;
	}
	else if (address >= 0x1F800000 && address <= 0x1F8003FF)
	{
		return EMU_scratch[(address & 0x3FF) >> 2] >> (address & 2) * 8;
	}
	else switch (address)
	{
	case 0x1F801070:
		return IRQ_GetStatus();
	case 0x1F801074:
		return IRQ_GetMask();
	CASE_SPU_IO_PORT(0x0):
		return SPU_Voice_GetVolumeLeft((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0x2):
		return SPU_Voice_GetVolumeRight((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0x4):
		return SPU_Voice_GetSampleRate((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0x6):
		return SPU_Voice_GetStartAddress((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0x8):
		return SPU_Voice_GetADSRLow((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0xA):
		return SPU_Voice_GetADSRHigh((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0xC):
		return SPU_Voice_GetADSRVolume((address & 0x1F0) >> 4);
	CASE_SPU_IO_PORT(0xE):
		return SPU_Voice_GetLoopAddress((address & 0x1F0) >> 4);
	case 0x1F801D98:
		return SPU_GetReverbLow();
	case 0x1F801D9A:
		return SPU_GetReverbHigh();
	case 0x1F801DA6:
		return SPU_GetTransferAddress();
	case 0x1F801DAA:
		return SPU_ReadControlRegister();
	case 0x1F801DAE:
		return SPU_ReadStatusRegister();
	case 0x1F801DB8:
		return SPU_GetCurrentVolumeLeft();
	case 0x1F801DBA:
		return SPU_GetCurrentVolumeRight();
	case 0x1F801E00:
	case 0x1F801E04:
	case 0x1F801E08:
	case 0x1F801E0C:
	case 0x1F801E10:
	case 0x1F801E14:
	case 0x1F801E18:
	case 0x1F801E1C:
	case 0x1F801E20:
	case 0x1F801E24:
	case 0x1F801E28:
	case 0x1F801E2C:
	case 0x1F801E30:
	case 0x1F801E34:
	case 0x1F801E38:
	case 0x1F801E3C:
	case 0x1F801E40:
	case 0x1F801E44:
	case 0x1F801E48:
	case 0x1F801E4C:
	case 0x1F801E50:
	case 0x1F801E54:
	case 0x1F801E58:
	case 0x1F801E5C:
		return SPU_Voice_GetCurrentVolumeLeft((address & 0x7C) >> 2);
	case 0x1F801E02:
	case 0x1F801E06:
	case 0x1F801E0A:
	case 0x1F801E0E:
	case 0x1F801E12:
	case 0x1F801E16:
	case 0x1F801E1A:
	case 0x1F801E1E:
	case 0x1F801E22:
	case 0x1F801E26:
	case 0x1F801E2A:
	case 0x1F801E2E:
	case 0x1F801E32:
	case 0x1F801E36:
	case 0x1F801E3A:
	case 0x1F801E3E:
	case 0x1F801E42:
	case 0x1F801E46:
	case 0x1F801E4A:
	case 0x1F801E4E:
	case 0x1F801E52:
	case 0x1F801E56:
	case 0x1F801E5A:
	case 0x1F801E5E:
		return SPU_Voice_GetCurrentVolumeRight((address & 0x7C) >> 2);
	default:
		SDL_LogError(LOG_EMU,"Unrecognized 16-bit read address %.8X",address);
		return PCSX_Read16(address);
	}
}

// Assembly:
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %ebx
testb $0x1, %bl
je 0x161fb3
leaq 0x4a8d(%rip), %rsi      # 0x166a34
movl $0x13, %edi
xorl %eax, %eax
callq 0x10d0
testl $0x7fe00000, %ebx       # imm = 0x7FE00000
setne %al
movl %ebx, %ecx
andl $0xffe00000, %ecx       # imm = 0xFFE00000
cmpl $0xa0000000, %ecx       # imm = 0xA0000000
setne %cl
testb %cl, %al
jne 0x162013
movl %ebx, %r14d
shrl $0x2, %r14d
andl $0x7ffff, %r14d         # imm = 0x7FFFF
leaq 0x937a(%rip), %rax      # 0x16b360
leaq -0x221(%rip), %rcx      # 0x161dcc
cmpq %rcx, (%rax,%r14,8)
je 0x162006
leaq 0x4a00(%rip), %rsi      # 0x1669fa
movl $0x13, %edi
xorl %eax, %eax
callq 0x10d0
movq 0x409353(%rip), %rax    # 0x56b360
movl (%rax,%r14,4), %eax
jmp 0x162033
movl %ebx, %eax
andl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0x1f800000, %eax       # imm = 0x1F800000
jne 0x162045
movq 0x409340(%rip), %rax    # 0x56b368
movl %ebx, %ecx
andl $0x3fc, %ecx            # imm = 0x3FC
movl (%rax,%rcx), %eax
shll $0x3, %ebx
andb $0x10, %bl
movl %ebx, %ecx
shrl %cl, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leal -0x1f801c00(%rbx), %eax
cmpl $0x25e, %eax            # imm = 0x25E
ja 0x16212e
leaq 0x37bb(%rip), %rcx      # 0x165818
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1630c4
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1631d5
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163187
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163112
shrl $0x2, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1633da
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163139
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1630eb
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163160
shrl $0x2, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1633ea
shrl $0x4, %ebx
andl $0x1f, %ebx
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1631ae
cmpl $0x1f801074, %ebx       # imm = 0x1F801074
je 0x1621c9
cmpl $0x1f801070, %ebx       # imm = 0x1F801070
jne 0x16214e
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x160fd0
leaq 0x48f7(%rip), %rsi      # 0x166a4c
movl $0x13, %edi
movl %ebx, %edx
xorl %eax, %eax
callq 0x10d0
leaq 0x911e(%rip), %rax      # 0x16b288
movl %ebx, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
jmpq *(%rax)
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1633ab
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x16331d
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1632e9
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1633b9
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x1632b5
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163294
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x163351
addq $0x8, %rsp
popq %rbx
popq %r14
jmp 0x160ff1