// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
callq 0x6800
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x77d0
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x77d0
movl $0x1, %edi
callq 0x6ef0
callq 0x6800
movl $0x400e0940, %edi       # imm = 0x400E0940
callq 0x7740
movl %eax, -0xc(%rbp)
movl $0x400e0944, %edi       # imm = 0x400E0944
callq 0x7740
movl %eax, -0x10(%rbp)
movl -0xc(%rbp), %eax
andl $-0x10, %eax
movl %eax, -0x14(%rbp)
movl -0xc(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
cmpl $0x21, -0x1c(%rbp)
jge 0xcd02
movslq -0x1c(%rbp), %rcx
leaq 0x115ea(%rip), %rax     # 0x1e0f0
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0xcb2e
movslq -0x1c(%rbp), %rcx
leaq 0x115d1(%rip), %rax     # 0x1e0f0
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
cmpl -0x10(%rbp), %eax
je 0xcb33
jmp 0xccf4
movslq -0x1c(%rbp), %rcx
leaq 0x115b2(%rip), %rax     # 0x1e0f0
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
movl -0x18(%rbp), %edx
addl $0x41, %edx
leaq 0x9530(%rip), %rdi      # 0x16086
movb $0x0, %al
callq 0x8630
movl $0x400e0c04, %edi       # imm = 0x400E0C04
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x77d0
movl $0x400e0c08, %edi       # imm = 0x400E0C08
callq 0x7740
movl %eax, %ecx
andl $0x1, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0xcb83
jmp 0xcb6c
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x9d3d(%rip), %rsi      # 0x168e1
xorl %eax, %eax
callq 0x8880
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movslq -0x1c(%rbp), %rdx
leaq 0x1152a(%rip), %rcx     # 0x1e0f0
shlq $0x5, %rdx
addq %rdx, %rcx
cmpl 0x18(%rcx), %eax
sete %al
movl -0x24(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0x9d32(%rip), %rsi      # 0x16915
xorl %eax, %eax
callq 0x8880
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl %eax, -0x28(%rbp)
cmpl $0x200, -0x28(%rbp)     # imm = 0x200
sete %al
movl -0x28(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0x9d25(%rip), %rsi      # 0x16936
xorl %eax, %eax
callq 0x8880
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl %eax, -0x2c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jae 0xcc49
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xcc2c
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl %eax, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x30(%rbp), %eax
jae 0xcc7a
movl $0x400e0c0c, %edi       # imm = 0x400E0C0C
callq 0x7740
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xcc5d
movslq -0x1c(%rbp), %rcx
leaq 0x1146b(%rip), %rax     # 0x1e0f0
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x75632(%rip)     # 0x822c8
movq 0x8(%rax), %rcx
movq %rcx, 0x7562f(%rip)     # 0x822d0
movq 0x10(%rax), %rcx
movq %rcx, 0x7562c(%rip)     # 0x822d8
movq 0x18(%rax), %rax
movq %rax, 0x75629(%rip)     # 0x822e0
movq -0x8(%rbp), %rsi
leaq 0x75626(%rip), %rdi     # 0x822e8
movl $0x38, %edx
callq 0x61a0
movslq -0x1c(%rbp), %rcx
leaq 0x11419(%rip), %rax     # 0x1e0f0
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x18(%rax), %esi
leaq 0x75600(%rip), %rdi     # 0x822e8
movl $0x2000, %edx           # imm = 0x2000
callq 0xa5f0
jmp 0xcd13
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xcaf1
movl -0xc(%rbp), %esi
leaq 0x9d39(%rip), %rdi      # 0x16a45
movb $0x0, %al
callq 0x8970
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)