// File: /workspace/github_cpp_makefile_star2_O1/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 0x4271
testl %eax, %eax
je 0x5097
movq %rbx, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq 0x48e2
testl %eax, %eax
je 0x5075
cmpl $0x2000, 0x188(%rbx)    # imm = 0x2000
jne 0x4ff1
cmpl $0x5d000, (%r14)        # imm = 0x5D000
jne 0x4ff1
leaq 0x6abd(%rip), %rdi      # 0xba90
callq 0x2270
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x4a76
testl %eax, %eax
je 0x5097
movl (%r14), %esi
movl 0x4(%r14), %edx
movq %rbx, %rdi
callq 0x4a76
testl %eax, %eax
je 0x5075
leaq 0x6a9a(%rip), %rdi      # 0xbaa5
callq 0x2270
movq 0x18(%r14), %rsi
movl (%r14), %edx
movl 0x4(%r14), %ecx
movq %rbx, %rdi
callq 0x4aab
movl $0x0, %ebp
testl %eax, %eax
je 0x5097
leaq 0x6a89(%rip), %rdi      # 0xbabc
callq 0x2270
movq 0x18(%r14), %rsi
movl 0x4(%r14), %ecx
xorl %ebp, %ebp
xorl %edi, %edi
xorl %edx, %edx
callq 0x8966
movzwl %ax, %r15d
movl (%r14), %edx
movl 0x4(%r14), %ecx
leaq 0x4(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x47dd
movl (%r14), %esi
cmpl %r15d, %esi
jne 0x5079
movl %eax, %ebp
jmp 0x508a
xorl %ebp, %ebp
jmp 0x5097
leaq 0x6987(%rip), %rdi      # 0xba07
movl %r15d, %edx
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
movl $0x7c, %esi
callq 0x4789
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq