// File: /workspace/github_c_makefile_star2_O1/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 0x7074
callq 0x6789
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x723e
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x723e
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x723e
movl $0x1, %edi
callq 0x7074
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x71ef
cmpl $0x16080413, %eax       # imm = 0x16080413
jne 0xc729
leaq 0x6246(%rip), %rdi      # 0x12875
leaq 0x6f9e(%rip), %rsi      # 0x135d4
xorl %eax, %eax
callq 0x7a78
movq 0x30(%rbx), %rax
movq %rax, 0x72708(%rip)     # 0x7ed50
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x726e6(%rip)    # 0x7ed40
movups %xmm1, 0x726cf(%rip)    # 0x7ed30
movups %xmm0, 0x726b8(%rip)    # 0x7ed20
movl $0x1fffc001, %edi       # imm = 0x1FFFC001
callq 0x71b5
cmpb $-0x56, %al
je 0xc68a
cmpb $0x0, 0x8(%rbx)
jne 0xc68a
leaq 0x59b8(%rip), %rdi      # 0x1203b
xorl %eax, %eax
callq 0x7c8d
movl $0x1fff7a20, %edi       # imm = 0x1FFF7A20
callq 0x71ef
shrl $0x6, %eax
andl $0x3fffc00, %eax        # imm = 0x3FFFC00
leaq 0x7267d(%rip), %rdi     # 0x7ed20
movl %eax, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x8edf
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x723e
movl $0x40023c04, %edi       # imm = 0x40023C04
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x723e
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x723e
movl $0x40023c08, %edi       # imm = 0x40023C08
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x723e
movl $0x40023c10, %edi       # imm = 0x40023C10
xorl %esi, %esi
callq 0x723e
movl $0x40023c10, %edi       # imm = 0x40023C10
callq 0x71ef
testl %eax, %eax
setns %bl
movl $0x40023c14, %edi       # imm = 0x40023C14
callq 0x71ef
testb $0x1, %al
sete %al
andb %bl, %al
movzbl %al, %edi
leaq 0x6c96(%rip), %rsi      # 0x133b7
xorl %eax, %eax
popq %rbx
jmp 0x7bb4
leaq 0x6e73(%rip), %rdi      # 0x135a3
movl %eax, %esi
xorl %eax, %eax
popq %rbx
jmp 0x7c8d