// File: /workspace/github_c_makefile_star2_O2/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
subq $0x18, %rsp
movl %edx, %ebx
movq %rsi, %r14
movq %rdi, 0x8(%rsp)
cmpl $0x400, %edx            # imm = 0x400
jl 0xd4a5
movq 0x7293a(%rip), %r15     # 0x7fdc0
movq %r15, %rdi
callq 0x6090
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
movl %eax, %ecx
callq 0x7c88
testq %rax, %rax
jne 0xd4b3
leaq 0x63e9(%rip), %rdi      # 0x13895
xorl %eax, %eax
callq 0x7a0e
leaq 0x63c1(%rip), %rdx      # 0x1387b
pushq $0x7
popq %rcx
movq %r14, %rdi
movl %ebx, %esi
callq 0x7c88
movq %rax, %r15
testq %rax, %rax
jne 0xd4dd
leaq 0x63ee(%rip), %rdi      # 0x138c4
xorl %eax, %eax
callq 0x7a0e
subl %r14d, %r15d
addl $0x7, %r15d
movq 0x8(%rsp), %rax
movq (%rax), %rdi
movslq 0x8(%rax), %rdx
xorl %esi, %esi
callq 0x60e0
movslq %r15d, %r13
cmpl %r13d, %ebx
movl %r13d, %eax
cmovgl %ebx, %eax
movl %eax, 0x4(%rsp)
movl %ebx, 0x14(%rsp)
movslq %ebx, %rbp
pushq $0x8
popq %rbx
leaq 0x63d3(%rip), %r12      # 0x138eb
xorl %r15d, %r15d
cmpq %rbp, %r13
jge 0xd577
movzbl (%r14,%r13), %esi
leal -0x30(%rsi), %eax
cmpl $0x2, %eax
jae 0xd56d
addl $0xd0, %esi
movl %r15d, %ecx
andb $0x7, %cl
shll %cl, %esi
movq 0x8(%rsp), %rdi
movq (%rdi), %rcx
movl %r15d, %eax
cltd
idivl %ebx
cltq
orb %sil, (%rcx,%rax)
incl %r15d
movl 0x8(%rdi), %eax
shll $0x3, %eax
xorl %edi, %edi
cmpl %eax, %r15d
setl %dil
movq %r12, %rsi
xorl %eax, %eax
callq 0x793d
jmp 0xd572
cmpl $0x2a, %esi
je 0xd57d
incq %r13
jmp 0xd51b
movl 0x4(%rsp), %eax
jmp 0xd580
movl %r13d, %eax
movl 0x14(%rsp), %ebx
cmpl %ebx, %eax
jne 0xd596
leaq 0x638e(%rip), %rdi      # 0x1391d
xorl %eax, %eax
callq 0x7a0e
testb $0x7f, %r15b
je 0xd5aa
leaq 0x63a8(%rip), %rdi      # 0x1394b
xorl %eax, %eax
callq 0x7a0e
leaq 0x62d2(%rip), %rdx      # 0x13883
pushq $0x11
popq %rcx
movq %r14, %rdi
movl %ebx, %esi
callq 0x7c88
movq %rax, %r12
testq %rax, %rax
jne 0xd5d4
leaq 0x63cb(%rip), %rdi      # 0x13998
xorl %eax, %eax
callq 0x7a0e
movq 0x8(%rsp), %rax
andq $0x0, 0x10(%rax)
subl %r14d, %r12d
andw $0x0, 0xc(%rax)
addl $0x11, %r12d
movslq %r12d, %r13
cmpl %r13d, %ebx
movl %r13d, %eax
cmovgl %ebx, %eax
movl %eax, 0x4(%rsp)
xorl %r12d, %r12d
cmpq %rbp, %r13
jge 0xd65d
movzbl (%r14,%r13), %eax
leal -0x30(%rax), %ebx
cmpl $0x2, %ebx
jae 0xd640
xorl %edi, %edi
cmpl $0x50, %r12d
setl %dil
leaq 0x63a1(%rip), %rsi      # 0x139c1
xorl %eax, %eax
callq 0x793d
cmpl $0x3f, %r12d
jg 0xd647
movl %ebx, %eax
movl %r12d, %ecx
shlq %cl, %rax
movq 0x8(%rsp), %rcx
orq %rax, 0x10(%rcx)
jmp 0xd655
cmpl $0x2a, %eax
jne 0xd658
jmp 0xd663
movl %r12d, %ecx
shll %cl, %ebx
movq 0x8(%rsp), %rax
orw %bx, 0xc(%rax)
incl %r12d
incq %r13
jmp 0xd5fd
movl 0x4(%rsp), %eax
jmp 0xd666
movl %r13d, %eax
cmpl 0x14(%rsp), %eax
jne 0xd67a
leaq 0x62aa(%rip), %rdi      # 0x1391d
xorl %eax, %eax
callq 0x7a0e
cmpl $0x50, %r12d
je 0xd68e
leaq 0x636a(%rip), %rdi      # 0x139f1
xorl %eax, %eax
callq 0x7a0e
movq 0x8(%rsp), %rax
movl %r15d, 0x8(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop