// File: /workspace/github_c_makefile_star2_O3/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, %rbx
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
movl $0x400e0740, %edi       # imm = 0x400E0740
callq 0x721a
movl %eax, %ebp
movl $0x400e0744, %edi       # imm = 0x400E0744
callq 0x721a
movl $0x1c, %ecx
leaq 0xf8b1(%rip), %rdx      # 0x19c00
cmpl %ebp, -0x1c(%rcx,%rdx)
jne 0xa35b
cmpl %eax, -0x18(%rcx,%rdx)
je 0xa386
addq $0x20, %rcx
cmpq $0x6fc, %rcx            # imm = 0x6FC
jne 0xa34f
leaq 0x8666(%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 0x7ca5
movq %rbx, 0x10(%rsp)
leaq (%rdx,%rcx), %r14
leaq (%rcx,%rdx), %rbx
addq $-0x1c, %rbx
movq 0x10(%rbx), %rsi
leaq 0x84c3(%rip), %rdi      # 0x12865
xorl %eax, %eax
callq 0x7a90
movq %rbx, 0x8(%rsp)
movl 0x18(%rbx), %r15d
testl %r15d, %r15d
je 0xa495
xorl %r12d, %r12d
movl %r12d, %r13d
shll $0x9, %r13d
leal 0x400e0a04(%r13), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x7269
leal 0x400e0a08(%r13), %ebx
movl %ebx, %edi
callq 0x721a
testb $0x1, %al
je 0xa3dd
addl $0x400e0a0c, %r13d      # imm = 0x400E0A0C
movl %r13d, %edi
callq 0x721a
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x846b(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
movl (%r14), %ebp
xorl %edi, %edi
cmpl %ebp, %eax
sete %dil
leaq 0x847e(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x847c(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl %r13d, %edi
callq 0x721a
movl %eax, %ebx
testl %eax, %eax
je 0xa46d
movl %r13d, %edi
callq 0x721a
decl %ebx
jne 0xa461
movl %r13d, %edi
callq 0x721a
movl %eax, %ebx
testl %eax, %eax
je 0xa487
movl %r13d, %edi
callq 0x721a
decl %ebx
jne 0xa47b
incl %r12d
cmpl %r15d, %r12d
jne 0xa3be
jmp 0xa498
movl (%r14), %ebp
movq 0x8(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x74675(%rip)    # 0x7eb20
movups %xmm0, 0x7465e(%rip)    # 0x7eb10
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x74667(%rip)    # 0x7eb30
movups %xmm1, 0x74670(%rip)    # 0x7eb40
movups %xmm2, 0x74679(%rip)    # 0x7eb50
movq 0x30(%rax), %rax
movq %rax, 0x7467e(%rip)     # 0x7eb60
imull 0x7463f(%rip), %ebp     # 0x7eb28
leaq 0x74640(%rip), %rdi     # 0x7eb30
movl %ebp, %esi
movl $0x2000, %edx           # imm = 0x2000
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8ec8