// File: chenhn123[P]wdt_util_src/wdt_ct/w8790_funcs.cpp
// Function: wh_w8790_dev_write_flash(WdtDevice*, int, unsigned char*, int, int)
int wh_w8790_dev_write_flash(WDT_DEV* pdev, int addr, BYTE* buf, int start, int size)
{
	int byte_count = size;
	int offset = start;
	int max_payload_size = W8790_USB_MAX_PAYLOAD_SIZE - 2;
	int cur_addr = 0xFFFFFFFF;

	while (byte_count >= max_payload_size) {
		if ((addr & 0xfff) == 0)
			printf("base addr: 0x%x\n", addr);
		if (!check_is_all_ff(&buf[offset], max_payload_size)) {
			if (cur_addr != addr) {
				if (!wh_w8790_dev_set_flash_address(pdev, addr))
					return 0;
			}
			if (wh_w8790_dev_write_flash_cmd(pdev, buf, offset, max_payload_size) <= 0)
				return 0;
			cur_addr = addr + max_payload_size;
		}
		else
			wh_printf("Already ff, no need to set: 0x%x\n", addr);
		offset += max_payload_size;
		byte_count -= max_payload_size;
		addr += max_payload_size;
	}

	if (cur_addr != addr) {
		if (!wh_w8790_dev_set_flash_address(pdev, addr))
			return 0;
	}
	if (byte_count > 0) {
		if ((addr & 0xfff) == 0)
			wh_printf("base addr: 0x%x\n", addr);
		return wh_w8790_dev_write_flash_cmd(pdev, buf, offset, byte_count);

	}

	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 %r8d, -0x28(%rbp)
movl -0x28(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x3d, -0x34(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl -0x2c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x7a7f
movl -0x14(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
cmpl $0x0, %eax
jne 0x79de
movl -0x14(%rbp), %esi
leaq 0x7afe(%rip), %rdi      # 0xf4d5
movb $0x0, %al
callq 0x2050
movq -0x20(%rbp), %rdi
movslq -0x30(%rbp), %rax
addq %rax, %rdi
movl -0x34(%rbp), %esi
callq 0x9240
cmpl $0x0, %eax
jne 0x7a4c
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x7a1d
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x6ef0
cmpl $0x0, %eax
jne 0x7a1b
movl $0x0, -0x4(%rbp)
jmp 0x7ae6
jmp 0x7a1d
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
callq 0x78c0
cmpl $0x0, %eax
jg 0x7a41
movl $0x0, -0x4(%rbp)
jmp 0x7ae6
movl -0x14(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x7a5d
movl -0x14(%rbp), %esi
leaq 0x7f33(%rip), %rdi      # 0xf989
movb $0x0, %al
callq 0x24e0
movl -0x34(%rbp), %eax
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x34(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
movl -0x34(%rbp), %eax
addl -0x14(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x79b4
movl -0x38(%rbp), %eax
cmpl -0x14(%rbp), %eax
je 0x7aa3
movq -0x10(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x6ef0
cmpl $0x0, %eax
jne 0x7aa1
movl $0x0, -0x4(%rbp)
jmp 0x7ae6
jmp 0x7aa3
cmpl $0x0, -0x2c(%rbp)
jle 0x7adf
movl -0x14(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
cmpl $0x0, %eax
jne 0x7ac7
movl -0x14(%rbp), %esi
leaq 0x7a15(%rip), %rdi      # 0xf4d5
movb $0x0, %al
callq 0x24e0
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x2c(%rbp), %ecx
callq 0x78c0
movl %eax, -0x4(%rbp)
jmp 0x7ae6
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nop