// File: /workspace/github_c_makefile_star2_O3/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 0x6fba
movl $0x1002927, %edi        # imm = 0x1002927
callq 0x6503
callq 0x6789
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x7269
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
movl $0x1, %esi
callq 0x7269
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x7269
movl $0x18000058, %edi       # imm = 0x18000058
callq 0x721a
xorl %edi, %edi
cmpl $0x51535049, %eax       # imm = 0x51535049
sete %dil
leaq 0x5d87(%rip), %rsi      # 0x13add
xorl %eax, %eax
callq 0x7bcc
movl $0x13, %edi
callq 0x71e0
movzbl %al, %esi
leal -0x1(%rsi), %eax
cmpb $0x2, %al
ja 0xdd83
decl %esi
leaq 0x5d7d(%rip), %rdi      # 0x13af7
xorl %eax, %eax
callq 0x7a90
jmp 0xdd91
leaq 0x5d87(%rip), %rdi      # 0x13b11
xorl %eax, %eax
callq 0x7ca5
leaq 0x10(%rsp), %r14
movl $0x0, (%r14)
movl $0x4000f000, %edi       # imm = 0x4000F000
movl $0x244, %esi            # imm = 0x244
callq 0x711f
movl $0x40018014, %edi       # imm = 0x40018014
xorl %esi, %esi
callq 0x711f
movl $0x4001801c, %edi       # imm = 0x4001801C
xorl %esi, %esi
callq 0x711f
movl $0x40018024, %edi       # imm = 0x40018024
xorl %esi, %esi
callq 0x711f
movl $0x4001802c, %edi       # imm = 0x4001802C
xorl %esi, %esi
callq 0x711f
movl $0x40018004, %edi       # imm = 0x40018004
xorl %esi, %esi
callq 0x711f
movl $0x4001800c, %edi       # imm = 0x4001800C
xorl %esi, %esi
callq 0x711f
callq 0x6a6f
movl $0x18000008, %edi       # imm = 0x18000008
xorl %esi, %esi
callq 0x711f
movl $0x18000014, %edi       # imm = 0x18000014
movl $0x2, %esi
callq 0x711f
movl $0x18000000, %edi       # imm = 0x18000000
movl $0x70000, %esi          # imm = 0x70000
callq 0x711f
movl $0x1, %r15d
movl $0x18000010, %edi       # imm = 0x18000010
movl $0x1, %esi
callq 0x711f
movl $0x18000008, %edi       # imm = 0x18000008
movl $0x1, %esi
callq 0x711f
movl $0x1800004c, %edi       # imm = 0x1800004C
movl $0x3, %esi
callq 0x711f
movl $0x50000014, %edi       # imm = 0x50000014
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x711f
movl $0x50000010, %edi       # imm = 0x50000010
movl $0x138023, %esi         # imm = 0x138023
callq 0x711f
movl $0x50000058, %edi       # imm = 0x50000058
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x711f
movl $0x50000050, %edi       # imm = 0x50000050
movl $0x130811, %esi         # imm = 0x130811
callq 0x711f
callq 0x6a6f
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xd6, %esi
callq 0x711f
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xd6, %esi
callq 0x711f
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xd6, %esi
callq 0x711f
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xd6, %esi
callq 0x711f
callq 0x6a6f
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x4, %edx
callq 0xe719
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xda, %esi
callq 0x711f
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xda, %esi
callq 0x711f
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xda, %esi
callq 0x711f
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xda, %esi
callq 0x711f
callq 0x6a6f
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x711f
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x4, %edx
callq 0xe719
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
callq 0x6a6f
movl $0x40020008, %edi       # imm = 0x40020008
movl $0x56, %esi
callq 0x711f
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0x56, %esi
callq 0x711f
movl $0x40020010, %edi       # imm = 0x40020010
movl $0x5a, %esi
callq 0x711f
movl $0x40020014, %edi       # imm = 0x40020014
movl $0x5a, %esi
callq 0x711f
callq 0x6a6f
movw $0xffff, (%r14)         # imm = 0xFFFF
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x711f
callq 0x6a6f
movq %r14, %rdi
movl $0x2, %esi
movl $0x2, %edx
callq 0xe719
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
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 0x711f
callq 0x6a6f
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movl $0x15, %esi
movl $0x5, %edx
callq 0xe719
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
callq 0x6a6f
movdqa (%r14), %xmm0
movd %xmm0, %eax
cmpl $0x50444653, %eax       # imm = 0x50444653
jne 0xe184
cmpb $0x0, 0x18(%rsp)
jne 0xe184
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 0x711f
callq 0x6a6f
movq %r14, %rdi
movl $0x45, %esi
movl $0x5, %edx
callq 0xe719
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
callq 0x6a6f
movzbl 0x2(%r14), %r13d
movzbl 0x1(%r14), %r12d
movb (%r14), %al
movzbl 0x7(%r14), %r15d
movzbl 0x6(%r14), %ebp
movzwl 0x4(%r14), %ebx
movzbl 0xb(%r14), %ecx
movl %ecx, 0x4(%rsp)
movzbl 0xa(%r14), %r14d
andb $0x3, %al
cmpb $0x1, %al
je 0xe127
leaq 0x5acf(%rip), %rdi      # 0x13bef
xorl %eax, %eax
callq 0x7ca5
shll $0x10, %ebp
movl %r12d, 0xdea7(%rip)     # 0x1bfd8
testb $0x6, %r13b
je 0xe145
leaq 0x5acd(%rip), %rdi      # 0x13c0b
xorl %eax, %eax
callq 0x7ca5
shll $0x18, %r15d
orl %ebx, %ebp
testb $0x40, %r13b
je 0xe158
movb $0x1, 0x70ce0(%rip)     # 0x7ee38
orl %r15d, %ebp
movl 0x4(%rsp), %eax
movl %eax, 0xde77(%rip)      # 0x1bfdc
andl $0x1f, %r14d
movl %r14d, 0x70ccc(%rip)    # 0x7ee3c
testl %r15d, %r15d
movq 0x8(%rsp), %rbx
js 0xe1de
incl %ebp
shrl $0x3, %ebp
movl %ebp, %r15d
jmp 0xe1ed
leaq 0x5aaa(%rip), %rdi      # 0x13c35
xorl %eax, %eax
callq 0x62f0
leaq 0x10(%rsp), %r14
movb $-0x61, (%r14)
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x200, %esi            # imm = 0x200
callq 0x711f
callq 0x6a6f
movq %r14, %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0xe719
movl $0x4001800c, %edi       # imm = 0x4001800C
movl $0x300, %esi            # imm = 0x300
callq 0x711f
callq 0x6a6f
movb 0x2(%r14), %cl
shll %cl, %r15d
jmp 0xe1ed
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 0xe215
movl %ebp, %esi
shrl $0x14, %esi
leaq 0x5928(%rip), %rdi      # 0x13b3b
jmp 0xe225
testl %ebp, %ebp
jle 0xe296
movl %ebp, %esi
shrl $0xa, %esi
leaq 0x5929(%rip), %rdi      # 0x13b4e
xorl %eax, %eax
callq 0x7a90
xorl %edi, %edi
cmpl $0x1000001, %ebp        # imm = 0x1000001
setl %dil
leaq 0x5937(%rip), %rsi      # 0x13b76
xorl %eax, %eax
callq 0x7bcc
movq 0x30(%rbx), %rax
movq %rax, 0x70bdf(%rip)     # 0x7ee30
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x70bbd(%rip)    # 0x7ee20
movups %xmm1, 0x70ba6(%rip)    # 0x7ee10
movups %xmm0, 0x70b8f(%rip)    # 0x7ee00
leaq 0x70b88(%rip), %rdi     # 0x7ee00
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x8ec8
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x58c4(%rip), %rdi      # 0x13b61
xorl %eax, %eax
callq 0x7ca5
jmp 0xe22c