// File: 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
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x2110
leaq 0x9667(%rip), %rdi      # 0xf6bb
movb $0x0, %al
callq 0x2050
movq -0x10(%rbp), %rax
movl $0x3, 0x278(%rax)
movq -0x10(%rbp), %rdi
movl $0x96, %esi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x5060
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x608f
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6313
movq -0x18(%rbp), %rax
movl 0x20(%rax), %eax
addl $0xff, %eax
shrl $0x8, %eax
movl %eax, -0x8c(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
shrl $0x8, %eax
shll $0x10, %eax
andl $0xffff0000, %eax       # imm = 0xFFFF0000
orl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x8c(%rbp), %edx
movl $0x11, %esi
callq 0x5930
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x60e9
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6313
movq -0x10(%rbp), %rax
cmpl $0x2000, 0x188(%rax)    # imm = 0x2000
jne 0x613b
movq -0x50(%rbp), %rax
cmpl $0x5d000, (%rax)        # imm = 0x5D000
jne 0x6139
leaq 0x9927(%rip), %rdi      # 0xfa33
movb $0x0, %al
callq 0x2050
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x5b60
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x6137
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6313
jmp 0x6139
jmp 0x613b
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
cmpl $0x1000, -0x24(%rbp)    # imm = 0x1000
jle 0x618e
movl $0x1, -0x34(%rbp)
movl -0x24(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x24(%rbp)
movl -0x28(%rbp), %eax
addl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x28(%rbp)
movq -0x40(%rbp), %rax
addq $0x1000, %rax           # imm = 0x1000
movq %rax, -0x40(%rbp)
jmp 0x6190
cmpl $0x0, -0x24(%rbp)
je 0x62ce
cmpl $0x1000, -0x24(%rbp)    # imm = 0x1000
jle 0x61b7
movl $0x1000, -0x2c(%rbp)    # imm = 0x1000
movl -0x24(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x24(%rbp)
jmp 0x61c4
movl -0x24(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x626c
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %edx
movl $0x13, %esi
callq 0x5930
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x61f1
jmp 0x625e
movq -0x10(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x5bb0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x620f
jmp 0x625e
movq -0x40(%rbp), %rsi
movl -0x2c(%rbp), %ecx
xorl %edx, %edx
movl %edx, %edi
callq 0xb780
movzwl %ax, %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rdi
movl -0x28(%rbp), %edx
movl -0x2c(%rbp), %ecx
leaq -0x48(%rbp), %rsi
callq 0x5c30
movl %eax, -0x20(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jne 0x6245
jmp 0x626c
movl -0x30(%rbp), %esi
movl -0x48(%rbp), %edx
movl -0x44(%rbp), %ecx
leaq 0x949c(%rip), %rdi      # 0xf6f1
movb $0x0, %al
callq 0x2050
jmp 0x625e
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x61cb
cmpl $0x3, -0x30(%rbp)
jne 0x6282
leaq 0x9498(%rip), %rdi      # 0xf711
movb $0x0, %al
callq 0x2050
jmp 0x62ce
movl -0x28(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x62c9
cmpl $0x0, -0x34(%rbp)
je 0x62c9
movl $0x1000, -0x24(%rbp)    # imm = 0x1000
movq -0x50(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
jmp 0x6190
movq -0x10(%rbp), %rdi
movl $0x10, %esi
xorl %edx, %edx
callq 0x5930
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jne 0x62ef
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6313
cmpl $0x3, -0x30(%rbp)
jne 0x630c
leaq 0x942f(%rip), %rdi      # 0xf72b
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x6313
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
nop