// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/dap.c
// Function: dap_reset_link
void dap_reset_link(void)
{
  uint8_t buf[32];

  if (DAP_INTERFACE_SWD == dap_interface)
  {
    if (dap_dp_version == 1)
    {
      buf[0] = ID_DAP_SWJ_SEQUENCE;
      buf[1] = (7 + 2 + 7 + 1) * 8;
      buf[2] = 0xff;
      buf[3] = 0xff;
      buf[4] = 0xff;
      buf[5] = 0xff;
      buf[6] = 0xff;
      buf[7] = 0xff;
      buf[8] = 0xff;
      buf[9] = 0x9e;
      buf[10] = 0xe7;
      buf[11] = 0xff;
      buf[12] = 0xff;
      buf[13] = 0xff;
      buf[14] = 0xff;
      buf[15] = 0xff;
      buf[16] = 0xff;
      buf[17] = 0xff;
      buf[18] = 0x00;

      dbg_dap_cmd(buf, sizeof(buf), 19);
      check(DAP_OK == buf[0], "SWJ_SEQUENCE failed");

      dap_read_idcode();
    }
    else if (dap_dp_version == 2)
    {
      // Switch to SWD
      buf[0] = ID_DAP_SWJ_SEQUENCE;
      buf[1] = (1 + 16 + 7 + 1) * 8;
      buf[2] = 0xff;
      buf[3] = 0x92; // Selection Alert Sequence
      buf[4] = 0xf3;
      buf[5] = 0x09;
      buf[6] = 0x62;
      buf[7] = 0x95;
      buf[8] = 0x2d;
      buf[9] = 0x85;
      buf[10] = 0x86;
      buf[11] = 0xe9;
      buf[12] = 0xaf;
      buf[13] = 0xdd;
      buf[14] = 0xe3;
      buf[15] = 0xa2;
      buf[16] = 0x0e;
      buf[17] = 0xbc;
      buf[18] = 0x19;
      buf[19] = 0xa0; // 4 cycles with SWDIO/TMS low and Activation Code
      buf[20] = 0xf1; // Activation Code and SWD Line Reset
      buf[21] = 0xff;
      buf[22] = 0xff;
      buf[23] = 0xff;
      buf[24] = 0xff;
      buf[25] = 0xff;
      buf[26] = 0x3f;

      dbg_dap_cmd(buf, sizeof(buf), 27);
      check(DAP_OK == buf[0], "SWJ_SEQUENCE failed");

      // Target Select
      buf[0] = ID_DAP_SWD_SEQUENCE;
      buf[1] = 5; // Request Count
      // 1
      buf[2] = SWD_SEQUENCE_COUNT(7 * 8);
      buf[3] = 0xff;
      buf[4] = 0xff;
      buf[5] = 0xff;
      buf[6] = 0xff;
      buf[7] = 0xff;
      buf[8] = 0xff;
      buf[9] = 0x3f;
      // 2
      buf[10] = SWD_SEQUENCE_COUNT(8);
      buf[11] = 0x99; // DP, Write, TARGETSEL
      // 3
      buf[12] = SWD_SEQUENCE_COUNT(5) | SWD_SEQUENCE_DIN;
      // 4
      buf[13] = SWD_SEQUENCE_COUNT(32+1);
      buf[14] = dap_target_id >> 0;
      buf[15] = dap_target_id >> 8;
      buf[16] = dap_target_id >> 16;
      buf[17] = dap_target_id >> 24;
      buf[18] = dap_parity(dap_target_id);
      // 5
      buf[19] = SWD_SEQUENCE_COUNT(2);
      buf[20] = 0x00;

      dbg_dap_cmd(buf, sizeof(buf), 21);
      check(DAP_OK == buf[0], "SWD_SEQUENCE failed");

      dap_read_idcode();
    }
    else
    {
      error_exit("internal: unknown dap_dp_version value (%d)", dap_dp_version);
    }
  }
  else if (DAP_INTERFACE_JTAG == dap_interface)
  {
    buf[0] = ID_DAP_SWJ_SEQUENCE;
    buf[1] = (7 + 2 + 1 + 1) * 8;
    buf[2] = 0xff;
    buf[3] = 0xff;
    buf[4] = 0xff;
    buf[5] = 0xff;
    buf[6] = 0xff;
    buf[7] = 0xff;
    buf[8] = 0xff;
    buf[9] = 0x3c;
    buf[10] = 0xe7;
    buf[11] = 0xff;
    buf[12] = 0x00;

    dbg_dap_cmd(buf, sizeof(buf), 13);
    check(DAP_OK == buf[0], "SWJ_SEQUENCE failed");
  }
  else
  {
    error_exit("no interface selected in dap_reset_link()");
  }

  dap_add_req(TRANSFER_TYPE_WRITE_REG, TRANSFER_SIZE_WORD, SWD_DP_W_ABORT,
      DP_ABORT_STKCMPCLR | DP_ABORT_STKERRCLR | DP_ABORT_ORUNERRCLR | DP_ABORT_WDERRCLR);
  dap_add_req(TRANSFER_TYPE_WRITE_REG, TRANSFER_SIZE_WORD, SWD_DP_W_SELECT,
      DP_SELECT_APBANKSEL(0) | DP_SELECT_APSEL(0));
  dap_add_req(TRANSFER_TYPE_WRITE_REG, TRANSFER_SIZE_WORD, SWD_DP_CTRL_STAT,
      DP_CST_CDBGPWRUPREQ | DP_CST_CSYSPWRUPREQ | DP_CST_MASKLANE(0xf));
  dap_transfer();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl $0x1, %eax
cmpl 0x1909d(%rip), %eax     # 0x1f8b0
jne 0x6a2b
cmpl $0x1, 0x15960(%rip)     # 0x1c180
jne 0x68ae
movb $0x12, -0x20(%rbp)
movb $-0x78, -0x1f(%rbp)
movb $-0x1, -0x1e(%rbp)
movb $-0x1, -0x1d(%rbp)
movb $-0x1, -0x1c(%rbp)
movb $-0x1, -0x1b(%rbp)
movb $-0x1, -0x1a(%rbp)
movb $-0x1, -0x19(%rbp)
movb $-0x1, -0x18(%rbp)
movb $-0x62, -0x17(%rbp)
movb $-0x19, -0x16(%rbp)
movb $-0x1, -0x15(%rbp)
movb $-0x1, -0x14(%rbp)
movb $-0x1, -0x13(%rbp)
movb $-0x1, -0x12(%rbp)
movb $-0x1, -0x11(%rbp)
movb $-0x1, -0x10(%rbp)
movb $-0x1, -0xf(%rbp)
movb $0x0, -0xe(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x20, %esi
movl $0x13, %edx
callq 0x130d0
movzbl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xd7df(%rip), %rsi      # 0x1407c
xorl %eax, %eax
callq 0x8880
callq 0x6b00
jmp 0x6a26
cmpl $0x2, 0x158cb(%rip)     # 0x1c180
jne 0x6a10
movb $0x12, -0x20(%rbp)
movb $-0x38, -0x1f(%rbp)
movb $-0x1, -0x1e(%rbp)
movb $-0x6e, -0x1d(%rbp)
movb $-0xd, -0x1c(%rbp)
movb $0x9, -0x1b(%rbp)
movb $0x62, -0x1a(%rbp)
movb $-0x6b, -0x19(%rbp)
movb $0x2d, -0x18(%rbp)
movb $-0x7b, -0x17(%rbp)
movb $-0x7a, -0x16(%rbp)
movb $-0x17, -0x15(%rbp)
movb $-0x51, -0x14(%rbp)
movb $-0x23, -0x13(%rbp)
movb $-0x1d, -0x12(%rbp)
movb $-0x5e, -0x11(%rbp)
movb $0xe, -0x10(%rbp)
movb $-0x44, -0xf(%rbp)
movb $0x19, -0xe(%rbp)
movb $-0x60, -0xd(%rbp)
movb $-0xf, -0xc(%rbp)
movb $-0x1, -0xb(%rbp)
movb $-0x1, -0xa(%rbp)
movb $-0x1, -0x9(%rbp)
movb $-0x1, -0x8(%rbp)
movb $-0x1, -0x7(%rbp)
movb $0x3f, -0x6(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x20, %esi
movl $0x1b, %edx
callq 0x130d0
movzbl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xd72a(%rip), %rsi      # 0x1407c
xorl %eax, %eax
callq 0x8880
movb $0x1d, -0x20(%rbp)
movb $0x5, -0x1f(%rbp)
movb $0x38, -0x1e(%rbp)
movb $-0x1, -0x1d(%rbp)
movb $-0x1, -0x1c(%rbp)
movb $-0x1, -0x1b(%rbp)
movb $-0x1, -0x1a(%rbp)
movb $-0x1, -0x19(%rbp)
movb $-0x1, -0x18(%rbp)
movb $0x3f, -0x17(%rbp)
movb $0x8, -0x16(%rbp)
movb $-0x67, -0x15(%rbp)
movb $-0x7b, -0x14(%rbp)
movb $0x21, -0x13(%rbp)
movl 0x157ed(%rip), %eax     # 0x1c184
shrl $0x0, %eax
movb %al, -0x12(%rbp)
movl 0x157e1(%rip), %eax     # 0x1c184
shrl $0x8, %eax
movb %al, -0x11(%rbp)
movl 0x157d5(%rip), %eax     # 0x1c184
shrl $0x10, %eax
movb %al, -0x10(%rbp)
movl 0x157c9(%rip), %eax     # 0x1c184
shrl $0x18, %eax
movb %al, -0xf(%rbp)
movl 0x157bd(%rip), %edi     # 0x1c184
callq 0x6b90
movb %al, -0xe(%rbp)
movb $0x2, -0xd(%rbp)
movb $0x0, -0xc(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x20, %esi
movl $0x15, %edx
callq 0x130d0
movzbl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xd68e(%rip), %rsi      # 0x14090
xorl %eax, %eax
callq 0x8880
callq 0x6b00
jmp 0x6a24
movl 0x1576a(%rip), %esi     # 0x1c180
leaq 0xd687(%rip), %rdi      # 0x140a4
movb $0x0, %al
callq 0x8970
jmp 0x6a26
jmp 0x6ab0
movl $0x2, %eax
cmpl 0x18e7a(%rip), %eax     # 0x1f8b0
jne 0x6aa0
movb $0x12, -0x20(%rbp)
movb $0x58, -0x1f(%rbp)
movb $-0x1, -0x1e(%rbp)
movb $-0x1, -0x1d(%rbp)
movb $-0x1, -0x1c(%rbp)
movb $-0x1, -0x1b(%rbp)
movb $-0x1, -0x1a(%rbp)
movb $-0x1, -0x19(%rbp)
movb $-0x1, -0x18(%rbp)
movb $0x3c, -0x17(%rbp)
movb $-0x19, -0x16(%rbp)
movb $-0x1, -0x15(%rbp)
movb $0x0, -0x14(%rbp)
leaq -0x20(%rbp), %rdi
movl $0x20, %esi
movl $0xd, %edx
callq 0x130d0
movzbl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0xd5e5(%rip), %rsi      # 0x1407c
xorl %eax, %eax
callq 0x8880
jmp 0x6aae
leaq 0xd629(%rip), %rdi      # 0x140d0
movb $0x0, %al
callq 0x8970
jmp 0x6ab0
movl $0x4, %edi
movl $0x2, %esi
xorl %edx, %edx
movl $0x1e, %ecx
callq 0x6be0
movl $0x4, %edi
movl $0x2, %esi
movl $0x8, %edx
xorl %ecx, %ecx
callq 0x6be0
movl $0x4, %edx
movl $0x2, %esi
movl $0x50000f00, %ecx       # imm = 0x50000F00
movl %edx, %edi
callq 0x6be0
callq 0x6c70
addq $0x20, %rsp
popq %rbp
retq
nopl (%rax)