// File: /workspace/github_c_makefile_star2_O2/ughman[P]c2c/src/emu.c
// Function: EMU_Write16
void EMU_Write16(uint32_t address,uint16_t value)
{
	if (address & 1)
		SDL_LogError(LOG_EMU,"Unaligned halfword store");
	if (address >= 0x80000000 && address <= 0x801FFFFF)
	{
normal:
		{
			uint32_t (*codehandler)(uint32_t) = EMU_codemap[(address & 0x1FFFFF) >> 2];
			if (codehandler != EMU_ExecuteUnrecognizedCode)
			{
				SDL_LogError(LOG_EMU,"Writing over code");
			}
		}
		uint32_t *word = &EMU_ram[(address & 0x1FFFFF) >> 2];
		*word &= ~(0xFFFF << (address & 2) * 8);
		*word |= value << (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)
	{
		uint32_t *word = &EMU_scratch[(address & 0x3FF) >> 2];
		*word &= ~(0xFFFF << (address & 2) * 8);
		*word |= value << (address & 2) * 8;
	}
	else switch (address)
	{
	case 0x1F80104A:
		PAD_SetControl(value);
		break;
	case 0x1F801070:
		IRQ_AcknowledgeBits(value);
		break;
	case 0x1F801074:
		IRQ_SetMask(value);
		break;
	case 0x1F801124:
		RCNT_SetMode(RCNT_SYSDIV8,value);
		break;
	case 0x1F801128:
		RCNT_SetTarget(RCNT_SYSDIV8,value);
		break;
	CASE_SPU_IO_PORT(0x0):
		SPU_Voice_SetVolumeLeft((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0x2):
		SPU_Voice_SetVolumeRight((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0x4):
		SPU_Voice_SetSampleRate((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0x6):
		SPU_Voice_SetStartAddress((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0x8):
		SPU_Voice_SetADSRLow((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0xA):
		SPU_Voice_SetADSRHigh((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0xC):
		SPU_Voice_SetADSRVolume((address & 0x1F0) >> 4,value);
		break;
	CASE_SPU_IO_PORT(0xE):
		SPU_Voice_SetLoopAddress((address & 0x1F0) >> 4,value);
		break;
	case 0x1F801D80:
		SPU_SetVolumeLeft(value);
		break;
	case 0x1F801D82:
		SPU_SetVolumeRight(value);
		break;
	case 0x1F801D84:
		SPU_SetReverbVolumeLeft(value);
		break;
	case 0x1F801D86:
		SPU_SetReverbVolumeRight(value);
		break;
	case 0x1F801D88:
		SPU_NoteOnLow(value);
		break;
	case 0x1F801D8A:
		SPU_NoteOnHigh(value);
		break;
	case 0x1F801D8C:
		SPU_NoteOffLow(value);
		break;
	case 0x1F801D8E:
		SPU_NoteOffHigh(value);
		break;
	case 0x1F801D90:
		SPU_SetFMLow(value);
		break;
	case 0x1F801D92:
		SPU_SetFMHigh(value);
		break;
	case 0x1F801D94:
		SPU_SetNoiseLow(value);
		break;
	case 0x1F801D96:
		SPU_SetNoiseHigh(value);
		break;
	case 0x1F801D98:
		SPU_SetReverbLow(value);
		break;
	case 0x1F801D9A:
		SPU_SetReverbHigh(value);
		break;
	case 0x1F801D9C:
	case 0x1F801D9E:
		// nonsense write to read-only registers
		PCSX_Write16(address,value);
		break;
	case 0x1F801DA2:
		SPU_SetReverbStartAddress(value);
		break;
	case 0x1F801DA6:
		SPU_SetTransferAddress(value);
		break;
	case 0x1F801DA8:
		SPU_Write(value);
		break;
	case 0x1F801DAA:
		SPU_WriteControlRegister(value);
		break;
	case 0x1F801DAC:
		SPU_SetTransferMode(value);
		break;
	case 0x1F801DB0:
		SPU_SetCDVolumeLeft(value);
		break;
	case 0x1F801DB2:
		SPU_SetCDVolumeRight(value);
		break;
	case 0x1F801DB4:
		SPU_SetExternVolumeLeft(value);
		break;
	case 0x1F801DB6:
		SPU_SetExternVolumeRight(value);
		break;
	case 0x1F801DC0:
	case 0x1F801DC2:
	case 0x1F801DC4:
	case 0x1F801DC6:
	case 0x1F801DC8:
	case 0x1F801DCA:
	case 0x1F801DCC:
	case 0x1F801DCE:
	case 0x1F801DD0:
	case 0x1F801DD2:
	case 0x1F801DD4:
	case 0x1F801DD6:
	case 0x1F801DD8:
	case 0x1F801DDA:
	case 0x1F801DDC:
	case 0x1F801DDE:
	case 0x1F801DE0:
	case 0x1F801DE2:
	case 0x1F801DE4:
	case 0x1F801DE6:
	case 0x1F801DE8:
	case 0x1F801DEA:
	case 0x1F801DEC:
	case 0x1F801DEE:
	case 0x1F801DF0:
	case 0x1F801DF2:
	case 0x1F801DF4:
	case 0x1F801DF6:
	case 0x1F801DF8:
	case 0x1F801DFA:
	case 0x1F801DFC:
	case 0x1F801DFE:
		SPU_ConfigureReverb((address - 0x1F801DC0) / 2,value);
		break;
	default:
		SDL_LogError(LOG_EMU,"Unrecognized 16-bit write address %.8X",address);
		PCSX_Write16(address,value);
		break;
	}
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movl %esi, %ebx
movl %edi, %r14d
testb $0x1, %r14b
je 0x1359b2
leaq 0x4820(%rip), %rsi      # 0x13a1c8
pushq $0x13
popq %rdi
xorl %eax, %eax
callq 0x10d0
testl $0x7fe00000, %r14d      # imm = 0x7FE00000
setne %al
movl %r14d, %ecx
andl $0xffe00000, %ecx       # imm = 0xFFE00000
cmpl $0xa0000000, %ecx       # imm = 0xA0000000
setne %cl
testb %cl, %al
jne 0x135a31
movl %r14d, %r15d
shrl $0x2, %r15d
andl $0x7ffff, %r15d         # imm = 0x7FFFF
leaq 0x24979(%rip), %rax     # 0x15a360
leaq -0x6f6(%rip), %rcx      # 0x1352f8
cmpq %rcx, (%rax,%r15,8)
je 0x135a05
leaq 0x4795(%rip), %rsi      # 0x13a190
pushq $0x13
popq %rdi
xorl %eax, %eax
callq 0x10d0
shll $0x3, %r14d
andb $0x10, %r14b
movl $0xffff, %eax           # imm = 0xFFFF
movl %r14d, %ecx
shll %cl, %eax
movq 0x424942(%rip), %rdx    # 0x55a360
notl %eax
movzwl %bx, %esi
shll %cl, %esi
andl (%rdx,%r15,4), %eax
orl %eax, %esi
movl %esi, (%rdx,%r15,4)
jmp 0x135a70
movl %r14d, %eax
andl $0xfffffc00, %eax       # imm = 0xFFFFFC00
cmpl $0x1f800000, %eax       # imm = 0x1F800000
jne 0x135a76
movl %r14d, %eax
shll $0x3, %r14d
andb $0x10, %r14b
movl $0xffff, %edx           # imm = 0xFFFF
movl %r14d, %ecx
shll %cl, %edx
movq 0x42490c(%rip), %rsi    # 0x55a368
andl $0x3fc, %eax            # imm = 0x3FC
notl %edx
movzwl %bx, %edi
shll %cl, %edi
andl (%rsi,%rax), %edx
orl %edx, %edi
movl %edi, (%rsi,%rax)
popq %rbx
popq %r14
popq %r15
retq
leal -0x1f801c00(%r14), %eax
cmpl $0x1fe, %eax            # imm = 0x1FE
ja 0x135b72
leaq 0x3df9(%rip), %rcx      # 0x139888
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
addl $0xe07fe240, %r14d      # imm = 0xE07FE240
shrl %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136827
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136584
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1365f9
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1365ab
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13655d
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136620
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1365d2
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136536
shrl $0x4, %r14d
andl $0x1f, %r14d
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136647
cmpl $0x1f801128, %r14d      # imm = 0x1F801128
je 0x135d12
cmpl $0x1f801070, %r14d      # imm = 0x1F801070
je 0x135d32
cmpl $0x1f801074, %r14d      # imm = 0x1F801074
je 0x135d3f
cmpl $0x1f801124, %r14d      # imm = 0x1F801124
je 0x135d22
cmpl $0x1f80104a, %r14d      # imm = 0x1F80104A
jne 0x135bbc
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x123b
leaq 0x461e(%rip), %rsi      # 0x13a1e1
pushq $0x13
popq %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x10d0
leaq 0x246c9(%rip), %rax     # 0x15a2a0
movq (%rax), %rax
movzwl %bx, %esi
movl %r14d, %edi
popq %rbx
popq %r14
popq %r15
jmpq *%rax
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1366a8
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13678b
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1366ce
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136682
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13676a
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1367d2
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136757
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1364fe
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136511
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136702
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1364d8
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136695
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1366bb
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13665c
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136723
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1367f8
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1364eb
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1367e5
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1366e1
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x136736
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13679e
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13666f
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1367bf
movzwl %bx, %esi
pushq $0x2
popq %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x13468b
movzwl %bx, %esi
pushq $0x2
popq %rdi
popq %rbx
popq %r14
popq %r15
jmp 0x134676
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x1345ea
movzwl %bx, %edi
popq %rbx
popq %r14
popq %r15
jmp 0x13460b