// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_st_stm32wb55.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(DBGMCU_IDCODE);

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

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

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

    uint32_t total_size = (dap_read_word(FLASH_SIZE_REG) & FLASH_SIZE_REG_MASK) * FLASH_SIZE_REG_MULT;

    verbose("Total flash size: %d bytes\n", total_size);

    uint32_t sfr = dap_read_word(FLASH_SFR);

    if (sfr & FLASH_SFR_FSD)
    {
      target_flash_size = total_size;
    }
    else
    {
      target_flash_size = (sfr & FLASH_SFR_SFSA_MASK) * FLASH_SFR_SFSA_MULT;
      verbose("Accessible due to SFSA: %d bytes\n", target_flash_size);
    }

    target_check_options(&target_options, target_flash_size, FLASH_PAGE_SIZE);

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

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

    dap_write_word(FLASH_KEYR, FLASH_KEYR_KEY1);
    dap_write_word(FLASH_KEYR, FLASH_KEYR_KEY2);
    dap_write_word(FLASH_OPTKEYR, FLASH_OPTKEYR_KEY1);
    dap_write_word(FLASH_OPTKEYR, FLASH_OPTKEYR_KEY2);
    dap_write_word(FLASH_CR, 0);
    dap_write_word(FLASH_SR, FLASH_SR_ALL_ERRORS);

    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 (DBGMCU_IDCODE = 0x%08x)", idcode);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
callq 0x64f0
movl $0x1, %edi
callq 0x6480
xorl %edi, %edi
callq 0x6f90
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 $0x1, %edi
callq 0x6f90
movl $0xa, %edi
callq 0x8a90
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x7740
movl %eax, -0xc(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jge 0xed8b
movslq -0x14(%rbp), %rcx
leaq 0x106af(%rip), %rax     # 0x1f260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
movl -0xc(%rbp), %ecx
andl $0xfff, %ecx            # imm = 0xFFF
cmpl %ecx, %eax
je 0xebcc
jmp 0xed7d
movslq -0x14(%rbp), %rcx
leaq 0x10689(%rip), %rax     # 0x1f260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
leaq 0x7cec(%rip), %rdi      # 0x168d5
movb $0x0, %al
callq 0x8630
movslq -0x14(%rbp), %rcx
leaq 0x10665(%rip), %rax     # 0x1f260
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rcx
movq %rcx, 0x73834(%rip)     # 0x82440
movq 0x8(%rax), %rcx
movq %rcx, 0x73831(%rip)     # 0x82448
movq 0x10(%rax), %rax
movq %rax, 0x7382e(%rip)     # 0x82450
movq -0x8(%rbp), %rsi
leaq 0x7382b(%rip), %rdi     # 0x82458
movl $0x38, %edx
callq 0x61a0
movl $0x1fff75e0, %edi       # imm = 0x1FFF75E0
callq 0x7740
andl $0xffff, %eax           # imm = 0xFFFF
shll $0xa, %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %esi
leaq 0x8916(%rip), %rdi      # 0x1756c
movb $0x0, %al
callq 0x8630
movl $0x58004080, %edi       # imm = 0x58004080
callq 0x7740
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xec82
movl -0x18(%rbp), %eax
movl %eax, 0x73810(%rip)     # 0x82490
jmp 0xeca7
movl -0x1c(%rbp), %eax
andl $0xff, %eax
shll $0xc, %eax
movl %eax, 0x737fd(%rip)     # 0x82490
movl 0x737f7(%rip), %esi     # 0x82490
leaq 0x88e8(%rip), %rdi      # 0x17588
movb $0x0, %al
callq 0x8630
movl 0x737e3(%rip), %esi     # 0x82490
leaq 0x737a4(%rip), %rdi     # 0x82458
movl $0x1000, %edx           # imm = 0x1000
callq 0xa5f0
movl $0x58004020, %edi       # imm = 0x58004020
callq 0x7740
movl %eax, %ecx
andl $0xff, %ecx
movl $0xaa, %eax
cmpl %ecx, %eax
setne %al
andb $0x1, %al
movb %al, -0xd(%rbp)
testb $0x1, -0xd(%rbp)
je 0xecfd
movq -0x8(%rbp), %rax
testb $0x1, 0x8(%rax)
jne 0xecfd
leaq 0x73a5(%rip), %rdi      # 0x1609b
movb $0x0, %al
callq 0x8970
movl $0x58004008, %edi       # imm = 0x58004008
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x77d0
movl $0x58004008, %edi       # imm = 0x58004008
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x77d0
movl $0x5800400c, %edi       # imm = 0x5800400C
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x77d0
movl $0x5800400c, %edi       # imm = 0x5800400C
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x77d0
movl $0x58004014, %edi       # imm = 0x58004014
xorl %esi, %esi
callq 0x77d0
movl $0x58004010, %edi       # imm = 0x58004010
movl $0xc3fa, %esi           # imm = 0xC3FA
callq 0x77d0
movl $0x58004014, %edi       # imm = 0x58004014
callq 0x7740
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x8690(%rip), %rsi      # 0x17404
xorl %eax, %eax
callq 0x8880
jmp 0xed9c
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0xeb9c
movl -0xc(%rbp), %esi
leaq 0x8815(%rip), %rdi      # 0x175aa
movb $0x0, %al
callq 0x8970
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)