// File: /workspace/github_c_makefile_star2_O2/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
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x6481
pushq $0x1
popq %rbp
movl %ebp, %edi
callq 0x6429
xorl %edi, %edi
callq 0x6e95
callq 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl %ebp, %esi
callq 0x700c
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x700c
movl %ebp, %edi
callq 0x6e95
pushq $0xa
popq %rdi
callq 0x7aeb
movl $0xe0042000, %edi       # imm = 0xE0042000
callq 0x6fe1
movl %eax, %ecx
andl $0xfff, %ecx            # imm = 0xFFF
cmpl $0x495, %ecx            # imm = 0x495
jne 0xbbee
leaq 0x6d02(%rip), %rdi      # 0x12865
leaq 0x7a1c(%rip), %rsi      # 0x13586
xorl %eax, %eax
callq 0x773c
movq 0x30(%rbx), %rax
movq %rax, 0x74194(%rip)     # 0x7fd10
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x74172(%rip)    # 0x7fd00
movups %xmm1, 0x7415b(%rip)    # 0x7fcf0
movups %xmm0, 0x74144(%rip)    # 0x7fce0
movl $0x1fff75e0, %edi       # imm = 0x1FFF75E0
callq 0x6fe1
movzwl %ax, %ebp
shll $0xa, %ebp
leaq 0x795c(%rip), %rdi      # 0x1350f
movl %ebp, %esi
xorl %eax, %eax
callq 0x773c
movl $0x58004080, %edi       # imm = 0x58004080
callq 0x6fe1
btl $0x8, %eax
jb 0xbc04
movzbl %al, %esi
shll $0xc, %esi
movl %esi, 0x74140(%rip)     # 0x7fd18
leaq 0x794c(%rip), %rdi      # 0x1352b
xorl %eax, %eax
callq 0x773c
movl 0x7412c(%rip), %ebp     # 0x7fd18
jmp 0xbc0a
leaq 0x7958(%rip), %rdi      # 0x1354d
movl %eax, %esi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x7a0e
movl %ebp, 0x7410e(%rip)     # 0x7fd18
leaq 0x740cf(%rip), %rdi     # 0x7fce0
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x8b8e
movl $0x58004020, %edi       # imm = 0x58004020
callq 0x6fe1
cmpb $-0x56, %al
je 0xbc3f
cmpb $0x0, 0x8(%rbx)
jne 0xbc3f
leaq 0x63f3(%rip), %rdi      # 0x1202b
xorl %eax, %eax
callq 0x7a0e
movl $0x58004008, %edi       # imm = 0x58004008
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x700c
movl $0x58004008, %edi       # imm = 0x58004008
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x700c
movl $0x5800400c, %edi       # imm = 0x5800400C
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x700c
movl $0x5800400c, %edi       # imm = 0x5800400C
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x700c
movl $0x58004014, %edi       # imm = 0x58004014
xorl %esi, %esi
callq 0x700c
movl $0x58004010, %edi       # imm = 0x58004010
movl $0xc3fa, %esi           # imm = 0xC3FA
callq 0x700c
movl $0x58004014, %edi       # imm = 0x58004014
callq 0x6fe1
xorl %edi, %edi
testl %eax, %eax
sete %dil
leaq 0x76f8(%rip), %rsi      # 0x133a7
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %rbp
jmp 0x793d