// File: /workspace/github_c_makefile_star2_O1/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, %ebx        # imm = 0x8000000
addl 0x724db(%rip), %ebx     # 0x7ed38
movq 0x724ec(%rip), %r14     # 0x7ed50
movl 0x724de(%rip), %edi     # 0x7ed48
movl $0x100, %esi            # imm = 0x100
callq 0x7d65
xorl %r10d, %r10d
leaq 0x6d92(%rip), %rdx      # 0x13610
movl 0x724b4(%rip), %esi     # 0x7ed38
xorl %ecx, %ecx
movl %ecx, %edi
movl (%rdx), %ecx
shll $0xa, %ecx
addl %edi, %ecx
cmpl %esi, %edi
setle %r8b
cmpl %ecx, %esi
setl %r9b
testb %r9b, %r8b
cmovnel %edi, %ecx
jne 0xc8b6
incq %r10
addq $0x4, %rdx
cmpq $0x1c, %r10
jne 0xc886
movl $0x1c, %r10d
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl %r10d, %edx
cmpl $0x1b, %r10d
ja 0xc905
movl 0x72469(%rip), %esi     # 0x7ed38
addl %eax, %esi
movl %r10d, %edx
leaq 0x6d35(%rip), %rdi      # 0x13610
leaq (%rdi,%rdx,4), %rdi
movl %ecx, %r8d
movl (%rdi), %ecx
shll $0xa, %ecx
addl %r8d, %ecx
cmpl %esi, %r8d
jg 0xc8f3
cmpl %ecx, %esi
jle 0xc905
incq %rdx
addq $0x4, %rdi
cmpq $0x1c, %rdx
jne 0xc8df
movl $0x1c, %edx
movl %eax, 0x4(%rsp)
cmpl %edx, %r10d
ja 0xc97c
movl %r10d, %r13d
movl %edx, %r12d
subq %r13, %r12
shlq $0x2, %r13
incq %r12
leaq 0x6d5b(%rip), %r15      # 0x13680
leaq 0x3ef3(%rip), %r14      # 0x1081f
xorl %ebx, %ebx
leaq (,%rbx,4), %rax
addq %r13, %rax
movl (%r15,%rax), %eax
leal 0x2(,%rax,8), %esi
movl %eax, %ebp
shll $0x3, %ebp
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x723e
orl $0x10002, %ebp          # imm = 0x10002
movl $0x40023c10, %edi       # imm = 0x40023C10
movl %ebp, %esi
callq 0x723e
callq 0xcc44
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a78
incq %rbx
cmpq %rbx, %r12
jne 0xc92e
leaq 0x6449(%rip), %rdi      # 0x12dcc
xorl %eax, %eax
callq 0x7a78
movl $0x40023c10, %edi       # imm = 0x40023C10
movl $0x201, %esi            # imm = 0x201
callq 0x723e
movl 0x4(%rsp), %ebp
testl %ebp, %ebp
movq 0x10(%rsp), %r15
movq 0x8(%rsp), %r12
je 0xc9e3
leaq 0x3e6d(%rip), %r14      # 0x1081f
xorl %ebx, %ebx
leal (%r15,%rbx), %edi
movl %ebx, %esi
addq %r12, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x73b6
addl $0x100, %ebx            # imm = 0x100
testl $0x3f00, %ebx           # imm = 0x3F00
jne 0xc9df
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a78
cmpl %ebx, %ebp
jne 0xc9b4
callq 0xcc44
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x723e