// File: /workspace/github_c_makefile_star2_O2/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 %rbp
pushq %rbx
pushq %rax
movq %rdi, %rbx
xorl %edi, %edi
callq 0x6e95
callq 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
pushq $0x1
popq %rbp
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl %ebp, %esi
callq 0x700c
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x700c
movl %ebp, %edi
callq 0x6e95
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x6fe1
cmpl $0x16080413, %eax       # imm = 0x16080413
jne 0xc1c1
leaq 0x67a3(%rip), %rdi      # 0x12865
leaq 0x74fb(%rip), %rsi      # 0x135c4
xorl %eax, %eax
callq 0x773c
movq 0x30(%rbx), %rax
movq %rax, 0x73c75(%rip)     # 0x7fd50
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x73c53(%rip)    # 0x7fd40
movups %xmm1, 0x73c3c(%rip)    # 0x7fd30
movups %xmm0, 0x73c25(%rip)    # 0x7fd20
movl $0x1fffc001, %edi       # imm = 0x1FFFC001
callq 0x6fba
cmpb $-0x56, %al
je 0xc11d
cmpb $0x0, 0x8(%rbx)
jne 0xc11d
leaq 0x5f15(%rip), %rdi      # 0x1202b
xorl %eax, %eax
callq 0x7a0e
movl $0x1fff7a20, %edi       # imm = 0x1FFF7A20
callq 0x6fe1
shrl $0x6, %eax
andl $0x3fffc00, %eax        # imm = 0x3FFFC00
leaq 0x73bea(%rip), %rdi     # 0x7fd20
movl %eax, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x8b8e
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x700c
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x700c
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x700c
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x700c
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
callq 0x700c
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x6fe1
testl %eax, %eax
setns %bl
movl $0x40023c14, %edi       # imm = 0x40023C14
callq 0x6fe1
testb $0x1, %al
sete %al
andb %bl, %al
movzbl %al, %edi
leaq 0x71f3(%rip), %rsi      # 0x133a7
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x793d
leaq 0x73cb(%rip), %rdi      # 0x13593
movl %eax, %esi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x7a0e