// File: /workspace/github_c_makefile_star2_O2/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 0x7348f(%rip), %ebx     # 0x7fd90
movq 0x734a0(%rip), %r13     # 0x7fda8
movl $0x1000, %ecx           # imm = 0x1000
movl %ebx, %eax
cltd
idivl %ecx
movl %eax, %ebp
movl $0xfff, %eax            # imm = 0xFFF
addl 0x73481(%rip), %eax     # 0x7fda0
shrl $0xc, %eax
movl %eax, (%rsp)
pushq $0x9
popq %rsi
movl $0x4000c000, %edi       # imm = 0x4000C000
callq 0x700c
shll $0xc, %ebp
pushq $0x22
popq %r14
pushq $0x50
popq %r15
xorl %r12d, %r12d
cmpl %r12d, (%rsp)
je 0xc97c
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %ebp, %esi
callq 0x700c
movl %r14d, %edi
movl %r15d, %esi
callq 0xccf7
testb $0x3, %r12b
jne 0xc971
leaq 0x3ea5(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x773c
incl %r12d
addl $0x1000, %ebp           # imm = 0x1000
jmp 0xc940
leaq 0x6439(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x773c
pushq $0x61
popq %rsi
movl $0x4000c00c, %edi       # imm = 0x4000C00C
callq 0x700c
pushq $0x1
popq %rbp
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpl (%rsp), %r12d
je 0xca30
movl %r12d, 0x4(%rsp)
xorl %r12d, %r12d
leal (%rbx,%r12), %r15d
cmpl $0x1000, %r12d          # imm = 0x1000
je 0xca07
movl $0x4000c004, %edi       # imm = 0x4000C004
movl %r15d, %esi
callq 0x6f02
movl %r14d, %eax
movl (%r13,%rax), %esi
movl $0x4000c080, %edi       # imm = 0x4000C080
callq 0x6f02
leal 0x4(%r14), %eax
movl (%r13,%rax), %esi
movl $0x4000c084, %edi       # imm = 0x4000C084
callq 0x6f02
movl $0x4000c010, %edi       # imm = 0x4000C010
movl %ebp, %esi
callq 0x6f02
addl $0x8, %r14d
addl $0x8, %r12d
jmp 0xc9b2
callq 0x68f4
movl 0x4(%rsp), %r12d
testb $0x3, %r12b
jne 0xca25
leaq 0x3df1(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x773c
incl %r12d
movl %r15d, %ebx
jmp 0xc9a0
movl $0x4000c040, %edi       # imm = 0x4000C040
callq 0x6fe1
testb $0x40, %al
jne 0xca4d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6cf0(%rip), %rdi      # 0x13744
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7a0e