// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x1000000, %edi        # imm = 0x1000000
callq 0x8ae0
movq %rax, -0x8(%rbp)
movq 0x71ccb(%rip), %rdi     # 0x82568
movq -0x8(%rbp), %rsi
movl $0x1000000, %edx        # imm = 0x1000000
callq 0x8b50
movl %eax, -0xc(%rbp)
movl $0x200000, %edi         # imm = 0x200000
callq 0x8ae0
movq %rax, -0x28(%rbp)
movl $0x200000, -0x20(%rbp)  # imm = 0x200000
movq -0x8(%rbp), %rsi
movl -0xc(%rbp), %edx
leaq -0x28(%rbp), %rdi
callq 0x10e40
movl -0x20(%rbp), %eax
movl $0x80, %ecx
cltd
idivl %ecx
movl %eax, -0x10(%rbp)
testb $0x1, 0x71c74(%rip)     # 0x8255c
jne 0x108f1
callq 0x107d0
jmp 0x108f6
callq 0x10dc0
movl $0xc6, %edi
movl $0x8, %esi
callq 0x8260
movb $0x8, -0x29(%rbp)
leaq -0x29(%rbp), %rdi
movl $0x8, %esi
callq 0x82f0
movl $0x8, %edi
callq 0x81e0
movl $0x46, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x109ab
movl $0x70, %edi
movl $0x8, %esi
callq 0x8260
movq -0x28(%rbp), %rdi
movl -0x30(%rbp), %eax
shll $0x4, %eax
cltq
addq %rax, %rdi
movl $0x80, %esi
callq 0x82f0
movl $0x3e8, %edi            # imm = 0x3E8
callq 0x81e0
callq 0x10d20
movl -0x30(%rbp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0x1099e
leaq 0x3ed8(%rip), %rdi      # 0x1486f
movb $0x0, %al
callq 0x8630
jmp 0x109a0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x10941
leaq 0x647a(%rip), %rdi      # 0x16e2c
movb $0x0, %al
callq 0x8630
movl $0x46, %edi
movl $0x8, %esi
callq 0x8260
movl $0x8, %edi
callq 0x81e0
movl $0xe4, %edi
movl $0x8, %esi
callq 0x8260
leaq -0x28(%rbp), %rdi
addq $0x10, %rdi
movl $0x40, %esi
callq 0x82f0
movl $0x8, %edi
callq 0x81e0
callq 0x10d20
leaq 0x6423(%rip), %rdi      # 0x16e2c
movb $0x0, %al
callq 0x8630
movl $0xf8, %edi
movl $0x8, %esi
callq 0x8260
leaq -0x28(%rbp), %rdi
addq $0xc, %rdi
movl $0x10, %esi
callq 0x82f0
movl $0x8, %edi
callq 0x81e0
callq 0x10d20
movq -0x8(%rbp), %rdi
callq 0x8b30
movq -0x28(%rbp), %rdi
callq 0x8b30
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)