// File: /workspace/github_c_makefile_star2_O2/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
pushq %rbx
subq $0x28, %rsp
movl 0x16afa(%rip), %eax     # 0x1d150
cmpl $0x2, %eax
je 0x66c1
cmpl $0x1, %eax
jne 0x6705
movl 0x15b16(%rip), %esi     # 0x1c180
cmpl $0x2, %esi
je 0x6718
cmpl $0x1, %esi
jne 0x67e9
movq %rsp, %rbx
movw $0x8812, (%rbx)         # imm = 0x8812
orl $-0x1, 0x2(%rbx)
orl $-0x1, 0x5(%rbx)
movw $0xe79e, 0x9(%rbx)      # imm = 0xE79E
orl $-0x1, 0xb(%rbx)
orl $-0x1, 0xe(%rbx)
movb $0x0, 0x12(%rbx)
pushq $0x20
popq %rsi
pushq $0x13
popq %rdx
movq %rbx, %rdi
callq 0xed2e
xorl %edi, %edi
cmpb $0x0, (%rbx)
sete %dil
leaq 0x99dc(%rip), %rsi      # 0x10098
jmp 0x67db
movq %rsp, %rbx
movw $0x5812, (%rbx)         # imm = 0x5812
movabsq $0xffe73cffffffff, %rax # imm = 0xFFE73CFFFFFFFF
movq %rax, 0x5(%rbx)
orl $-0x1, 0x2(%rbx)
pushq $0x20
popq %rsi
pushq $0xd
popq %rdx
movq %rbx, %rdi
callq 0xed2e
xorl %edi, %edi
cmpb $0x0, (%rbx)
sete %dil
leaq 0x999f(%rip), %rsi      # 0x10098
xorl %eax, %eax
callq 0x793d
jmp 0x67f7
leaq 0x99e0(%rip), %rdi      # 0x100ec
xorl %eax, %eax
callq 0x7a0e
jmp 0x67f7
movaps 0x98f1(%rip), %xmm0     # 0x10010
movq %rsp, %rbx
movaps %xmm0, (%rbx)
movl $0xa019bc0e, 0x10(%rbx) # imm = 0xA019BC0E
movb $-0xf, 0x14(%rbx)
orl $-0x1, 0x15(%rbx)
movw $0x3fff, 0x19(%rbx)     # imm = 0x3FFF
pushq $0x20
popq %rbp
pushq $0x1b
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0xed2e
xorl %edi, %edi
cmpb $0x0, (%rbx)
sete %dil
leaq 0x993e(%rip), %rsi      # 0x10098
xorl %eax, %eax
callq 0x793d
movw $0x51d, (%rbx)          # imm = 0x51D
movb $0x38, 0x2(%rbx)
orl $-0x1, 0x3(%rbx)
orw $-0x1, 0x7(%rbx)
movl $0x8599083f, 0x9(%rbx)  # imm = 0x8599083F
movb $0x21, 0xd(%rbx)
movl 0x15a00(%rip), %eax     # 0x1c184
movb %al, 0xe(%rbx)
movb %ah, 0xf(%rsp)
movl %eax, %ecx
shrl $0x10, %ecx
movb %cl, 0x10(%rbx)
xorl %eax, %ecx
shrl $0x18, %eax
movb %al, 0x11(%rbx)
movl %ecx, %eax
shrl $0x8, %eax
xorl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
xorl %eax, %ecx
andl $0xf, %ecx
movl $0x6996, %eax           # imm = 0x6996
btl %ecx, %eax
setb 0x12(%rbx)
movw $0x2, 0x13(%rbx)
pushq $0x15
popq %rdx
movq %rbx, %rdi
movl %ebp, %esi
callq 0xed2e
xorl %edi, %edi
cmpb $0x0, (%rbx)
sete %dil
leaq 0x98d1(%rip), %rsi      # 0x100ac
xorl %eax, %eax
callq 0x793d
callq 0x6835
jmp 0x67f7
leaq 0x98d0(%rip), %rdi      # 0x100c0
xorl %eax, %eax
callq 0x7a0e
pushq $0x4
popq %rbx
pushq $0x2
popq %rbp
pushq $0x1e
popq %rcx
movl %ebx, %edi
movl %ebp, %esi
xorl %edx, %edx
callq 0x689b
pushq $0x8
popq %rdx
movl %ebx, %edi
movl %ebp, %esi
xorl %ecx, %ecx
callq 0x689b
movl %ebx, %edi
movl %ebp, %esi
movl %ebx, %edx
movl $0x50000f00, %ecx       # imm = 0x50000F00
callq 0x689b
callq 0x68f4
addq $0x28, %rsp
popq %rbx
popq %rbp
retq