// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_atmel_cm7.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t chip_id, chip_exid, id, rev;

  dap_reset_link();

  // Stop the core
  dap_write_word(DHCSR, DHCSR_DBGKEY | DHCSR_DEBUGEN | DHCSR_HALT);
  dap_write_word(DEMCR, DEMCR_VC_CORERESET);

  dap_reset_target_hw(1);
  dap_reset_link();

  chip_id = dap_read_word(CHIPID_CIDR);
  chip_exid = dap_read_word(CHIPID_EXID);

  id  = chip_id & DEVICE_ID_MASK;
  rev = chip_id & DEVICE_REV_MASK;

  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 != id || devices[i].chip_exid != chip_exid)
      continue;

    verbose("Target: %s (Rev %c)\n", devices[i].name, 'A' + rev);

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

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

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

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

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

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

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

    target_check_options(&target_options, devices[i].flash_size,
        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 %r12
pushq %rbx
movq %rdi, %rbx
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 $0x1, %edi
callq 0x7013
callq 0x6789
movl $0x400e0940, %edi       # imm = 0x400E0940
callq 0x721a
movl %eax, %ebp
movl $0x400e0944, %edi       # imm = 0x400E0944
callq 0x721a
movl %ebp, %ecx
andl $-0x10, %ecx
movl %ebp, %edx
andl $0xf, %edx
xorl %r14d, %r14d
leaq 0xf93d(%rip), %r12      # 0x1a2e0
cmpl %ecx, (%r14,%r12)
jne 0xa9b0
cmpl %eax, 0x4(%r14,%r12)
je 0xa9d5
addq $0x20, %r14
cmpq $0x420, %r14            # imm = 0x420
jne 0xa9a3
leaq 0x8011(%rip), %rdi      # 0x129d5
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7ca5
leaq (%r12,%r14), %r15
movq 0x10(%r14,%r12), %rsi
addl $0x41, %edx
leaq 0x762e(%rip), %rdi      # 0x12016
xorl %eax, %eax
callq 0x7a90
movl $0x400e0c04, %edi       # imm = 0x400E0C04
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x7269
movl $0x400e0c08, %edi       # imm = 0x400E0C08
callq 0x721a
testb $0x1, %al
je 0xa9fe
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x7e4c(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x7bcc
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
movl 0x18(%r14,%r12), %ebp
xorl %edi, %edi
cmpl %ebp, %eax
sete %dil
leaq 0x7e5b(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x7e57(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bcc
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
testl %eax, %eax
je 0xaa98
movl %eax, %r14d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
decl %r14d
jne 0xaa89
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
testl %eax, %eax
je 0xaab8
movl %eax, %r14d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x721a
decl %r14d
jne 0xaaa9
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x740b0(%rip)    # 0x7eb78
movups %xmm0, 0x74099(%rip)    # 0x7eb68
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x740a7(%rip)    # 0x7eb88
movups %xmm1, 0x740b0(%rip)    # 0x7eb98
movups %xmm2, 0x740b9(%rip)    # 0x7eba8
movq 0x30(%rbx), %rax
movq %rax, 0x740be(%rip)     # 0x7ebb8
leaq 0x74087(%rip), %rdi     # 0x7eb88
movl %ebp, %esi
movl $0x2000, %edx           # imm = 0x2000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8ec8