// File: chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_write_flash_page(WdtDevice*, int, unsigned char*, int)
int wh_w8760_dev_write_flash_page(WDT_DEV* pdev, int addr, BYTE* buf, int size)
{
	int retval = 0;
	unsigned int	write_base;
	unsigned int	write_size;

	write_base = W8760_FLASH_PAGE_SIZE;

	if (addr & 0xFF) {
		unsigned int 	size_partial;
		size_partial = write_base - (addr & 0xFF);
		
		if (size > (int) size_partial) {				
			write_size = size_partial;				
			size = size - size_partial;				
		} else {											
			write_size = size;								
			size = 0;										
		}													
		retval = wh_w8760_dev_write_flash(pdev, addr, (BYTE*) buf, 0, write_size);	
		if (!retval)										
			return retval;									
		buf = buf + size_partial;				
		addr = addr + size_partial;	
	}

	while (size)
	{
		if ((addr & 0xfff) == 0)
			printf("base addr: 0x%x\n", addr);
		if (size > (int) write_base) {				
			write_size = write_base;				
			size = size - write_base;				
		} else {											
			write_size = size;								
			size = 0;										
		}													
		retval = wh_w8760_dev_write_flash(pdev, addr, (BYTE*) buf, 0, write_size);	
		if (!retval)										
			return retval;									
		buf = buf + write_base;				
		addr = addr + write_base;			
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x28(%rbp)
movl $0x100, -0x2c(%rbp)     # imm = 0x100
movl -0x14(%rbp), %eax
andl $0xff, %eax
cmpl $0x0, %eax
je 0x56b9
movl -0x2c(%rbp), %eax
movl -0x14(%rbp), %ecx
andl $0xff, %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x34(%rbp), %eax
jle 0x566b
movl -0x34(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x24(%rbp), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x5678
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x30(%rbp), %r8d
xorl %ecx, %ecx
callq 0x54d0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x56a2
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x5753
movq -0x20(%rbp), %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x56bb
cmpl $0x0, -0x24(%rbp)
je 0x574c
movl -0x14(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
cmpl $0x0, %eax
jne 0x56e3
movl -0x14(%rbp), %esi
leaq 0x9df9(%rip), %rdi      # 0xf4d5
movb $0x0, %al
callq 0x2050
movl -0x24(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jle 0x56fc
movl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x24(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x5709
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
movq -0x20(%rbp), %rdx
movl -0x30(%rbp), %r8d
xorl %ecx, %ecx
callq 0x54d0
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x5730
movl -0x28(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x5753
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movl -0x14(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x56bb
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)