// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_atmel_cm3.c
// Function: target_select
static void target_select(target_options_t *options)
{
  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);

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

    chip_id = dap_read_word(CHIPID_CIDR(devices[i].chipid_base));
    chip_exid = dap_read_word(CHIPID_EXID(devices[i].chipid_base));

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

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

    for (int i = 0; i < devices[i].n_planes; i++)
    {
      uint32_t eefc_base = devices[i].plane[i].eefc_base;

      dap_write_word(EEFC_FCR(eefc_base), CMD_GETD);
      while (0 == (dap_read_word(EEFC_FSR(eefc_base)) & FSR_FRDY));

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

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

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

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

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

      flash_size += fl_size;
    }

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

    target_check_options(&target_options, flash_size, FLASH_PAGE_SIZE);

    return;
  }

  error_exit("unknown target device");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r13
movl $0x1, %edi
callq 0x7013
callq 0x6789
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x7269
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x7269
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x7269
xorl %r14d, %r14d
leaq 0xfc8d(%rip), %r15      # 0x19920
movl (%r14,%r15), %r12d
movl 0x18(%r14,%r15), %ebx
movl %ebx, %edi
callq 0x721a
movl %eax, %ebp
addl $0x4, %ebx
movl %ebx, %edi
callq 0x721a
cmpl %ebp, %r12d
jne 0x9cb8
testl %eax, %eax
je 0x9ce1
addq $0x38, %r14
cmpq $0x2d8, %r14            # imm = 0x2D8
jne 0x9c93
leaq 0x8c1a(%rip), %rdi      # 0x128e6
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7ca5
movq %r13, 0x10(%rsp)
leaq (%r15,%r14), %rax
movq %rax, 0x8(%rsp)
movq 0x10(%r14,%r15), %rsi
leaq 0x8b6a(%rip), %rdi      # 0x12865
xorl %eax, %eax
callq 0x7a90
xorl %r12d, %r12d
movq %r15, %rax
xorl %ebp, %ebp
leaq (%r12,%r12,2), %rcx
leaq (%rax,%rcx,4), %r14
addq $0x20, %r14
movl (%r14), %r13d
leal 0x4(%r13), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x7269
leal 0x8(%r13), %ebx
movl %ebx, %edi
callq 0x721a
testb $0x1, %al
je 0x9d2b
addl $0xc, %r13d
movl %r13d, %edi
callq 0x721a
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x8b20(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
movl %eax, %ebx
xorl %edi, %edi
cmpl 0x8(%r14), %eax
sete %dil
leaq 0x8b32(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
xorl %edi, %edi
cmpl $0x100, %eax            # imm = 0x100
sete %dil
leaq 0x8b30(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
movl %eax, %r14d
testl %eax, %eax
je 0x9dbb
movl %r13d, %edi
callq 0x721a
decl %r14d
jne 0x9dae
movl %r13d, %edi
callq 0x721a
movl %eax, %r14d
testl %eax, %eax
je 0x9dd7
movl %r13d, %edi
callq 0x721a
decl %r14d
jne 0x9dca
addl %ebx, %ebp
incq %r12
imulq $0x38, %r12, %rcx
leaq (%r15,%rcx), %rax
movslq 0x1c(%rcx,%r15), %rcx
cmpq %rcx, %r12
jl 0x9d0a
movq 0x8(%rsp), %rcx
movq 0x30(%rcx), %rax
movq %rax, 0x74cce(%rip)     # 0x7ead0
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x74cac(%rip)    # 0x7eac0
movups %xmm1, 0x74c95(%rip)    # 0x7eab0
movups %xmm0, 0x74c7e(%rip)    # 0x7eaa0
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x74c9f(%rip)    # 0x7ead8
movups %xmm1, 0x74ca8(%rip)    # 0x7eae8
movups %xmm2, 0x74cb1(%rip)    # 0x7eaf8
movq 0x30(%rax), %rax
movq %rax, 0x74cb6(%rip)     # 0x7eb08
leaq 0x74c7f(%rip), %rdi     # 0x7ead8
movl %ebp, %esi
movl $0x100, %edx            # imm = 0x100
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ec8