// File: /workspace/github_c_makefile_star2_O3/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 0x7dd2
movq %rax, %rbx
movq 0x7180a(%rip), %rdi     # 0x7edd8
movq %rax, %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x7dfa
movl %eax, %ebp
movl $0x200000, %edi         # imm = 0x200000
callq 0x7dd2
leaq 0x10(%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), %r14d
leal 0x7f(%r14), %ebp
testl %r14d, %r14d
cmovnsl %r14d, %ebp
cmpb $0x0, 0x717b2(%rip)     # 0x7edcc
je 0xd623
callq 0xda3c
jmp 0xd628
callq 0xd52d
movl $0xc6, %edi
movl $0x8, %esi
callq 0x7845
leaq 0xf(%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
cmpl $0x80, %r14d
jl 0xd6d0
sarl $0x7, %ebp
movl %ebp, %r12d
movq 0x10(%rsp), %r14
leaq 0x3188(%rip), %r15      # 0x1080f
xorl %r13d, %r13d
movl $0x70, %edi
movl $0x8, %esi
callq 0x7845
movq %r14, %rdi
movl $0x80, %esi
callq 0x78cd
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x7809
callq 0xd9a5
testb %r13b, %r13b
jne 0xd6c4
movq %r15, %rdi
xorl %eax, %eax
callq 0x7a90
incq %r13
addq $0x10, %r14
cmpq %r13, %r12
jne 0xd68a
leaq 0x56e5(%rip), %r14      # 0x12dbc
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a90
movl $0x46, %edi
movl $0x8, %esi
callq 0x7845
movl $0x8, %edi
callq 0x7809
movl $0xe4, %edi
movl $0x8, %esi
callq 0x7845
leaq 0x20(%rsp), %rdi
movl $0x40, %esi
callq 0x78cd
movl $0x8, %edi
callq 0x7809
callq 0xd9a5
movq %r14, %rdi
xorl %eax, %eax
callq 0x7a90
movl $0xf8, %edi
movl $0x8, %esi
callq 0x7845
leaq 0x1c(%rsp), %r14
movq %r14, %rdi
movl $0x10, %esi
callq 0x78cd
movl $0x8, %edi
callq 0x7809
callq 0xd9a5
movq %rbx, %rdi
callq 0x7df5
movq -0xc(%r14), %rdi
callq 0x7df5
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq