// File: /workspace/github_c_makefile_star2_O2/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 0x7b1b
movq %rax, %rbx
movq 0x72c58(%rip), %rdi     # 0x7fdd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7b41
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7b1b
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movl $0x200000, 0x8(%r14)    # imm = 0x200000
movq %r14, %rdi
movq %rbx, 0x8(%rsp)
movq %rbx, %rsi
movl %ebp, %edx
callq 0xd45f
movl 0x8(%r14), %eax
movl $0x80, %ecx
cltd
idivl %ecx
movl %eax, %r15d
callq 0xd403
pushq $0x8
popq %rbp
movl $0xc6, %edi
movl %ebp, %esi
callq 0x7508
leaq 0x5(%rsp), %rdi
movb $0x8, (%rdi)
movl %ebp, %esi
callq 0x7589
movl %ebp, %edi
callq 0x74c6
pushq $0x46
popq %rdi
movl %ebp, %esi
callq 0x7508
movl %ebp, %edi
callq 0x74c6
pushq $0x73
popq %rdi
movl %ebp, %esi
callq 0x7508
movl %ebp, %edi
callq 0x74c6
movq (%r14), %r14
xorl %eax, %eax
testl %r15d, %r15d
cmovlel %eax, %r15d
shlq $0x4, %r15
leaq 0x10(%rsp), %r12
leaq 0x67ec(%rip), %r13      # 0x13a1e
xorl %ebx, %ebx
cmpq %rbx, %r15
je 0xd274
movq %r12, %rdi
movl $0x80, %esi
callq 0x7607
movl %ebp, %edi
callq 0x74c6
movdqu (%r14,%rbx), %xmm0
pcmpeqb 0x10(%rsp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0xd26e
movq %r13, %rdi
xorl %eax, %eax
callq 0x7a0e
addq $0x10, %rbx
jmp 0xd234
pushq $0x46
popq %rdi
pushq $0x8
popq %rbp
movl %ebp, %esi
callq 0x7508
movl %ebp, %edi
callq 0x74c6
movl $0xe7, %edi
movl %ebp, %esi
callq 0x7508
leaq 0x10(%rsp), %r15
pushq $0x40
popq %rsi
movq %r15, %rdi
callq 0x7607
movl %ebp, %edi
callq 0x74c6
movq (%r15), %rax
xorl %edi, %edi
cmpq 0x30(%rsp), %rax
sete %dil
leaq 0x6780(%rip), %rsi      # 0x13a40
xorl %eax, %eax
callq 0x793d
movl $0xfb, %edi
movl %ebp, %esi
callq 0x7508
movl %ebp, %edi
callq 0x74c6
leaq 0x6(%rsp), %r15
pushq $0x10
popq %rsi
movq %r15, %rdi
callq 0x7607
movzwl (%r15), %eax
xorl %edi, %edi
cmpw 0x2c(%rsp), %ax
sete %dil
leaq 0x6760(%rip), %rsi      # 0x13a60
xorl %eax, %eax
callq 0x793d
movq 0x8(%rsp), %rdi
callq 0x7b3c
movq %r14, %rdi
callq 0x7b3c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq