// File: /workspace/github_c_makefile_star2_O3/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 0x71eda(%rip), %ebx     # 0x7ed90
movq 0x71eeb(%rip), %r15     # 0x7eda8
movl $0xfff, %r13d           # imm = 0xFFF
leal (%rbx,%r13), %ebp
testl %ebx, %ebx
cmovnsl %ebx, %ebp
addl 0x71ecd(%rip), %r13d    # 0x7eda0
movl $0x4000c000, %edi       # imm = 0x4000C000
movl $0x9, %esi
callq 0x7269
cmpl $0x1000, %r13d          # imm = 0x1000
jae 0xcf0d
leaq 0x5eca(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x7a90
movl $0x4000c00c, %edi       # imm = 0x4000C00C
movl $0x61, %esi
callq 0x7269
jmp 0xcff5
sarl $0xc, %ebp
movl %r13d, %r12d
shrl $0xc, %r12d
shll $0xc, %ebp
xorl %r14d, %r14d
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %ebp, %esi
callq 0x7269
movl $0x22, %edi
movl $0x50, %esi
callq 0xd2de
testb $0x3, %r14b
jne 0xcf4c
leaq 0x38ca(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x7a90
incl %r14d
addl $0x1000, %ebp           # imm = 0x1000
cmpl %r14d, %r12d
jne 0xcf1d
leaq 0x5e5b(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x7a90
movl $0x4000c00c, %edi       # imm = 0x4000C00C
movl $0x61, %esi
callq 0x7269
cmpl $0x1000, %r13d          # imm = 0x1000
jb 0xcff5
xorl %ebp, %ebp
xorl %r13d, %r13d
movl $0x200, %r14d           # imm = 0x200
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %ebx, %esi
callq 0x711f
movl %r13d, %eax
movl (%r15,%rax), %esi
movl $0x4000c080, %edi       # imm = 0x4000C080
callq 0x711f
leal 0x4(%r13), %eax
movl (%r15,%rax), %esi
movl $0x4000c084, %edi       # imm = 0x4000C084
callq 0x711f
movl $0x4000c010, %edi       # imm = 0x4000C010
movl $0x1, %esi
callq 0x711f
addl $0x8, %ebx
addl $0x8, %r13d
decl %r14d
jne 0xcf8b
callq 0x6a6f
testb $0x3, %bpl
jne 0xcfee
leaq 0x3828(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x7a90
incl %ebp
cmpl %r12d, %ebp
jne 0xcf85
movl $0x4000c040, %edi       # imm = 0x4000C040
callq 0x721a
testb $0x40, %al
jne 0xd012
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x672b(%rip), %rdi      # 0x13744
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7ca5