// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x71ec6(%rip), %ebx     # 0x7ed90
movq 0x71ed7(%rip), %r15     # 0x7eda8
movl $0xfff, %r13d           # imm = 0xFFF
leal (%rbx,%r13), %ebp
testl %ebx, %ebx
cmovnsl %ebx, %ebp
addl 0x71eb9(%rip), %r13d    # 0x7eda0
movl %r13d, %r12d
shrl $0xc, %r12d
movl $0x4000c000, %edi       # imm = 0x4000C000
movl $0x9, %esi
callq 0x723e
cmpl $0x1000, %r13d          # imm = 0x1000
jb 0xcf4c
sarl $0xc, %ebp
shll $0xc, %ebp
xorl %r14d, %r14d
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %ebp, %esi
callq 0x723e
movl $0x22, %edi
movl $0x50, %esi
callq 0xd2ce
testb $0x3, %r14b
jne 0xcf3e
leaq 0x38e8(%rip), %rdi      # 0x1081f
xorl %eax, %eax
callq 0x7a78
incl %r14d
addl $0x1000, %ebp           # imm = 0x1000
cmpl %r14d, %r12d
jne 0xcf0f
leaq 0x5e79(%rip), %rdi      # 0x12dcc
xorl %eax, %eax
callq 0x7a78
movl $0x4000c00c, %edi       # imm = 0x4000C00C
movl $0x61, %esi
callq 0x723e
cmpl $0x1000, %r13d          # imm = 0x1000
jb 0xcfe7
xorl %ebp, %ebp
xorl %r13d, %r13d
movl $0x200, %r14d           # imm = 0x200
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %ebx, %esi
callq 0x70f4
movl %r13d, %eax
movl (%r15,%rax), %esi
movl $0x4000c080, %edi       # imm = 0x4000C080
callq 0x70f4
leal 0x4(%r13), %eax
movl (%r15,%rax), %esi
movl $0x4000c084, %edi       # imm = 0x4000C084
callq 0x70f4
movl $0x4000c010, %edi       # imm = 0x4000C010
movl $0x1, %esi
callq 0x70f4
addl $0x8, %ebx
addl $0x8, %r13d
decl %r14d
jne 0xcf7d
callq 0x6a6f
testb $0x3, %bpl
jne 0xcfe0
leaq 0x3846(%rip), %rdi      # 0x1081f
xorl %eax, %eax
callq 0x7a78
incl %ebp
cmpl %r12d, %ebp
jne 0xcf77
movl $0x4000c040, %edi       # imm = 0x4000C040
callq 0x71ef
testb $0x40, %al
jne 0xd004
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6749(%rip), %rdi      # 0x13754
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7c8d