// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movq %rdi, 0x8(%rsp)
movl $0x1, %edi
callq 0x6fe8
callq 0x6789
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x723e
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x723e
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x723e
xorl %r14d, %r14d
leaq 0xfccf(%rip), %r12      # 0x19920
xorl %r15d, %r15d
imulq $0x38, %r15, %r13
movl 0x18(%r13,%r12), %ebx
movl %ebx, %edi
callq 0x71ef
movl %eax, %ebp
addl $0x4, %ebx
movl %ebx, %edi
callq 0x71ef
xorl (%r13,%r12), %ebp
orl %eax, %ebp
setne %al
jne 0x9e19
movb %al, 0x7(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
addq %r12, %r13
movq %r13, 0x10(%rsp)
movq 0x10(%r13), %rsi
xorl %r13d, %r13d
leaq 0x8bd1(%rip), %rdi      # 0x12875
xorl %eax, %eax
callq 0x7a78
movq %r12, %rax
xorl %r14d, %r14d
leaq (,%r13,2), %rcx
addq %r13, %rcx
leaq 0x20(%rax,%rcx,4), %r15
movl (%r15), %ebp
leal 0x4(%rbp), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x723e
leal 0x8(%rbp), %ebx
movl %ebx, %edi
callq 0x71ef
testb $0x1, %al
je 0x9cd4
addl $0xc, %ebp
movl %ebp, %edi
callq 0x71ef
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x8b89(%rip), %rsi      # 0x12881
xorl %eax, %eax
callq 0x7bb4
movl %ebp, %edi
callq 0x71ef
movl %eax, %ebx
xorl %edi, %edi
cmpl 0x8(%r15), %eax
sete %dil
leaq 0x8b9c(%rip), %rsi      # 0x128b5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl %ebp, %edi
callq 0x71ef
xorl %edi, %edi
cmpl $0x100, %eax            # imm = 0x100
sete %dil
leaq 0x8b9b(%rip), %rsi      # 0x128d6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl %ebp, %edi
callq 0x71ef
movl %eax, %r15d
testl %eax, %eax
je 0x9d5e
movl %ebp, %edi
callq 0x71ef
decl %r15d
jne 0x9d52
movl %ebp, %edi
callq 0x71ef
movl %eax, %r15d
testl %eax, %eax
je 0x9d78
movl %ebp, %edi
callq 0x71ef
decl %r15d
jne 0x9d6c
addl %ebx, %r14d
incq %r13
imulq $0x38, %r13, %rcx
leaq (%r12,%rcx), %rax
movslq 0x1c(%rcx,%r12), %rcx
cmpq %rcx, %r13
jl 0x9cb1
movq 0x10(%rsp), %rcx
movq 0x30(%rcx), %rax
movq %rax, 0x74d2c(%rip)     # 0x7ead0
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x74d0a(%rip)    # 0x7eac0
movups %xmm1, 0x74cf3(%rip)    # 0x7eab0
movups %xmm0, 0x74cdc(%rip)    # 0x7eaa0
movq 0x8(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x74cfd(%rip)    # 0x7ead8
movups %xmm1, 0x74d06(%rip)    # 0x7eae8
movups %xmm2, 0x74d0f(%rip)    # 0x7eaf8
movq 0x30(%rax), %rax
movq %rax, 0x74d14(%rip)     # 0x7eb08
leaq 0x74cdd(%rip), %rdi     # 0x7ead8
movl %r14d, %esi
movl $0x100, %edx            # imm = 0x100
callq 0x8edf
cmpb $0x0, 0x7(%rsp)
movq 0x20(%rsp), %rax
movq 0x18(%rsp), %r15
je 0x9e34
cmpq $0xc, %r15
leaq 0x1(%r15), %rax
setae %r14b
movq %rax, %r15
cmpq $0xd, %rax
jne 0x9c54
jmp 0x9e38
testb $0x1, %al
je 0x9e54
leaq 0x8ab7(%rip), %rdi      # 0x128f6
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7c8d
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq