// File: /workspace/github_c_makefile_star2_O1/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 0x7dba
movq %rax, %rbx
movq 0x71812(%rip), %rdi     # 0x7edd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7dec
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7dba
leaq 0x10(%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), %r14d
leal 0x7f(%r14), %ebp
testl %r14d, %r14d
cmovnsl %r14d, %ebp
cmpb $0x0, 0x717ba(%rip)     # 0x7edcc
je 0xd61b
callq 0xda3f
jmp 0xd620
callq 0xd525
movl $0xc6, %edi
movl $0x8, %esi
callq 0x782a
leaq 0xf(%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
cmpl $0x80, %r14d
jl 0xd6cb
sarl $0x7, %ebp
movl %ebp, %r15d
leaq 0x31a5(%rip), %r14      # 0x1081f
xorl %r12d, %r12d
xorl %r13d, %r13d
movl $0x70, %edi
movl $0x8, %esi
callq 0x782a
movq 0x10(%rsp), %rdi
addq %r12, %rdi
movl $0x80, %esi
callq 0x78b2
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x77ee
callq 0xd9ad
testb %r13b, %r13b
jne 0xd6bf
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a78
incq %r13
addq $0x10, %r12
cmpq %r13, %r15
jne 0xd680
leaq 0x56fa(%rip), %r14      # 0x12dcc
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a78
movl $0x46, %edi
movl $0x8, %esi
callq 0x782a
movl $0x8, %edi
callq 0x77ee
movl $0xe4, %edi
movl $0x8, %esi
callq 0x782a
leaq 0x20(%rsp), %rdi
movl $0x40, %esi
callq 0x78b2
movl $0x8, %edi
callq 0x77ee
callq 0xd9ad
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a78
movl $0xf8, %edi
movl $0x8, %esi
callq 0x782a
leaq 0x1c(%rsp), %r14
movq %r14, %rdi
movl $0x10, %esi
callq 0x78b2
movl $0x8, %edi
callq 0x77ee
callq 0xd9ad
movq %rbx, %rdi
callq 0x7de7
movq -0xc(%r14), %rdi
callq 0x7de7
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq