// File: /workspace/github_c_makefile_star2_O1/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 0x73b86(%rip), %ebx     # 0x7ebf8
movq 0x73b97(%rip), %r15     # 0x7ec10
movl $0x1fff, %ebp           # imm = 0x1FFF
addl 0x73b84(%rip), %ebp     # 0x7ec08
movl $0x41004000, %edi       # imm = 0x41004000
movl $0xc0c4, %esi           # imm = 0xC0C4
callq 0x7224
movl %ebp, 0x4(%rsp)
cmpl $0x2000, %ebp           # imm = 0x2000
jb 0xb17d
shrl $0xd, 0x4(%rsp)
movl $0x200, %r12d           # imm = 0x200
xorl %r13d, %r13d
xorl %r14d, %r14d
movl $0x41004014, %edi       # imm = 0x41004014
movl %ebx, %esi
callq 0x723e
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa512, %esi           # imm = 0xA512
callq 0x7224
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x71d0
testb $0x1, %al
je 0xb0cf
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa501, %esi           # imm = 0xA501
callq 0x7224
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x71d0
testb $0x1, %al
je 0xb0ec
xorl %ebp, %ebp
movl $0x41004014, %edi       # imm = 0x41004014
movl %ebx, %esi
callq 0x723e
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa515, %esi           # imm = 0xA515
callq 0x7224
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x71d0
testb $0x1, %al
je 0xb117
movl %r14d, %esi
addq %r15, %rsi
movl %ebx, %edi
movl $0x200, %edx            # imm = 0x200
callq 0x73b6
movl $0x41004004, %edi       # imm = 0x41004004
movl $0xa503, %esi           # imm = 0xA503
callq 0x7224
movl $0x41004012, %edi       # imm = 0x41004012
callq 0x71d0
testb $0x1, %al
je 0xb146
addl %r12d, %ebx
addl %r12d, %r14d
incl %ebp
cmpl $0x10, %ebp
jne 0xb0fc
leaq 0x56b7(%rip), %rdi      # 0x1081f
xorl %eax, %eax
callq 0x7a78
incl %r13d
cmpl 0x4(%rsp), %r13d
jne 0xb0b4
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq