// File: /workspace/github_c_makefile_star2_O2/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
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x6481
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x6429
xorl %edi, %edi
callq 0x6e95
callq 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
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
pushq $0xa
popq %rdi
callq 0x7aeb
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x6fe1
cmpl $0xd48410, %eax         # imm = 0xD48410
jne 0xc7f2
leaq 0x612f(%rip), %rdi      # 0x12865
leaq 0x6fd2(%rip), %rsi      # 0x1370f
xorl %eax, %eax
callq 0x773c
movaps 0xf5d5(%rip), %xmm0     # 0x1bd20
movups %xmm0, 0x73616(%rip)    # 0x7fd68
movaps 0xf5b7(%rip), %xmm0     # 0x1bd10
movups %xmm0, 0x735f8(%rip)    # 0x7fd58
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x73606(%rip)    # 0x7fd78
movups %xmm1, 0x7360f(%rip)    # 0x7fd88
movups %xmm2, 0x73618(%rip)    # 0x7fd98
movq 0x30(%rbx), %rax
movq %rax, 0x7361d(%rip)     # 0x7fda8
leaq 0x735e6(%rip), %rdi     # 0x7fd78
movl $0x80000, %esi          # imm = 0x80000
movl $0x1000, %edx           # imm = 0x1000
callq 0x8b8e
pushq $0x59
popq %rsi
movl $0x40000100, %edi       # imm = 0x40000100
callq 0x700c
pushq $0x16
popq %rsi
movl $0x40000100, %edi       # imm = 0x40000100
callq 0x700c
movl $0x40000100, %edi       # imm = 0x40000100
movl $0x88, %esi
callq 0x700c
pushq $0x1
popq %rsi
movl $0x4000c000, %edi       # imm = 0x4000C000
callq 0x700c
movl $0x300000, %edi         # imm = 0x300000
callq 0xccaa
testb $0x2, %al
jne 0xc7eb
cmpb $0x0, 0x8(%rbx)
je 0xc808
addq $0x8, %rsp
popq %rbx
popq %rbp
retq
leaq 0x6ee7(%rip), %rdi      # 0x136e0
movl %eax, %esi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x7a0e
leaq 0x581c(%rip), %rdi      # 0x1202b
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x7a0e