// File: /workspace/github_c_makefile_star2_O2/ataradov[P]edbg/target_atmel_cm4.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t chip_id, chip_exid;

  dap_reset_target_hw(1);
  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);

  chip_id = dap_read_word(CHIPID_CIDR);
  chip_exid = dap_read_word(CHIPID_EXID);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    uint32_t fl_id, fl_size, fl_page_size, fl_nb_palne, fl_nb_lock;

    if (devices[i].chip_id != chip_id || devices[i].chip_exid != chip_exid)
      continue;

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

    for (uint32_t plane = 0; plane < devices[i].n_planes; plane++)
    {
      dap_write_word(EEFC_FCR(plane), CMD_GETD);
      while (0 == (dap_read_word(EEFC_FSR(plane)) & FSR_FRDY));

      fl_id = dap_read_word(EEFC_FRR(plane));
      check(fl_id, "Cannot read flash descriptor, check Erase pin state");

      fl_size = dap_read_word(EEFC_FRR(plane));
      check(fl_size == devices[i].flash_size, "Invalid reported Flash size (%d)", fl_size);

      fl_page_size = dap_read_word(EEFC_FRR(plane));
      check(fl_page_size == FLASH_PAGE_SIZE, "Invalid reported page size (%d)", fl_page_size);

      fl_nb_palne = dap_read_word(EEFC_FRR(plane));
      for (uint32_t i = 0; i < fl_nb_palne; i++)
        dap_read_word(EEFC_FRR(plane));

      fl_nb_lock =  dap_read_word(EEFC_FRR(plane));
      for (uint32_t i = 0; i < fl_nb_lock; i++)
        dap_read_word(EEFC_FRR(plane));
    }

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

    target_check_options(&target_options, devices[i].flash_size * target_device.n_planes,
        FLASH_PAGE_SIZE * PAGES_IN_ERASE_BLOCK);

    return;
  }

  error_exit("unknown target device (CHIP_ID = 0x%08x)", chip_id);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
pushq $0x1
popq %rbx
movl %ebx, %edi
callq 0x6e14
callq 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl %ebx, %esi
callq 0x700c
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x700c
movl $0x400e0740, %edi       # imm = 0x400E0740
callq 0x6fe1
movl %eax, %ebp
movl $0x400e0744, %edi       # imm = 0x400E0744
callq 0x6fe1
pushq $0x1c
popq %rcx
leaq 0x10c9a(%rip), %rdx     # 0x1ac00
cmpq $0x6fc, %rcx            # imm = 0x6FC
je 0x9f81
cmpl %ebp, -0x1c(%rcx,%rdx)
jne 0x9f7b
cmpl %eax, -0x18(%rcx,%rdx)
je 0x9f9f
addq $0x20, %rcx
jmp 0x9f66
leaq 0x8a4d(%rip), %rdi      # 0x129d5
movl %ebp, %esi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7a0e
movq %r14, 0x10(%rsp)
leaq (%rdx,%rcx), %r14
leaq (%rcx,%rdx), %rbx
addq $-0x1c, %rbx
movq 0x10(%rbx), %rsi
leaq 0x88aa(%rip), %rdi      # 0x12865
xorl %eax, %eax
callq 0x773c
movq %rbx, 0x8(%rsp)
movl 0x18(%rbx), %r15d
xorl %r12d, %r12d
cmpl %r15d, %r12d
je 0xa09b
movl %r12d, %r13d
shll $0x9, %r13d
leal 0x400e0a04(%r13), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x700c
leal 0x400e0a08(%r13), %ebx
movl %ebx, %edi
callq 0x6fe1
testb $0x1, %al
je 0x9ff6
addl $0x400e0a0c, %r13d      # imm = 0x400E0A0C
movl %r13d, %edi
callq 0x6fe1
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x8852(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
xorl %edi, %edi
cmpl (%r14), %eax
sete %dil
leaq 0x8867(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x8865(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
movl %eax, %ebx
incl %ebx
movl %r13d, %edi
callq 0x6fe1
decl %ebx
jne 0xa076
movl %eax, %ebp
subl $0x1, %ebp
jb 0xa093
movl %r13d, %edi
callq 0x6fe1
jmp 0xa084
incl %r12d
jmp 0x9fce
movq 0x8(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x75a72(%rip)    # 0x7fb20
movups %xmm0, 0x75a5b(%rip)    # 0x7fb10
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x75a64(%rip)    # 0x7fb30
movups %xmm1, 0x75a6d(%rip)    # 0x7fb40
movups %xmm2, 0x75a76(%rip)    # 0x7fb50
movq 0x30(%rax), %rax
movq %rax, 0x75a7b(%rip)     # 0x7fb60
movl 0x75a3d(%rip), %esi     # 0x7fb28
imull (%r14), %esi
leaq 0x75a3a(%rip), %rdi     # 0x7fb30
movl $0x2000, %edx           # imm = 0x2000
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b8e