// File: chenhn123[P]wdt_util_src/wdt_ct/w8755_funcs.cpp
// Function: wh_w8755_dev_flash_erase(WdtDevice*, unsigned int, int)
int wh_w8755_dev_flash_erase(WDT_DEV* pdev, unsigned int address, int size)
{
	if (address == 0 && size == FLS_SZ) {
		if (!wh_w8755_dev_send_commands(pdev, WH_CMD_FLASH_ERASEALL, 0)) {			
			printf("Flash chip erase fails\n");
			return 0;
		}

		return 1;
	} else {
		if (!IS_ADDR_SECTOR_ALIGNED(address))
			return 0;
		
		while (size > 0)
		{
			if (IS_ADDR_BLK64_ALIGNED(address) && size >= FLS_BLK64_SZ) {
				if (!wh_w8755_dev_send_commands(pdev, WH_CMD_FLASH_ERASE64K, address)) {
					printf("Flash 64k block erase fails: 0x%x\n", address);
					break;
				}			
				
				address += FLS_BLK64_SZ;
				size -= FLS_BLK64_SZ;
			} else if (IS_ADDR_BLK64_ALIGNED(address) && size >= FLS_BLK32_SZ) {
				if (!wh_w8755_dev_send_commands(pdev, WH_CMD_FLASH_ERASE32K, address)) {
					printf("Flash 32k block erase fails: 0x%x\n", address);
					break;
				}
				
				address += FLS_BLK32_SZ;
				size -= FLS_BLK32_SZ;
			} else	{
				if (!wh_w8755_dev_send_commands(pdev, WH_CMD_FLASH_ERASE4K, address)) {
					printf("Flash 4k block erase fails: 0x%x\n", address);
					break;
				}
		
				address += FLS_SEC_SZ;
				size -= FLS_SEC_SZ;
			}
		}
	}
	
	return (size <= 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x382c
cmpl $0x80000, -0x18(%rbp)   # imm = 0x80000
jne 0x382c
movq -0x10(%rbp), %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x3100
cmpl $0x0, %eax
jne 0x3820
leaq 0xbdb7(%rip), %rdi      # 0xf5c4
movb $0x0, %al
callq 0x2050
movl $0x0, -0x4(%rbp)
jmp 0x395c
movl $0x1, -0x4(%rbp)
jmp 0x395c
movl -0x14(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
cmpl $0x0, %eax
je 0x3845
movl $0x0, -0x4(%rbp)
jmp 0x395c
jmp 0x3847
cmpl $0x0, -0x18(%rbp)
jle 0x394b
movl -0x14(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cmpl $0x0, %eax
jne 0x38ae
cmpl $0x10000, -0x18(%rbp)   # imm = 0x10000
jl 0x38ae
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
movl $0x14, %esi
callq 0x3100
cmpl $0x0, %eax
jne 0x3893
movl -0x14(%rbp), %esi
leaq 0xbd55(%rip), %rdi      # 0xf5dc
movb $0x0, %al
callq 0x2050
jmp 0x394b
movl -0x14(%rbp), %eax
addl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
subl $0x10000, %eax          # imm = 0x10000
movl %eax, -0x18(%rbp)
jmp 0x3946
movl -0x14(%rbp), %eax
andl $0xffff, %eax           # imm = 0xFFFF
cmpl $0x0, %eax
jne 0x3905
cmpl $0x8000, -0x18(%rbp)    # imm = 0x8000
jl 0x3905
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
movl $0x15, %esi
callq 0x3100
cmpl $0x0, %eax
jne 0x38ed
movl -0x14(%rbp), %esi
leaq 0xbd1b(%rip), %rdi      # 0xf5ff
movb $0x0, %al
callq 0x2050
jmp 0x394b
movl -0x14(%rbp), %eax
addl $0x8000, %eax           # imm = 0x8000
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
subl $0x8000, %eax           # imm = 0x8000
movl %eax, -0x18(%rbp)
jmp 0x3944
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %edx
movl $0x13, %esi
callq 0x3100
cmpl $0x0, %eax
jne 0x392e
movl -0x14(%rbp), %esi
leaq 0xbcfd(%rip), %rdi      # 0xf622
movb $0x0, %al
callq 0x2050
jmp 0x394b
movl -0x14(%rbp), %eax
addl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x14(%rbp)
movl -0x18(%rbp), %eax
subl $0x1000, %eax           # imm = 0x1000
movl %eax, -0x18(%rbp)
jmp 0x3946
jmp 0x3847
jmp 0x394d
cmpl $0x0, -0x18(%rbp)
setle %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)