// File: /workspace/github_cpp_makefile_star2_O3/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 0x4295
testl %eax, %eax
je 0x507f
movq %rbx, %rdi
movl $0x11, %esi
xorl %edx, %edx
callq 0x48d8
testl %eax, %eax
je 0x507d
movl $0x2000, %eax           # imm = 0x2000
xorl 0x188(%rbx), %eax
movl (%r14), %esi
movl %esi, %ecx
xorl $0x5d000, %ecx          # imm = 0x5D000
orl %eax, %ecx
jne 0x4fc7
leaq 0x6aea(%rip), %rdi      # 0xba90
callq 0x2270
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x4a6c
testl %eax, %eax
je 0x507f
movl (%r14), %esi
movl 0x4(%r14), %edx
movq %rbx, %rdi
callq 0x4a6c
testl %eax, %eax
je 0x507d
leaq 0x6ac3(%rip), %rdi      # 0xbaa5
callq 0x2270
movq 0x18(%r14), %rsi
movl (%r14), %edx
movl 0x4(%r14), %ecx
movq %rbx, %rdi
callq 0x4aa1
movl $0x0, %ebp
testl %eax, %eax
je 0x507f
leaq 0x6ab2(%rip), %rdi      # 0xbabc
callq 0x2270
movq 0x18(%r14), %rsi
movl 0x4(%r14), %ecx
xorl %edi, %edi
xorl %edx, %edx
callq 0x88e6
movzwl %ax, %ebp
movl (%r14), %edx
movl 0x4(%r14), %ecx
leaq 0x4(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
xorl %r8d, %r8d
callq 0x47da
movl (%r15), %esi
cmpl %ebp, %esi
jne 0x5060
movl %eax, %r14d
movq %rbx, %rdi
movl $0x7c, %esi
callq 0x4786
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x507f
jmp 0x507d
leaq 0x69a0(%rip), %rdi      # 0xba07
movl %ebp, %edx
xorl %eax, %eax
callq 0x2050
movq %rbx, %rdi
movl $0x7c, %esi
callq 0x4786
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq