// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_program_chunk_verify(WdtDevice*, ChunkInfoEx*, int)
int wh_w8760_dev_program_chunk_verify(WDT_DEV* pdev, CHUNK_INFO_EX* pInputChunk, int option)
{
	int retval = 0;

	CHUNK_INFO_EX	*pChunk = pInputChunk;
	FUNC_PTR_STRUCT_DEV_OPERATION funcs;
	UINT32	calc_checksum, read_checksum;

	memset(&funcs, 0, sizeof(FUNC_PTR_STRUCT_DEV_OPERATION));
	
	retval = wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_FLASH_PROGRAM, 0, 0);
	if (!retval)
		return retval;
		
	retval = wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_UNLOCK, 0);
	if (!retval)
		return retval;
	
	if(pdev->board_info.dev_type == FW_WDT8762)
	{
		if(pChunk->chuckInfo.targetStartAddr == 0x5d000)
		{
			printf("Erase Sector 0x0000 \n");
			retval = wh_w8760_dev_flash_erase(pdev, 0x0000, 0x1000);
			if (!retval)
				return retval;
		}
	}

	retval = wh_w8760_dev_flash_erase(pdev, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	if (!retval)
		return retval;
		
	printf("Chunk program start...\n");
	retval = wh_w8760_dev_flash_write_data(pdev, pChunk->pData, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	if (!retval)
		return retval;
		
	printf("Chunk program end ...\n");

	calc_checksum = misr_for_bytes(0, (BYTE*) pChunk->pData, 0,  pChunk->chuckInfo.length);
	retval = wh_w8760_dev_flash_get_checksum(pdev, &read_checksum, pChunk->chuckInfo.targetStartAddr, pChunk->chuckInfo.length);
	
	if (read_checksum != calc_checksum) {
		printf("checksum failed : %d <> %d\n", read_checksum, calc_checksum);
		retval = 0;
	}
	
	wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_LOCK, 0);
	if (!retval)
		return retval;
	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
xorl %ebp, %ebp
movl $0x96, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3f5f
testl %eax, %eax
je 0x4ba8
pushq $0x11
popq %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x44ac
testl %eax, %eax
je 0x4b88
movl $0x2000, %eax           # imm = 0x2000
xorl 0x188(%rbx), %eax
movl (%r14), %esi
movl %esi, %ecx
xorl $0x5d000, %ecx          # imm = 0x5D000
orl %eax, %ecx
jne 0x4b0a
leaq 0x5fa7(%rip), %rdi      # 0xaa90
callq 0x2270
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x45f6
testl %eax, %eax
je 0x4ba8
movl (%r14), %esi
movl 0x4(%r14), %edx
movq %rbx, %rdi
callq 0x45f6
testl %eax, %eax
je 0x4b88
leaq 0x5f84(%rip), %rdi      # 0xaaa5
callq 0x2270
movq 0x18(%r14), %rsi
movl (%r14), %edx
movl 0x4(%r14), %ecx
movq %rbx, %rdi
callq 0x462b
movl $0x0, %ebp
testl %eax, %eax
je 0x4ba8
leaq 0x5f73(%rip), %rdi      # 0xaabc
callq 0x2270
movq 0x18(%r14), %rsi
movl 0x4(%r14), %ecx
xorl %ebp, %ebp
xorl %edi, %edi
xorl %edx, %edx
callq 0x7ddb
movzwl %ax, %r15d
movl (%r14), %edx
movl 0x4(%r14), %ecx
leaq 0x4(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4686
movl (%r14), %esi
cmpl %r15d, %esi
jne 0x4b8c
movl %eax, %ebp
jmp 0x4b9d
xorl %ebp, %ebp
jmp 0x4ba8
leaq 0x5e74(%rip), %rdi      # 0xaa07
movl %r15d, %edx
xorl %eax, %eax
callq 0x2050
pushq $0x7c
popq %rsi
movq %rbx, %rdi
callq 0x437d
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq