// File: /workspace/github_c_makefile_star2_O3/ataradov[P]edbg/target_lattice_lcmxo2.c
// Function: parse_jed_file
static void parse_jed_file(jed_file_t *file, uint8_t *data, int size)
{
  static const char *start_text = "L000000";
  static const char *fr_text = "NOTE FEATURE_ROW*";
  uint8_t  *ptr;
  int bit_count = 0;
  int fr_bit_count = 0;
  int offset;

  // This is a very primitive parser. It expects a fixed format and will fail
  // if it finds something unexpected. It will also ignore EBR initialization data.

  if (!bitstream_valid(data, size))
    error_exit("malformed JED file: device signature not found");

  ptr = mem_find(data, size, (uint8_t *)start_text, strlen(start_text));

  if (NULL == ptr)
    error_exit("malformed JED file: no 'L000000' found");

  offset = ptr - data + strlen(start_text);

  memset(file->config, 0, file->config_size);

  for (; offset < size; offset++)
  {
    if (data[offset] == '*')
      break;

    if (data[offset] == '0' || data[offset] == '1')
    {
      file->config[bit_count / 8] |= ((data[offset] - '0') << (bit_count % 8));
      bit_count++;
      check(bit_count < (file->config_size*8), "malformed JED file: configuration data is too big");
    }
  }

  if (offset == size)
    error_exit("malformed JED file: no field terminator found");

  if (bit_count % FLASH_ROW_SIZE)
    error_exit("malformed JED file: size of the configuration data must be a multiple of 128");

  ptr = mem_find(data, size, (uint8_t *)fr_text, strlen(fr_text));

  if (NULL == ptr)
    error_exit("malformed JED file: no feature row found");

  offset = ptr - data + strlen(fr_text);

  file->feature = 0;
  file->feabits = 0;

  for (; offset < size; offset++)
  {
    if (data[offset] == '*')
      break;

    if (data[offset] == '0' || data[offset] == '1')
    {
      int bit = data[offset] - '0';

      check(fr_bit_count < (64 + 16), "malformed JED file: feature row data is too big");

      if (fr_bit_count < 64)
        file->feature |= ((uint64_t)bit << fr_bit_count);
      else
        file->feabits |= (bit << (fr_bit_count-64));

      fr_bit_count++;
    }
  }

  if (offset == size)
    error_exit("malformed JED file: no field terminator found");

  if (fr_bit_count != (64 + 16))
    error_exit("malformed JED file: invalid feature row size");

  file->config_size = bit_count;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x400, %edx            # imm = 0x400
jl 0xdae5
movq 0x712fa(%rip), %r15     # 0x7edc0
movq %r15, %rdi
callq 0x6090
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
movl %eax, %ecx
callq 0x7f25
testq %rax, %rax
jne 0xdaf3
leaq 0x5da9(%rip), %rdi      # 0x13895
xorl %eax, %eax
callq 0x7ca5
leaq 0x5d81(%rip), %rdx      # 0x1387b
movq %r14, %rdi
movl %ebp, %esi
movl $0x7, %ecx
callq 0x7f25
movq %rax, %r15
testq %rax, %rax
jne 0xdb1f
leaq 0x5dac(%rip), %rdi      # 0x138c4
xorl %eax, %eax
callq 0x7ca5
subl %r14d, %r15d
addl $0x7, %r15d
movq (%rbx), %rdi
movslq 0x8(%rbx), %rdx
xorl %r13d, %r13d
xorl %esi, %esi
callq 0x60e0
cmpl %ebp, %r15d
jge 0xdbaa
movslq %r15d, %r12
addq %r14, %r12
xorl %r13d, %r13d
movzbl (%r12), %eax
leal -0x30(%rax), %ecx
cmpl $0x2, %ecx
jae 0xdb98
addl $0xd0, %eax
movl %r13d, %ecx
andb $0x7, %cl
shll %cl, %eax
movq (%rbx), %rcx
leal 0x7(%r13), %edx
testl %r13d, %r13d
cmovnsl %r13d, %edx
sarl $0x3, %edx
movslq %edx, %rdx
orb %al, (%rcx,%rdx)
incl %r13d
movl 0x8(%rbx), %eax
shll $0x3, %eax
xorl %edi, %edi
cmpl %eax, %r13d
setl %dil
leaq 0x5d5c(%rip), %rsi      # 0x138eb
xorl %eax, %eax
callq 0x7bcc
jmp 0xdb9d
cmpl $0x2a, %eax
je 0xdbaa
incl %r15d
incq %r12
cmpl %r15d, %ebp
jne 0xdb45
jmp 0xdbaf
cmpl %ebp, %r15d
jne 0xdbbd
leaq 0x5d67(%rip), %rdi      # 0x1391d
xorl %eax, %eax
callq 0x7ca5
testb $0x7f, %r13b
je 0xdbd1
leaq 0x5d81(%rip), %rdi      # 0x1394b
xorl %eax, %eax
callq 0x7ca5
leaq 0x5cab(%rip), %rdx      # 0x13883
movq %r14, %rdi
movl %ebp, %esi
movl $0x11, %ecx
callq 0x7f25
movq %rax, %r15
testq %rax, %rax
jne 0xdbfd
leaq 0x5da2(%rip), %rdi      # 0x13998
xorl %eax, %eax
callq 0x7ca5
subl %r14d, %r15d
addl $0x11, %r15d
movq $0x0, 0x10(%rbx)
movq %rbx, (%rsp)
movw $0x0, 0xc(%rbx)
xorl %ebx, %ebx
cmpl %ebp, %r15d
jge 0xdc8e
movslq %r15d, %rax
addq %rax, %r14
xorl %r12d, %r12d
movzbl (%r14), %eax
leal -0x30(%rax), %ebx
cmpl $0x2, %ebx
jae 0xdc62
xorl %edi, %edi
cmpl $0x50, %r12d
setl %dil
leaq 0x5d7e(%rip), %rsi      # 0x139c1
xorl %eax, %eax
callq 0x7bcc
cmpl $0x3f, %r12d
jg 0xdc69
movl %ebx, %eax
movl %r12d, %ecx
shlq %cl, %rax
movq (%rsp), %rcx
orq %rax, 0x10(%rcx)
jmp 0xdc76
cmpl $0x2a, %eax
jne 0xdc79
jmp 0xdc87
movl %r12d, %ecx
shll %cl, %ebx
movq (%rsp), %rax
orw %bx, 0xc(%rax)
incl %r12d
incl %r15d
incq %r14
cmpl %r15d, %ebp
jne 0xdc26
movl %ebp, %r15d
cmpl $0x50, %r12d
sete %bl
cmpl %ebp, %r15d
jne 0xdca1
leaq 0x5c83(%rip), %rdi      # 0x1391d
xorl %eax, %eax
callq 0x7ca5
testb %bl, %bl
jne 0xdcb3
leaq 0x5d45(%rip), %rdi      # 0x139f1
xorl %eax, %eax
callq 0x7ca5
movq (%rsp), %rax
movl %r13d, 0x8(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop