// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_lattice_lcmxo2.c
// Function: target_verify
static void target_verify(void)
{
  uint8_t *file_data;
  int file_size, row_count;
  jed_file_t jed;
  uint16_t feabits;
  uint64_t feature;

  file_data = buf_alloc(MAX_FILE_SIZE);
  file_size = load_file(target_options.name, file_data, MAX_FILE_SIZE);

  jed.config = buf_alloc(MAX_CONFIG_SIZE);
  jed.config_size = MAX_CONFIG_SIZE;

  parse_jed_file(&jed, file_data, file_size);

  row_count = jed.config_size / FLASH_ROW_SIZE;

  erase_sram();

  // Enable flash
  dap_jtag_write_ir(CMD_ISC_ENABLE, IR_LENGTH);
  dap_jtag_write_dr((uint8_t[]){ ISC_ENABLE_FLASH }, 8);
  dap_jtag_idle(8);

  // Verify configuration data
  dap_jtag_write_ir(CMD_LSC_INIT_ADDRESS, IR_LENGTH);
  dap_jtag_idle(8);

  dap_jtag_write_ir(CMD_LSC_READ_INCR_NV, IR_LENGTH);
  dap_jtag_idle(8);

  for (int row = 0; row < row_count; row++)
  {
    uint8_t tmp[16];
    dap_jtag_read_dr(tmp, FLASH_ROW_SIZE);
    dap_jtag_idle(8);

    if (memcmp(tmp, &jed.config[row * 16], 16))
      error_exit("configuration verification failed");
  }

  dap_jtag_write_ir(CMD_LSC_INIT_ADDRESS, IR_LENGTH);
  dap_jtag_idle(8);

  // Verify Feature Row
  dap_jtag_write_ir(CMD_LSC_READ_FEATURE, IR_LENGTH);
  dap_jtag_read_dr((uint8_t *)&feature, 64);
  dap_jtag_idle(8);

  check(feature == jed.feature, "Feature Row verification failed");

  // Verify FEABITS
  dap_jtag_write_ir(CMD_LSC_READ_FEABITS, IR_LENGTH);
  dap_jtag_idle(8);
  dap_jtag_read_dr((uint8_t *)&feabits, 16);

  check(feabits == jed.feabits, "FEABITS verification failed");

  buf_free(file_data);
  buf_free(jed.config);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x7dd2
movq %rax, %rbx
movq 0x71635(%rip), %rdi     # 0x7edd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7dfa
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7dd2
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movl $0x200000, 0x8(%r14)    # imm = 0x200000
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0xdaa4
movl 0x8(%r14), %r13d
callq 0xda3c
movl $0xc6, %edi
movl $0x8, %esi
callq 0x7845
leaq 0xd(%rsp), %rdi
movb $0x8, (%rdi)
movl $0x8, %esi
callq 0x78cd
movl $0x8, %edi
callq 0x7809
movl $0x46, %edi
movl $0x8, %esi
callq 0x7845
movl $0x8, %edi
callq 0x7809
movl $0x73, %edi
movl $0x8, %esi
callq 0x7845
movl $0x8, %edi
callq 0x7809
movq (%r14), %r14
cmpl $0x80, %r13d
jl 0xd89e
shrl $0x7, %r13d
leaq 0x10(%rsp), %r15
leaq 0x61c3(%rip), %r12      # 0x13a1e
movq %r14, %rbp
movq %r15, %rdi
movl $0x80, %esi
callq 0x795a
movl $0x8, %edi
callq 0x7809
movdqu (%rbp), %xmm0
pcmpeqb 0x10(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xd895
movq %r12, %rdi
xorl %eax, %eax
callq 0x7ca5
addq $0x10, %rbp
decq %r13
jne 0xd85e
movl $0x46, %edi
movl $0x8, %esi
callq 0x7845
movl $0x8, %edi
callq 0x7809
movl $0xe7, %edi
movl $0x8, %esi
callq 0x7845
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movl $0x40, %esi
callq 0x795a
movl $0x8, %edi
callq 0x7809
movq (%r15), %rax
xorl %edi, %edi
cmpq 0x30(%rsp), %rax
sete %dil
leaq 0x6149(%rip), %rsi      # 0x13a40
xorl %eax, %eax
callq 0x7bcc
movl $0xfb, %edi
movl $0x8, %esi
callq 0x7845
movl $0x8, %edi
callq 0x7809
leaq 0xe(%rsp), %r15
movq %r15, %rdi
movl $0x10, %esi
callq 0x795a
movzwl (%r15), %eax
xorl %edi, %edi
cmpw 0x2c(%rsp), %ax
sete %dil
leaq 0x6121(%rip), %rsi      # 0x13a60
xorl %eax, %eax
callq 0x7bcc
movq %rbx, %rdi
callq 0x7df5
movq %r14, %rdi
callq 0x7df5
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq