// File: /workspace/github_c_makefile_star2_O2/ataradov[P]edbg/target_atmel_cm4v2.c
// Function: target_program
static void target_program(void)
{
  uint32_t addr = FLASH_ADDR + target_options.offset;
  uint32_t offs = 0;
  uint32_t number_of_rows;
  uint8_t *buf = target_options.file_data;
  uint32_t size = target_options.file_size;

  dap_write_half(NVMCTRL_CTRLA, NVMCTRL_CTRLA_AUTOWS | NVMCTRL_CTRLA_WMODE_MAN |
      NVMCTRL_CTRLA_PRM_MANUAL | NVMCTRL_CTRLA_CACHEDIS0 | NVMCTRL_CTRLA_CACHEDIS1);

  number_of_rows = (size + FLASH_ROW_SIZE - 1) / FLASH_ROW_SIZE;

  for (uint32_t row = 0; row < number_of_rows; row++)
  {
    dap_write_word(NVMCTRL_ADDR, addr);

    dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_UR); // Unlock Region
    while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

    dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_EB);
    while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

    for (int page = 0; page < PAGES_IN_ERASE_BLOCK; page++)
    {
      dap_write_word(NVMCTRL_ADDR, addr);

      dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_PBC);
      while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

      dap_write_block(addr, &buf[offs], FLASH_PAGE_SIZE);

      dap_write_half(NVMCTRL_CTRLB, NVMCTRL_CMD_WP); // Write page
      while (0 == (dap_read_half(NVMCTRL_STATUS) & NVMCTRL_STATUS_READY));

      addr += FLASH_PAGE_SIZE;
      offs += FLASH_PAGE_SIZE;
    }

    verbose(".");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl 0x75065(%rip), %ebx     # 0x7fbf8
movq 0x75076(%rip), %r15     # 0x7fc10
movl $0x1fff, %ebp           # imm = 0x1FFF
addl 0x75063(%rip), %ebp     # 0x7fc08
movl $0x41004000, %edi       # imm = 0x41004000
movl $0xc0c4, %esi           # imm = 0xC0C4
callq 0x7000
shrl $0xd, %ebp
movl %ebp, 0x4(%rsp)
movl $0x200, %r12d           # imm = 0x200
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpl 0x4(%rsp), %r14d
je 0xac97
movl $0x41004014, %edi       # imm = 0x41004014
movl %ebx, %esi
callq 0x700c
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa512, %esi           # imm = 0xA512
callq 0x7000
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x6fcd
testb $0x1, %al
je 0xabed
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa501, %esi           # imm = 0xA501
callq 0x7000
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x6fcd
testb $0x1, %al
je 0xac0a
xorl %ebp, %ebp
cmpl $0x10, %ebp
je 0xac81
movl $0x41004014, %edi       # imm = 0x41004014
movl %ebx, %esi
callq 0x700c
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa515, %esi           # imm = 0xA515
callq 0x7000
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x6fcd
testb $0x1, %al
je 0xac3a
movl %r13d, %esi
addq %r15, %rsi
movl %ebx, %edi
movl $0x200, %edx            # imm = 0x200
callq 0x70ef
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa503, %esi           # imm = 0xA503
callq 0x7000
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x6fcd
testb $0x1, %al
je 0xac69
addl %r12d, %ebx
addl %r12d, %r13d
incl %ebp
jmp 0xac1a
leaq 0x5b87(%rip), %rdi      # 0x1080f
xorl %eax, %eax
callq 0x773c
incl %r14d
jmp 0xabc7
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq