// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_atmel_cm3.c
// Function: target_select
static void target_select(target_options_t *options)
{
  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);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    uint32_t chip_id, chip_exid;
    uint32_t fl_id, fl_size, fl_page_size, fl_nb_palne, fl_nb_lock;
    uint32_t flash_size = 0;

    chip_id = dap_read_word(CHIPID_CIDR(devices[i].chipid_base));
    chip_exid = dap_read_word(CHIPID_EXID(devices[i].chipid_base));

    if (devices[i].chip_id != chip_id || CHIPID_EXID_VALUE != chip_exid)
      continue;

    verbose("Target: %s\n", devices[i].name);

    for (int i = 0; i < devices[i].n_planes; i++)
    {
      uint32_t eefc_base = devices[i].plane[i].eefc_base;

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

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

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

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

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

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

      flash_size += fl_size;
    }

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

    target_check_options(&target_options, flash_size, FLASH_PAGE_SIZE);

    return;
  }

  error_exit("unknown target device");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
callq 0x6ef0
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 $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x77d0
movl $0x0, -0xc(%rbp)
cmpl $0xd, -0xc(%rbp)
jge 0xbab0
movl $0x0, -0x2c(%rbp)
movslq -0xc(%rbp), %rcx
leaq 0x11d95(%rip), %rax     # 0x1d5d0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %edi
addl $0x0, %edi
callq 0x7740
movl %eax, -0x10(%rbp)
movslq -0xc(%rbp), %rcx
leaq 0x11d75(%rip), %rax     # 0x1d5d0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %edi
addl $0x4, %edi
callq 0x7740
movl %eax, -0x14(%rbp)
movslq -0xc(%rbp), %rcx
leaq 0x11d55(%rip), %rax     # 0x1d5d0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
cmpl -0x10(%rbp), %eax
jne 0xb890
xorl %eax, %eax
cmpl -0x14(%rbp), %eax
je 0xb895
jmp 0xbaa2
movslq -0xc(%rbp), %rcx
leaq 0x11d30(%rip), %rax     # 0x1d5d0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0xb023(%rip), %rdi      # 0x168d5
movb $0x0, %al
callq 0x8630
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movslq -0x30(%rbp), %rdx
leaq 0x11d02(%rip), %rcx     # 0x1d5d0
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
jge 0xba54
movslq -0x30(%rbp), %rcx
leaq 0x11ce7(%rip), %rax     # 0x1d5d0
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
addq $0x20, %rax
movslq -0x30(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %edi
addl $0x4, %edi
movl $0x5a000000, %esi       # imm = 0x5A000000
callq 0x77d0
movl -0x34(%rbp), %edi
addl $0x8, %edi
callq 0x7740
movl %eax, %eax
movl %eax, %ecx
andq $0x1, %rcx
xorl %eax, %eax
cmpq %rcx, %rax
jne 0xb930
jmp 0xb914
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
setne %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xaf8f(%rip), %rsi      # 0x168e1
xorl %eax, %eax
callq 0x8880
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movslq -0x30(%rbp), %rdx
leaq 0x11c5b(%rip), %rcx     # 0x1d5d0
imulq $0x38, %rdx, %rdx
addq %rdx, %rcx
addq $0x20, %rcx
movslq -0x30(%rbp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x8(%rcx), %eax
sete %al
movl -0x1c(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0xaf74(%rip), %rsi      # 0x16915
xorl %eax, %eax
callq 0x8880
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl %eax, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
sete %al
movl -0x20(%rbp), %edx
movzbl %al, %edi
andl $0x1, %edi
leaq 0xaf66(%rip), %rsi      # 0x16936
xorl %eax, %eax
callq 0x8880
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl %eax, -0x24(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0xba0a
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0xb9ec
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl %eax, -0x28(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jae 0xba3d
movl -0x34(%rbp), %edi
addl $0xc, %edi
callq 0x7740
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0xba1f
movl -0x1c(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xb8c0
movslq -0xc(%rbp), %rax
leaq 0x11b71(%rip), %rsi     # 0x1d5d0
imulq $0x38, %rax, %rax
addq %rax, %rsi
leaq 0x76793(%rip), %rdi     # 0x82200
movl $0x38, %edx
callq 0x61a0
movq -0x8(%rbp), %rsi
leaq 0x767b6(%rip), %rdi     # 0x82238
movl $0x38, %edx
callq 0x61a0
movl -0x2c(%rbp), %esi
leaq 0x767a2(%rip), %rdi     # 0x82238
movl $0x100, %edx            # imm = 0x100
callq 0xa5f0
jmp 0xbabe
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
jmp 0xb81f
leaq 0xae9f(%rip), %rdi      # 0x16956
movb $0x0, %al
callq 0x8970
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)