// File: /workspace/github_c_makefile_star2_O2/ataradov[P]edbg/target_lattice_lcmxo2.c
// Function: target_program
static void target_program(void)
{
  uint8_t *file_data;
  int file_size, row_count;
  jed_file_t jed;

  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;

  if (!target_options.erase)
    target_erase();
  else
    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);

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

  for (int row = 0; row < row_count; row++)
  {
    dap_jtag_write_ir(CMD_LSC_PROG_INCR_NV, IR_LENGTH);
    dap_jtag_write_dr(&jed.config[row * 16], FLASH_ROW_SIZE);
    dap_jtag_idle(1000);
    poll_busy_flag();

    if (row % 256 == 0)
      verbose(".");
  }

  verbose(",");

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

  // Program Feature Row
  dap_jtag_write_ir(CMD_LSC_PROG_FEATURE, IR_LENGTH);
  dap_jtag_write_dr((uint8_t *)&jed.feature, 64);
  dap_jtag_idle(8);

  poll_busy_flag();

  verbose(",");

  // Program and verify FEABITS
  dap_jtag_write_ir(CMD_LSC_PROG_FEABITS, IR_LENGTH);
  dap_jtag_write_dr((uint8_t *)&jed.feabits, 16);
  dap_jtag_idle(8);

  poll_busy_flag();

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x7b1b
movq %rax, %rbx
movq 0x72e0d(%rip), %rdi     # 0x7fdd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7b41
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7b1b
leaq 0x10(%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, %r14d
cmpb $0x0, 0x72db0(%rip)     # 0x7fdcc
je 0xd025
callq 0xd403
jmp 0xd02a
callq 0xcf33
pushq $0x8
popq %rbp
movl $0xc6, %edi
movl %ebp, %esi
callq 0x7508
leaq 0x7(%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
xorl %eax, %eax
testl %r14d, %r14d
cmovlel %eax, %r14d
pushq $0x70
popq %r15
leaq 0x379b(%rip), %r12      # 0x1080f
xorl %r13d, %r13d
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0xd0c0
movl %r15d, %edi
movl %ebp, %esi
callq 0x7508
movq 0x10(%rsp), %rdi
addq %r13, %rdi
movl $0x80, %esi
callq 0x7589
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x74c6
callq 0xd368
testb %bl, %bl
jne 0xd0b7
movq %r12, %rdi
xorl %eax, %eax
callq 0x773c
incq %rbx
addq $0x10, %r13
jmp 0xd079
leaq 0x5cf5(%rip), %r14      # 0x12dbc
movq %r14, %rdi
xorl %eax, %eax
callq 0x773c
pushq $0x46
popq %rdi
pushq $0x8
popq %rbp
movl %ebp, %esi
callq 0x7508
movl %ebp, %edi
callq 0x74c6
movl $0xe4, %edi
movl %ebp, %esi
callq 0x7508
leaq 0x20(%rsp), %rdi
pushq $0x40
popq %rsi
callq 0x7589
movl %ebp, %edi
callq 0x74c6
callq 0xd368
movq %r14, %rdi
xorl %eax, %eax
callq 0x773c
movl $0xf8, %edi
movl %ebp, %esi
callq 0x7508
leaq 0x1c(%rsp), %r14
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x7589
movl %ebp, %edi
callq 0x74c6
callq 0xd368
movq 0x8(%rsp), %rdi
callq 0x7b3c
movq -0xc(%r14), %rdi
callq 0x7b3c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq