// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_rpi_rp2040.c
// Function: target_select
static void target_select(target_options_t *options)
{
  uint32_t idr;
  int flash_size, rev;

  dap_set_dp_version(2);

  dap_set_target_id(TARGET_ID_RESCUE);
  dap_reset_link();
  dap_clear_pwrup_req();

  dap_set_target_id(TARGET_ID_CORE0);
  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);

  idr = dap_read_word(QSPI_IDR);

  check(idr == 0x51535049, "QSPI controller not found");

  rev = dap_read_byte(ROM_REVISION_ADDR);

  if (rev == 1 || rev == 2 || rev == 3)
    verbose("Target: RP2040 (Rev B%d)\n", rev-1);
  else
    error_exit("unknown target device (ROM revision = %d)", rev);

  flash_prepare();

  flash_size = flash_get_size();

  if (flash_size > 1024*1024)
    verbose("Flash size: %d MB\n", flash_size / (1024*1024));
  else if (flash_size > 0)
    verbose("Flash size: %d KB\n", flash_size / 1024);
  else
    error_exit("unknown flash device");

  check(flash_size <= 16*1024*1024, "flash size larger than 16 MB is not supported");

  target_options = *options;
  target_check_options(&target_options, flash_size, FLASH_SECTOR_SIZE);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x8(%rbp)
movl $0x2, %edi
callq 0x63e0
movl $0xf1002927, %edi       # imm = 0xF1002927
callq 0x6400
callq 0x6800
callq 0x6e80
movl $0x1002927, %edi        # imm = 0x1002927
callq 0x6400
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 $0x18000058, %edi       # imm = 0x18000058
callq 0x7740
movl %eax, -0xc(%rbp)
cmpl $0x51535049, -0xc(%rbp) # imm = 0x51535049
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x68a3(%rip), %rsi      # 0x17b3d
xorl %eax, %eax
callq 0x8880
movl $0x13, %edi
callq 0x76e0
movzbl %al, %eax
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
je 0x112c3
cmpl $0x2, -0x14(%rbp)
je 0x112c3
cmpl $0x3, -0x14(%rbp)
jne 0x112d9
movl -0x14(%rbp), %esi
subl $0x1, %esi
leaq 0x6887(%rip), %rdi      # 0x17b57
movb $0x0, %al
callq 0x8630
jmp 0x112ea
movl -0x14(%rbp), %esi
leaq 0x688e(%rip), %rdi      # 0x17b71
movb $0x0, %al
callq 0x8970
callq 0x11810
callq 0x119c0
movl %eax, -0x10(%rbp)
cmpl $0x100000, -0x10(%rbp)  # imm = 0x100000
jle 0x1131d
movl -0x10(%rbp), %eax
movl $0x100000, %ecx         # imm = 0x100000
cltd
idivl %ecx
movl %eax, %esi
leaq 0x6887(%rip), %rdi      # 0x17b9b
movb $0x0, %al
callq 0x8630
jmp 0x11350
cmpl $0x0, -0x10(%rbp)
jle 0x11340
movl -0x10(%rbp), %eax
movl $0x400, %ecx            # imm = 0x400
cltd
idivl %ecx
movl %eax, %esi
leaq 0x6877(%rip), %rdi      # 0x17bae
movb $0x0, %al
callq 0x8630
jmp 0x1134e
leaq 0x687a(%rip), %rdi      # 0x17bc1
movb $0x0, %al
callq 0x8970
jmp 0x11350
cmpl $0x1000000, -0x10(%rbp) # imm = 0x1000000
setle %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x686f(%rip), %rsi      # 0x17bd6
xorl %eax, %eax
callq 0x8880
movq -0x8(%rbp), %rsi
leaq 0x71217(%rip), %rdi     # 0x82590
movl $0x38, %edx
callq 0x61a0
movl -0x10(%rbp), %esi
leaq 0x71203(%rip), %rdi     # 0x82590
movl $0x1000, %edx           # imm = 0x1000
callq 0xa5f0
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)