// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_nu_m480.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idcode;
  bool locked;

  dap_disconnect();
  dap_connect(DAP_INTERFACE_SWD);
  dap_reset_pin(0);
  dap_reset_link();

  // Stop the core
  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);
  sleep_ms(10);

  idcode = dap_read_word(SYS_PDID);

  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;

    target_check_options(&target_options, devices[i].flash_size, FLASH_PAGE_SIZE);

    dap_write_word(SYS_REGLCTL, 0x59);
    dap_write_word(SYS_REGLCTL, 0x16);
    dap_write_word(SYS_REGLCTL, 0x88);

    dap_write_word(FMC_ISPCTL, FMC_ISPCTL_ISPEN);

    locked = (0 == (fmc_read(CONFIG0) & CONFIG0_LOCK));

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

    return;
  }

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
callq 0x64f0
movl $0x1, %edi
callq 0x6480
xorl %edi, %edi
callq 0x6f90
callq 0x6800
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x77d0
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x77d0
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x77d0
movl $0x1, %edi
callq 0x6f90
movl $0xa, %edi
callq 0x8a90
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x7740
movl %eax, -0xc(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jge 0xfe55
movslq -0x14(%rbp), %rcx
leaq 0xf82f(%rip), %rax      # 0x1f550
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0xc(%rbp), %eax
je 0xfd34
jmp 0xfe47
movslq -0x14(%rbp), %rcx
leaq 0xf811(%rip), %rax      # 0x1f550
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0x6b84(%rip), %rdi      # 0x168d5
movb $0x0, %al
callq 0x8630
movslq -0x14(%rbp), %rcx
leaq 0xf7ed(%rip), %rax      # 0x1f550
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x72774(%rip)     # 0x824e8
movq 0x8(%rax), %rcx
movq %rcx, 0x72771(%rip)     # 0x824f0
movq 0x10(%rax), %rcx
movq %rcx, 0x7276e(%rip)     # 0x824f8
movq 0x18(%rax), %rax
movq %rax, 0x7276b(%rip)     # 0x82500
movq -0x8(%rbp), %rsi
leaq 0x72768(%rip), %rdi     # 0x82508
movl $0x38, %edx
callq 0x61a0
movslq -0x14(%rbp), %rcx
leaq 0xf79b(%rip), %rax      # 0x1f550
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x18(%rax), %esi
leaq 0x72742(%rip), %rdi     # 0x82508
movl $0x1000, %edx           # imm = 0x1000
callq 0xa5f0
movl $0x40000100, %edi       # imm = 0x40000100
movl $0x59, %esi
callq 0x77d0
movl $0x40000100, %edi       # imm = 0x40000100
movl $0x16, %esi
callq 0x77d0
movl $0x40000100, %edi       # imm = 0x40000100
movl $0x88, %esi
callq 0x77d0
movl $0x4000c000, %edi       # imm = 0x4000C000
movl $0x1, %esi
callq 0x77d0
movl $0x300000, %edi         # imm = 0x300000
callq 0x104b0
movl %eax, %ecx
andl $0x2, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
andb $0x1, %al
movb %al, -0xd(%rbp)
testb $0x1, -0xd(%rbp)
je 0xfe45
movq -0x8(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xfe45
leaq 0x625d(%rip), %rdi      # 0x1609b
movb $0x0, %al
callq 0x8970
jmp 0xfe66
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xfd0c
movl -0xc(%rbp), %esi
leaq 0x78e1(%rip), %rdi      # 0x17740
movb $0x0, %al
callq 0x8970
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)