// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
movl $0x2, %edi
callq 0x64fc
movl $0xf1002927, %edi       # imm = 0xF1002927
callq 0x6503
callq 0x6789
callq 0x6f8f
movl $0x1002927, %edi        # imm = 0x1002927
callq 0x6503
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 $0x18000058, %edi       # imm = 0x18000058
callq 0x71ef
xorl %edi, %edi
cmpl $0x51535049, %eax       # imm = 0x51535049
sete %dil
leaq 0x5d93(%rip), %rsi      # 0x13aed
xorl %eax, %eax
callq 0x7bb4
movl $0x13, %edi
callq 0x71b5
movzbl %al, %esi
leal -0x1(%rsi), %eax
cmpb $0x2, %al
ja 0xdd87
decl %esi
leaq 0x5d89(%rip), %rdi      # 0x13b07
xorl %eax, %eax
callq 0x7a78
jmp 0xdd95
leaq 0x5d93(%rip), %rdi      # 0x13b21
xorl %eax, %eax
callq 0x7c8d
leaq 0x10(%rsp), %r14
movl $0x0, (%r14)
movl $0x4000f000, %edi       # imm = 0x4000F000
movl $0x244, %esi            # imm = 0x244
callq 0x70f4
movl $0x40018014, %edi       # imm = 0x40018014
xorl %esi, %esi
callq 0x70f4
movl $0x4001801c, %edi       # imm = 0x4001801C
xorl %esi, %esi
callq 0x70f4
movl $0x40018024, %edi       # imm = 0x40018024
xorl %esi, %esi
callq 0x70f4
movl $0x4001802c, %edi       # imm = 0x4001802C
xorl %esi, %esi
callq 0x70f4
movl $0x40018004, %edi       # imm = 0x40018004
xorl %esi, %esi
callq 0x70f4
movl $0x4001800c, %edi       # imm = 0x4001800C
xorl %esi, %esi
callq 0x70f4
callq 0x6a6f
movl $0x18000008, %edi       # imm = 0x18000008
xorl %esi, %esi
callq 0x70f4
movl $0x18000014, %edi       # imm = 0x18000014
movl $0x2, %esi
callq 0x70f4
movl $0x18000000, %edi       # imm = 0x18000000
movl $0x70000, %esi          # imm = 0x70000
callq 0x70f4
movl $0x1, %r15d
movl $0x18000010, %edi       # imm = 0x18000010
movl $0x1, %esi
callq 0x70f4
movl $0x18000008, %edi       # imm = 0x18000008
movl $0x1, %esi
callq 0x70f4
movl $0x1800004c, %edi       # imm = 0x1800004C
movl $0x3, %esi
callq 0x70f4
movl $0x50000014, %edi       # imm = 0x50000014
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x70f4
movl $0x50000010, %edi       # imm = 0x50000010
movl $0x138023, %esi         # imm = 0x138023
callq 0x70f4
movl $0x50000058, %edi       # imm = 0x50000058
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x70f4
movl $0x50000050, %edi       # imm = 0x50000050
movl $0x130811, %esi         # imm = 0x130811
callq 0x70f4
callq 0x6a6f
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xd6, %esi
callq 0x70f4
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xd6, %esi
callq 0x70f4
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xd6, %esi
callq 0x70f4
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xd6, %esi
callq 0x70f4
callq 0x6a6f
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x4, %edx
callq 0xe727
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xda, %esi
callq 0x70f4
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xda, %esi
callq 0x70f4
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xda, %esi
callq 0x70f4
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xda, %esi
callq 0x70f4
callq 0x6a6f
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x70f4
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x4, %edx
callq 0xe727
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
movl $0x40020008, %edi       # imm = 0x40020008
movl $0x56, %esi
callq 0x70f4
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0x56, %esi
callq 0x70f4
movl $0x40020010, %edi       # imm = 0x40020010
movl $0x5a, %esi
callq 0x70f4
movl $0x40020014, %edi       # imm = 0x40020014
movl $0x5a, %esi
callq 0x70f4
callq 0x6a6f
movw $0xffff, (%r14)         # imm = 0xFFFF
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x70f4
callq 0x6a6f
movq %r14, %rdi
movl $0x2, %esi
movl $0x2, %edx
callq 0xe727
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
xorps %xmm0, %xmm0
movaps %xmm0, (%r14)
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movb $0x5a, (%r14)
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x70f4
callq 0x6a6f
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl $0x15, %esi
movl $0x5, %edx
callq 0xe727
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
cmpb $0x53, (%r14)
jne 0xe1af
cmpb $0x46, 0x11(%rsp)
jne 0xe1af
cmpb $0x44, 0x12(%rsp)
jne 0xe1af
cmpb $0x50, 0x13(%rsp)
jne 0xe1af
cmpb $0x0, 0x18(%rsp)
jne 0xe1af
movq %rbx, 0x8(%rsp)
leaq 0x10(%rsp), %r14
movb $0x5a, (%r14)
movb 0xe(%r14), %al
movb %al, 0x1(%r14)
movzwl 0xc(%r14), %eax
rolw $0x8, %ax
movw %ax, 0x2(%r14)
movb $0x0, 0x4(%r14)
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x70f4
callq 0x6a6f
movq %r14, %rdi
movl $0x45, %esi
movl $0x5, %edx
callq 0xe727
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
movzbl 0x2(%r14), %r12d
movzbl 0x1(%r14), %edx
movb (%r14), %al
movzbl 0x7(%r14), %r15d
movzbl 0x6(%r14), %ebp
shll $0x10, %ebp
movzbl 0x5(%r14), %ebx
movzbl 0x4(%r14), %r13d
movzbl 0xb(%r14), %ecx
movl %ecx, 0x4(%rsp)
movzbl 0xa(%r14), %r14d
andb $0x3, %al
cmpb $0x1, %al
je 0xe150
leaq 0x5abc(%rip), %rdi      # 0x13bff
xorl %eax, %eax
movl %edx, (%rsp)
callq 0x7c8d
movl (%rsp), %edx
shll $0x8, %ebx
orl %r13d, %ebp
movl %edx, 0xde7c(%rip)      # 0x1bfd8
testb $0x6, %r12b
je 0xe170
leaq 0x5ab2(%rip), %rdi      # 0x13c1b
xorl %eax, %eax
callq 0x7c8d
shll $0x18, %r15d
orl %ebx, %ebp
testb $0x40, %r12b
je 0xe183
movb $0x1, 0x70cb5(%rip)     # 0x7ee38
orl %r15d, %ebp
movl 0x4(%rsp), %eax
movl %eax, 0xde4c(%rip)      # 0x1bfdc
andl $0x1f, %r14d
movl %r14d, 0x70ca1(%rip)    # 0x7ee3c
testl %r15d, %r15d
movq 0x8(%rsp), %rbx
js 0xe209
incl %ebp
shrl $0x3, %ebp
movl %ebp, %r15d
jmp 0xe218
leaq 0x5a8f(%rip), %rdi      # 0x13c45
xorl %eax, %eax
callq 0x62f0
leaq 0x10(%rsp), %r14
movb $-0x61, (%r14)
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x70f4
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xe727
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x70f4
callq 0x6a6f
movb 0x2(%r14), %cl
shll %cl, %r15d
jmp 0xe218
movl $0x1, %r15d
movl %ebp, %ecx
shlq %cl, %r15
shrq $0x3, %r15
leal -0x40000001(%r15), %eax
xorl %ebp, %ebp
cmpl $0xc00000ff, %eax       # imm = 0xC00000FF
cmovael %r15d, %ebp
cmpl $0x100001, %ebp         # imm = 0x100001
jl 0xe240
movl %ebp, %esi
shrl $0x14, %esi
leaq 0x590d(%rip), %rdi      # 0x13b4b
jmp 0xe250
testl %ebp, %ebp
jle 0xe2c1
movl %ebp, %esi
shrl $0xa, %esi
leaq 0x590e(%rip), %rdi      # 0x13b5e
xorl %eax, %eax
callq 0x7a78
xorl %edi, %edi
cmpl $0x1000001, %ebp        # imm = 0x1000001
setl %dil
leaq 0x591c(%rip), %rsi      # 0x13b86
xorl %eax, %eax
callq 0x7bb4
movq 0x30(%rbx), %rax
movq %rax, 0x70bb4(%rip)     # 0x7ee30
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x70b92(%rip)    # 0x7ee20
movups %xmm1, 0x70b7b(%rip)    # 0x7ee10
movups %xmm0, 0x70b64(%rip)    # 0x7ee00
leaq 0x70b5d(%rip), %rdi     # 0x7ee00
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x8edf
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58a9(%rip), %rdi      # 0x13b71
xorl %eax, %eax
callq 0x7c8d
jmp 0xe257