// File: /workspace/github_c_makefile_star2_O2/ataradov[P]edbg/target_st_stm32g0.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idcode, flash_size;
  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 & DEVICE_ID_MASK))
      continue;

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

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

    flash_size = (dap_read_word(FLASH_SIZE_REG) & FLASH_SIZE_REG_MASK) * FLASH_SIZE_REG_MULT;

    target_check_options(&target_options, 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(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);

    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 %rbp
pushq %r14
pushq %rbx
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 $0x40015800, %edi       # imm = 0x40015800
callq 0x6fe1
cmpw $0x6460, %ax            # imm = 0x6460
je 0xb67b
movzwl %ax, %ecx
cmpl $0x6466, %ecx           # imm = 0x6466
jne 0xb787
leaq 0x1067f(%rip), %r14     # 0x1bcf8
jmp 0xb682
leaq 0x1065e(%rip), %r14     # 0x1bce0
movq 0x10(%r14), %rsi
leaq 0x71d8(%rip), %rdi      # 0x12865
xorl %eax, %eax
callq 0x773c
movq 0x10(%r14), %rax
movq %rax, 0x74601(%rip)     # 0x7fca0
movups (%r14), %xmm0
movups %xmm0, 0x745e6(%rip)    # 0x7fc90
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm0, 0x745ec(%rip)    # 0x7fca8
movups %xmm1, 0x745f5(%rip)    # 0x7fcb8
movups %xmm2, 0x745fe(%rip)    # 0x7fcc8
movq 0x30(%rbx), %rax
movq %rax, 0x74603(%rip)     # 0x7fcd8
leaq 0x745cc(%rip), %r14     # 0x7fca8
movl $0x1fff75e0, %edi       # imm = 0x1FFF75E0
callq 0x6fe1
movzbl %al, %esi
shll $0xa, %esi
movq %r14, %rdi
movl $0x800, %edx            # imm = 0x800
callq 0x8b8e
movl $0x1fff7800, %edi       # imm = 0x1FFF7800
callq 0x6fe1
cmpb $-0x56, %al
je 0xb71b
cmpb $0x0, 0x8(%rbx)
jne 0xb71b
leaq 0x6917(%rip), %rdi      # 0x1202b
xorl %eax, %eax
callq 0x7a0e
movl $0x40022008, %edi       # imm = 0x40022008
movl $0x45670123, %esi       # imm = 0x45670123
callq 0x700c
movl $0x40022008, %edi       # imm = 0x40022008
movl $0xcdef89ab, %esi       # imm = 0xCDEF89AB
callq 0x700c
movl $0x4002200c, %edi       # imm = 0x4002200C
movl $0x8192a3b, %esi        # imm = 0x8192A3B
callq 0x700c
movl $0x4002200c, %edi       # imm = 0x4002200C
movl $0x4c5d6e7f, %esi       # imm = 0x4C5D6E7F
callq 0x700c
movl $0x40022014, %edi       # imm = 0x40022014
xorl %esi, %esi
callq 0x700c
movl $0x40022014, %edi       # imm = 0x40022014
callq 0x6fe1
xorl %edi, %edi
testl %eax, %eax
sete %dil
leaq 0x7c2b(%rip), %rsi      # 0x133a7
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
jmp 0x793d
leaq 0x7c68(%rip), %rdi      # 0x133f6
movl %eax, %esi
xorl %eax, %eax
popq %rbx
popq %r14
popq %rbp
jmp 0x7a0e