// File: /workspace/github_cpp_makefile_star2_O3/chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_program_4k_chunk_verify(WdtDevice*, ChunkInfoEx*, int)
int wh_w8760_dev_program_4k_chunk_verify(WDT_DEV* pdev, CHUNK_INFO_EX* pInputChunk, int option)
{
	int retval = 0;
	int size;
	int start_addr = 0;
	int page_size;
	int retry_count = 0;
	int is_first_page = 0;
	char* pdata;
	UINT32 calc_checksum, read_checksum;
	CHUNK_INFO_EX	*pChunk = pInputChunk;
	FUNC_PTR_STRUCT_DEV_OPERATION funcs;

	memset(&funcs, 0, sizeof(FUNC_PTR_STRUCT_DEV_OPERATION));
	
	printf("start 4k chunk program ...\n");
	pdev->dev_state = DS_PROGRAM;

	retval = wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_FLASH_PROGRAM, 0, 0);
	if (!retval)
		return retval;
		
	UINT32 value = (pInputChunk->length + 255) >> 8;
	value |= (((pChunk->chuckInfo.targetStartAddr >> 8) << 16) & 0xFFFF0000);
	retval = wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_UNLOCK, value);
	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;
		}
	}

	size = pChunk->chuckInfo.length;	
	start_addr = pChunk->chuckInfo.targetStartAddr;
	pdata =	(char*) pChunk->pData;

	if (size > WDT_PAGE_SIZE) {
		is_first_page = 1;
		size = size - WDT_PAGE_SIZE;
		start_addr = start_addr + WDT_PAGE_SIZE;
		pdata = pdata + WDT_PAGE_SIZE;
	}

	while (size) {
		if (size > (int) WDT_PAGE_SIZE)	{
			page_size = WDT_PAGE_SIZE;
			size = size - WDT_PAGE_SIZE;
		} else	{
			page_size = size;
			size = 0;
		}

		for (retry_count = 0; retry_count < RETRY_COUNT; retry_count ++) {		
			retval = wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_ERASE4K, start_addr);
			
			if (!retval)
				continue;

			retval = wh_w8760_dev_flash_write_data(pdev, (BYTE*) pdata, start_addr, page_size);
			if (!retval)
				continue;

			calc_checksum = misr_for_bytes(0, (BYTE*) pdata, 0, page_size);

			retval = wh_w8760_dev_flash_get_checksum(pdev, &read_checksum, start_addr, page_size);

			if (read_checksum == calc_checksum)
				break;
			else
				printf("checksum failed (%d): %d <> %d\n", retry_count, read_checksum, calc_checksum);
		}

		if (retry_count == RETRY_COUNT)	{
			printf("***** retry failed *****\n");
			break;
		}

		start_addr = start_addr + page_size;
		pdata =	pdata + page_size;

		/* keep the first page to write to the flash in the last loop */
		if (size == 0 && is_first_page) {		
			size = WDT_PAGE_SIZE;
			start_addr = pChunk->chuckInfo.targetStartAddr;
			pdata = (char*) pChunk->pData;
			is_first_page = 0;		
		}
		
	}

	retval = wh_w8760_dev_send_commands(pdev, WH_CMD_FLASH_LOCK, 0);
	if (!retval)
		return retval;
	
	if (retry_count == RETRY_COUNT) {
		printf("stop 4k chunk program : fail \n");
		return 0;
	}

	return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x6a44(%rip), %rdi      # 0xb77d
callq 0x2270
movl $0x3, 0x278(%rbx)
xorl %ebp, %ebp
movq %rbx, %rdi
movl $0x96, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x4295
testl %eax, %eax
je 0x4f38
movl $0xff, %eax
addl 0x20(%r14), %eax
shrl $0x8, %eax
movl (%r14), %edx
shll $0x8, %edx
andl $0xffff0000, %edx       # imm = 0xFFFF0000
orl %eax, %edx
movq %rbx, %rdi
movl $0x11, %esi
callq 0x48d8
testl %eax, %eax
je 0x4f38
movl $0x2000, %eax           # imm = 0x2000
xorl 0x188(%rbx), %eax
movl (%r14), %ebp
movl %ebp, %ecx
xorl $0x5d000, %ecx          # imm = 0x5D000
orl %eax, %ecx
jne 0x4dd4
leaq 0x6cdd(%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 0x4f38
movl (%r14), %ebp
movl 0x4(%r14), %edx
movq %r14, 0x8(%rsp)
movq 0x18(%r14), %r15
cmpl $0x1001, %edx           # imm = 0x1001
jl 0x4e03
addl $0xfffff000, %edx       # imm = 0xFFFFF000
addl $0x1000, %ebp           # imm = 0x1000
addq $0x1000, %r15           # imm = 0x1000
movl $0x1, %esi
jmp 0x4e0d
xorl %esi, %esi
testl %edx, %edx
je 0x4ef6
movl $0x1000, %eax           # imm = 0x1000
xorl %ecx, %ecx
movl %esi, (%rsp)
cmpl %eax, %edx
movl $0x1000, %r13d          # imm = 0x1000
cmovll %edx, %r13d
cmpl $0x1001, %edx           # imm = 0x1001
leal -0x1000(%rdx), %eax
cmovll %ecx, %eax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
movq %rbx, %rdi
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x4a6c
testl %eax, %eax
je 0x4ea6
movq %rbx, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %r13d, %ecx
callq 0x4aa1
testl %eax, %eax
je 0x4ea6
xorl %edi, %edi
movq %r15, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x88e6
movzwl %ax, %r14d
movq %rbx, %rdi
leaq 0x4(%rsp), %rsi
movl %ebp, %edx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x47da
movl 0x4(%rsp), %edx
cmpl %r14d, %edx
je 0x4eb1
leaq 0x67d5(%rip), %rdi      # 0xb66e
movl %r12d, %esi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2050
incl %r12d
cmpl $0x3, %r12d
jne 0x4e3a
jmp 0x4f0d
movq 0x10(%rsp), %rdx
testl %edx, %edx
sete %al
movl (%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x4ede
movq 0x8(%rsp), %rax
movl (%rax), %ebp
movq 0x18(%rax), %r15
movl $0x1000, %edx           # imm = 0x1000
xorl %esi, %esi
jmp 0x4ee7
addl %r13d, %ebp
movslq %r13d, %rax
addq %rax, %r15
xorl %ecx, %ecx
testl %edx, %edx
movl $0x1000, %eax           # imm = 0x1000
jne 0x4e14
movq %rbx, %rdi
movl $0x7c, %esi
callq 0x4786
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
jmp 0x4f38
leaq 0x689d(%rip), %rdi      # 0xb7b1
callq 0x2270
movq %rbx, %rdi
movl $0x7c, %esi
callq 0x4786
xorl %ebp, %ebp
testl %eax, %eax
je 0x4f38
leaq 0x6897(%rip), %rdi      # 0xb7ca
callq 0x2270
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq