// File: /workspace/github_c_makefile_star2_O1/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 0x7dba
movq %rax, %rbx
movq 0x7163a(%rip), %rdi     # 0x7edd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7dec
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7dba
leaq 0x8(%rsp), %r14
movq %rax, (%r14)
movl $0x200000, 0x8(%r14)    # imm = 0x200000
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0xdaa7
movl 0x8(%r14), %r12d
callq 0xda3f
movl $0xc6, %edi
movl $0x8, %esi
callq 0x782a
leaq 0x5(%rsp), %rdi
movb $0x8, (%rdi)
movl $0x8, %esi
callq 0x78b2
movl $0x8, %edi
callq 0x77ee
movl $0x46, %edi
movl $0x8, %esi
callq 0x782a
movl $0x8, %edi
callq 0x77ee
movl $0x73, %edi
movl $0x8, %esi
callq 0x782a
movl $0x8, %edi
callq 0x77ee
cmpl $0x80, %r12d
jl 0xd899
shrl $0x7, %r12d
movq 0x8(%rsp), %r13
leaq 0x20(%rsp), %r14
leaq 0x61d6(%rip), %r15      # 0x13a2e
movq %r14, %rdi
movl $0x80, %esi
callq 0x793f
movl $0x8, %edi
callq 0x77ee
movdqu (%r13), %xmm0
pcmpeqb 0x20(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xd890
movq %r15, %rdi
xorl %eax, %eax
callq 0x7c8d
addq $0x10, %r13
decq %r12
jne 0xd858
movl $0x46, %edi
movl $0x8, %esi
callq 0x782a
movl $0x8, %edi
callq 0x77ee
movl $0xe7, %edi
movl $0x8, %esi
callq 0x782a
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movl $0x40, %esi
callq 0x793f
movl $0x8, %edi
callq 0x77ee
movq (%r14), %rax
xorl %edi, %edi
cmpq 0x18(%rsp), %rax
sete %dil
leaq 0x615e(%rip), %rsi      # 0x13a50
xorl %eax, %eax
callq 0x7bb4
movl $0xfb, %edi
movl $0x8, %esi
callq 0x782a
movl $0x8, %edi
callq 0x77ee
leaq 0x6(%rsp), %r14
movq %r14, %rdi
movl $0x10, %esi
callq 0x793f
movzwl (%r14), %eax
xorl %edi, %edi
cmpw 0x14(%rsp), %ax
sete %dil
leaq 0x6136(%rip), %rsi      # 0x13a70
xorl %eax, %eax
callq 0x7bb4
movq %rbx, %rdi
callq 0x7de7
movq 0x8(%rsp), %rdi
callq 0x7de7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq