// File: /workspace/github_c_makefile_star2_O2/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 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
pushq $0x1
popq %rbp
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl %ebp, %esi
callq 0x700c
movl %ebp, %edi
callq 0x6e14
callq 0x664a
movl $0x400e0940, %edi       # imm = 0x400E0940
callq 0x6fe1
movl %eax, %ebp
movl $0x400e0944, %edi       # imm = 0x400E0944
callq 0x6fe1
movl %ebp, %ecx
andl $-0x10, %ecx
movl %ebp, %edx
andl $0xf, %edx
xorl %r14d, %r14d
leaq 0x10d98(%rip), %r15     # 0x1b2e0
cmpq $0x420, %r14            # imm = 0x420
je 0xa564
cmpl %ecx, (%r14,%r15)
jne 0xa55e
cmpl %eax, 0x4(%r14,%r15)
je 0xa57c
addq $0x20, %r14
jmp 0xa548
leaq 0x846a(%rip), %rdi      # 0x129d5
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7a0e
leaq (%r15,%r14), %r12
movq 0x10(%r14,%r15), %rsi
addl $0x41, %edx
leaq 0x7a87(%rip), %rdi      # 0x12016
xorl %eax, %eax
callq 0x773c
movl $0x400e0c04, %edi       # imm = 0x400E0C04
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x700c
movl $0x400e0c08, %edi       # imm = 0x400E0C08
callq 0x6fe1
testb $0x1, %al
je 0xa5a5
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x82a5(%rip), %rsi      # 0x12871
xorl %eax, %eax
callq 0x793d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
movl 0x18(%r14,%r15), %ebp
xorl %edi, %edi
cmpl %ebp, %eax
sete %dil
leaq 0x82b4(%rip), %rsi      # 0x128a5
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x82b0(%rip), %rsi      # 0x128c6
movl %eax, %edx
xorl %eax, %eax
callq 0x793d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
movl %eax, %r15d
incl %r15d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
decl %r15d
jne 0xa62f
movl %eax, %r14d
subl $0x1, %r14d
jb 0xa653
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x6fe1
jmp 0xa641
movups (%r12), %xmm0
movups 0x10(%r12), %xmm1
movups %xmm1, 0x75513(%rip)    # 0x7fb78
movups %xmm0, 0x754fc(%rip)    # 0x7fb68
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x7550a(%rip)    # 0x7fb88
movups %xmm1, 0x75513(%rip)    # 0x7fb98
movups %xmm2, 0x7551c(%rip)    # 0x7fba8
movq 0x30(%rbx), %rax
movq %rax, 0x75521(%rip)     # 0x7fbb8
leaq 0x754ea(%rip), %rdi     # 0x7fb88
movl %ebp, %esi
movl $0x2000, %edx           # imm = 0x2000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8b8e