// File: /workspace/github_c_makefile_star2_O2/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, %r12
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
xorl %ecx, %ecx
leaq 0x11045(%rip), %r13     # 0x1a920
leaq 0x38(%rcx), %r14
cmpq $0x310, %r14            # imm = 0x310
je 0x9a29
movl (%rcx,%r13), %r15d
movl 0x18(%rcx,%r13), %ebx
movl %ebx, %edi
callq 0x6fe1
movl %eax, %ebp
addl $0x4, %ebx
movl %ebx, %edi
callq 0x6fe1
movq %r14, %rcx
cmpl %ebp, %r15d
jne 0x98db
movq %r14, %rcx
testl %eax, %eax
jne 0x98db
movq %r12, 0x10(%rsp)
leaq (%r14,%r13), %rax
addq $-0x38, %rax
movq %rax, 0x8(%rsp)
movq 0x10(%rax), %rsi
leaq 0x8f31(%rip), %rdi      # 0x12865
xorl %eax, %eax
callq 0x773c
xorl %r12d, %r12d
xorl %ebp, %ebp
imulq $0x38, %r12, %rax
movslq 0x1c(%rax,%r13), %rcx
cmpq %rcx, %r12
jge 0x9a45
addq %r13, %rax
imulq $0xc, %r12, %rcx
leaq (%rax,%rcx), %r14
addq $0x20, %r14
movl (%r14), %r13d
leal 0x4(%r13), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x700c
leal 0x8(%r13), %ebx
movl %ebx, %edi
callq 0x6fe1
testb $0x1, %al
je 0x9976
addl $0xc, %r13d
movl %r13d, %edi
callq 0x6fe1
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x8ed5(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
movl %eax, %ebx
xorl %edi, %edi
cmpl 0x8(%r14), %eax
sete %dil
leaq 0x8ee7(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
xorl %edi, %edi
cmpl $0x100, %eax            # imm = 0x100
sete %dil
leaq 0x8ee5(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl %r13d, %edi
callq 0x6fe1
movl %eax, %r15d
incl %r15d
movl %r13d, %edi
callq 0x6fe1
decl %r15d
jne 0x99f8
movl %eax, %r14d
subl $0x1, %r14d
jb 0x9a18
movl %r13d, %edi
callq 0x6fe1
jmp 0x9a08
addl %ebx, %ebp
incq %r12
leaq 0x10efc(%rip), %r13     # 0x1a920
jmp 0x9940
leaq 0x8eb6(%rip), %rdi      # 0x128e6
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7a0e
movq 0x8(%rsp), %rcx
movq 0x30(%rcx), %rax
movq %rax, 0x7607b(%rip)     # 0x7fad0
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x76059(%rip)    # 0x7fac0
movups %xmm1, 0x76042(%rip)    # 0x7fab0
movups %xmm0, 0x7602b(%rip)    # 0x7faa0
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x7604c(%rip)    # 0x7fad8
movups %xmm1, 0x76055(%rip)    # 0x7fae8
movups %xmm2, 0x7605e(%rip)    # 0x7faf8
movq 0x30(%rax), %rax
movq %rax, 0x76063(%rip)     # 0x7fb08
leaq 0x7602c(%rip), %rdi     # 0x7fad8
movl %ebp, %esi
movl $0x100, %edx            # imm = 0x100
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8b8e