// File: /workspace/github_c_makefile_star2_O1/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 0xdae8
movq 0x712f7(%rip), %r15     # 0x7edc0
movq %r15, %rdi
callq 0x6090
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %r15, %rdx
movl %eax, %ecx
callq 0x7f17
testq %rax, %rax
jne 0xdaf6
leaq 0x5db6(%rip), %rdi      # 0x138a5
xorl %eax, %eax
callq 0x7c8d
leaq 0x5d8e(%rip), %rdx      # 0x1388b
movq %r14, %rdi
movl %ebp, %esi
movl $0x7, %ecx
callq 0x7f17
movq %rax, %r15
testq %rax, %rax
jne 0xdb22
leaq 0x5db9(%rip), %rdi      # 0x138d4
xorl %eax, %eax
callq 0x7c8d
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 0xdbae
movslq %r15d, %r12
addq %r14, %r12
xorl %r13d, %r13d
movzbl (%r12), %eax
leal -0x30(%rax), %ecx
cmpl $0x2, %ecx
jae 0xdb9b
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 0x5d69(%rip), %rsi      # 0x138fb
xorl %eax, %eax
callq 0x7bb4
jmp 0xdba0
cmpl $0x2a, %eax
je 0xdbae
incl %r15d
incq %r12
cmpl %r15d, %ebp
jne 0xdb48
movl %ebp, %r15d
cmpl %ebp, %r15d
jne 0xdbc1
leaq 0x5d73(%rip), %rdi      # 0x1392d
xorl %eax, %eax
callq 0x7c8d
testb $0x7f, %r13b
je 0xdbd5
leaq 0x5d8d(%rip), %rdi      # 0x1395b
xorl %eax, %eax
callq 0x7c8d
leaq 0x5cb7(%rip), %rdx      # 0x13893
movq %r14, %rdi
movl %ebp, %esi
movl $0x11, %ecx
callq 0x7f17
movq %rax, %r15
testq %rax, %rax
jne 0xdc01
leaq 0x5dae(%rip), %rdi      # 0x139a8
xorl %eax, %eax
callq 0x7c8d
subl %r14d, %r15d
addl $0x11, %r15d
movq $0x0, 0x10(%rbx)
movq %rbx, (%rsp)
movw $0x0, 0xc(%rbx)
xorl %ebx, %ebx
cmpl %ebp, %r15d
jge 0xdc91
movslq %r15d, %rax
addq %rax, %r14
xorl %r12d, %r12d
movzbl (%r14), %ebx
leal -0x30(%rbx), %eax
cmpl $0x2, %eax
jae 0xdc68
addq $-0x30, %rbx
xorl %edi, %edi
cmpl $0x50, %r12d
setl %dil
leaq 0x5d86(%rip), %rsi      # 0x139d1
xorl %eax, %eax
callq 0x7bb4
movl %r12d, %ecx
cmpl $0x3f, %r12d
jg 0xdc6f
shlq %cl, %rbx
movq (%rsp), %rax
orq %rbx, 0x10(%rax)
jmp 0xdc79
cmpl $0x2a, %ebx
jne 0xdc7c
jmp 0xdc8a
shll %cl, %ebx
movq (%rsp), %rax
orw %bx, 0xc(%rax)
incl %r12d
incl %r15d
incq %r14
cmpl %r15d, %ebp
jne 0xdc2a
movl %ebp, %r15d
cmpl $0x50, %r12d
sete %bl
cmpl %ebp, %r15d
jne 0xdca4
leaq 0x5c90(%rip), %rdi      # 0x1392d
xorl %eax, %eax
callq 0x7c8d
testb %bl, %bl
jne 0xdcb6
leaq 0x5d52(%rip), %rdi      # 0x13a01
xorl %eax, %eax
callq 0x7c8d
movq (%rsp), %rax
movl %r13d, 0x8(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)