// File: /workspace/github_c_makefile_star2_O1/ataradov[P]edbg/target_puya_py32f0.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idcode;
  bool locked;

  dap_disconnect();
  dap_connect(DAP_INTERFACE_SWD);
  dap_reset_pin(0);
  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);

  dap_reset_pin(1);
  sleep_ms(10);

  idcode = dap_read_word(DBG_IDCODE);

  for (int i = 0; i < ARRAY_SIZE(devices); i++)
  {
    if (devices[i].idcode != idcode)
      continue;

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

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

    target_check_options(&target_options, target_device.flash_size, FLASH_PAGE_SIZE);

    locked = (0xaa != (dap_read_word(OPTIONS_OPTR) & FLASH_OPTR_RDP_MASK));

    if (locked && !options->unlock)
      error_exit("target is locked, unlock is necessary");

    dap_write_word_req(FLASH_KEYR, FLASH_KEYR_KEY1);
    dap_write_word_req(FLASH_KEYR, FLASH_KEYR_KEY2);
    dap_write_word_req(FLASH_OPTKEYR, FLASH_OPTKEYR_KEY1);
    dap_write_word_req(FLASH_OPTKEYR, FLASH_OPTKEYR_KEY2);
    dap_write_word_req(FLASH_CR, 0);
    dap_transfer();

    check(0 == dap_read_word(FLASH_CR), "Failed to unlock the flash for write operation. Try to power cycle the target.");

    return;
  }

  error_exit("unknown target device (DBG_IDCODE = 0x%08x)", idcode);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
callq 0x65ab
movl $0x1, %edi
callq 0x654e
xorl %r14d, %r14d
xorl %edi, %edi
callq 0x7074
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 $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x723e
movl $0x1, %edi
callq 0x7074
movl $0xa, %edi
callq 0x7d74
movl $0x40015800, %edi       # imm = 0x40015800
callq 0x71ef
leaq 0xc418(%rip), %r15      # 0x1ad80
cmpl %eax, (%r14,%r15)
je 0xe98d
addq $0x20, %r14
cmpq $0x20, %r14
je 0xe968
leaq 0x4a87(%rip), %rdi      # 0x13406
movl %eax, %esi
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
jmp 0x7c8d
movq 0x10(%r14,%r15), %rsi
leaq 0x3edc(%rip), %rdi      # 0x12875
xorl %eax, %eax
callq 0x7a78
movups (%r14,%r15), %xmm0
movups 0x10(%r14,%r15), %xmm1
movups %xmm1, 0x7049e(%rip)    # 0x7ee50
movups %xmm0, 0x70487(%rip)    # 0x7ee40
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x70495(%rip)    # 0x7ee60
movups %xmm1, 0x7049e(%rip)    # 0x7ee70
movups %xmm2, 0x704a7(%rip)    # 0x7ee80
movq 0x30(%rbx), %rax
movq %rax, 0x704ac(%rip)     # 0x7ee90
leaq 0x70475(%rip), %rdi     # 0x7ee60
movl 0x70467(%rip), %esi     # 0x7ee58
movl $0x80, %edx
callq 0x8edf
movl $0x1fff0e80, %edi       # imm = 0x1FFF0E80
callq 0x71ef
cmpb $-0x56, %al
je 0xea1d
cmpb $0x0, 0x8(%rbx)
jne 0xea1d
leaq 0x3625(%rip), %rdi      # 0x1203b
xorl %eax, %eax
callq 0x7c8d
movl $0x40022008, %edi       # imm = 0x40022008
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x70f4
movl $0x40022008, %edi       # imm = 0x40022008
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x70f4
movl $0x4002200c, %edi       # imm = 0x4002200C
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x70f4
movl $0x4002200c, %edi       # imm = 0x4002200C
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x70f4
movl $0x40022014, %edi       # imm = 0x40022014
xorl %esi, %esi
callq 0x70f4
callq 0x6a6f
movl $0x40022014, %edi       # imm = 0x40022014
callq 0x71ef
xorl %edi, %edi
testl %eax, %eax
sete %dil
leaq 0x4934(%rip), %rsi      # 0x133b7
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
jmp 0x7bb4