// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_nu_m480.c
// Function: target_program
static void target_program(void)
{
  uint32_t addr = target_options.offset;
  uint32_t offs = 0;
  uint32_t start_page, number_of_pages;
  uint8_t *buf = target_options.file_data;
  uint32_t size = target_options.file_size;

  start_page = target_options.offset / FLASH_PAGE_SIZE;
  number_of_pages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;

  dap_write_word(FMC_ISPCTL, FMC_ISPCTL_ISPEN | FMC_ISPCTL_APUEN);

  for (uint32_t page = 0; page < number_of_pages; page++)
  {
    dap_write_word(FMC_ISPADDR, (start_page + page) * FLASH_PAGE_SIZE);

    fmc_cmd(FMC_ISPCMD_PAGE_ERASE, PAGE_ERASE_TIME);

    if (0 == (page % STATUS_INTERVAL))
      verbose(".");
  }

  verbose(",");

  dap_write_word(FMC_ISPCMD, FMC_ISPCMD_64B_PROG);

  for (uint32_t page = 0; page < number_of_pages; page++)
  {
    for (uint32_t i = 0; i < FLASH_PAGE_SIZE / sizeof(uint64_t); i++)
    {
      dap_write_word_req(FMC_ISPADDR, addr);
      dap_write_word_req(FMC_MPDAT0, *(uint32_t *)&buf[offs]);
      dap_write_word_req(FMC_MPDAT1, *(uint32_t *)&buf[offs+4]);
      dap_write_word_req(FMC_ISPTRG, FMC_ISPTRG_ISPGO);
      addr += sizeof(uint64_t);
      offs += sizeof(uint64_t);
    }

    dap_transfer();

    if (0 == (page % STATUS_INTERVAL))
      verbose(".");
  }

  if (dap_read_word(FMC_ISPSTS) & FMC_ISPSTS_ISPFF)
    error_exit("flash error");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl 0x72592(%rip), %eax     # 0x82520
movl %eax, -0x4(%rbp)
movl $0x0, -0x8(%rbp)
movq 0x72599(%rip), %rax     # 0x82538
movq %rax, -0x18(%rbp)
movl 0x72587(%rip), %eax     # 0x82530
movl %eax, -0x1c(%rbp)
movl 0x7256e(%rip), %eax     # 0x82520
movl $0x1000, %ecx           # imm = 0x1000
cltd
idivl %ecx
movl %eax, -0xc(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1000, %eax           # imm = 0x1000
subl $0x1, %eax
shrl $0xc, %eax
movl %eax, -0x10(%rbp)
movl $0x4000c000, %edi       # imm = 0x4000C000
movl $0x9, %esi
callq 0x77d0
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
cmpl -0x10(%rbp), %eax
jae 0x10035
movl -0xc(%rbp), %esi
addl -0x20(%rbp), %esi
shll $0xc, %esi
movl $0x4000c004, %edi       # imm = 0x4000C004
callq 0x77d0
movl $0x22, %edi
movl $0x50, %esi
callq 0x10510
movl -0x20(%rbp), %ecx
andl $0x3, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x10028
leaq 0x484e(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
jmp 0x1002a
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0xffe4
leaq 0x6df0(%rip), %rdi      # 0x16e2c
movb $0x0, %al
callq 0x8630
movl $0x4000c00c, %edi       # imm = 0x4000C00C
movl $0x61, %esi
callq 0x77d0
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x10(%rbp), %eax
jae 0x1010e
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpq $0x200, %rax            # imm = 0x200
jae 0x100df
movl -0x4(%rbp), %esi
movl $0x4000c004, %edi       # imm = 0x4000C004
callq 0x70c0
movq -0x18(%rbp), %rax
movl -0x8(%rbp), %ecx
movl (%rax,%rcx), %esi
movl $0x4000c080, %edi       # imm = 0x4000C080
callq 0x70c0
movq -0x18(%rbp), %rax
movl -0x8(%rbp), %ecx
addl $0x4, %ecx
movl %ecx, %ecx
movl (%rax,%rcx), %esi
movl $0x4000c084, %edi       # imm = 0x4000C084
callq 0x70c0
movl $0x4000c010, %edi       # imm = 0x4000C010
movl $0x1, %esi
callq 0x70c0
movl -0x4(%rbp), %eax
addq $0x8, %rax
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addq $0x8, %rax
movl %eax, -0x8(%rbp)
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1006c
callq 0x6c70
movl -0x24(%rbp), %ecx
andl $0x3, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x100fe
leaq 0x4778(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
jmp 0x10100
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x10059
movl $0x4000c040, %edi       # imm = 0x4000C040
callq 0x7740
andl $0x40, %eax
cmpl $0x0, %eax
je 0x1012e
leaq 0x767d(%rip), %rdi      # 0x177a4
movb $0x0, %al
callq 0x8970
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)