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

  size = round_up(size, FLASH_ALIGN_SIZE);

  for (start_sector = 0; start_sector < FLASH_SECTOR_COUNT; start_sector++)
  {
    int sector_end = sector_offset + flash_sector_size[start_sector] * 1024;
    int start = target_options.offset;

    if (sector_offset <= start && start < sector_end)
      break;

    sector_offset = sector_end;
  }

  for (end_sector = start_sector; end_sector < FLASH_SECTOR_COUNT; end_sector++)
  {
    int sector_end = sector_offset + flash_sector_size[end_sector] * 1024;
    int end = target_options.offset + size;

    if (sector_offset <= end && end <= sector_end)
      break;

    sector_offset = sector_end;
  }

  for (int i = start_sector; i <= end_sector; i++)
  {
    uint32_t cmd = FMC_CTL_SER | FMC_CTL_SN(flash_sector_index[i]);

    dap_write_word(FMC_CTL, cmd);
    dap_write_word(FMC_CTL, cmd | FMC_CTL_START);
    flash_wait_done();

    verbose(".");
  }

  verbose(",");

  // TODO: Verify this works with fast SWD clocks

  dap_write_word(FMC_CTL, FMC_CTL_PSZ_WORD | FMC_CTL_PG);

  while (size)
  {
    dap_write_block(addr, &buf[offs], FLASH_ALIGN_SIZE);

    addr += FLASH_ALIGN_SIZE;
    offs += FLASH_ALIGN_SIZE;
    size -= FLASH_ALIGN_SIZE;

    if (0 == (offs % (FLASH_ALIGN_SIZE * 64)))
      verbose(".");
  }

  flash_wait_done();

  dap_write_word(FMC_CTL, 0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl $0x8000000, %eax        # imm = 0x8000000
addl 0x73a3e(%rip), %eax     # 0x7fd38
movq %rax, 0x10(%rsp)
movq 0x73a4a(%rip), %rax     # 0x7fd50
movq %rax, 0x8(%rsp)
movl 0x73a37(%rip), %edi     # 0x7fd48
movl $0x100, %esi            # imm = 0x100
callq 0x7adc
movl %eax, %ebx
xorl %r13d, %r13d
leaq 0x7349(%rip), %r12      # 0x13670
leaq 0x72d2(%rip), %rax      # 0x13600
movl 0x73a04(%rip), %ecx     # 0x7fd38
xorl %edx, %edx
cmpq $0x1c, %r13
je 0xc363
movl (%rax), %esi
shll $0xa, %esi
addl %edx, %esi
cmpl %ecx, %edx
setle %dil
cmpl %esi, %ecx
setl %r8b
testb %r8b, %dil
jne 0xc363
incq %r13
addq $0x4, %r12
addq $0x4, %rax
movl %esi, %edx
jmp 0xc336
addl %ebx, %ecx
movq %r13, %r15
cmpq $0x1c, %r15
je 0xc394
movl (%rax), %esi
shll $0xa, %esi
addl %edx, %esi
cmpl %ecx, %edx
setg %dl
cmpl %esi, %ecx
setg %dil
orb %dl, %dil
cmpb $0x1, %dil
jne 0xc394
incq %r15
addq $0x4, %rax
movl %esi, %edx
jmp 0xc368
leaq 0x4474(%rip), %r14      # 0x1080f
cmpq %r15, %r13
ja 0xc3e4
movl (%r12), %eax
leal 0x2(,%rax,8), %esi
movl %eax, %ebp
shll $0x3, %ebp
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x700c
orl $0x10002, %ebp          # imm = 0x10002
movl $0x40023c10, %edi       # imm = 0x40023C10
movl %ebp, %esi
callq 0x700c
callq 0xc68a
movq %r14, %rdi
xorl %eax, %eax
callq 0x773c
incq %r13
addq $0x4, %r12
jmp 0xc39b
leaq 0x69d1(%rip), %rdi      # 0x12dbc
xorl %eax, %eax
callq 0x773c
movl $0x40023c10, %edi       # imm = 0x40023C10
movl $0x201, %esi            # imm = 0x201
callq 0x700c
leaq 0x4407(%rip), %r14      # 0x1080f
xorl %r15d, %r15d
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
cmpl %r15d, %ebx
je 0xc449
leal (%r12,%r15), %edi
movl %r15d, %esi
addq %r13, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x70ef
addl $0x100, %r15d           # imm = 0x100
testw $0x3f00, %r15w          # imm = 0x3F00
jne 0xc415
movq %r14, %rdi
xorl %eax, %eax
callq 0x773c
jmp 0xc415
callq 0xc68a
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x700c