// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x2
popq %rdi
callq 0x63dc
movl $0xf1002927, %edi       # imm = 0xF1002927
callq 0x63e3
callq 0x664a
callq 0x6dc7
movl $0x1002927, %edi        # imm = 0x1002927
callq 0x63e3
callq 0x664a
movl $0xe000edf0, %edi       # imm = 0xE000EDF0
movl $0xa05f0003, %esi       # imm = 0xA05F0003
callq 0x700c
pushq $0x1
popq %rsi
movl $0xe000edfc, %edi       # imm = 0xE000EDFC
callq 0x700c
movl $0xe000ed0c, %edi       # imm = 0xE000ED0C
movl $0x5fa0004, %esi        # imm = 0x5FA0004
callq 0x700c
movl $0x18000058, %edi       # imm = 0x18000058
callq 0x6fe1
xorl %edi, %edi
cmpl $0x51535049, %eax       # imm = 0x51535049
sete %dil
leaq 0x63af(%rip), %rsi      # 0x13add
xorl %eax, %eax
callq 0x793d
pushq $0x13
popq %rdi
callq 0x6fba
movzbl %al, %esi
leal -0x1(%rsi), %eax
cmpb $0x2, %al
ja 0xd759
decl %esi
leaq 0x63a7(%rip), %rdi      # 0x13af7
xorl %eax, %eax
callq 0x773c
jmp 0xd767
leaq 0x63b1(%rip), %rdi      # 0x13b11
xorl %eax, %eax
callq 0x7a0e
leaq 0x10(%rsp), %r15
andl $0x0, (%r15)
movl $0x4000f000, %edi       # imm = 0x4000F000
movl $0x244, %esi            # imm = 0x244
callq 0x6f02
movl $0x40018014, %edi       # imm = 0x40018014
xorl %esi, %esi
callq 0x6f02
movl $0x4001801c, %edi       # imm = 0x4001801C
xorl %esi, %esi
callq 0x6f02
movl $0x40018024, %edi       # imm = 0x40018024
xorl %esi, %esi
callq 0x6f02
movl $0x4001802c, %edi       # imm = 0x4001802C
xorl %esi, %esi
callq 0x6f02
movl $0x40018004, %edi       # imm = 0x40018004
xorl %esi, %esi
callq 0x6f02
movl $0x4001800c, %edi       # imm = 0x4001800C
xorl %esi, %esi
callq 0x6f02
callq 0x68f4
movl $0x18000008, %edi       # imm = 0x18000008
xorl %esi, %esi
callq 0x6f02
pushq $0x2
popq %rbp
movl $0x18000014, %edi       # imm = 0x18000014
movl %ebp, %esi
callq 0x6f02
movl $0x18000000, %edi       # imm = 0x18000000
movl $0x70000, %esi          # imm = 0x70000
callq 0x6f02
pushq $0x1
popq %r14
movl $0x18000010, %edi       # imm = 0x18000010
movl %r14d, %esi
callq 0x6f02
movl $0x18000008, %edi       # imm = 0x18000008
movl %r14d, %esi
callq 0x6f02
pushq $0x3
popq %rsi
movl $0x1800004c, %edi       # imm = 0x1800004C
callq 0x6f02
movl $0x50000014, %edi       # imm = 0x50000014
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x6f02
movl $0x50000010, %edi       # imm = 0x50000010
movl $0x138023, %esi         # imm = 0x138023
callq 0x6f02
movl $0x50000058, %edi       # imm = 0x50000058
movl $0x18000060, %esi       # imm = 0x18000060
callq 0x6f02
movl $0x50000050, %edi       # imm = 0x50000050
movl $0x130811, %esi         # imm = 0x130811
callq 0x6f02
callq 0x68f4
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xd6, %esi
callq 0x6f02
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xd6, %esi
callq 0x6f02
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xd6, %esi
callq 0x6f02
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xd6, %esi
callq 0x6f02
callq 0x68f4
movl %r14d, %edi
callq 0xdfcb
pushq $0x4
popq %r12
movq %r15, %rdi
movl %r12d, %esi
movl %r12d, %edx
callq 0xdfd7
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xda, %esi
callq 0x6f02
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xda, %esi
callq 0x6f02
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xda, %esi
callq 0x6f02
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xda, %esi
callq 0x6f02
callq 0x68f4
xorl %edi, %edi
callq 0xdfcb
movq %r15, %rdi
movl %r12d, %esi
movl %r12d, %edx
callq 0xdfd7
movl %r14d, %edi
callq 0xdfcb
pushq $0x56
popq %r12
movl $0x40020008, %edi       # imm = 0x40020008
movl %r12d, %esi
callq 0x6f02
movl $0x4002000c, %edi       # imm = 0x4002000C
movl %r12d, %esi
callq 0x6f02
pushq $0x5a
popq %r12
movl $0x40020010, %edi       # imm = 0x40020010
movl %r12d, %esi
callq 0x6f02
movl $0x40020014, %edi       # imm = 0x40020014
movl %r12d, %esi
callq 0x6f02
callq 0x68f4
orw $-0x1, (%r15)
xorl %edi, %edi
callq 0xdfcb
movq %r15, %rdi
movl %ebp, %esi
movl %ebp, %edx
callq 0xdfd7
movl %r14d, %edi
callq 0xdfcb
xorps %xmm0, %xmm0
movaps %xmm0, (%r15)
movaps %xmm0, 0x70(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x10(%r15)
movb $0x5a, (%r15)
xorl %edi, %edi
callq 0xdfcb
leaq 0x10(%rsp), %r15
pushq $0x15
popq %rsi
pushq $0x5
popq %rdx
movq %r15, %rdi
callq 0xdfd7
movl %r14d, %edi
callq 0xdfcb
movdqa (%r15), %xmm0
movd %xmm0, %eax
cmpl $0x50444653, %eax       # imm = 0x50444653
jne 0xdacc
cmpb $0x0, 0x18(%rsp)
jne 0xdacc
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)
xorl %edi, %edi
callq 0xdfcb
pushq $0x45
popq %rsi
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0xdfd7
pushq $0x1
popq %rdi
callq 0xdfcb
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 0xda6f
leaq 0x6187(%rip), %rdi      # 0x13bef
xorl %eax, %eax
callq 0x7a0e
shll $0x10, %ebp
movl %r12d, 0xf55f(%rip)     # 0x1cfd8
testb $0x6, %r13b
je 0xda8d
leaq 0x6185(%rip), %rdi      # 0x13c0b
xorl %eax, %eax
callq 0x7a0e
shll $0x18, %r15d
orl %ebx, %ebp
testb $0x40, %r13b
je 0xdaa0
movb $0x1, 0x72398(%rip)     # 0x7fe38
orl %r15d, %ebp
movl 0x4(%rsp), %eax
movl %eax, 0xf52f(%rip)      # 0x1cfdc
andl $0x1f, %r14d
movl %r14d, 0x72384(%rip)    # 0x7fe3c
testl %r15d, %r15d
movq 0x8(%rsp), %rbx
js 0xdb09
incl %ebp
shrl $0x3, %ebp
movl %ebp, %r14d
jmp 0xdb16
leaq 0x6162(%rip), %rdi      # 0x13c35
xorl %eax, %eax
callq 0x7878
leaq 0x10(%rsp), %r15
movb $-0x61, (%r15)
xorl %edi, %edi
callq 0xdfcb
pushq $0x4
popq %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0xdfd7
movl %r14d, %edi
callq 0xdfcb
movb 0x2(%r15), %cl
shll %cl, %r14d
jmp 0xdb16
pushq $0x1
popq %r14
movl %ebp, %ecx
shlq %cl, %r14
shrq $0x3, %r14
leal -0x40000001(%r14), %eax
xorl %ebp, %ebp
cmpl $0xc00000ff, %eax       # imm = 0xC00000FF
cmovael %r14d, %ebp
cmpl $0x100001, %ebp         # imm = 0x100001
jl 0xdb3e
movl %ebp, %esi
shrl $0x14, %esi
leaq 0x5fff(%rip), %rdi      # 0x13b3b
jmp 0xdb4e
testl %ebp, %ebp
jle 0xdbbf
movl %ebp, %esi
shrl $0xa, %esi
leaq 0x6000(%rip), %rdi      # 0x13b4e
xorl %eax, %eax
callq 0x773c
xorl %edi, %edi
cmpl $0x1000001, %ebp        # imm = 0x1000001
setl %dil
leaq 0x600e(%rip), %rsi      # 0x13b76
xorl %eax, %eax
callq 0x793d
movq 0x30(%rbx), %rax
movq %rax, 0x722b6(%rip)     # 0x7fe30
movups (%rbx), %xmm0
movups 0x10(%rbx), %xmm1
movups 0x20(%rbx), %xmm2
movups %xmm2, 0x72294(%rip)    # 0x7fe20
movups %xmm1, 0x7227d(%rip)    # 0x7fe10
movups %xmm0, 0x72266(%rip)    # 0x7fe00
leaq 0x7225f(%rip), %rdi     # 0x7fe00
movl %ebp, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x8b8e
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5f9b(%rip), %rdi      # 0x13b61
xorl %eax, %eax
callq 0x7a0e
jmp 0xdb55