// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_send_commands(WdtDevice*, int, unsigned int)
int wh_w8760_dev_send_commands(WDT_DEV* pdev, int cmd, UINT32 value)
{
	switch(cmd)
	{
		case 	WH_CMD_ALGO_STOP: {
			return wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_COMMAND, 0, 0);
		}
		break;
		case 	WH_CMD_ALGO_START: 
		case 	WH_CMD_ALGO_RESTART: {	
			return wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_SENSING, 0, 0);
		}
		break;
		case 	WH_CMD_SET_DEV_MODE: {
			return wh_w8760_dev_set_n_check_device_mode(pdev, value, 0, 0);
		}
		break;
		case 	WH_CMD_RESET: {
			/**
 		 	 *  Hard reset doesn't make OSC back to default. So we need this workaround to set OSC to default.
                 	 * If not, the OSC clock for ROM code is too high and might cause booting fails.
                 	 */
                        if (pdev->board_info.dev_type & FW_WDT8762) {
                                wh_w8760_dev_write_register(pdev, 0x90801408, 0x1B);
                        }else if (pdev->board_info.dev_type & FW_WDT8760) {
                                wh_w8760_dev_write_register(pdev, 0x00801408, 0x28);
                        }
	
			return wh_w8760_dev_reboot(pdev);
		}
		break;
		case	WH_CMD_FLASH_ERASE4K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x1000);
		}
		break;
		case	WH_CMD_FLASH_ERASE32K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x8000);
		}
		break;
		case	WH_CMD_FLASH_ERASE64K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x10000);
		}
		break;
		case	WH_CMD_FLASH_LOCK:	
		case	WH_CMD_FLASH_PROTECTION_ON: 
		{
			return wh_w8760_dev_protect_flash(pdev, W8760_ProtectAll512k);
		}
		break;
		case	WH_CMD_FLASH_UNLOCK: 
		case	WH_CMD_FLASH_PROTECTION_OFF: 
		{
			/* address align to 0x100 */
			UINT32 addr = (value & 0xFFFF0000) >> 8;
			/*  size align to 0x100 */
			UINT32 size = (value & 0xFFFF) << 8; 	
			int ret = 0;
			
			if (addr < 128 * 1024 && addr + size <= 128 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower128k);
					else if (addr < 256 * 1024 && addr + size <= 256 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower256k);
			else if (addr < 384 * 1024 && addr + size <= 384 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower384k);
	  		else if (addr < 508 * 1024 && addr + size <= 508 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower508k);
			return ret;
		}
		break;		
 	
		default: {
			return 0;
		}
	}

	return 0;
}

// Assembly:
pushq %rbx
subq $0x10, %rsp
xorl %eax, %eax
decl %esi
cmpl $0x16, %esi
ja 0x4a3b
movq %rdi, %rbx
leaq 0x6f4c(%rip), %rcx      # 0xb840
movslq (%rcx,%rsi,4), %rsi
addq %rcx, %rsi
jmpq *%rsi
movq %rbx, %rdi
movl $0x1, %esi
jmp 0x49db
movl %edx, %esi
shrl $0x8, %esi
andl $0xffff00, %esi         # imm = 0xFFFF00
movzwl %dx, %ecx
shll $0x8, %ecx
cmpl $0x2000000, %edx        # imm = 0x2000000
setb %dil
addl %esi, %ecx
cmpl $0x20001, %ecx          # imm = 0x20001
setb %sil
andb %dil, %sil
cmpb $0x1, %sil
jne 0x496c
movq %rbx, %rdi
movl $0x4028, %esi           # imm = 0x4028
jmp 0x4a62
movq %rbx, %rdi
movl $0x7c, %esi
jmp 0x4a62
movq %rbx, %rdi
movl %edx, %esi
movl $0x10000, %edx          # imm = 0x10000
jmp 0x49cb
movq %rbx, %rdi
movl %edx, %esi
movl $0x8000, %edx           # imm = 0x8000
jmp 0x49cb
cmpl $0x4000000, %edx        # imm = 0x4000000
setb %sil
cmpl $0x40001, %ecx          # imm = 0x40001
setb %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x49e9
movq %rbx, %rdi
movl $0xc, %esi
jmp 0x4a62
movl 0x188(%rbx), %eax
btl $0xd, %eax
jb 0x4a10
btl $0xb, %eax
jae 0x4a22
movq %rbx, %rdi
movl $0x801408, %esi         # imm = 0x801408
movl $0x28, %edx
jmp 0x4a1d
movq %rbx, %rdi
movl $0x90, %esi
jmp 0x49db
movq %rbx, %rdi
movl %edx, %esi
movl $0x1000, %edx           # imm = 0x1000
addq $0x10, %rsp
popq %rbx
jmp 0x4a6c
movzbl %dl, %esi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
addq $0x10, %rsp
popq %rbx
jmp 0x4295
cmpl $0x6000000, %edx        # imm = 0x6000000
setb %sil
cmpl $0x60001, %ecx          # imm = 0x60001
setb %dil
andb %sil, %dil
cmpb $0x1, %dil
jne 0x4a41
movq %rbx, %rdi
movl $0x8, %esi
jmp 0x4a62
movq %rbx, %rdi
movl $0x90801408, %esi       # imm = 0x90801408
movl $0x1b, %edx
callq 0x4890
leaq 0x8(%rsp), %rsi
movw $0x248a, 0x4(%rsi)      # imm = 0x248A
movl $0xcb9ce06, (%rsi)      # imm = 0xCB9CE06
movq %rbx, %rdi
callq 0x3e56
addq $0x10, %rsp
popq %rbx
retq
cmpl $0x7f00000, %edx        # imm = 0x7F00000
setb %dl
cmpl $0x7f001, %ecx          # imm = 0x7F001
setb %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x4a3b
movq %rbx, %rdi
movl $0x44, %esi
addq $0x10, %rsp
popq %rbx
jmp 0x4786