// File: /workspace/github_c_makefile_star2_O1/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 0x723e
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x723e
movl $0x1, %edi
callq 0x6fe8
callq 0x6789
movl $0x400e0940, %edi       # imm = 0x400E0940
callq 0x71ef
movl %eax, %ebp
movl $0x400e0944, %edi       # imm = 0x400E0944
callq 0x71ef
movl %ebp, %ecx
andl $-0x10, %ecx
movl %ebp, %edx
andl $0xf, %edx
xorl %r14d, %r14d
leaq 0xf919(%rip), %r12      # 0x1a2e0
cmpl %ecx, (%r14,%r12)
jne 0xa9d4
cmpl %eax, 0x4(%r14,%r12)
je 0xa9f9
addq $0x20, %r14
cmpq $0x420, %r14            # imm = 0x420
jne 0xa9c7
leaq 0x7ffd(%rip), %rdi      # 0x129e5
movl %ebp, %esi
xorl %eax, %eax
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x7c8d
leaq (%r12,%r14), %r15
movq 0x10(%r14,%r12), %rsi
addl $0x41, %edx
leaq 0x761a(%rip), %rdi      # 0x12026
xorl %eax, %eax
callq 0x7a78
movl $0x400e0c04, %edi       # imm = 0x400E0C04
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x723e
movl $0x400e0c08, %edi       # imm = 0x400E0C08
callq 0x71ef
testb $0x1, %al
je 0xaa22
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
xorl %edi, %edi
testl %eax, %eax
setne %dil
leaq 0x7e38(%rip), %rsi      # 0x12881
xorl %eax, %eax
callq 0x7bb4
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
movl 0x18(%r14,%r12), %ebp
xorl %edi, %edi
cmpl %ebp, %eax
sete %dil
leaq 0x7e47(%rip), %rsi      # 0x128b5
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
xorl %edi, %edi
cmpl $0x200, %eax            # imm = 0x200
sete %dil
leaq 0x7e43(%rip), %rsi      # 0x128d6
movl %eax, %edx
xorl %eax, %eax
callq 0x7bb4
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
testl %eax, %eax
je 0xaabc
movl %eax, %r14d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
decl %r14d
jne 0xaaad
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
testl %eax, %eax
je 0xaadc
movl %eax, %r14d
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x71ef
decl %r14d
jne 0xaacd
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movups %xmm1, 0x7408c(%rip)    # 0x7eb78
movups %xmm0, 0x74075(%rip)    # 0x7eb68
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x74083(%rip)    # 0x7eb88
movups %xmm1, 0x7408c(%rip)    # 0x7eb98
movups %xmm2, 0x74095(%rip)    # 0x7eba8
movq 0x30(%rbx), %rax
movq %rax, 0x7409a(%rip)     # 0x7ebb8
leaq 0x74063(%rip), %rdi     # 0x7eb88
movl %ebp, %esi
movl $0x2000, %edx           # imm = 0x2000
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x8edf