// File: /workspace/github_c_makefile_star2_O1/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, %r15
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
movl $0x400e0740, %edi       # imm = 0x400E0740
callq 0x71ef
movl %eax, %ebp
movl $0x400e0744, %edi       # imm = 0x400E0744
callq 0x71ef
movl $0x1c, %ebx
leaq 0xf871(%rip), %r14      # 0x19c00
cmpl %ebp, -0x1c(%rbx,%r14)
jne 0xa39d
cmpl %eax, -0x18(%rbx,%r14)
je 0xa3c8
addq $0x20, %rbx
cmpq $0x6fc, %rbx            # imm = 0x6FC
jne 0xa38f
leaq 0x8634(%rip), %rdi      # 0x129e5
movl %ebp, %esi
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x7c8d
movq %r15, 0x10(%rsp)
leaq (%rbx,%r14), %r15
addq $-0x1c, %r15
movq 0x10(%r15), %rsi
leaq 0x8495(%rip), %rdi      # 0x12875
xorl %eax, %eax
callq 0x7a78
movq %r15, 0x8(%rsp)
movl 0x18(%r15), %r15d
testl %r15d, %r15d
je 0xa4d0
xorl %r12d, %r12d
movl %r12d, %r13d
shll $0x9, %r13d
leal 0x400e0a04(%r13), %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x723e
leal 0x400e0a08(%r13), %ebp
movl %ebp, %edi
callq 0x71ef
testb $0x1, %al
je 0xa41b
addl $0x400e0a0c, %r13d      # imm = 0x400E0A0C
movl %r13d, %edi
callq 0x71ef
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x843d(%rip), %rsi      # 0x12881
xorl %eax, %eax
callq 0x7bb4
movl %r13d, %edi
callq 0x71ef
xorl %edi, %edi
cmpl (%rbx,%r14), %eax
sete %dil
leaq 0x8451(%rip), %rsi      # 0x128b5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl %r13d, %edi
callq 0x71ef
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x844f(%rip), %rsi      # 0x128d6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl %r13d, %edi
callq 0x71ef
movl %eax, %ebp
testl %eax, %eax
je 0xa4aa
movl %r13d, %edi
callq 0x71ef
decl %ebp
jne 0xa49e
movl %r13d, %edi
callq 0x71ef
movl %eax, %ebp
testl %eax, %eax
je 0xa4c4
movl %r13d, %edi
callq 0x71ef
decl %ebp
jne 0xa4b8
incl %r12d
cmpl %r15d, %r12d
jne 0xa3fc
movq 0x8(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups %xmm1, 0x7463d(%rip)    # 0x7eb20
movups %xmm0, 0x74626(%rip)    # 0x7eb10
movq 0x10(%rsp), %rax
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm0, 0x7462f(%rip)    # 0x7eb30
movups %xmm1, 0x74638(%rip)    # 0x7eb40
movups %xmm2, 0x74641(%rip)    # 0x7eb50
movq 0x30(%rax), %rax
movq %rax, 0x74646(%rip)     # 0x7eb60
movl 0x74608(%rip), %esi     # 0x7eb28
imull (%rbx,%r14), %esi
leaq 0x74604(%rip), %rdi     # 0x7eb30
movl $0x2000, %edx           # imm = 0x2000
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x8edf