// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_gd_gd32f4xx.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idcode, flash_size;
  bool locked, ctl_lk, ob_lk;

  dap_reset_pin(0);
  dap_reset_link();
  dap_write_word(DHCSR, DHCSR_DBGKEY | DHCSR_DEBUGEN | DHCSR_HALT);
  dap_write_word(DEMCR, DEMCR_VC_CORERESET);
  dap_write_word(AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  dap_reset_pin(1);

  idcode = dap_read_word(DBG_ID);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    if (devices[i].idcode != idcode)
      continue;

    verbose("Target: %s\n", devices[i].name);

    target_device = devices[i];
    target_options = *options;

    locked = (0xaa != dap_read_byte(OPTIONS_SPC));

    if (locked && !options->unlock)
      error_exit("target is locked, unlock is necessary");

    flash_size = (dap_read_word(FLASH_SRAM_SIZE_REG) >> FLASH_SIZE_REG_OFFS) * FLASH_SIZE_REG_MULT;

    target_check_options(&target_options, flash_size, FLASH_ALIGN_SIZE);

    dap_write_word(FMC_KEY, FMC_KEY_KEY1);
    dap_write_word(FMC_KEY, FMC_KEY_KEY2);
    dap_write_word(FMC_OBKEY, FMC_OBKEY_KEY1);
    dap_write_word(FMC_OBKEY, FMC_OBKEY_KEY2);
    dap_write_word(FMC_CTL, 0);

    ctl_lk = (dap_read_word(FMC_CTL) & FMC_CTL_LK) > 0;
    ob_lk  = (dap_read_word(FMC_OBCTL0) & FMC_OBCTL0_OB_LK) > 0;
    check(!ctl_lk && !ob_lk, "Failed to unlock the flash for write operation. Try to power cycle the target.");

    return;
  }

  error_exit("unknown target device (DBG_ID = 0x%08x)", idcode);
}

// Assembly:
pushq %rbx
movq %rdi, %rbx
xorl %edi, %edi
callq 0x709f
callq 0x6789
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x7269
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x7269
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x7269
movl $0x1, %edi
callq 0x709f
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x721a
cmpl $0x16080413, %eax       # imm = 0x16080413
jne 0xc721
leaq 0x623e(%rip), %rdi      # 0x12865
leaq 0x6f96(%rip), %rsi      # 0x135c4
xorl %eax, %eax
callq 0x7a90
movq 0x30(%rbx), %rax
movq %rax, 0x72710(%rip)     # 0x7ed50
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x726ee(%rip)    # 0x7ed40
movups %xmm1, 0x726d7(%rip)    # 0x7ed30
movups %xmm0, 0x726c0(%rip)    # 0x7ed20
movl $0x1fffc001, %edi       # imm = 0x1FFFC001
callq 0x71e0
cmpb $-0x56, %al
je 0xc682
cmpb $0x0, 0x8(%rbx)
jne 0xc682
leaq 0x59b0(%rip), %rdi      # 0x1202b
xorl %eax, %eax
callq 0x7ca5
movl $0x1fff7a20, %edi       # imm = 0x1FFF7A20
callq 0x721a
shrl $0x6, %eax
andl $0x3fffc00, %eax        # imm = 0x3FFFC00
leaq 0x72685(%rip), %rdi     # 0x7ed20
movl %eax, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x8ec8
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x7269
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x7269
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x7269
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x7269
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
callq 0x7269
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x721a
testl %eax, %eax
setns %bl
movl $0x40023c14, %edi       # imm = 0x40023C14
callq 0x721a
testb $0x1, %al
sete %al
andb %bl, %al
movzbl %al, %edi
leaq 0x6c8e(%rip), %rsi      # 0x133a7
xorl %eax, %eax
popq %rbx
jmp 0x7bcc
leaq 0x6e6b(%rip), %rdi      # 0x13593
movl %eax, %esi
xorl %eax, %eax
popq %rbx
jmp 0x7ca5